blob: cac136932112d3114c5a76b6dbc3e65555476d52 [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 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
740 //TODO: Handle fractional sampling rate configuration for LL
741 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
742 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
743 __func__, sample_rate, bit_width);
744 }
745
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800746 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
747 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700748 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530749 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530750 /* Along with flags do profile matching if set at either end.*/
751 if (s_info->flags.out_flags == flags &&
752 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
753 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530754 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700755 sf_info = node_to_item(node_j, struct stream_format, list);
756 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530757 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700758 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700759 }
760 }
761 }
762 }
763 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530764 s_info = node_to_item(node_i, struct streams_io_cfg, list);
765 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700766 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530767 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
768 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
769 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700770 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530771 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700772 return;
773 }
774 }
775 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700776 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700777 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700778 app_type_cfg->bit_width = 16;
779}
780
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530781static bool audio_is_this_native_usecase(struct audio_usecase *uc)
782{
783 bool native_usecase = false;
784 struct stream_out *out = (struct stream_out*) uc->stream.out;
785
786 if (PCM_PLAYBACK == uc->type && out != NULL &&
787 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
788 is_offload_usecase(uc->id) &&
789 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
790 native_usecase = true;
791
792 return native_usecase;
793}
794
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800795bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
796{
797 /* DSP bitwidth enforce mode for ADM and AFE:
798 * includes:
799 * deep buffer, low latency, direct pcm and offload.
800 * excludes:
801 * ull(raw+fast), VOIP.
802 */
803 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
804 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
805 (flags & AUDIO_OUTPUT_FLAG_FAST)))
806 return false;
807
808
809 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
810 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
811 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
812 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
813 return true;
814 else
815 return false;
816}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800817
818static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
819{
820 return adev->vr_audio_mode_enabled;
821}
822
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530823void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
824 struct audio_device *adev,
825 struct audio_usecase *usecase)
826{
827 ALOGV("%s", __func__);
828
829 switch(usecase->type) {
830 case PCM_PLAYBACK:
831 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
832 &adev->streams_output_cfg_list,
833 usecase->stream.out->devices,
834 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700835 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530836 usecase->stream.out->sample_rate,
837 usecase->stream.out->bit_width,
838 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530839 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530840 &usecase->stream.out->app_type_cfg);
841 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
842 break;
843 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700844 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
845 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
846 else
847 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530848 &adev->streams_input_cfg_list,
849 usecase->stream.in->device,
850 usecase->stream.in->flags,
851 usecase->stream.in->format,
852 usecase->stream.in->sample_rate,
853 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530854 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530855 &usecase->stream.in->app_type_cfg);
856 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
857 break;
Siddartha Shaik343abc62017-08-08 11:15:25 +0530858 case TRANSCODE_LOOPBACK :
859 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
860 &adev->streams_output_cfg_list,
861 usecase->stream.inout->out_config.devices,
862 0,
863 usecase->stream.inout->out_config.format,
864 usecase->stream.inout->out_config.sample_rate,
865 usecase->stream.inout->out_config.bit_width,
866 usecase->stream.inout->out_config.channel_mask,
867 usecase->stream.inout->profile,
868 &usecase->stream.inout->out_app_type_cfg);
869 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
870 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530871 default:
872 ALOGE("%s: app type cfg not supported for usecase type (%d)",
873 __func__, usecase->type);
874 }
875}
876
Siena Richard7c2db772016-12-21 11:32:34 -0800877static int send_app_type_cfg_for_device(struct audio_device *adev,
878 struct audio_usecase *usecase,
879 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700880{
881 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530882 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
883 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700884 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800885 int pcm_device_id = 0, acdb_dev_id, app_type;
886 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530887 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530888 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +0530889 struct streams_io_cfg *s_info = NULL;
890 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530891 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700892
Siena Richard7c2db772016-12-21 11:32:34 -0800893 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
894 __func__, platform_get_snd_device_name(usecase->out_snd_device),
895 platform_get_snd_device_name(usecase->in_snd_device),
896 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700897
Siddartha Shaik343abc62017-08-08 11:15:25 +0530898 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
899 usecase->type != TRANSCODE_LOOPBACK) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530900 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700901 rc = 0;
902 goto exit_send_app_type_cfg;
903 }
904 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
905 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
906 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800907 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700908 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Siddartha Shaik343abc62017-08-08 11:15:25 +0530909 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK) &&
Varun Balaraje49253e2017-07-06 19:48:56 +0530910 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530911 (!is_offload_usecase(usecase->id)) &&
912 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530913 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 -0700914 rc = 0;
915 goto exit_send_app_type_cfg;
916 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800917
918 //if VR is active then only send the mixer control
919 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
920 ALOGI("ULL doesnt need sending app type cfg, returning");
921 rc = 0;
922 goto exit_send_app_type_cfg;
923 }
924
Siddartha Shaik343abc62017-08-08 11:15:25 +0530925 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700926 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530927 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
928 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700929 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700930 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530931 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
932 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530933 }
Siena Richard7c2db772016-12-21 11:32:34 -0800934
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700935 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
936 if (!ctl) {
937 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
938 mixer_ctl_name);
939 rc = -EINVAL;
940 goto exit_send_app_type_cfg;
941 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530942 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530943
Rohit Kumar1181b292017-01-31 18:07:17 +0530944 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
945 if (acdb_dev_id <= 0) {
946 ALOGE("%s: Couldn't get the acdb dev id", __func__);
947 rc = -EINVAL;
948 goto exit_send_app_type_cfg;
949 }
950
Siena Richard7c2db772016-12-21 11:32:34 -0800951 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
952 if (snd_device_be_idx < 0) {
953 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
954 __func__, platform_get_snd_device_name(snd_device),
955 snd_device_be_idx);
956 }
957
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530958 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530959
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700960 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530961 if (!strncmp("true", value, sizeof("true"))) {
962 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
963 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700964 !(usecase->stream.out->flags &
965 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530966 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
967 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530968
kunleizcdf25942017-09-20 14:01:21 +0800969 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
970 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
971 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700972 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530973 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
974 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
975 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
976 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
977 /*
978 * To best utlize DSP, check if the stream sample rate is supported/multiple of
979 * configured device sample rate, if not update the COPP rate to be equal to the
980 * device sample rate, else open COPP at stream sample rate
981 */
982 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
983 usecase->stream.out->sample_rate,
984 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530985 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
986 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700987 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
988 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530989 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700990 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
991 }
992 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
993
Varun Balaraje49253e2017-07-06 19:48:56 +0530994 /* Interactive streams are supported with only direct app type id.
995 * Get Direct profile app type and use it for interactive streams
996 */
997 list_for_each(node, &adev->streams_output_cfg_list) {
998 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700999 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301000 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1001 AUDIO_OUTPUT_FLAG_DIRECT))
1002 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301003 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001004 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301005 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301006 else
1007 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001008 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001009 app_type_cfg[len++] = acdb_dev_id;
1010 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001011 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1012 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301013 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1014 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001015
1016 sample_rate = sample_rate * 4;
1017 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1018 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001019 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001020 app_type_cfg[len++] = sample_rate;
1021
Siena Richard7c2db772016-12-21 11:32:34 -08001022 if (snd_device_be_idx > 0)
1023 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301024
Siena Richard7c2db772016-12-21 11:32:34 -08001025 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1026 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301027
1028 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001029 app_type = usecase->stream.in->app_type_cfg.app_type;
1030 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301031 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001032 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1033 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001034 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1035 app_type_cfg[len++] = sample_rate;
1036 if (snd_device_be_idx > 0)
1037 app_type_cfg[len++] = snd_device_be_idx;
1038 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1039 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301040 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001041 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301042 if(usecase->type == TRANSCODE_LOOPBACK) {
1043 sample_rate = usecase->stream.inout->out_config.sample_rate;
1044 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1045 }
Siena Richard7c2db772016-12-21 11:32:34 -08001046 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301047 app_type_cfg[len++] = acdb_dev_id;
1048 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001049 if (snd_device_be_idx > 0)
1050 app_type_cfg[len++] = snd_device_be_idx;
1051 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1052 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301053 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301054
Siddartha Shaik343abc62017-08-08 11:15:25 +05301055 if(ctl)
1056 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001057 rc = 0;
1058exit_send_app_type_cfg:
1059 return rc;
1060}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001061
Siena Richard7c2db772016-12-21 11:32:34 -08001062int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1063 struct audio_usecase *usecase)
1064{
1065 int i, num_devices = 0;
1066 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
1067 int rc = 0;
1068
1069 switch (usecase->type) {
1070 case PCM_PLAYBACK:
Siddartha Shaik343abc62017-08-08 11:15:25 +05301071 case TRANSCODE_LOOPBACK:
Siena Richard7c2db772016-12-21 11:32:34 -08001072 ALOGD("%s: usecase->out_snd_device %s",
1073 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1074 /* check for out combo device */
1075 if (platform_split_snd_device(adev->platform,
1076 usecase->out_snd_device,
1077 &num_devices, new_snd_devices)) {
1078 new_snd_devices[0] = usecase->out_snd_device;
1079 num_devices = 1;
1080 }
1081 break;
1082 case PCM_CAPTURE:
1083 ALOGD("%s: usecase->in_snd_device %s",
1084 __func__, platform_get_snd_device_name(usecase->in_snd_device));
1085 /* check for in combo device */
1086 if (platform_split_snd_device(adev->platform,
1087 usecase->in_snd_device,
1088 &num_devices, new_snd_devices)) {
1089 new_snd_devices[0] = usecase->in_snd_device;
1090 num_devices = 1;
1091 }
1092 break;
1093 default:
1094 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1095 rc = 0;
1096 break;
1097 }
1098
1099 for (i = 0; i < num_devices; i++) {
1100 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1101 if (rc)
1102 break;
1103 }
1104
1105 return rc;
1106}
1107
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301108int read_line_from_file(const char *path, char *buf, size_t count)
1109{
1110 char * fgets_ret;
1111 FILE * fd;
1112 int rv;
1113
1114 fd = fopen(path, "r");
1115 if (fd == NULL)
1116 return -1;
1117
1118 fgets_ret = fgets(buf, (int)count, fd);
1119 if (NULL != fgets_ret) {
1120 rv = (int)strlen(buf);
1121 } else {
1122 rv = ferror(fd);
1123 }
1124 fclose(fd);
1125
1126 return rv;
1127}
1128
Ashish Jainf1eaa582016-05-23 20:54:24 +05301129/*Translates ALSA formats to AOSP PCM formats*/
1130audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1131{
1132 audio_format_t format;
1133
1134 switch(alsa_format) {
1135 case SNDRV_PCM_FORMAT_S16_LE:
1136 format = AUDIO_FORMAT_PCM_16_BIT;
1137 break;
1138 case SNDRV_PCM_FORMAT_S24_3LE:
1139 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1140 break;
1141 case SNDRV_PCM_FORMAT_S24_LE:
1142 format = AUDIO_FORMAT_PCM_8_24_BIT;
1143 break;
1144 case SNDRV_PCM_FORMAT_S32_LE:
1145 format = AUDIO_FORMAT_PCM_32_BIT;
1146 break;
1147 default:
1148 ALOGW("Incorrect ALSA format");
1149 format = AUDIO_FORMAT_INVALID;
1150 }
1151 return format;
1152}
1153
1154/*Translates hal format (AOSP) to alsa formats*/
1155uint32_t hal_format_to_alsa(audio_format_t hal_format)
1156{
1157 uint32_t alsa_format;
1158
1159 switch (hal_format) {
1160 case AUDIO_FORMAT_PCM_32_BIT: {
1161 if (platform_supports_true_32bit())
1162 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1163 else
1164 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1165 }
1166 break;
1167 case AUDIO_FORMAT_PCM_8_BIT:
1168 alsa_format = SNDRV_PCM_FORMAT_S8;
1169 break;
1170 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1171 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1172 break;
1173 case AUDIO_FORMAT_PCM_8_24_BIT: {
1174 if (platform_supports_true_32bit())
1175 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1176 else
1177 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1178 }
1179 break;
1180 case AUDIO_FORMAT_PCM_FLOAT:
1181 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1182 break;
1183 default:
1184 case AUDIO_FORMAT_PCM_16_BIT:
1185 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1186 break;
1187 }
1188 return alsa_format;
1189}
1190
Ashish Jain83a6cc22016-06-28 14:34:17 +05301191/*Translates PCM formats to AOSP formats*/
1192audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1193{
1194 audio_format_t format = AUDIO_FORMAT_INVALID;
1195
1196 switch(pcm_format) {
1197 case PCM_FORMAT_S16_LE:
1198 format = AUDIO_FORMAT_PCM_16_BIT;
1199 break;
1200 case PCM_FORMAT_S24_3LE:
1201 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1202 break;
1203 case PCM_FORMAT_S24_LE:
1204 format = AUDIO_FORMAT_PCM_8_24_BIT;
1205 break;
1206 case PCM_FORMAT_S32_LE:
1207 format = AUDIO_FORMAT_PCM_32_BIT;
1208 break;
1209 default:
1210 ALOGW("Incorrect PCM format");
1211 format = AUDIO_FORMAT_INVALID;
1212 }
1213 return format;
1214}
1215
1216/*Translates hal format (AOSP) to alsa formats*/
1217uint32_t hal_format_to_pcm(audio_format_t hal_format)
1218{
1219 uint32_t pcm_format;
1220
1221 switch (hal_format) {
1222 case AUDIO_FORMAT_PCM_32_BIT:
1223 case AUDIO_FORMAT_PCM_8_24_BIT:
1224 case AUDIO_FORMAT_PCM_FLOAT: {
1225 if (platform_supports_true_32bit())
1226 pcm_format = PCM_FORMAT_S32_LE;
1227 else
1228 pcm_format = PCM_FORMAT_S24_3LE;
1229 }
1230 break;
1231 case AUDIO_FORMAT_PCM_8_BIT:
1232 pcm_format = PCM_FORMAT_S8;
1233 break;
1234 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1235 pcm_format = PCM_FORMAT_S24_3LE;
1236 break;
1237 default:
1238 case AUDIO_FORMAT_PCM_16_BIT:
1239 pcm_format = PCM_FORMAT_S16_LE;
1240 break;
1241 }
1242 return pcm_format;
1243}
1244
Ashish Jainf1eaa582016-05-23 20:54:24 +05301245uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1246 uint32_t sample_rate,
1247 uint32_t noOfChannels)
1248{
1249 uint32_t fragment_size = 0;
1250 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1251
1252 fragment_size = (pcm_offload_time
1253 * sample_rate
1254 * bytes_per_sample
1255 * noOfChannels)/1000;
1256 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1257 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1258 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1259 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1260 /*To have same PCM samples for all channels, the buffer size requires to
1261 *be multiple of (number of channels * bytes per sample)
1262 *For writes to succeed, the buffer must be written at address which is multiple of 32
1263 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001264 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301265
1266 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1267 return fragment_size;
1268}
1269
1270/* Calculates the fragment size required to configure compress session.
1271 * Based on the alsa format selected, decide if conversion is needed in
1272
1273 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1274 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1275 */
1276void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1277{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301278 audio_format_t dst_format = out->hal_op_format;
1279 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301280 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1281 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1282
1283 out->compr_config.fragment_size =
1284 get_alsa_fragment_size(hal_op_bytes_per_sample,
1285 out->sample_rate,
1286 popcount(out->channel_mask));
1287
1288 if ((src_format != dst_format) &&
1289 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1290
Ashish Jain83a6cc22016-06-28 14:34:17 +05301291 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301292 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1293 hal_op_bytes_per_sample);
1294 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301295 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301296 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301297 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301298 }
1299}
1300
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301301/* converts pcm format 24_8 to 8_24 inplace */
1302size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1303{
1304 size_t i = 0;
1305 int *int_buf_stream = buf;
1306
1307 if ((bytes % 4) != 0) {
1308 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1309 return -EINVAL;
1310 }
1311
1312 for (; i < (bytes / 4); i++)
1313 int_buf_stream[i] >>= 8;
1314
1315 return bytes;
1316}
1317
1318int get_snd_codec_id(audio_format_t format)
1319{
1320 int id = 0;
1321
1322 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1323 case AUDIO_FORMAT_MP3:
1324 id = SND_AUDIOCODEC_MP3;
1325 break;
1326 case AUDIO_FORMAT_AAC:
1327 id = SND_AUDIOCODEC_AAC;
1328 break;
1329 case AUDIO_FORMAT_AAC_ADTS:
1330 id = SND_AUDIOCODEC_AAC;
1331 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301332 case AUDIO_FORMAT_AAC_LATM:
1333 id = SND_AUDIOCODEC_AAC;
1334 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301335 case AUDIO_FORMAT_PCM:
1336 id = SND_AUDIOCODEC_PCM;
1337 break;
1338 case AUDIO_FORMAT_FLAC:
1339 id = SND_AUDIOCODEC_FLAC;
1340 break;
1341 case AUDIO_FORMAT_ALAC:
1342 id = SND_AUDIOCODEC_ALAC;
1343 break;
1344 case AUDIO_FORMAT_APE:
1345 id = SND_AUDIOCODEC_APE;
1346 break;
1347 case AUDIO_FORMAT_VORBIS:
1348 id = SND_AUDIOCODEC_VORBIS;
1349 break;
1350 case AUDIO_FORMAT_WMA:
1351 id = SND_AUDIOCODEC_WMA;
1352 break;
1353 case AUDIO_FORMAT_WMA_PRO:
1354 id = SND_AUDIOCODEC_WMA_PRO;
1355 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301356 case AUDIO_FORMAT_MP2:
1357 id = SND_AUDIOCODEC_MP2;
1358 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301359 case AUDIO_FORMAT_AC3:
1360 id = SND_AUDIOCODEC_AC3;
1361 break;
1362 case AUDIO_FORMAT_E_AC3:
1363 case AUDIO_FORMAT_E_AC3_JOC:
1364 id = SND_AUDIOCODEC_EAC3;
1365 break;
1366 case AUDIO_FORMAT_DTS:
1367 case AUDIO_FORMAT_DTS_HD:
1368 id = SND_AUDIOCODEC_DTS;
1369 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001370 case AUDIO_FORMAT_DOLBY_TRUEHD:
1371 id = SND_AUDIOCODEC_TRUEHD;
1372 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001373 case AUDIO_FORMAT_IEC61937:
1374 id = SND_AUDIOCODEC_IEC61937;
1375 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301376 case AUDIO_FORMAT_DSD:
1377 id = SND_AUDIOCODEC_DSD;
1378 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301379 case AUDIO_FORMAT_APTX:
1380 id = SND_AUDIOCODEC_APTX;
1381 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301382 default:
1383 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1384 }
1385
1386 return id;
1387}
1388
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001389void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1390 struct audio_usecase *usecase)
1391{
1392 int type = usecase->type;
1393
Dhananjay Kumar14245982017-01-16 20:21:00 +05301394 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001395 struct stream_out *out = usecase->stream.out;
1396 int snd_device = usecase->out_snd_device;
1397 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001398 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301399 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001400 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301401 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301402 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301403 platform_send_audio_calibration(adev->platform, usecase,
1404 usecase->stream.in->app_type_cfg.app_type,
1405 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001406 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301407 /* when app type is default. the sample rate is not used to send cal */
1408 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301409 platform_get_default_app_type_v2(adev->platform, usecase->type),
1410 48000);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301411 } else if (type == TRANSCODE_LOOPBACK && usecase->stream.inout != NULL) {
1412 int snd_device = usecase->out_snd_device;
1413 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1414 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1415 platform_send_audio_calibration(adev->platform, usecase,
1416 platform_get_default_app_type_v2(adev->platform, usecase->type),
1417 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001418 } else {
1419 /* No need to send audio calibration for voice and voip call usecases */
1420 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1421 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001422 }
1423}
1424
Ben Rombergera04fabc2014-11-14 12:16:03 -08001425// Base64 Encode and Decode
1426// Not all features supported. This must be used only with following conditions.
1427// Decode Modes: Support with and without padding
1428// CRLF not handling. So no CRLF in string to decode.
1429// Encode Modes: Supports only padding
1430int b64decode(char *inp, int ilen, uint8_t* outp)
1431{
1432 int i, j, k, ii, num;
1433 int rem, pcnt;
1434 uint32_t res=0;
1435 uint8_t getIndex[MAX_BASEINDEX_LEN];
1436 uint8_t tmp, cflag;
1437
1438 if(inp == NULL || outp == NULL || ilen <= 0) {
1439 ALOGE("[%s] received NULL pointer or zero length",__func__);
1440 return -1;
1441 }
1442
1443 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1444 for(i=0;i<BASE_TABLE_SIZE;i++) {
1445 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1446 }
1447 getIndex[(uint8_t)'=']=0;
1448
1449 j=0;k=0;
1450 num = ilen/4;
1451 rem = ilen%4;
1452 if(rem==0)
1453 num = num-1;
1454 cflag=0;
1455 for(i=0; i<num; i++) {
1456 res=0;
1457 for(ii=0;ii<4;ii++) {
1458 res = res << 6;
1459 tmp = getIndex[(uint8_t)inp[j++]];
1460 res = res | tmp;
1461 cflag = cflag | tmp;
1462 }
1463 outp[k++] = (res >> 16)&0xFF;
1464 outp[k++] = (res >> 8)&0xFF;
1465 outp[k++] = res & 0xFF;
1466 }
1467
1468 // Handle last bytes special
1469 pcnt=0;
1470 if(rem == 0) {
1471 //With padding or full data
1472 res = 0;
1473 for(ii=0;ii<4;ii++) {
1474 if(inp[j] == '=')
1475 pcnt++;
1476 res = res << 6;
1477 tmp = getIndex[(uint8_t)inp[j++]];
1478 res = res | tmp;
1479 cflag = cflag | tmp;
1480 }
1481 outp[k++] = res >> 16;
1482 if(pcnt == 2)
1483 goto done;
1484 outp[k++] = (res>>8)&0xFF;
1485 if(pcnt == 1)
1486 goto done;
1487 outp[k++] = res&0xFF;
1488 } else {
1489 //without padding
1490 res = 0;
1491 for(i=0;i<rem;i++) {
1492 res = res << 6;
1493 tmp = getIndex[(uint8_t)inp[j++]];
1494 res = res | tmp;
1495 cflag = cflag | tmp;
1496 }
1497 for(i=rem;i<4;i++) {
1498 res = res << 6;
1499 pcnt++;
1500 }
1501 outp[k++] = res >> 16;
1502 if(pcnt == 2)
1503 goto done;
1504 outp[k++] = (res>>8)&0xFF;
1505 if(pcnt == 1)
1506 goto done;
1507 outp[k++] = res&0xFF;
1508 }
1509done:
1510 if(cflag == 0xFF) {
1511 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1512 __func__, inp);
1513 return 0;
1514 }
1515 return k;
1516}
1517
1518int b64encode(uint8_t *inp, int ilen, char* outp)
1519{
1520 int i,j,k, num;
1521 int rem=0;
1522 uint32_t res=0;
1523
1524 if(inp == NULL || outp == NULL || ilen<=0) {
1525 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1526 return -1;
1527 }
1528
1529 num = ilen/3;
1530 rem = ilen%3;
1531 j=0;k=0;
1532 for(i=0; i<num; i++) {
1533 //prepare index
1534 res = inp[j++]<<16;
1535 res = res | inp[j++]<<8;
1536 res = res | inp[j++];
1537 //get output map from index
1538 outp[k++] = (char) bTable[(res>>18)&0x3F];
1539 outp[k++] = (char) bTable[(res>>12)&0x3F];
1540 outp[k++] = (char) bTable[(res>>6)&0x3F];
1541 outp[k++] = (char) bTable[res&0x3F];
1542 }
1543
1544 switch(rem) {
1545 case 1:
1546 res = inp[j++]<<16;
1547 outp[k++] = (char) bTable[res>>18];
1548 outp[k++] = (char) bTable[(res>>12)&0x3F];
1549 //outp[k++] = '=';
1550 //outp[k++] = '=';
1551 break;
1552 case 2:
1553 res = inp[j++]<<16;
1554 res = res | inp[j++]<<8;
1555 outp[k++] = (char) bTable[res>>18];
1556 outp[k++] = (char) bTable[(res>>12)&0x3F];
1557 outp[k++] = (char) bTable[(res>>6)&0x3F];
1558 //outp[k++] = '=';
1559 break;
1560 default:
1561 break;
1562 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001563 outp[k] = '\0';
1564 return k;
1565}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301566
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301567
1568int audio_extn_utils_get_codec_version(const char *snd_card_name,
1569 int card_num,
1570 char *codec_version)
1571{
1572 char procfs_path[50];
1573 FILE *fp;
1574
1575 if (strstr(snd_card_name, "tasha")) {
1576 snprintf(procfs_path, sizeof(procfs_path),
1577 "/proc/asound/card%d/codecs/tasha/version", card_num);
1578 if ((fp = fopen(procfs_path, "r")) != NULL) {
1579 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1580 fclose(fp);
1581 } else {
1582 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1583 return -ENOENT;
1584 }
1585 ALOGD("%s: codec version %s", __func__, codec_version);
1586 }
1587
1588 return 0;
1589}
1590
1591
Ashish Jain81eb2a82015-05-13 10:52:34 +05301592#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1593
1594void get_default_compressed_channel_status(
1595 unsigned char *channel_status)
1596{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301597 memset(channel_status,0,24);
1598
1599 /* block start bit in preamble bit 3 */
1600 channel_status[0] |= PROFESSIONAL;
1601 //compre out
1602 channel_status[0] |= NON_LPCM;
1603 // sample rate; fixed 48K for default/transcode
1604 channel_status[3] |= SR_48000;
1605}
1606
1607#ifdef HDMI_PASSTHROUGH_ENABLED
1608int32_t get_compressed_channel_status(void *audio_stream_data,
1609 uint32_t audio_frame_size,
1610 unsigned char *channel_status,
1611 enum audio_parser_code_type codec_type)
1612 // codec_type - AUDIO_PARSER_CODEC_AC3
1613 // - AUDIO_PARSER_CODEC_DTS
1614{
1615 unsigned char *stream;
1616 int ret = 0;
1617 stream = (unsigned char *)audio_stream_data;
1618
1619 if (audio_stream_data == NULL || audio_frame_size == 0) {
1620 ALOGW("no buffer to get channel status, return default for compress");
1621 get_default_compressed_channel_status(channel_status);
1622 return ret;
1623 }
1624
1625 memset(channel_status,0,24);
1626 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1627 {
1628 ALOGE("init audio parser failed");
1629 return -1;
1630 }
1631 ret = get_channel_status(channel_status, codec_type);
1632 return ret;
1633
1634}
1635
1636#endif
1637
1638void get_lpcm_channel_status(uint32_t sampleRate,
1639 unsigned char *channel_status)
1640{
1641 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301642 memset(channel_status,0,24);
1643 /* block start bit in preamble bit 3 */
1644 channel_status[0] |= PROFESSIONAL;
1645 //LPCM OUT
1646 channel_status[0] &= ~NON_LPCM;
1647
1648 switch (sampleRate) {
1649 case 8000:
1650 case 11025:
1651 case 12000:
1652 case 16000:
1653 case 22050:
1654 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301655 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301656 case 24000:
1657 channel_status[3] |= SR_24000;
1658 break;
1659 case 32000:
1660 channel_status[3] |= SR_32000;
1661 break;
1662 case 44100:
1663 channel_status[3] |= SR_44100;
1664 break;
1665 case 48000:
1666 channel_status[3] |= SR_48000;
1667 break;
1668 case 88200:
1669 channel_status[3] |= SR_88200;
1670 break;
1671 case 96000:
1672 channel_status[3] |= SR_96000;
1673 break;
1674 case 176400:
1675 channel_status[3] |= SR_176400;
1676 break;
1677 case 192000:
1678 channel_status[3] |= SR_192000;
1679 break;
1680 default:
1681 ALOGV("Invalid sample_rate %u\n", sampleRate);
1682 status = -1;
1683 break;
1684 }
1685}
1686
1687void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1688{
1689 unsigned char channel_status[24]={0};
1690 struct snd_aes_iec958 iec958;
1691 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1692 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301693 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301694#ifdef HDMI_PASSTHROUGH_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301695 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301696 /*TODO:Extend code to support DTS passthrough*/
1697 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301698 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301699 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1700 } else
1701#endif
1702 {
1703 /*set channel status bit for LPCM*/
1704 get_lpcm_channel_status(out->sample_rate, channel_status);
1705 }
1706
1707 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1708 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1709 if (!ctl) {
1710 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1711 __func__, mixer_ctl_name);
1712 return;
1713 }
1714 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1715 ALOGE("%s: Could not set channel status for ext HDMI ",
1716 __func__);
1717 return;
1718 }
1719
1720}
1721#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301722
1723int audio_extn_utils_get_avt_device_drift(
1724 struct audio_usecase *usecase,
1725 struct audio_avt_device_drift_param *drift_param)
1726{
1727 int ret = 0, count = 0;
1728 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301729 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301730 struct mixer_ctl *ctl = NULL;
1731 struct audio_avt_device_drift_stats drift_stats;
1732 struct audio_device *adev = NULL;
1733
1734 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301735 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1736 if (!backend) {
1737 ALOGE("%s: Unsupported device %d", __func__,
1738 usecase->stream.out->devices);
1739 ret = -EINVAL;
1740 goto done;
1741 }
1742 strlcpy(avt_device_drift_mixer_ctl_name,
1743 backend,
1744 MIXER_PATH_MAX_LENGTH);
1745
1746 count = strlen(backend);
1747 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1748 strlcat(&avt_device_drift_mixer_ctl_name[count],
1749 " DRIFT",
1750 MIXER_PATH_MAX_LENGTH - count);
1751 } else {
1752 ret = -EINVAL;
1753 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301754 }
1755 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301756 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301757 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301758 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301759 }
1760
Naresh Tanniru6160c712017-04-17 15:43:48 +05301761 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301762 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1763 if (!ctl) {
1764 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1765 __func__, avt_device_drift_mixer_ctl_name);
1766
1767 ret = -EINVAL;
1768 goto done;
1769 }
1770
1771 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1772 avt_device_drift_mixer_ctl_name);
1773
1774 mixer_ctl_update(ctl);
1775 count = mixer_ctl_get_num_values(ctl);
1776 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1777 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1778 __func__);
1779
1780 ret = -EINVAL;
1781 goto done;
1782 }
1783
1784 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1785 if (ret != 0) {
1786 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1787 __func__);
1788
1789 ret = -EINVAL;
1790 goto done;
1791 }
1792 memcpy(drift_param, &drift_stats.drift_param,
1793 sizeof(struct audio_avt_device_drift_param));
1794done:
1795 return ret;
1796}
Manish Dewangan07de2142017-02-27 19:27:20 +05301797
1798#ifdef SNDRV_COMPRESS_PATH_DELAY
1799int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1800{
1801 int ret = -EINVAL;
1802 struct snd_compr_metadata metadata;
1803 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1804
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001805 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301806 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1807 if (!(is_offload_usecase(out->usecase))) {
1808 ALOGE("%s:: not supported for non offload session", __func__);
1809 goto exit;
1810 }
1811
1812 if (!out->compr) {
1813 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1814 __func__);
1815 goto exit;
1816 }
1817
1818 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1819 ret = compress_get_metadata(out->compr, &metadata);
1820 if(ret) {
1821 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1822 goto exit;
1823 }
1824 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1825 } else {
1826 ALOGD("%s:: Using Fix DSP delay",__func__);
1827 }
1828
1829exit:
1830 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1831 return delay_ms;
1832}
1833#else
1834int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1835{
1836 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1837}
1838#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301839
1840#ifdef SNDRV_COMPRESS_RENDER_MODE
1841int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1842{
1843 struct snd_compr_metadata metadata;
1844 int ret = -EINVAL;
1845
1846 if (!(is_offload_usecase(out->usecase))) {
1847 ALOGE("%s:: not supported for non offload session", __func__);
1848 goto exit;
1849 }
1850
1851 if (!out->compr) {
1852 ALOGD("%s:: Invalid compress handle",
1853 __func__);
1854 goto exit;
1855 }
1856
1857 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1858
1859 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1860 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1861 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1862 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1863 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1864 } else {
1865 ret = 0;
1866 goto exit;
1867 }
1868 ret = compress_set_metadata(out->compr, &metadata);
1869 if(ret) {
1870 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1871 }
1872exit:
1873 return ret;
1874}
1875#else
1876int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1877{
1878 ALOGD("%s:: configuring render mode not supported", __func__);
1879 return 0;
1880}
1881#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301882
1883#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1884int audio_extn_utils_compress_set_clk_rec_mode(
1885 struct audio_usecase *usecase)
1886{
1887 struct snd_compr_metadata metadata;
1888 struct stream_out *out = NULL;
1889 int ret = -EINVAL;
1890
Naresh Tanniru7586e292017-04-13 10:38:13 +05301891 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301892 ALOGE("%s:: Invalid use case", __func__);
1893 goto exit;
1894 }
1895
1896 out = usecase->stream.out;
1897 if (!out) {
1898 ALOGE("%s:: invalid stream", __func__);
1899 goto exit;
1900 }
1901
1902 if (!is_offload_usecase(out->usecase)) {
1903 ALOGE("%s:: not supported for non offload session", __func__);
1904 goto exit;
1905 }
1906
1907 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1908 ALOGD("%s:: clk recovery is only supported in STC render mode",
1909 __func__);
1910 ret = 0;
1911 goto exit;
1912 }
1913
1914 if (!out->compr) {
1915 ALOGD("%s:: Invalid compress handle",
1916 __func__);
1917 goto exit;
1918 }
1919 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1920 switch(usecase->out_snd_device) {
1921 case SND_DEVICE_OUT_HDMI:
1922 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1923 case SND_DEVICE_OUT_DISPLAY_PORT:
1924 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1925 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1926 break;
1927 default:
1928 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1929 break;
1930 }
1931
1932 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1933
1934 ret = compress_set_metadata(out->compr, &metadata);
1935 if(ret) {
1936 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1937 }
1938
1939exit:
1940 return ret;
1941}
1942#else
1943int audio_extn_utils_compress_set_clk_rec_mode(
1944 struct audio_usecase *usecase __unused)
1945{
1946 ALOGD("%s:: configuring render mode not supported", __func__);
1947 return 0;
1948}
1949#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301950
1951#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1952int audio_extn_utils_compress_set_render_window(
1953 struct stream_out *out,
1954 struct audio_out_render_window_param *render_window)
1955{
1956 struct snd_compr_metadata metadata;
1957 int ret = -EINVAL;
1958
Manish Dewangan27346042017-03-01 12:56:12 +05301959 if(render_window == NULL) {
1960 ALOGE("%s:: Invalid render_window", __func__);
1961 goto exit;
1962 }
1963
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001964 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1965 __func__,render_window->render_ws, render_window->render_we);
1966
Manish Dewangan27346042017-03-01 12:56:12 +05301967 if (!is_offload_usecase(out->usecase)) {
1968 ALOGE("%s:: not supported for non offload session", __func__);
1969 goto exit;
1970 }
1971
Naresh Tanniru6160c712017-04-17 15:43:48 +05301972 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1973 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301974 ALOGD("%s:: only supported in timestamp mode, current "
1975 "render mode mode %d", __func__, out->render_mode);
1976 goto exit;
1977 }
1978
1979 if (!out->compr) {
1980 ALOGW("%s:: offload session not yet opened,"
1981 "render window will be configure later", __func__);
1982 /* store render window to reconfigure in start_output_stream() */
1983 goto exit;
1984 }
1985
1986 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1987 /*render window start value */
1988 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1989 metadata.value[1] = \
1990 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1991 /*render window end value */
1992 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1993 metadata.value[3] = \
1994 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1995
1996 ret = compress_set_metadata(out->compr, &metadata);
1997 if(ret) {
1998 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1999 }
2000
2001exit:
2002 return ret;
2003}
2004#else
2005int audio_extn_utils_compress_set_render_window(
2006 struct stream_out *out __unused,
2007 struct audio_out_render_window_param *render_window __unused)
2008{
2009 ALOGD("%s:: configuring render window not supported", __func__);
2010 return 0;
2011}
2012#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302013
2014#ifdef SNDRV_COMPRESS_START_DELAY
2015int audio_extn_utils_compress_set_start_delay(
2016 struct stream_out *out,
2017 struct audio_out_start_delay_param *delay_param)
2018{
2019 struct snd_compr_metadata metadata;
2020 int ret = -EINVAL;
2021
2022 if(delay_param == NULL) {
2023 ALOGE("%s:: Invalid delay_param", __func__);
2024 goto exit;
2025 }
2026
2027 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2028 delay_param->start_delay);
2029
2030 if (!is_offload_usecase(out->usecase)) {
2031 ALOGE("%s:: not supported for non offload session", __func__);
2032 goto exit;
2033 }
2034
Naresh Tanniru6160c712017-04-17 15:43:48 +05302035 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2036 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302037 ALOGD("%s:: only supported in timestamp mode, current "
2038 "render mode mode %d", __func__, out->render_mode);
2039 goto exit;
2040 }
2041
2042 if (!out->compr) {
2043 ALOGW("%s:: offload session not yet opened,"
2044 "start delay will be configure later", __func__);
2045 goto exit;
2046 }
2047
2048 metadata.key = SNDRV_COMPRESS_START_DELAY;
2049 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2050 metadata.value[1] = \
2051 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2052
2053 ret = compress_set_metadata(out->compr, &metadata);
2054 if(ret) {
2055 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2056 }
2057
2058exit:
2059 return ret;
2060}
2061#else
2062int audio_extn_utils_compress_set_start_delay(
2063 struct stream_out *out __unused,
2064 struct audio_out_start_delay_param *delay_param __unused)
2065{
2066 ALOGD("%s:: configuring render window not supported", __func__);
2067 return 0;
2068}
2069#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002070
2071#define MAX_SND_CARD 8
2072#define RETRY_US 500000
2073#define RETRY_NUMBER 10
2074
2075int audio_extn_utils_get_snd_card_num()
2076{
2077
2078 void *hw_info = NULL;
2079 struct mixer *mixer = NULL;
2080 int retry_num = 0;
2081 int snd_card_num = 0;
2082 char* snd_card_name = NULL;
2083
2084 while (snd_card_num < MAX_SND_CARD) {
2085 mixer = mixer_open(snd_card_num);
2086
2087 while (!mixer && retry_num < RETRY_NUMBER) {
2088 usleep(RETRY_US);
2089 mixer = mixer_open(snd_card_num);
2090 retry_num++;
2091 }
2092
2093 if (!mixer) {
2094 ALOGE("%s: Unable to open the mixer card: %d", __func__,
2095 snd_card_num);
2096 retry_num = 0;
2097 snd_card_num++;
2098 continue;
2099 }
2100
2101 snd_card_name = strdup(mixer_get_name(mixer));
2102 if (!snd_card_name) {
2103 ALOGE("failed to allocate memory for snd_card_name\n");
2104 mixer_close(mixer);
2105 return -1;
2106 }
2107 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2108
2109 hw_info = hw_info_init(snd_card_name);
2110 if (hw_info) {
2111 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2112 break;
2113 }
2114 ALOGE("%s: Failed to init hardware info", __func__);
2115 retry_num = 0;
2116 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002117
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302118 free(snd_card_name);
2119 snd_card_name = NULL;
2120
2121 mixer_close(mixer);
2122 mixer = NULL;
2123 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002124 if (snd_card_name)
2125 free(snd_card_name);
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302126 if (mixer)
2127 mixer_close(mixer);
2128 if (hw_info)
2129 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002130
2131 if (snd_card_num >= MAX_SND_CARD) {
2132 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2133 return -1;
2134 }
2135
2136 return snd_card_num;
2137}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302138
2139#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2140int audio_extn_utils_compress_enable_drift_correction(
2141 struct stream_out *out,
2142 struct audio_out_enable_drift_correction *drift)
2143{
2144 struct snd_compr_metadata metadata;
2145 int ret = -EINVAL;
2146
2147 if(drift == NULL) {
2148 ALOGE("%s:: Invalid param", __func__);
2149 goto exit;
2150 }
2151
2152 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2153
2154 if (!is_offload_usecase(out->usecase)) {
2155 ALOGE("%s:: not supported for non offload session", __func__);
2156 goto exit;
2157 }
2158
2159 if (!out->compr) {
2160 ALOGW("%s:: offload session not yet opened,"
2161 "start delay will be configure later", __func__);
2162 goto exit;
2163 }
2164
2165 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2166 metadata.value[0] = drift->enable;
2167 out->drift_correction_enabled = drift->enable;
2168
2169 ret = compress_set_metadata(out->compr, &metadata);
2170 if(ret) {
2171 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2172 out->drift_correction_enabled = false;
2173 }
2174
2175exit:
2176 return ret;
2177}
2178#else
2179int audio_extn_utils_compress_enable_drift_correction(
2180 struct stream_out *out __unused,
2181 struct audio_out_enable_drift_correction *drift __unused)
2182{
2183 ALOGD("%s:: configuring drift enablement not supported", __func__);
2184 return 0;
2185}
2186#endif
2187
2188#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2189int audio_extn_utils_compress_correct_drift(
2190 struct stream_out *out,
2191 struct audio_out_correct_drift *drift_param)
2192{
2193 struct snd_compr_metadata metadata;
2194 int ret = -EINVAL;
2195
2196 if (drift_param == NULL) {
2197 ALOGE("%s:: Invalid drift_param", __func__);
2198 goto exit;
2199 }
2200
2201 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2202 drift_param->adjust_time);
2203
2204 if (!is_offload_usecase(out->usecase)) {
2205 ALOGE("%s:: not supported for non offload session", __func__);
2206 goto exit;
2207 }
2208
2209 if (!out->compr) {
2210 ALOGW("%s:: offload session not yet opened", __func__);
2211 goto exit;
2212 }
2213
2214 if (!out->drift_correction_enabled) {
2215 ALOGE("%s:: drift correction not enabled", __func__);
2216 goto exit;
2217 }
2218
2219 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2220 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2221 metadata.value[1] = \
2222 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2223
2224 ret = compress_set_metadata(out->compr, &metadata);
2225 if(ret)
2226 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2227exit:
2228 return ret;
2229}
2230#else
2231int audio_extn_utils_compress_correct_drift(
2232 struct stream_out *out __unused,
2233 struct audio_out_correct_drift *drift_param __unused)
2234{
2235 ALOGD("%s:: setting adjust clock not supported", __func__);
2236 return 0;
2237}
2238#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302239
2240int audio_extn_utils_set_channel_map(
2241 struct stream_out *out,
2242 struct audio_out_channel_map_param *channel_map_param)
2243{
2244 int ret = -EINVAL, i = 0;
2245 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2246
2247 if (channel_map_param == NULL) {
2248 ALOGE("%s:: Invalid channel_map", __func__);
2249 goto exit;
2250 }
2251
2252 if (channel_map_param->channels != channels) {
2253 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2254 __func__, channel_map_param->channels, channels);
2255 goto exit;
2256 }
2257
2258 for ( i = 0; i < channels; i++) {
2259 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2260 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2261 }
2262 ret = 0;
2263exit:
2264 return ret;
2265}
Varun Balaraje49253e2017-07-06 19:48:56 +05302266
2267int audio_extn_utils_set_pan_scale_params(
2268 struct stream_out *out,
2269 struct mix_matrix_params *mm_params)
2270{
2271 int ret = -EINVAL, i = 0, j = 0;
2272
Varun Balaraj003ee752017-08-07 17:54:55 +05302273 if (mm_params == NULL || out == NULL) {
2274 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302275 goto exit;
2276 }
2277
2278 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2279 mm_params->num_output_channels <= 0 ||
2280 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2281 mm_params->num_input_channels <= 0)
2282 goto exit;
2283
2284 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2285 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2286 out->pan_scale_params.has_output_channel_map =
2287 mm_params->has_output_channel_map;
2288 for (i = 0; i < mm_params->num_output_channels; i++)
2289 out->pan_scale_params.output_channel_map[i] =
2290 mm_params->output_channel_map[i];
2291
2292 out->pan_scale_params.has_input_channel_map =
2293 mm_params->has_input_channel_map;
2294 for (i = 0; i < mm_params->num_input_channels; i++)
2295 out->pan_scale_params.input_channel_map[i] =
2296 mm_params->input_channel_map[i];
2297
2298 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2299 for (i = 0; i < mm_params->num_output_channels; i++)
2300 for (j = 0; j < mm_params->num_input_channels; j++) {
2301 //Convert the channel coefficient gains in Q14 format
2302 out->pan_scale_params.mixer_coeffs[i][j] =
2303 mm_params->mixer_coeffs[i][j] * (2 << 13);
2304 }
2305
2306 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2307 out->pcm_device_id,
2308 out->pan_scale_params);
2309
2310exit:
2311 return ret;
2312}
2313
2314int audio_extn_utils_set_downmix_params(
2315 struct stream_out *out,
2316 struct mix_matrix_params *mm_params)
2317{
2318 int ret = -EINVAL, i = 0, j = 0;
2319 struct audio_usecase *usecase = NULL;
2320
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002321 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302322 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302323 goto exit;
2324 }
2325
2326 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2327 mm_params->num_output_channels <= 0 ||
2328 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2329 mm_params->num_input_channels <= 0)
2330 goto exit;
2331
2332 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302333 if (!usecase) {
2334 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002335 goto exit;
2336 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302337 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2338 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2339
2340 out->downmix_params.has_output_channel_map =
2341 mm_params->has_output_channel_map;
2342 for (i = 0; i < mm_params->num_output_channels; i++) {
2343 out->downmix_params.output_channel_map[i] =
2344 mm_params->output_channel_map[i];
2345 }
2346
2347 out->downmix_params.has_input_channel_map =
2348 mm_params->has_input_channel_map;
2349 for (i = 0; i < mm_params->num_input_channels; i++)
2350 out->downmix_params.input_channel_map[i] =
2351 mm_params->input_channel_map[i];
2352
2353 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2354 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302355 for (j = 0; j < mm_params->num_input_channels; j++) {
2356 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302357 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302358 mm_params->mixer_coeffs[i][j] * (2 << 13);
2359 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302360
2361 ret = platform_set_stream_downmix_params(out->dev->platform,
2362 out->pcm_device_id,
2363 usecase->out_snd_device,
2364 out->downmix_params);
2365
2366exit:
2367 return ret;
2368}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302369
2370bool audio_extn_utils_is_dolby_format(audio_format_t format)
2371{
2372 if (format == AUDIO_FORMAT_AC3 ||
2373 format == AUDIO_FORMAT_E_AC3 ||
2374 format == AUDIO_FORMAT_E_AC3_JOC)
2375 return true;
2376 else
2377 return false;
2378}
2379
`Deeraj Soman676c2702017-09-18 19:25:53 +05302380int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2381{
2382 int i;
2383 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2384 {"S32_LE", 32},
2385 {"S24_LE", 24},
2386 {"S16_LE", 16}};
2387 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302388
`Deeraj Soman676c2702017-09-18 19:25:53 +05302389 for (i = 0; i < num_configs; i++) {
2390 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2391 return mixer_bitwidth_config[i].value;
2392 }
2393
2394 return -EINVAL;
2395}
2396
2397int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2398{
2399 int i;
2400 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2401 {"KHZ_48", 48000},
2402 {"KHZ_96", 96000},
2403 {"KHZ_144", 144000},
2404 {"KHZ_192", 192000},
2405 {"KHZ_384", 384000},
2406 {"KHZ_44P1", 44100},
2407 {"KHZ_88P2", 88200},
2408 {"KHZ_176P4", 176400},
2409 {"KHZ_352P8", 352800}};
2410 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2411
2412 for (i = 0; i < num_configs; i++) {
2413 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2414 return mixer_samplerate_config[i].value;
2415 }
2416
2417 return -EINVAL;
2418}
2419
2420int audio_extn_utils_get_channels_from_string(const char *id_string)
2421{
2422 int i;
2423 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2424 {"Two", 2},
2425 {"Three",3},
2426 {"Four", 4},
2427 {"Five", 5},
2428 {"Six", 6},
2429 {"Seven", 7},
2430 {"Eight", 8}};
2431 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2432
2433 for (i = 0; i < num_configs; i++) {
2434 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2435 return mixer_channels_config[i].value;
2436 }
2437
2438 return -EINVAL;
2439}