blob: 3653444fb1e42eb1805fbe38fc1266b418dd89d8 [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
`Deeraj Soman676c2702017-09-18 19:25:53 +0530109typedef struct {
110 const char *id_string;
111 const int value;
112} mixer_config_lookup;
113
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700114struct string_to_enum {
115 const char *name;
116 uint32_t value;
117};
118
119const struct string_to_enum s_flag_name_to_enum_table[] = {
120 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
121 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
122 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800123 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700124 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
125 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
126 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700127 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128#ifdef INCALL_MUSIC_ENABLED
129 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
130#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700131 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530132 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700133 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700134 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530136 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
137 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
138 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
139 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
140 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530141 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700142};
143
144const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530145 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700146 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530147 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
148 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530149 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700150 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
151 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
152 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700153 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
154 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700155 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700156 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700157 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530158 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700159 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700160 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530161#ifdef AUDIO_EXTN_FORMATS_ENABLED
162 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700163 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
164 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
165 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700166 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
167 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
168 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
169 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
170 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
171 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
172 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700173 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530174 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
175 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700176 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800177 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
178 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
179 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530180 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530181 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
182 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
183 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530184 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530185 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
186 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
187 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
188 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530189 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700190#endif
191};
192
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530193/* payload structure avt_device drift query */
194struct audio_avt_device_drift_stats {
195 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530196
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530197 /* Indicates the device interface direction as either
198 * source (Tx) or sink (Rx).
199 */
200 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530201
202 /* Reference timer for drift accumulation and time stamp information.
203 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
204 */
205 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530206 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530207} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530208
Ben Rombergera04fabc2014-11-14 12:16:03 -0800209static char bTable[BASE_TABLE_SIZE] = {
210 'A','B','C','D','E','F','G','H','I','J','K','L',
211 'M','N','O','P','Q','R','S','T','U','V','W','X',
212 'Y','Z','a','b','c','d','e','f','g','h','i','j',
213 'k','l','m','n','o','p','q','r','s','t','u','v',
214 'w','x','y','z','0','1','2','3','4','5','6','7',
215 '8','9','+','/'
216};
217
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700218static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
219 const char *name)
220{
221 size_t i;
222 for (i = 0; i < size; i++) {
223 if (strcmp(table[i].name, name) == 0) {
224 ALOGV("%s found %s", __func__, table[i].name);
225 return table[i].value;
226 }
227 }
228 return 0;
229}
230
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530231static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700232{
233 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530234 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800235 char *last_r;
236 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700237 while (flag_name != NULL) {
238 if (strlen(flag_name) != 0) {
239 flag |= string_to_enum(s_flag_name_to_enum_table,
240 ARRAY_SIZE(s_flag_name_to_enum_table),
241 flag_name);
242 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800243 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700244 }
245
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800246 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530247 io_flags.in_flags = (audio_input_flags_t)flag;
248 io_flags.out_flags = (audio_output_flags_t)flag;
249 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700250}
251
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530252static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700253{
254 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800255 char *last_r;
256 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700257
258 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
259 return;
260
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530261 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700262 while (str != NULL) {
263 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
264 ARRAY_SIZE(s_format_name_to_enum_table), str);
265 ALOGV("%s: format - %d", __func__, format);
266 if (format != 0) {
267 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700268 if (sf_info == NULL)
269 break; /* return whatever was parsed */
270
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530272 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700273 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800274 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275 }
276}
277
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530278static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700279{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700280 struct stream_sample_rate *ss_info = NULL;
281 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800282 char *last_r;
283 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700284
Amit Shekhar6f461b12014-08-01 14:52:58 -0700285 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
286 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700287
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530288 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700289 while (str != NULL) {
290 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
291 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
292 if (0 != sample_rate) {
293 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800294 if (!ss_info) {
295 ALOGE("%s: memory allocation failure", __func__);
296 return;
297 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700298 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530299 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700300 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800301 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700302 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700303}
304
305static int parse_bit_width_names(char *name)
306{
307 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800308 char *last_r;
309 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700310
311 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
312 bit_width = (int)strtol(str, (char **)NULL, 10);
313
314 ALOGV("%s: bit_width - %d", __func__, bit_width);
315 return bit_width;
316}
317
318static int parse_app_type_names(void *platform, char *name)
319{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700320 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800321 char *last_r;
322 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700323
324 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
325 app_type = (int)strtol(str, (char **)NULL, 10);
326
327 ALOGV("%s: app_type - %d", __func__, app_type);
328 return app_type;
329}
330
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530331static void update_streams_cfg_list(cnode *root, void *platform,
332 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700333{
334 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530335 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700336
337 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530338 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700339
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530340 if (!s_info) {
341 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700342 return;
343 }
344
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700345 while (node) {
346 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530347 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530348 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
349 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700350 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530351 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700352 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530353 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
354 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700355 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530356 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700357 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700359 }
360 node = node->next;
361 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530362 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700363}
364
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365static void load_cfg_list(cnode *root, void *platform,
366 struct listnode *streams_output_cfg_list,
367 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369 cnode *node = NULL;
370
371 node = config_find(root, OUTPUTS_TAG);
372 if (node != NULL) {
373 node = node->first_child;
374 while (node) {
375 ALOGV("%s: loading output %s", __func__, node->name);
376 update_streams_cfg_list(node, platform, streams_output_cfg_list);
377 node = node->next;
378 }
379 } else {
380 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381 }
382
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530383 node = config_find(root, INPUTS_TAG);
384 if (node != NULL) {
385 node = node->first_child;
386 while (node) {
387 ALOGV("%s: loading input %s", __func__, node->name);
388 update_streams_cfg_list(node, platform, streams_input_cfg_list);
389 node = node->next;
390 }
391 } else {
392 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700393 }
394}
395
396static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530397 struct listnode *streams_output_cfg_list,
398 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700399{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530400 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700401 int length = 0, i, num_app_types = 0;
402 struct listnode *node;
403 bool update;
404 struct mixer_ctl *ctl = NULL;
405 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530406 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800407 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700408
409 if (!mixer) {
410 ALOGE("%s: mixer is null",__func__);
411 return;
412 }
413 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
414 if (!ctl) {
415 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
416 return;
417 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530418 app_type_cfg[length++] = num_app_types;
419
420 if (list_empty(streams_output_cfg_list)) {
421 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700422 app_type_cfg[length++] = 48000;
423 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530424 num_app_types += 1;
425 }
426 if (list_empty(streams_input_cfg_list)) {
427 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
428 app_type_cfg[length++] = 48000;
429 app_type_cfg[length++] = 16;
430 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700431 }
432
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800433 /* get target bit width for ADM enforce mode */
434 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
435
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700436 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530437 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700438 update = true;
439 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530440 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700441 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530443 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530444 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530445 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530446 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800447 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
448 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
449 (target_bit_width > app_type_cfg[i+3]))
450 app_type_cfg[i+3] = target_bit_width;
451
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700452 update = false;
453 break;
454 }
455 }
456 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530457 num_app_types += 1;
458 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
459 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800460 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
461 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
462 (target_bit_width > app_type_cfg[length]))
463 app_type_cfg[length] = target_bit_width;
464
465 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530466 }
467 }
468 list_for_each(node, streams_input_cfg_list) {
469 s_info = node_to_item(node, struct streams_io_cfg, list);
470 update = true;
471 for (i=0; i<length; i=i+3) {
472 if (app_type_cfg[i+1] == 0)
473 break;
474 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530475 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530476 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530477 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530478 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
479 update = false;
480 break;
481 }
482 }
483 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
484 num_app_types += 1;
485 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
486 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
487 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700488 }
489 }
490 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
491 if (num_app_types) {
492 app_type_cfg[0] = num_app_types;
493 mixer_ctl_set_array(ctl, app_type_cfg, length);
494 }
495}
496
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530497void audio_extn_utils_update_streams_cfg_lists(void *platform,
498 struct mixer *mixer,
499 struct listnode *streams_output_cfg_list,
500 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700501{
502 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530503 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700504
505 ALOGV("%s", __func__);
506 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530507 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700508
509 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700510 if (root == NULL) {
511 ALOGE("cfg_list, NULL config root");
512 return;
513 }
514
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530515 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
516 if (data == NULL) {
517 ALOGD("%s: failed to open io config file(%s), trying older config file",
518 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
519 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
520 if (data == NULL) {
521 send_app_type_cfg(platform, mixer,
522 streams_output_cfg_list,
523 streams_input_cfg_list);
524 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800525 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530526 return;
527 }
528 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700529
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530530 config_load(root, data);
531 load_cfg_list(root, platform, streams_output_cfg_list,
532 streams_input_cfg_list);
533
534 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
535 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800536
537 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800538 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800539 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700540}
541
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530542static void audio_extn_utils_dump_streams_cfg_list(
543 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700544{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700545 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530546 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700547 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700548 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549
550 list_for_each(node_i, streams_cfg_list) {
551 s_info = node_to_item(node_i, struct streams_io_cfg, list);
552 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
553 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
554 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
555 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700556 sf_info = node_to_item(node_j, struct stream_format, list);
557 ALOGV("format-%x", sf_info->format);
558 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530559 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700560 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
561 ALOGV("sample rate-%d", ss_info->sample_rate);
562 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700563 }
564}
565
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530566void audio_extn_utils_dump_streams_cfg_lists(
567 struct listnode *streams_output_cfg_list,
568 struct listnode *streams_input_cfg_list)
569{
570 ALOGV("%s", __func__);
571 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
572 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
573}
574
575static void audio_extn_utils_release_streams_cfg_list(
576 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700577{
578 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530579 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700580
581 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530582
583 while (!list_empty(streams_cfg_list)) {
584 node_i = list_head(streams_cfg_list);
585 s_info = node_to_item(node_i, struct streams_io_cfg, list);
586 while (!list_empty(&s_info->format_list)) {
587 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700588 list_remove(node_j);
589 free(node_to_item(node_j, struct stream_format, list));
590 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530591 while (!list_empty(&s_info->sample_rate_list)) {
592 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700593 list_remove(node_j);
594 free(node_to_item(node_j, struct stream_sample_rate, list));
595 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700596 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530597 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700598 }
599}
600
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530601void audio_extn_utils_release_streams_cfg_lists(
602 struct listnode *streams_output_cfg_list,
603 struct listnode *streams_input_cfg_list)
604{
605 ALOGV("%s", __func__);
606 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
607 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
608}
609
610static bool set_app_type_cfg(struct streams_io_cfg *s_info,
611 struct stream_app_type_cfg *app_type_cfg,
612 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700613 {
614 struct listnode *node_i;
615 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530616 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700617 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
618 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530619 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700620
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530621 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700622 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530623 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700624 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
625 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
626 return true;
627 }
628 }
629 /*
630 * Reiterate through the list assuming dafault sample rate.
631 * Handles scenario where input sample rate is higher
632 * than all sample rates in list for the input bit width.
633 */
634 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800635
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530636 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700637 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
638 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530639 (bit_width == s_info->app_type_cfg.bit_width)) {
640 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700641 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530642 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800643 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 -0700644 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
645 return true;
646 }
647 }
648 return false;
649}
650
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530651void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
652 struct listnode *streams_input_cfg_list,
653 audio_devices_t devices __unused,
654 audio_input_flags_t flags,
655 audio_format_t format,
656 uint32_t sample_rate,
657 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530658 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530659 struct stream_app_type_cfg *app_type_cfg)
660{
661 struct listnode *node_i, *node_j;
662 struct streams_io_cfg *s_info;
663 struct stream_format *sf_info;
664
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530665 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
666 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530667
668 list_for_each(node_i, streams_input_cfg_list) {
669 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530670 /* Along with flags do profile matching if set at either end.*/
671 if (s_info->flags.in_flags == flags &&
672 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
673 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530674 list_for_each(node_j, &s_info->format_list) {
675 sf_info = node_to_item(node_j, struct stream_format, list);
676 if (sf_info->format == format) {
677 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
678 return;
679 }
680 }
681 }
682 }
683 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
684 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
685 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
686 app_type_cfg->bit_width = 16;
687}
688
689void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700690 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700691 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700692 audio_output_flags_t flags,
693 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700694 uint32_t sample_rate,
695 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530696 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530697 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700698 struct stream_app_type_cfg *app_type_cfg)
699{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530700 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530701 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700702 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530703 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700704
Ashish Jain058165c2016-09-28 23:18:48 +0530705 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700706 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700707 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
708 if (-ENOSYS != bw)
709 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700710 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
711 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
712 }
713
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700714 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530715 if (!strncmp("true", value, sizeof("true"))) {
716 if ((popcount(channel_mask) > 2) &&
717 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700718 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530719 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
720 ALOGD("%s: MCH session defaulting sample rate to %d",
721 __func__, sample_rate);
722 }
723 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530724
725 /* Set sampling rate to 176.4 for DSD64
726 * and 352.8Khz for DSD128.
727 * Set Bit Width to 16. output will be 16 bit
728 * post DoP in ASM.
729 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700730 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530731 (format == AUDIO_FORMAT_DSD)) {
732 bit_width = 16;
733 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
734 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
735 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
736 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
737 }
738
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530739
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800740 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
741 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700742 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530743 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530744 /* Along with flags do profile matching if set at either end.*/
745 if (s_info->flags.out_flags == flags &&
746 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
747 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530748 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700749 sf_info = node_to_item(node_j, struct stream_format, list);
750 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530751 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700752 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700753 }
754 }
755 }
756 }
757 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530758 s_info = node_to_item(node_i, struct streams_io_cfg, list);
759 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700760 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530761 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
762 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
763 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700764 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530765 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700766 return;
767 }
768 }
769 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700770 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700771 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700772 app_type_cfg->bit_width = 16;
773}
774
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530775static bool audio_is_this_native_usecase(struct audio_usecase *uc)
776{
777 bool native_usecase = false;
778 struct stream_out *out = (struct stream_out*) uc->stream.out;
779
780 if (PCM_PLAYBACK == uc->type && out != NULL &&
781 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
782 is_offload_usecase(uc->id) &&
783 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
784 native_usecase = true;
785
786 return native_usecase;
787}
788
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800789bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
790{
791 /* DSP bitwidth enforce mode for ADM and AFE:
792 * includes:
793 * deep buffer, low latency, direct pcm and offload.
794 * excludes:
795 * ull(raw+fast), VOIP.
796 */
797 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
798 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
799 (flags & AUDIO_OUTPUT_FLAG_FAST)))
800 return false;
801
802
803 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
804 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
805 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
806 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
807 return true;
808 else
809 return false;
810}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800811
812static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
813{
814 return adev->vr_audio_mode_enabled;
815}
816
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530817void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
818 struct audio_device *adev,
819 struct audio_usecase *usecase)
820{
821 ALOGV("%s", __func__);
822
823 switch(usecase->type) {
824 case PCM_PLAYBACK:
825 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
826 &adev->streams_output_cfg_list,
827 usecase->stream.out->devices,
828 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700829 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530830 usecase->stream.out->sample_rate,
831 usecase->stream.out->bit_width,
832 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530833 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530834 &usecase->stream.out->app_type_cfg);
835 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
836 break;
837 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700838 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
839 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
840 else
841 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530842 &adev->streams_input_cfg_list,
843 usecase->stream.in->device,
844 usecase->stream.in->flags,
845 usecase->stream.in->format,
846 usecase->stream.in->sample_rate,
847 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530848 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530849 &usecase->stream.in->app_type_cfg);
850 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
851 break;
Siddartha Shaik343abc62017-08-08 11:15:25 +0530852 case TRANSCODE_LOOPBACK :
853 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
854 &adev->streams_output_cfg_list,
855 usecase->stream.inout->out_config.devices,
856 0,
857 usecase->stream.inout->out_config.format,
858 usecase->stream.inout->out_config.sample_rate,
859 usecase->stream.inout->out_config.bit_width,
860 usecase->stream.inout->out_config.channel_mask,
861 usecase->stream.inout->profile,
862 &usecase->stream.inout->out_app_type_cfg);
863 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
864 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530865 default:
866 ALOGE("%s: app type cfg not supported for usecase type (%d)",
867 __func__, usecase->type);
868 }
869}
870
Siena Richard7c2db772016-12-21 11:32:34 -0800871static int send_app_type_cfg_for_device(struct audio_device *adev,
872 struct audio_usecase *usecase,
873 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700874{
875 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530876 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
877 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700878 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800879 int pcm_device_id = 0, acdb_dev_id, app_type;
880 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530881 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530882 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +0530883 struct streams_io_cfg *s_info = NULL;
884 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530885 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700886
Siena Richard7c2db772016-12-21 11:32:34 -0800887 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
888 __func__, platform_get_snd_device_name(usecase->out_snd_device),
889 platform_get_snd_device_name(usecase->in_snd_device),
890 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700891
Siddartha Shaik343abc62017-08-08 11:15:25 +0530892 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
893 usecase->type != TRANSCODE_LOOPBACK) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530894 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700895 rc = 0;
896 goto exit_send_app_type_cfg;
897 }
898 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
899 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
900 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800901 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700902 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Siddartha Shaik343abc62017-08-08 11:15:25 +0530903 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK) &&
Varun Balaraje49253e2017-07-06 19:48:56 +0530904 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530905 (!is_offload_usecase(usecase->id)) &&
906 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530907 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 -0700908 rc = 0;
909 goto exit_send_app_type_cfg;
910 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800911
912 //if VR is active then only send the mixer control
913 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
914 ALOGI("ULL doesnt need sending app type cfg, returning");
915 rc = 0;
916 goto exit_send_app_type_cfg;
917 }
918
Siddartha Shaik343abc62017-08-08 11:15:25 +0530919 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700920 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530921 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
922 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700923 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700924 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530925 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
926 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530927 }
Siena Richard7c2db772016-12-21 11:32:34 -0800928
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700929 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
930 if (!ctl) {
931 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
932 mixer_ctl_name);
933 rc = -EINVAL;
934 goto exit_send_app_type_cfg;
935 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530936 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530937
Rohit Kumar1181b292017-01-31 18:07:17 +0530938 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
939 if (acdb_dev_id <= 0) {
940 ALOGE("%s: Couldn't get the acdb dev id", __func__);
941 rc = -EINVAL;
942 goto exit_send_app_type_cfg;
943 }
944
Siena Richard7c2db772016-12-21 11:32:34 -0800945 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
946 if (snd_device_be_idx < 0) {
947 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
948 __func__, platform_get_snd_device_name(snd_device),
949 snd_device_be_idx);
950 }
951
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530952 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530953
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700954 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530955 if (!strncmp("true", value, sizeof("true"))) {
956 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
957 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700958 !(usecase->stream.out->flags &
959 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530960 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
961 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530962
kunleizcdf25942017-09-20 14:01:21 +0800963 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
964 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
965 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700966 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530967 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
968 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
969 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
970 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
971 /*
972 * To best utlize DSP, check if the stream sample rate is supported/multiple of
973 * configured device sample rate, if not update the COPP rate to be equal to the
974 * device sample rate, else open COPP at stream sample rate
975 */
976 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
977 usecase->stream.out->sample_rate,
978 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530979 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
980 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700981 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
982 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530983 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700984 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +0530985 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
986 /*
987 * For a2dp playback get encoder sampling rate and set copp sampling rate,
988 * for bit width use the stream param only.
989 */
990 audio_extn_a2dp_get_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
991 ALOGI("%s using %d sample rate rate for A2DP CoPP",
992 __func__, usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -0700993 }
994 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
995
Varun Balaraje49253e2017-07-06 19:48:56 +0530996 /* Interactive streams are supported with only direct app type id.
997 * Get Direct profile app type and use it for interactive streams
998 */
999 list_for_each(node, &adev->streams_output_cfg_list) {
1000 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001001 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301002 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1003 AUDIO_OUTPUT_FLAG_DIRECT))
1004 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301005 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001006 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301007 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301008 else
1009 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001010 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001011 app_type_cfg[len++] = acdb_dev_id;
1012 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001013 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1014 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301015 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1016 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001017
1018 sample_rate = sample_rate * 4;
1019 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1020 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001021 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001022 app_type_cfg[len++] = sample_rate;
1023
Siena Richard7c2db772016-12-21 11:32:34 -08001024 if (snd_device_be_idx > 0)
1025 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301026
Siena Richard7c2db772016-12-21 11:32:34 -08001027 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1028 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301029
1030 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001031 app_type = usecase->stream.in->app_type_cfg.app_type;
1032 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301033 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001034 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1035 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001036 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1037 app_type_cfg[len++] = sample_rate;
1038 if (snd_device_be_idx > 0)
1039 app_type_cfg[len++] = snd_device_be_idx;
1040 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1041 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301042 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001043 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301044 if(usecase->type == TRANSCODE_LOOPBACK) {
1045 sample_rate = usecase->stream.inout->out_config.sample_rate;
1046 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1047 }
Siena Richard7c2db772016-12-21 11:32:34 -08001048 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301049 app_type_cfg[len++] = acdb_dev_id;
1050 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001051 if (snd_device_be_idx > 0)
1052 app_type_cfg[len++] = snd_device_be_idx;
1053 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1054 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301055 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301056
Siddartha Shaik343abc62017-08-08 11:15:25 +05301057 if(ctl)
1058 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001059 rc = 0;
1060exit_send_app_type_cfg:
1061 return rc;
1062}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001063
Siena Richard7c2db772016-12-21 11:32:34 -08001064int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1065 struct audio_usecase *usecase)
1066{
1067 int i, num_devices = 0;
1068 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
1069 int rc = 0;
1070
1071 switch (usecase->type) {
1072 case PCM_PLAYBACK:
Siddartha Shaik343abc62017-08-08 11:15:25 +05301073 case TRANSCODE_LOOPBACK:
Siena Richard7c2db772016-12-21 11:32:34 -08001074 ALOGD("%s: usecase->out_snd_device %s",
1075 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1076 /* check for out combo device */
1077 if (platform_split_snd_device(adev->platform,
1078 usecase->out_snd_device,
1079 &num_devices, new_snd_devices)) {
1080 new_snd_devices[0] = usecase->out_snd_device;
1081 num_devices = 1;
1082 }
1083 break;
1084 case PCM_CAPTURE:
1085 ALOGD("%s: usecase->in_snd_device %s",
1086 __func__, platform_get_snd_device_name(usecase->in_snd_device));
1087 /* check for in combo device */
1088 if (platform_split_snd_device(adev->platform,
1089 usecase->in_snd_device,
1090 &num_devices, new_snd_devices)) {
1091 new_snd_devices[0] = usecase->in_snd_device;
1092 num_devices = 1;
1093 }
1094 break;
1095 default:
1096 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1097 rc = 0;
1098 break;
1099 }
1100
1101 for (i = 0; i < num_devices; i++) {
1102 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1103 if (rc)
1104 break;
1105 }
1106
1107 return rc;
1108}
1109
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301110int read_line_from_file(const char *path, char *buf, size_t count)
1111{
1112 char * fgets_ret;
1113 FILE * fd;
1114 int rv;
1115
1116 fd = fopen(path, "r");
1117 if (fd == NULL)
1118 return -1;
1119
1120 fgets_ret = fgets(buf, (int)count, fd);
1121 if (NULL != fgets_ret) {
1122 rv = (int)strlen(buf);
1123 } else {
1124 rv = ferror(fd);
1125 }
1126 fclose(fd);
1127
1128 return rv;
1129}
1130
Ashish Jainf1eaa582016-05-23 20:54:24 +05301131/*Translates ALSA formats to AOSP PCM formats*/
1132audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1133{
1134 audio_format_t format;
1135
1136 switch(alsa_format) {
1137 case SNDRV_PCM_FORMAT_S16_LE:
1138 format = AUDIO_FORMAT_PCM_16_BIT;
1139 break;
1140 case SNDRV_PCM_FORMAT_S24_3LE:
1141 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1142 break;
1143 case SNDRV_PCM_FORMAT_S24_LE:
1144 format = AUDIO_FORMAT_PCM_8_24_BIT;
1145 break;
1146 case SNDRV_PCM_FORMAT_S32_LE:
1147 format = AUDIO_FORMAT_PCM_32_BIT;
1148 break;
1149 default:
1150 ALOGW("Incorrect ALSA format");
1151 format = AUDIO_FORMAT_INVALID;
1152 }
1153 return format;
1154}
1155
1156/*Translates hal format (AOSP) to alsa formats*/
1157uint32_t hal_format_to_alsa(audio_format_t hal_format)
1158{
1159 uint32_t alsa_format;
1160
1161 switch (hal_format) {
1162 case AUDIO_FORMAT_PCM_32_BIT: {
1163 if (platform_supports_true_32bit())
1164 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1165 else
1166 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1167 }
1168 break;
1169 case AUDIO_FORMAT_PCM_8_BIT:
1170 alsa_format = SNDRV_PCM_FORMAT_S8;
1171 break;
1172 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1173 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1174 break;
1175 case AUDIO_FORMAT_PCM_8_24_BIT: {
1176 if (platform_supports_true_32bit())
1177 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1178 else
1179 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1180 }
1181 break;
1182 case AUDIO_FORMAT_PCM_FLOAT:
1183 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1184 break;
1185 default:
1186 case AUDIO_FORMAT_PCM_16_BIT:
1187 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1188 break;
1189 }
1190 return alsa_format;
1191}
1192
Ashish Jain83a6cc22016-06-28 14:34:17 +05301193/*Translates PCM formats to AOSP formats*/
1194audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1195{
1196 audio_format_t format = AUDIO_FORMAT_INVALID;
1197
1198 switch(pcm_format) {
1199 case PCM_FORMAT_S16_LE:
1200 format = AUDIO_FORMAT_PCM_16_BIT;
1201 break;
1202 case PCM_FORMAT_S24_3LE:
1203 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1204 break;
1205 case PCM_FORMAT_S24_LE:
1206 format = AUDIO_FORMAT_PCM_8_24_BIT;
1207 break;
1208 case PCM_FORMAT_S32_LE:
1209 format = AUDIO_FORMAT_PCM_32_BIT;
1210 break;
1211 default:
1212 ALOGW("Incorrect PCM format");
1213 format = AUDIO_FORMAT_INVALID;
1214 }
1215 return format;
1216}
1217
1218/*Translates hal format (AOSP) to alsa formats*/
1219uint32_t hal_format_to_pcm(audio_format_t hal_format)
1220{
1221 uint32_t pcm_format;
1222
1223 switch (hal_format) {
1224 case AUDIO_FORMAT_PCM_32_BIT:
1225 case AUDIO_FORMAT_PCM_8_24_BIT:
1226 case AUDIO_FORMAT_PCM_FLOAT: {
1227 if (platform_supports_true_32bit())
1228 pcm_format = PCM_FORMAT_S32_LE;
1229 else
1230 pcm_format = PCM_FORMAT_S24_3LE;
1231 }
1232 break;
1233 case AUDIO_FORMAT_PCM_8_BIT:
1234 pcm_format = PCM_FORMAT_S8;
1235 break;
1236 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1237 pcm_format = PCM_FORMAT_S24_3LE;
1238 break;
1239 default:
1240 case AUDIO_FORMAT_PCM_16_BIT:
1241 pcm_format = PCM_FORMAT_S16_LE;
1242 break;
1243 }
1244 return pcm_format;
1245}
1246
Ashish Jainf1eaa582016-05-23 20:54:24 +05301247uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1248 uint32_t sample_rate,
1249 uint32_t noOfChannels)
1250{
1251 uint32_t fragment_size = 0;
1252 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1253
1254 fragment_size = (pcm_offload_time
1255 * sample_rate
1256 * bytes_per_sample
1257 * noOfChannels)/1000;
1258 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1259 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1260 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1261 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1262 /*To have same PCM samples for all channels, the buffer size requires to
1263 *be multiple of (number of channels * bytes per sample)
1264 *For writes to succeed, the buffer must be written at address which is multiple of 32
1265 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001266 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301267
1268 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1269 return fragment_size;
1270}
1271
1272/* Calculates the fragment size required to configure compress session.
1273 * Based on the alsa format selected, decide if conversion is needed in
1274
1275 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1276 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1277 */
1278void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1279{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301280 audio_format_t dst_format = out->hal_op_format;
1281 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301282 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1283 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1284
1285 out->compr_config.fragment_size =
1286 get_alsa_fragment_size(hal_op_bytes_per_sample,
1287 out->sample_rate,
1288 popcount(out->channel_mask));
1289
1290 if ((src_format != dst_format) &&
1291 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1292
Ashish Jain83a6cc22016-06-28 14:34:17 +05301293 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301294 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1295 hal_op_bytes_per_sample);
1296 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301297 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301298 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301299 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301300 }
1301}
1302
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301303/* converts pcm format 24_8 to 8_24 inplace */
1304size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1305{
1306 size_t i = 0;
1307 int *int_buf_stream = buf;
1308
1309 if ((bytes % 4) != 0) {
1310 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1311 return -EINVAL;
1312 }
1313
1314 for (; i < (bytes / 4); i++)
1315 int_buf_stream[i] >>= 8;
1316
1317 return bytes;
1318}
1319
1320int get_snd_codec_id(audio_format_t format)
1321{
1322 int id = 0;
1323
1324 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1325 case AUDIO_FORMAT_MP3:
1326 id = SND_AUDIOCODEC_MP3;
1327 break;
1328 case AUDIO_FORMAT_AAC:
1329 id = SND_AUDIOCODEC_AAC;
1330 break;
1331 case AUDIO_FORMAT_AAC_ADTS:
1332 id = SND_AUDIOCODEC_AAC;
1333 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301334 case AUDIO_FORMAT_AAC_LATM:
1335 id = SND_AUDIOCODEC_AAC;
1336 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301337 case AUDIO_FORMAT_PCM:
1338 id = SND_AUDIOCODEC_PCM;
1339 break;
1340 case AUDIO_FORMAT_FLAC:
1341 id = SND_AUDIOCODEC_FLAC;
1342 break;
1343 case AUDIO_FORMAT_ALAC:
1344 id = SND_AUDIOCODEC_ALAC;
1345 break;
1346 case AUDIO_FORMAT_APE:
1347 id = SND_AUDIOCODEC_APE;
1348 break;
1349 case AUDIO_FORMAT_VORBIS:
1350 id = SND_AUDIOCODEC_VORBIS;
1351 break;
1352 case AUDIO_FORMAT_WMA:
1353 id = SND_AUDIOCODEC_WMA;
1354 break;
1355 case AUDIO_FORMAT_WMA_PRO:
1356 id = SND_AUDIOCODEC_WMA_PRO;
1357 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301358 case AUDIO_FORMAT_MP2:
1359 id = SND_AUDIOCODEC_MP2;
1360 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301361 case AUDIO_FORMAT_AC3:
1362 id = SND_AUDIOCODEC_AC3;
1363 break;
1364 case AUDIO_FORMAT_E_AC3:
1365 case AUDIO_FORMAT_E_AC3_JOC:
1366 id = SND_AUDIOCODEC_EAC3;
1367 break;
1368 case AUDIO_FORMAT_DTS:
1369 case AUDIO_FORMAT_DTS_HD:
1370 id = SND_AUDIOCODEC_DTS;
1371 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001372 case AUDIO_FORMAT_DOLBY_TRUEHD:
1373 id = SND_AUDIOCODEC_TRUEHD;
1374 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001375 case AUDIO_FORMAT_IEC61937:
1376 id = SND_AUDIOCODEC_IEC61937;
1377 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301378 case AUDIO_FORMAT_DSD:
1379 id = SND_AUDIOCODEC_DSD;
1380 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301381 case AUDIO_FORMAT_APTX:
1382 id = SND_AUDIOCODEC_APTX;
1383 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301384 default:
1385 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1386 }
1387
1388 return id;
1389}
1390
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001391void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1392 struct audio_usecase *usecase)
1393{
1394 int type = usecase->type;
1395
Dhananjay Kumar14245982017-01-16 20:21:00 +05301396 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001397 struct stream_out *out = usecase->stream.out;
1398 int snd_device = usecase->out_snd_device;
1399 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001400 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301401 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001402 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301403 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301404 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301405 platform_send_audio_calibration(adev->platform, usecase,
1406 usecase->stream.in->app_type_cfg.app_type,
1407 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001408 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301409 /* when app type is default. the sample rate is not used to send cal */
1410 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301411 platform_get_default_app_type_v2(adev->platform, usecase->type),
1412 48000);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301413 } else if (type == TRANSCODE_LOOPBACK && usecase->stream.inout != NULL) {
1414 int snd_device = usecase->out_snd_device;
1415 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1416 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1417 platform_send_audio_calibration(adev->platform, usecase,
1418 platform_get_default_app_type_v2(adev->platform, usecase->type),
1419 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001420 } else {
1421 /* No need to send audio calibration for voice and voip call usecases */
1422 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1423 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001424 }
1425}
1426
Ben Rombergera04fabc2014-11-14 12:16:03 -08001427// Base64 Encode and Decode
1428// Not all features supported. This must be used only with following conditions.
1429// Decode Modes: Support with and without padding
1430// CRLF not handling. So no CRLF in string to decode.
1431// Encode Modes: Supports only padding
1432int b64decode(char *inp, int ilen, uint8_t* outp)
1433{
1434 int i, j, k, ii, num;
1435 int rem, pcnt;
1436 uint32_t res=0;
1437 uint8_t getIndex[MAX_BASEINDEX_LEN];
1438 uint8_t tmp, cflag;
1439
1440 if(inp == NULL || outp == NULL || ilen <= 0) {
1441 ALOGE("[%s] received NULL pointer or zero length",__func__);
1442 return -1;
1443 }
1444
1445 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1446 for(i=0;i<BASE_TABLE_SIZE;i++) {
1447 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1448 }
1449 getIndex[(uint8_t)'=']=0;
1450
1451 j=0;k=0;
1452 num = ilen/4;
1453 rem = ilen%4;
1454 if(rem==0)
1455 num = num-1;
1456 cflag=0;
1457 for(i=0; i<num; i++) {
1458 res=0;
1459 for(ii=0;ii<4;ii++) {
1460 res = res << 6;
1461 tmp = getIndex[(uint8_t)inp[j++]];
1462 res = res | tmp;
1463 cflag = cflag | tmp;
1464 }
1465 outp[k++] = (res >> 16)&0xFF;
1466 outp[k++] = (res >> 8)&0xFF;
1467 outp[k++] = res & 0xFF;
1468 }
1469
1470 // Handle last bytes special
1471 pcnt=0;
1472 if(rem == 0) {
1473 //With padding or full data
1474 res = 0;
1475 for(ii=0;ii<4;ii++) {
1476 if(inp[j] == '=')
1477 pcnt++;
1478 res = res << 6;
1479 tmp = getIndex[(uint8_t)inp[j++]];
1480 res = res | tmp;
1481 cflag = cflag | tmp;
1482 }
1483 outp[k++] = res >> 16;
1484 if(pcnt == 2)
1485 goto done;
1486 outp[k++] = (res>>8)&0xFF;
1487 if(pcnt == 1)
1488 goto done;
1489 outp[k++] = res&0xFF;
1490 } else {
1491 //without padding
1492 res = 0;
1493 for(i=0;i<rem;i++) {
1494 res = res << 6;
1495 tmp = getIndex[(uint8_t)inp[j++]];
1496 res = res | tmp;
1497 cflag = cflag | tmp;
1498 }
1499 for(i=rem;i<4;i++) {
1500 res = res << 6;
1501 pcnt++;
1502 }
1503 outp[k++] = res >> 16;
1504 if(pcnt == 2)
1505 goto done;
1506 outp[k++] = (res>>8)&0xFF;
1507 if(pcnt == 1)
1508 goto done;
1509 outp[k++] = res&0xFF;
1510 }
1511done:
1512 if(cflag == 0xFF) {
1513 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1514 __func__, inp);
1515 return 0;
1516 }
1517 return k;
1518}
1519
1520int b64encode(uint8_t *inp, int ilen, char* outp)
1521{
1522 int i,j,k, num;
1523 int rem=0;
1524 uint32_t res=0;
1525
1526 if(inp == NULL || outp == NULL || ilen<=0) {
1527 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1528 return -1;
1529 }
1530
1531 num = ilen/3;
1532 rem = ilen%3;
1533 j=0;k=0;
1534 for(i=0; i<num; i++) {
1535 //prepare index
1536 res = inp[j++]<<16;
1537 res = res | inp[j++]<<8;
1538 res = res | inp[j++];
1539 //get output map from index
1540 outp[k++] = (char) bTable[(res>>18)&0x3F];
1541 outp[k++] = (char) bTable[(res>>12)&0x3F];
1542 outp[k++] = (char) bTable[(res>>6)&0x3F];
1543 outp[k++] = (char) bTable[res&0x3F];
1544 }
1545
1546 switch(rem) {
1547 case 1:
1548 res = inp[j++]<<16;
1549 outp[k++] = (char) bTable[res>>18];
1550 outp[k++] = (char) bTable[(res>>12)&0x3F];
1551 //outp[k++] = '=';
1552 //outp[k++] = '=';
1553 break;
1554 case 2:
1555 res = inp[j++]<<16;
1556 res = res | inp[j++]<<8;
1557 outp[k++] = (char) bTable[res>>18];
1558 outp[k++] = (char) bTable[(res>>12)&0x3F];
1559 outp[k++] = (char) bTable[(res>>6)&0x3F];
1560 //outp[k++] = '=';
1561 break;
1562 default:
1563 break;
1564 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001565 outp[k] = '\0';
1566 return k;
1567}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301568
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301569
1570int audio_extn_utils_get_codec_version(const char *snd_card_name,
1571 int card_num,
1572 char *codec_version)
1573{
1574 char procfs_path[50];
1575 FILE *fp;
1576
1577 if (strstr(snd_card_name, "tasha")) {
1578 snprintf(procfs_path, sizeof(procfs_path),
1579 "/proc/asound/card%d/codecs/tasha/version", card_num);
1580 if ((fp = fopen(procfs_path, "r")) != NULL) {
1581 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1582 fclose(fp);
1583 } else {
1584 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1585 return -ENOENT;
1586 }
1587 ALOGD("%s: codec version %s", __func__, codec_version);
1588 }
1589
1590 return 0;
1591}
1592
1593
Ashish Jain81eb2a82015-05-13 10:52:34 +05301594#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1595
1596void get_default_compressed_channel_status(
1597 unsigned char *channel_status)
1598{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301599 memset(channel_status,0,24);
1600
1601 /* block start bit in preamble bit 3 */
1602 channel_status[0] |= PROFESSIONAL;
1603 //compre out
1604 channel_status[0] |= NON_LPCM;
1605 // sample rate; fixed 48K for default/transcode
1606 channel_status[3] |= SR_48000;
1607}
1608
1609#ifdef HDMI_PASSTHROUGH_ENABLED
1610int32_t get_compressed_channel_status(void *audio_stream_data,
1611 uint32_t audio_frame_size,
1612 unsigned char *channel_status,
1613 enum audio_parser_code_type codec_type)
1614 // codec_type - AUDIO_PARSER_CODEC_AC3
1615 // - AUDIO_PARSER_CODEC_DTS
1616{
1617 unsigned char *stream;
1618 int ret = 0;
1619 stream = (unsigned char *)audio_stream_data;
1620
1621 if (audio_stream_data == NULL || audio_frame_size == 0) {
1622 ALOGW("no buffer to get channel status, return default for compress");
1623 get_default_compressed_channel_status(channel_status);
1624 return ret;
1625 }
1626
1627 memset(channel_status,0,24);
1628 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1629 {
1630 ALOGE("init audio parser failed");
1631 return -1;
1632 }
1633 ret = get_channel_status(channel_status, codec_type);
1634 return ret;
1635
1636}
1637
1638#endif
1639
1640void get_lpcm_channel_status(uint32_t sampleRate,
1641 unsigned char *channel_status)
1642{
1643 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301644 memset(channel_status,0,24);
1645 /* block start bit in preamble bit 3 */
1646 channel_status[0] |= PROFESSIONAL;
1647 //LPCM OUT
1648 channel_status[0] &= ~NON_LPCM;
1649
1650 switch (sampleRate) {
1651 case 8000:
1652 case 11025:
1653 case 12000:
1654 case 16000:
1655 case 22050:
1656 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301657 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301658 case 24000:
1659 channel_status[3] |= SR_24000;
1660 break;
1661 case 32000:
1662 channel_status[3] |= SR_32000;
1663 break;
1664 case 44100:
1665 channel_status[3] |= SR_44100;
1666 break;
1667 case 48000:
1668 channel_status[3] |= SR_48000;
1669 break;
1670 case 88200:
1671 channel_status[3] |= SR_88200;
1672 break;
1673 case 96000:
1674 channel_status[3] |= SR_96000;
1675 break;
1676 case 176400:
1677 channel_status[3] |= SR_176400;
1678 break;
1679 case 192000:
1680 channel_status[3] |= SR_192000;
1681 break;
1682 default:
1683 ALOGV("Invalid sample_rate %u\n", sampleRate);
1684 status = -1;
1685 break;
1686 }
1687}
1688
1689void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1690{
1691 unsigned char channel_status[24]={0};
1692 struct snd_aes_iec958 iec958;
1693 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1694 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301695 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301696#ifdef HDMI_PASSTHROUGH_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301697 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301698 /*TODO:Extend code to support DTS passthrough*/
1699 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301700 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301701 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1702 } else
1703#endif
1704 {
1705 /*set channel status bit for LPCM*/
1706 get_lpcm_channel_status(out->sample_rate, channel_status);
1707 }
1708
1709 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1710 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1711 if (!ctl) {
1712 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1713 __func__, mixer_ctl_name);
1714 return;
1715 }
1716 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1717 ALOGE("%s: Could not set channel status for ext HDMI ",
1718 __func__);
1719 return;
1720 }
1721
1722}
1723#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301724
1725int audio_extn_utils_get_avt_device_drift(
1726 struct audio_usecase *usecase,
1727 struct audio_avt_device_drift_param *drift_param)
1728{
1729 int ret = 0, count = 0;
1730 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301731 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301732 struct mixer_ctl *ctl = NULL;
1733 struct audio_avt_device_drift_stats drift_stats;
1734 struct audio_device *adev = NULL;
1735
1736 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301737 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1738 if (!backend) {
1739 ALOGE("%s: Unsupported device %d", __func__,
1740 usecase->stream.out->devices);
1741 ret = -EINVAL;
1742 goto done;
1743 }
1744 strlcpy(avt_device_drift_mixer_ctl_name,
1745 backend,
1746 MIXER_PATH_MAX_LENGTH);
1747
1748 count = strlen(backend);
1749 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1750 strlcat(&avt_device_drift_mixer_ctl_name[count],
1751 " DRIFT",
1752 MIXER_PATH_MAX_LENGTH - count);
1753 } else {
1754 ret = -EINVAL;
1755 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301756 }
1757 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301758 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301759 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301760 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301761 }
1762
Naresh Tanniru6160c712017-04-17 15:43:48 +05301763 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301764 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1765 if (!ctl) {
1766 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1767 __func__, avt_device_drift_mixer_ctl_name);
1768
1769 ret = -EINVAL;
1770 goto done;
1771 }
1772
1773 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1774 avt_device_drift_mixer_ctl_name);
1775
1776 mixer_ctl_update(ctl);
1777 count = mixer_ctl_get_num_values(ctl);
1778 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1779 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1780 __func__);
1781
1782 ret = -EINVAL;
1783 goto done;
1784 }
1785
1786 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1787 if (ret != 0) {
1788 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1789 __func__);
1790
1791 ret = -EINVAL;
1792 goto done;
1793 }
1794 memcpy(drift_param, &drift_stats.drift_param,
1795 sizeof(struct audio_avt_device_drift_param));
1796done:
1797 return ret;
1798}
Manish Dewangan07de2142017-02-27 19:27:20 +05301799
1800#ifdef SNDRV_COMPRESS_PATH_DELAY
1801int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1802{
1803 int ret = -EINVAL;
1804 struct snd_compr_metadata metadata;
1805 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1806
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001807 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301808 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1809 if (!(is_offload_usecase(out->usecase))) {
1810 ALOGE("%s:: not supported for non offload session", __func__);
1811 goto exit;
1812 }
1813
1814 if (!out->compr) {
1815 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1816 __func__);
1817 goto exit;
1818 }
1819
1820 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1821 ret = compress_get_metadata(out->compr, &metadata);
1822 if(ret) {
1823 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1824 goto exit;
1825 }
1826 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1827 } else {
1828 ALOGD("%s:: Using Fix DSP delay",__func__);
1829 }
1830
1831exit:
1832 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1833 return delay_ms;
1834}
1835#else
1836int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1837{
1838 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1839}
1840#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301841
1842#ifdef SNDRV_COMPRESS_RENDER_MODE
1843int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1844{
1845 struct snd_compr_metadata metadata;
1846 int ret = -EINVAL;
1847
1848 if (!(is_offload_usecase(out->usecase))) {
1849 ALOGE("%s:: not supported for non offload session", __func__);
1850 goto exit;
1851 }
1852
1853 if (!out->compr) {
1854 ALOGD("%s:: Invalid compress handle",
1855 __func__);
1856 goto exit;
1857 }
1858
1859 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1860
1861 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1862 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1863 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1864 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1865 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1866 } else {
1867 ret = 0;
1868 goto exit;
1869 }
1870 ret = compress_set_metadata(out->compr, &metadata);
1871 if(ret) {
1872 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1873 }
1874exit:
1875 return ret;
1876}
1877#else
1878int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1879{
1880 ALOGD("%s:: configuring render mode not supported", __func__);
1881 return 0;
1882}
1883#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301884
1885#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1886int audio_extn_utils_compress_set_clk_rec_mode(
1887 struct audio_usecase *usecase)
1888{
1889 struct snd_compr_metadata metadata;
1890 struct stream_out *out = NULL;
1891 int ret = -EINVAL;
1892
Naresh Tanniru7586e292017-04-13 10:38:13 +05301893 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301894 ALOGE("%s:: Invalid use case", __func__);
1895 goto exit;
1896 }
1897
1898 out = usecase->stream.out;
1899 if (!out) {
1900 ALOGE("%s:: invalid stream", __func__);
1901 goto exit;
1902 }
1903
1904 if (!is_offload_usecase(out->usecase)) {
1905 ALOGE("%s:: not supported for non offload session", __func__);
1906 goto exit;
1907 }
1908
1909 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1910 ALOGD("%s:: clk recovery is only supported in STC render mode",
1911 __func__);
1912 ret = 0;
1913 goto exit;
1914 }
1915
1916 if (!out->compr) {
1917 ALOGD("%s:: Invalid compress handle",
1918 __func__);
1919 goto exit;
1920 }
1921 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1922 switch(usecase->out_snd_device) {
1923 case SND_DEVICE_OUT_HDMI:
1924 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1925 case SND_DEVICE_OUT_DISPLAY_PORT:
1926 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1927 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1928 break;
1929 default:
1930 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1931 break;
1932 }
1933
1934 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1935
1936 ret = compress_set_metadata(out->compr, &metadata);
1937 if(ret) {
1938 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1939 }
1940
1941exit:
1942 return ret;
1943}
1944#else
1945int audio_extn_utils_compress_set_clk_rec_mode(
1946 struct audio_usecase *usecase __unused)
1947{
1948 ALOGD("%s:: configuring render mode not supported", __func__);
1949 return 0;
1950}
1951#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301952
1953#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1954int audio_extn_utils_compress_set_render_window(
1955 struct stream_out *out,
1956 struct audio_out_render_window_param *render_window)
1957{
1958 struct snd_compr_metadata metadata;
1959 int ret = -EINVAL;
1960
Manish Dewangan27346042017-03-01 12:56:12 +05301961 if(render_window == NULL) {
1962 ALOGE("%s:: Invalid render_window", __func__);
1963 goto exit;
1964 }
1965
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001966 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1967 __func__,render_window->render_ws, render_window->render_we);
1968
Manish Dewangan27346042017-03-01 12:56:12 +05301969 if (!is_offload_usecase(out->usecase)) {
1970 ALOGE("%s:: not supported for non offload session", __func__);
1971 goto exit;
1972 }
1973
Naresh Tanniru6160c712017-04-17 15:43:48 +05301974 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1975 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301976 ALOGD("%s:: only supported in timestamp mode, current "
1977 "render mode mode %d", __func__, out->render_mode);
1978 goto exit;
1979 }
1980
1981 if (!out->compr) {
1982 ALOGW("%s:: offload session not yet opened,"
1983 "render window will be configure later", __func__);
1984 /* store render window to reconfigure in start_output_stream() */
1985 goto exit;
1986 }
1987
1988 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1989 /*render window start value */
1990 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1991 metadata.value[1] = \
1992 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1993 /*render window end value */
1994 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1995 metadata.value[3] = \
1996 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1997
1998 ret = compress_set_metadata(out->compr, &metadata);
1999 if(ret) {
2000 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2001 }
2002
2003exit:
2004 return ret;
2005}
2006#else
2007int audio_extn_utils_compress_set_render_window(
2008 struct stream_out *out __unused,
2009 struct audio_out_render_window_param *render_window __unused)
2010{
2011 ALOGD("%s:: configuring render window not supported", __func__);
2012 return 0;
2013}
2014#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302015
2016#ifdef SNDRV_COMPRESS_START_DELAY
2017int audio_extn_utils_compress_set_start_delay(
2018 struct stream_out *out,
2019 struct audio_out_start_delay_param *delay_param)
2020{
2021 struct snd_compr_metadata metadata;
2022 int ret = -EINVAL;
2023
2024 if(delay_param == NULL) {
2025 ALOGE("%s:: Invalid delay_param", __func__);
2026 goto exit;
2027 }
2028
2029 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2030 delay_param->start_delay);
2031
2032 if (!is_offload_usecase(out->usecase)) {
2033 ALOGE("%s:: not supported for non offload session", __func__);
2034 goto exit;
2035 }
2036
Naresh Tanniru6160c712017-04-17 15:43:48 +05302037 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2038 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302039 ALOGD("%s:: only supported in timestamp mode, current "
2040 "render mode mode %d", __func__, out->render_mode);
2041 goto exit;
2042 }
2043
2044 if (!out->compr) {
2045 ALOGW("%s:: offload session not yet opened,"
2046 "start delay will be configure later", __func__);
2047 goto exit;
2048 }
2049
2050 metadata.key = SNDRV_COMPRESS_START_DELAY;
2051 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2052 metadata.value[1] = \
2053 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2054
2055 ret = compress_set_metadata(out->compr, &metadata);
2056 if(ret) {
2057 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2058 }
2059
2060exit:
2061 return ret;
2062}
2063#else
2064int audio_extn_utils_compress_set_start_delay(
2065 struct stream_out *out __unused,
2066 struct audio_out_start_delay_param *delay_param __unused)
2067{
2068 ALOGD("%s:: configuring render window not supported", __func__);
2069 return 0;
2070}
2071#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002072
2073#define MAX_SND_CARD 8
2074#define RETRY_US 500000
2075#define RETRY_NUMBER 10
2076
2077int audio_extn_utils_get_snd_card_num()
2078{
2079
2080 void *hw_info = NULL;
2081 struct mixer *mixer = NULL;
2082 int retry_num = 0;
2083 int snd_card_num = 0;
2084 char* snd_card_name = NULL;
2085
2086 while (snd_card_num < MAX_SND_CARD) {
2087 mixer = mixer_open(snd_card_num);
2088
2089 while (!mixer && retry_num < RETRY_NUMBER) {
2090 usleep(RETRY_US);
2091 mixer = mixer_open(snd_card_num);
2092 retry_num++;
2093 }
2094
2095 if (!mixer) {
2096 ALOGE("%s: Unable to open the mixer card: %d", __func__,
2097 snd_card_num);
2098 retry_num = 0;
2099 snd_card_num++;
2100 continue;
2101 }
2102
2103 snd_card_name = strdup(mixer_get_name(mixer));
2104 if (!snd_card_name) {
2105 ALOGE("failed to allocate memory for snd_card_name\n");
2106 mixer_close(mixer);
2107 return -1;
2108 }
2109 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2110
2111 hw_info = hw_info_init(snd_card_name);
2112 if (hw_info) {
2113 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2114 break;
2115 }
2116 ALOGE("%s: Failed to init hardware info", __func__);
2117 retry_num = 0;
2118 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002119
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302120 free(snd_card_name);
2121 snd_card_name = NULL;
2122
2123 mixer_close(mixer);
2124 mixer = NULL;
2125 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002126 if (snd_card_name)
2127 free(snd_card_name);
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302128 if (mixer)
2129 mixer_close(mixer);
2130 if (hw_info)
2131 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002132
2133 if (snd_card_num >= MAX_SND_CARD) {
2134 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2135 return -1;
2136 }
2137
2138 return snd_card_num;
2139}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302140
2141#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2142int audio_extn_utils_compress_enable_drift_correction(
2143 struct stream_out *out,
2144 struct audio_out_enable_drift_correction *drift)
2145{
2146 struct snd_compr_metadata metadata;
2147 int ret = -EINVAL;
2148
2149 if(drift == NULL) {
2150 ALOGE("%s:: Invalid param", __func__);
2151 goto exit;
2152 }
2153
2154 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2155
2156 if (!is_offload_usecase(out->usecase)) {
2157 ALOGE("%s:: not supported for non offload session", __func__);
2158 goto exit;
2159 }
2160
2161 if (!out->compr) {
2162 ALOGW("%s:: offload session not yet opened,"
2163 "start delay will be configure later", __func__);
2164 goto exit;
2165 }
2166
2167 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2168 metadata.value[0] = drift->enable;
2169 out->drift_correction_enabled = drift->enable;
2170
2171 ret = compress_set_metadata(out->compr, &metadata);
2172 if(ret) {
2173 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2174 out->drift_correction_enabled = false;
2175 }
2176
2177exit:
2178 return ret;
2179}
2180#else
2181int audio_extn_utils_compress_enable_drift_correction(
2182 struct stream_out *out __unused,
2183 struct audio_out_enable_drift_correction *drift __unused)
2184{
2185 ALOGD("%s:: configuring drift enablement not supported", __func__);
2186 return 0;
2187}
2188#endif
2189
2190#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2191int audio_extn_utils_compress_correct_drift(
2192 struct stream_out *out,
2193 struct audio_out_correct_drift *drift_param)
2194{
2195 struct snd_compr_metadata metadata;
2196 int ret = -EINVAL;
2197
2198 if (drift_param == NULL) {
2199 ALOGE("%s:: Invalid drift_param", __func__);
2200 goto exit;
2201 }
2202
2203 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2204 drift_param->adjust_time);
2205
2206 if (!is_offload_usecase(out->usecase)) {
2207 ALOGE("%s:: not supported for non offload session", __func__);
2208 goto exit;
2209 }
2210
2211 if (!out->compr) {
2212 ALOGW("%s:: offload session not yet opened", __func__);
2213 goto exit;
2214 }
2215
2216 if (!out->drift_correction_enabled) {
2217 ALOGE("%s:: drift correction not enabled", __func__);
2218 goto exit;
2219 }
2220
2221 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2222 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2223 metadata.value[1] = \
2224 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2225
2226 ret = compress_set_metadata(out->compr, &metadata);
2227 if(ret)
2228 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2229exit:
2230 return ret;
2231}
2232#else
2233int audio_extn_utils_compress_correct_drift(
2234 struct stream_out *out __unused,
2235 struct audio_out_correct_drift *drift_param __unused)
2236{
2237 ALOGD("%s:: setting adjust clock not supported", __func__);
2238 return 0;
2239}
2240#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302241
2242int audio_extn_utils_set_channel_map(
2243 struct stream_out *out,
2244 struct audio_out_channel_map_param *channel_map_param)
2245{
2246 int ret = -EINVAL, i = 0;
2247 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2248
2249 if (channel_map_param == NULL) {
2250 ALOGE("%s:: Invalid channel_map", __func__);
2251 goto exit;
2252 }
2253
2254 if (channel_map_param->channels != channels) {
2255 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2256 __func__, channel_map_param->channels, channels);
2257 goto exit;
2258 }
2259
2260 for ( i = 0; i < channels; i++) {
2261 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2262 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2263 }
2264 ret = 0;
2265exit:
2266 return ret;
2267}
Varun Balaraje49253e2017-07-06 19:48:56 +05302268
2269int audio_extn_utils_set_pan_scale_params(
2270 struct stream_out *out,
2271 struct mix_matrix_params *mm_params)
2272{
2273 int ret = -EINVAL, i = 0, j = 0;
2274
Varun Balaraj003ee752017-08-07 17:54:55 +05302275 if (mm_params == NULL || out == NULL) {
2276 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302277 goto exit;
2278 }
2279
2280 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2281 mm_params->num_output_channels <= 0 ||
2282 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2283 mm_params->num_input_channels <= 0)
2284 goto exit;
2285
2286 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2287 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2288 out->pan_scale_params.has_output_channel_map =
2289 mm_params->has_output_channel_map;
2290 for (i = 0; i < mm_params->num_output_channels; i++)
2291 out->pan_scale_params.output_channel_map[i] =
2292 mm_params->output_channel_map[i];
2293
2294 out->pan_scale_params.has_input_channel_map =
2295 mm_params->has_input_channel_map;
2296 for (i = 0; i < mm_params->num_input_channels; i++)
2297 out->pan_scale_params.input_channel_map[i] =
2298 mm_params->input_channel_map[i];
2299
2300 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2301 for (i = 0; i < mm_params->num_output_channels; i++)
2302 for (j = 0; j < mm_params->num_input_channels; j++) {
2303 //Convert the channel coefficient gains in Q14 format
2304 out->pan_scale_params.mixer_coeffs[i][j] =
2305 mm_params->mixer_coeffs[i][j] * (2 << 13);
2306 }
2307
2308 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2309 out->pcm_device_id,
2310 out->pan_scale_params);
2311
2312exit:
2313 return ret;
2314}
2315
2316int audio_extn_utils_set_downmix_params(
2317 struct stream_out *out,
2318 struct mix_matrix_params *mm_params)
2319{
2320 int ret = -EINVAL, i = 0, j = 0;
2321 struct audio_usecase *usecase = NULL;
2322
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002323 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302324 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302325 goto exit;
2326 }
2327
2328 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2329 mm_params->num_output_channels <= 0 ||
2330 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2331 mm_params->num_input_channels <= 0)
2332 goto exit;
2333
2334 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302335 if (!usecase) {
2336 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002337 goto exit;
2338 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302339 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2340 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2341
2342 out->downmix_params.has_output_channel_map =
2343 mm_params->has_output_channel_map;
2344 for (i = 0; i < mm_params->num_output_channels; i++) {
2345 out->downmix_params.output_channel_map[i] =
2346 mm_params->output_channel_map[i];
2347 }
2348
2349 out->downmix_params.has_input_channel_map =
2350 mm_params->has_input_channel_map;
2351 for (i = 0; i < mm_params->num_input_channels; i++)
2352 out->downmix_params.input_channel_map[i] =
2353 mm_params->input_channel_map[i];
2354
2355 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2356 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302357 for (j = 0; j < mm_params->num_input_channels; j++) {
2358 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302359 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302360 mm_params->mixer_coeffs[i][j] * (2 << 13);
2361 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302362
2363 ret = platform_set_stream_downmix_params(out->dev->platform,
2364 out->pcm_device_id,
2365 usecase->out_snd_device,
2366 out->downmix_params);
2367
2368exit:
2369 return ret;
2370}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302371
2372bool audio_extn_utils_is_dolby_format(audio_format_t format)
2373{
2374 if (format == AUDIO_FORMAT_AC3 ||
2375 format == AUDIO_FORMAT_E_AC3 ||
2376 format == AUDIO_FORMAT_E_AC3_JOC)
2377 return true;
2378 else
2379 return false;
2380}
2381
`Deeraj Soman676c2702017-09-18 19:25:53 +05302382int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2383{
2384 int i;
2385 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2386 {"S32_LE", 32},
2387 {"S24_LE", 24},
2388 {"S16_LE", 16}};
2389 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302390
`Deeraj Soman676c2702017-09-18 19:25:53 +05302391 for (i = 0; i < num_configs; i++) {
2392 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2393 return mixer_bitwidth_config[i].value;
2394 }
2395
2396 return -EINVAL;
2397}
2398
2399int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2400{
2401 int i;
2402 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2403 {"KHZ_48", 48000},
2404 {"KHZ_96", 96000},
2405 {"KHZ_144", 144000},
2406 {"KHZ_192", 192000},
2407 {"KHZ_384", 384000},
2408 {"KHZ_44P1", 44100},
2409 {"KHZ_88P2", 88200},
2410 {"KHZ_176P4", 176400},
2411 {"KHZ_352P8", 352800}};
2412 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2413
2414 for (i = 0; i < num_configs; i++) {
2415 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2416 return mixer_samplerate_config[i].value;
2417 }
2418
2419 return -EINVAL;
2420}
2421
2422int audio_extn_utils_get_channels_from_string(const char *id_string)
2423{
2424 int i;
2425 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2426 {"Two", 2},
2427 {"Three",3},
2428 {"Four", 4},
2429 {"Five", 5},
2430 {"Six", 6},
2431 {"Seven", 7},
2432 {"Eight", 8}};
2433 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2434
2435 for (i = 0; i < num_configs; i++) {
2436 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2437 return mixer_channels_config[i].value;
2438 }
2439
2440 return -EINVAL;
2441}