blob: c7ad1a3f2b4f8b13b75182666539b6327afe0d2f [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),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530142 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143};
144
145const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530146 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700147 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530148 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
149 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530150 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700151 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
152 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
153 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700154 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
155 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700156 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700157 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700158 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530159 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700160 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700161 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530162#ifdef AUDIO_EXTN_FORMATS_ENABLED
163 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700164 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
165 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
166 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
168 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
169 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
170 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
171 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
172 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
173 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530175 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
176 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700177 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800178 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
179 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
180 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530181 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530182 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
183 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
184 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530185 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530186 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
187 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
188 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
189 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530190 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700191#endif
192};
193
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530194/* payload structure avt_device drift query */
195struct audio_avt_device_drift_stats {
196 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530197
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530198 /* Indicates the device interface direction as either
199 * source (Tx) or sink (Rx).
200 */
201 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530202
203 /* Reference timer for drift accumulation and time stamp information.
204 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
205 */
206 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530207 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530208} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530209
Ben Rombergera04fabc2014-11-14 12:16:03 -0800210static char bTable[BASE_TABLE_SIZE] = {
211 'A','B','C','D','E','F','G','H','I','J','K','L',
212 'M','N','O','P','Q','R','S','T','U','V','W','X',
213 'Y','Z','a','b','c','d','e','f','g','h','i','j',
214 'k','l','m','n','o','p','q','r','s','t','u','v',
215 'w','x','y','z','0','1','2','3','4','5','6','7',
216 '8','9','+','/'
217};
218
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700219static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
220 const char *name)
221{
222 size_t i;
223 for (i = 0; i < size; i++) {
224 if (strcmp(table[i].name, name) == 0) {
225 ALOGV("%s found %s", __func__, table[i].name);
226 return table[i].value;
227 }
228 }
229 return 0;
230}
231
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530232static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700233{
234 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530235 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800236 char *last_r;
237 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700238 while (flag_name != NULL) {
239 if (strlen(flag_name) != 0) {
240 flag |= string_to_enum(s_flag_name_to_enum_table,
241 ARRAY_SIZE(s_flag_name_to_enum_table),
242 flag_name);
243 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800244 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700245 }
246
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800247 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530248 io_flags.in_flags = (audio_input_flags_t)flag;
249 io_flags.out_flags = (audio_output_flags_t)flag;
250 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251}
252
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530253static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700254{
255 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800256 char *last_r;
257 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700258
259 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
260 return;
261
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530262 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700263 while (str != NULL) {
264 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
265 ARRAY_SIZE(s_format_name_to_enum_table), str);
266 ALOGV("%s: format - %d", __func__, format);
267 if (format != 0) {
268 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700269 if (sf_info == NULL)
270 break; /* return whatever was parsed */
271
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700272 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530273 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700274 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800275 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700276 }
277}
278
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530279static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700281 struct stream_sample_rate *ss_info = NULL;
282 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800283 char *last_r;
284 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700285
Amit Shekhar6f461b12014-08-01 14:52:58 -0700286 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
287 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700288
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530289 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700290 while (str != NULL) {
291 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
292 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
293 if (0 != sample_rate) {
294 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800295 if (!ss_info) {
296 ALOGE("%s: memory allocation failure", __func__);
297 return;
298 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700299 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530300 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700301 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800302 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700303 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700304}
305
306static int parse_bit_width_names(char *name)
307{
308 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800309 char *last_r;
310 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700311
312 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
313 bit_width = (int)strtol(str, (char **)NULL, 10);
314
315 ALOGV("%s: bit_width - %d", __func__, bit_width);
316 return bit_width;
317}
318
319static int parse_app_type_names(void *platform, char *name)
320{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700321 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800322 char *last_r;
323 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700324
325 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
326 app_type = (int)strtol(str, (char **)NULL, 10);
327
328 ALOGV("%s: app_type - %d", __func__, app_type);
329 return app_type;
330}
331
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530332static void update_streams_cfg_list(cnode *root, void *platform,
333 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700334{
335 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530336 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700337
338 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530339 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700340
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530341 if (!s_info) {
342 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700343 return;
344 }
345
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700346 while (node) {
347 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530348 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530349 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
350 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700351 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530352 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700353 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530354 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
355 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700356 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530357 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700358 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530359 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700360 }
361 node = node->next;
362 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530363 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700364}
365
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530366static void load_cfg_list(cnode *root, void *platform,
367 struct listnode *streams_output_cfg_list,
368 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700369{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530370 cnode *node = NULL;
371
372 node = config_find(root, OUTPUTS_TAG);
373 if (node != NULL) {
374 node = node->first_child;
375 while (node) {
376 ALOGV("%s: loading output %s", __func__, node->name);
377 update_streams_cfg_list(node, platform, streams_output_cfg_list);
378 node = node->next;
379 }
380 } else {
381 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 }
383
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530384 node = config_find(root, INPUTS_TAG);
385 if (node != NULL) {
386 node = node->first_child;
387 while (node) {
388 ALOGV("%s: loading input %s", __func__, node->name);
389 update_streams_cfg_list(node, platform, streams_input_cfg_list);
390 node = node->next;
391 }
392 } else {
393 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700394 }
395}
396
397static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530398 struct listnode *streams_output_cfg_list,
399 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700400{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530401 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700402 int length = 0, i, num_app_types = 0;
403 struct listnode *node;
404 bool update;
405 struct mixer_ctl *ctl = NULL;
406 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530407 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800408 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700409
410 if (!mixer) {
411 ALOGE("%s: mixer is null",__func__);
412 return;
413 }
414 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
415 if (!ctl) {
416 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
417 return;
418 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530419 app_type_cfg[length++] = num_app_types;
420
421 if (list_empty(streams_output_cfg_list)) {
422 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700423 app_type_cfg[length++] = 48000;
424 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530425 num_app_types += 1;
426 }
427 if (list_empty(streams_input_cfg_list)) {
428 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
429 app_type_cfg[length++] = 48000;
430 app_type_cfg[length++] = 16;
431 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700432 }
433
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800434 /* get target bit width for ADM enforce mode */
435 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
436
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700437 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530438 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700439 update = true;
440 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530441 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700442 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530443 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530444 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530445 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530446 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530447 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800448 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
449 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
450 (target_bit_width > app_type_cfg[i+3]))
451 app_type_cfg[i+3] = target_bit_width;
452
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700453 update = false;
454 break;
455 }
456 }
457 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530458 num_app_types += 1;
459 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
460 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800461 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
462 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
463 (target_bit_width > app_type_cfg[length]))
464 app_type_cfg[length] = target_bit_width;
465
466 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530467 }
468 }
469 list_for_each(node, streams_input_cfg_list) {
470 s_info = node_to_item(node, struct streams_io_cfg, list);
471 update = true;
472 for (i=0; i<length; i=i+3) {
473 if (app_type_cfg[i+1] == 0)
474 break;
475 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530476 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530477 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530478 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530479 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
480 update = false;
481 break;
482 }
483 }
484 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
485 num_app_types += 1;
486 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
487 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
488 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700489 }
490 }
491 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
492 if (num_app_types) {
493 app_type_cfg[0] = num_app_types;
494 mixer_ctl_set_array(ctl, app_type_cfg, length);
495 }
496}
497
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530498void audio_extn_utils_update_streams_cfg_lists(void *platform,
499 struct mixer *mixer,
500 struct listnode *streams_output_cfg_list,
501 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700502{
503 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530504 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700505
506 ALOGV("%s", __func__);
507 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530508 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700509
510 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700511 if (root == NULL) {
512 ALOGE("cfg_list, NULL config root");
513 return;
514 }
515
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530516 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
517 if (data == NULL) {
518 ALOGD("%s: failed to open io config file(%s), trying older config file",
519 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
520 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
521 if (data == NULL) {
522 send_app_type_cfg(platform, mixer,
523 streams_output_cfg_list,
524 streams_input_cfg_list);
525 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800526 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530527 return;
528 }
529 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700530
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530531 config_load(root, data);
532 load_cfg_list(root, platform, streams_output_cfg_list,
533 streams_input_cfg_list);
534
535 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
536 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800537
538 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800539 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800540 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700541}
542
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530543static void audio_extn_utils_dump_streams_cfg_list(
544 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700545{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700546 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530547 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700548 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700549 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530550
551 list_for_each(node_i, streams_cfg_list) {
552 s_info = node_to_item(node_i, struct streams_io_cfg, list);
553 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
554 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
555 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
556 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700557 sf_info = node_to_item(node_j, struct stream_format, list);
558 ALOGV("format-%x", sf_info->format);
559 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530560 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700561 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
562 ALOGV("sample rate-%d", ss_info->sample_rate);
563 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700564 }
565}
566
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530567void audio_extn_utils_dump_streams_cfg_lists(
568 struct listnode *streams_output_cfg_list,
569 struct listnode *streams_input_cfg_list)
570{
571 ALOGV("%s", __func__);
572 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
573 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
574}
575
576static void audio_extn_utils_release_streams_cfg_list(
577 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700578{
579 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530580 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700581
582 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530583
584 while (!list_empty(streams_cfg_list)) {
585 node_i = list_head(streams_cfg_list);
586 s_info = node_to_item(node_i, struct streams_io_cfg, list);
587 while (!list_empty(&s_info->format_list)) {
588 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700589 list_remove(node_j);
590 free(node_to_item(node_j, struct stream_format, list));
591 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530592 while (!list_empty(&s_info->sample_rate_list)) {
593 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700594 list_remove(node_j);
595 free(node_to_item(node_j, struct stream_sample_rate, list));
596 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700597 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530598 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700599 }
600}
601
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530602void audio_extn_utils_release_streams_cfg_lists(
603 struct listnode *streams_output_cfg_list,
604 struct listnode *streams_input_cfg_list)
605{
606 ALOGV("%s", __func__);
607 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
608 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
609}
610
611static bool set_app_type_cfg(struct streams_io_cfg *s_info,
612 struct stream_app_type_cfg *app_type_cfg,
613 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700614 {
615 struct listnode *node_i;
616 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530617 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700618 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
619 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530620 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700621
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530622 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700623 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530624 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700625 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
626 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
627 return true;
628 }
629 }
630 /*
631 * Reiterate through the list assuming dafault sample rate.
632 * Handles scenario where input sample rate is higher
633 * than all sample rates in list for the input bit width.
634 */
635 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800636
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530637 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700638 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
639 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530640 (bit_width == s_info->app_type_cfg.bit_width)) {
641 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700642 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530643 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800644 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 -0700645 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
646 return true;
647 }
648 }
649 return false;
650}
651
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530652void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
653 struct listnode *streams_input_cfg_list,
654 audio_devices_t devices __unused,
655 audio_input_flags_t flags,
656 audio_format_t format,
657 uint32_t sample_rate,
658 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530659 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530660 struct stream_app_type_cfg *app_type_cfg)
661{
662 struct listnode *node_i, *node_j;
663 struct streams_io_cfg *s_info;
664 struct stream_format *sf_info;
665
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530666 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
667 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530668
669 list_for_each(node_i, streams_input_cfg_list) {
670 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530671 /* Along with flags do profile matching if set at either end.*/
672 if (s_info->flags.in_flags == flags &&
673 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
674 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530675 list_for_each(node_j, &s_info->format_list) {
676 sf_info = node_to_item(node_j, struct stream_format, list);
677 if (sf_info->format == format) {
678 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
679 return;
680 }
681 }
682 }
683 }
684 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
685 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
686 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
687 app_type_cfg->bit_width = 16;
688}
689
690void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700691 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700692 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700693 audio_output_flags_t flags,
694 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700695 uint32_t sample_rate,
696 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530697 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530698 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700699 struct stream_app_type_cfg *app_type_cfg)
700{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530701 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530702 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700703 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530704 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700705
Ashish Jain058165c2016-09-28 23:18:48 +0530706 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700707 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700708 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
709 if (-ENOSYS != bw)
710 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700711 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
712 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
713 }
714
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700715 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530716 if (!strncmp("true", value, sizeof("true"))) {
717 if ((popcount(channel_mask) > 2) &&
718 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700719 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530720 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
721 ALOGD("%s: MCH session defaulting sample rate to %d",
722 __func__, sample_rate);
723 }
724 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530725
726 /* Set sampling rate to 176.4 for DSD64
727 * and 352.8Khz for DSD128.
728 * Set Bit Width to 16. output will be 16 bit
729 * post DoP in ASM.
730 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700731 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530732 (format == AUDIO_FORMAT_DSD)) {
733 bit_width = 16;
734 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
735 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
736 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
737 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
738 }
739
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530740 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
741 //TODO: Handle fractional sampling rate configuration for LL
742 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
743 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
744 __func__, sample_rate, bit_width);
745 }
746
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800747 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
748 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700749 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530750 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530751 /* Along with flags do profile matching if set at either end.*/
752 if (s_info->flags.out_flags == flags &&
753 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
754 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530755 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700756 sf_info = node_to_item(node_j, struct stream_format, list);
757 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530758 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700759 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700760 }
761 }
762 }
763 }
764 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530765 s_info = node_to_item(node_i, struct streams_io_cfg, list);
766 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530768 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
769 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
770 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700771 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530772 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700773 return;
774 }
775 }
776 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700777 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700778 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700779 app_type_cfg->bit_width = 16;
780}
781
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530782static bool audio_is_this_native_usecase(struct audio_usecase *uc)
783{
784 bool native_usecase = false;
785 struct stream_out *out = (struct stream_out*) uc->stream.out;
786
787 if (PCM_PLAYBACK == uc->type && out != NULL &&
788 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
789 is_offload_usecase(uc->id) &&
790 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
791 native_usecase = true;
792
793 return native_usecase;
794}
795
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800796bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
797{
798 /* DSP bitwidth enforce mode for ADM and AFE:
799 * includes:
800 * deep buffer, low latency, direct pcm and offload.
801 * excludes:
802 * ull(raw+fast), VOIP.
803 */
804 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
805 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
806 (flags & AUDIO_OUTPUT_FLAG_FAST)))
807 return false;
808
809
810 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
811 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
812 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
813 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
814 return true;
815 else
816 return false;
817}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800818
819static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
820{
821 return adev->vr_audio_mode_enabled;
822}
823
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530824void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
825 struct audio_device *adev,
826 struct audio_usecase *usecase)
827{
828 ALOGV("%s", __func__);
829
830 switch(usecase->type) {
831 case PCM_PLAYBACK:
832 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
833 &adev->streams_output_cfg_list,
834 usecase->stream.out->devices,
835 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700836 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530837 usecase->stream.out->sample_rate,
838 usecase->stream.out->bit_width,
839 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530840 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530841 &usecase->stream.out->app_type_cfg);
842 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
843 break;
844 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700845 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
846 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
847 else
848 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530849 &adev->streams_input_cfg_list,
850 usecase->stream.in->device,
851 usecase->stream.in->flags,
852 usecase->stream.in->format,
853 usecase->stream.in->sample_rate,
854 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530855 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530856 &usecase->stream.in->app_type_cfg);
857 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
858 break;
Siddartha Shaik343abc62017-08-08 11:15:25 +0530859 case TRANSCODE_LOOPBACK :
860 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
861 &adev->streams_output_cfg_list,
862 usecase->stream.inout->out_config.devices,
863 0,
864 usecase->stream.inout->out_config.format,
865 usecase->stream.inout->out_config.sample_rate,
866 usecase->stream.inout->out_config.bit_width,
867 usecase->stream.inout->out_config.channel_mask,
868 usecase->stream.inout->profile,
869 &usecase->stream.inout->out_app_type_cfg);
870 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
871 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530872 default:
873 ALOGE("%s: app type cfg not supported for usecase type (%d)",
874 __func__, usecase->type);
875 }
876}
877
Siena Richard7c2db772016-12-21 11:32:34 -0800878static int send_app_type_cfg_for_device(struct audio_device *adev,
879 struct audio_usecase *usecase,
880 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700881{
882 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530883 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
884 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700885 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800886 int pcm_device_id = 0, acdb_dev_id, app_type;
887 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530888 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530889 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +0530890 struct streams_io_cfg *s_info = NULL;
891 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530892 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700893
Siena Richard7c2db772016-12-21 11:32:34 -0800894 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
895 __func__, platform_get_snd_device_name(usecase->out_snd_device),
896 platform_get_snd_device_name(usecase->in_snd_device),
897 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700898
Siddartha Shaik343abc62017-08-08 11:15:25 +0530899 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
900 usecase->type != TRANSCODE_LOOPBACK) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530901 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700902 rc = 0;
903 goto exit_send_app_type_cfg;
904 }
905 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
906 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
907 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800908 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700909 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Siddartha Shaik343abc62017-08-08 11:15:25 +0530910 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK) &&
Varun Balaraje49253e2017-07-06 19:48:56 +0530911 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530912 (!is_offload_usecase(usecase->id)) &&
913 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530914 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 -0700915 rc = 0;
916 goto exit_send_app_type_cfg;
917 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800918
919 //if VR is active then only send the mixer control
920 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
921 ALOGI("ULL doesnt need sending app type cfg, returning");
922 rc = 0;
923 goto exit_send_app_type_cfg;
924 }
925
Siddartha Shaik343abc62017-08-08 11:15:25 +0530926 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700927 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530928 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
929 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700930 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700931 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530932 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
933 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530934 }
Siena Richard7c2db772016-12-21 11:32:34 -0800935
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700936 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
937 if (!ctl) {
938 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
939 mixer_ctl_name);
940 rc = -EINVAL;
941 goto exit_send_app_type_cfg;
942 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530943 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530944
Rohit Kumar1181b292017-01-31 18:07:17 +0530945 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
946 if (acdb_dev_id <= 0) {
947 ALOGE("%s: Couldn't get the acdb dev id", __func__);
948 rc = -EINVAL;
949 goto exit_send_app_type_cfg;
950 }
951
Siena Richard7c2db772016-12-21 11:32:34 -0800952 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
953 if (snd_device_be_idx < 0) {
954 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
955 __func__, platform_get_snd_device_name(snd_device),
956 snd_device_be_idx);
957 }
958
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530959 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530960
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700961 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530962 if (!strncmp("true", value, sizeof("true"))) {
963 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
964 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700965 !(usecase->stream.out->flags &
966 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530967 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
968 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530969
kunleizcdf25942017-09-20 14:01:21 +0800970 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
971 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
972 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700973 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530974 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
975 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
976 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
977 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
978 /*
979 * To best utlize DSP, check if the stream sample rate is supported/multiple of
980 * configured device sample rate, if not update the COPP rate to be equal to the
981 * device sample rate, else open COPP at stream sample rate
982 */
983 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
984 usecase->stream.out->sample_rate,
985 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530986 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
987 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700988 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
989 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530990 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700991 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
992 }
993 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
994
Varun Balaraje49253e2017-07-06 19:48:56 +0530995 /* Interactive streams are supported with only direct app type id.
996 * Get Direct profile app type and use it for interactive streams
997 */
998 list_for_each(node, &adev->streams_output_cfg_list) {
999 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001000 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301001 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1002 AUDIO_OUTPUT_FLAG_DIRECT))
1003 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301004 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001005 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301006 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301007 else
1008 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001009 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001010 app_type_cfg[len++] = acdb_dev_id;
1011 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001012 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1013 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301014 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1015 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001016
1017 sample_rate = sample_rate * 4;
1018 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1019 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001020 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001021 app_type_cfg[len++] = sample_rate;
1022
Siena Richard7c2db772016-12-21 11:32:34 -08001023 if (snd_device_be_idx > 0)
1024 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301025
Siena Richard7c2db772016-12-21 11:32:34 -08001026 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1027 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301028
1029 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001030 app_type = usecase->stream.in->app_type_cfg.app_type;
1031 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301032 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001033 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1034 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001035 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1036 app_type_cfg[len++] = sample_rate;
1037 if (snd_device_be_idx > 0)
1038 app_type_cfg[len++] = snd_device_be_idx;
1039 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1040 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301041 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001042 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301043 if(usecase->type == TRANSCODE_LOOPBACK) {
1044 sample_rate = usecase->stream.inout->out_config.sample_rate;
1045 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1046 }
Siena Richard7c2db772016-12-21 11:32:34 -08001047 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301048 app_type_cfg[len++] = acdb_dev_id;
1049 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001050 if (snd_device_be_idx > 0)
1051 app_type_cfg[len++] = snd_device_be_idx;
1052 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1053 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301054 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301055
Siddartha Shaik343abc62017-08-08 11:15:25 +05301056 if(ctl)
1057 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001058 rc = 0;
1059exit_send_app_type_cfg:
1060 return rc;
1061}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001062
Siena Richard7c2db772016-12-21 11:32:34 -08001063int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1064 struct audio_usecase *usecase)
1065{
1066 int i, num_devices = 0;
1067 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
1068 int rc = 0;
1069
1070 switch (usecase->type) {
1071 case PCM_PLAYBACK:
Siddartha Shaik343abc62017-08-08 11:15:25 +05301072 case TRANSCODE_LOOPBACK:
Siena Richard7c2db772016-12-21 11:32:34 -08001073 ALOGD("%s: usecase->out_snd_device %s",
1074 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1075 /* check for out combo device */
1076 if (platform_split_snd_device(adev->platform,
1077 usecase->out_snd_device,
1078 &num_devices, new_snd_devices)) {
1079 new_snd_devices[0] = usecase->out_snd_device;
1080 num_devices = 1;
1081 }
1082 break;
1083 case PCM_CAPTURE:
1084 ALOGD("%s: usecase->in_snd_device %s",
1085 __func__, platform_get_snd_device_name(usecase->in_snd_device));
1086 /* check for in combo device */
1087 if (platform_split_snd_device(adev->platform,
1088 usecase->in_snd_device,
1089 &num_devices, new_snd_devices)) {
1090 new_snd_devices[0] = usecase->in_snd_device;
1091 num_devices = 1;
1092 }
1093 break;
1094 default:
1095 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1096 rc = 0;
1097 break;
1098 }
1099
1100 for (i = 0; i < num_devices; i++) {
1101 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1102 if (rc)
1103 break;
1104 }
1105
1106 return rc;
1107}
1108
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301109int read_line_from_file(const char *path, char *buf, size_t count)
1110{
1111 char * fgets_ret;
1112 FILE * fd;
1113 int rv;
1114
1115 fd = fopen(path, "r");
1116 if (fd == NULL)
1117 return -1;
1118
1119 fgets_ret = fgets(buf, (int)count, fd);
1120 if (NULL != fgets_ret) {
1121 rv = (int)strlen(buf);
1122 } else {
1123 rv = ferror(fd);
1124 }
1125 fclose(fd);
1126
1127 return rv;
1128}
1129
Ashish Jainf1eaa582016-05-23 20:54:24 +05301130/*Translates ALSA formats to AOSP PCM formats*/
1131audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1132{
1133 audio_format_t format;
1134
1135 switch(alsa_format) {
1136 case SNDRV_PCM_FORMAT_S16_LE:
1137 format = AUDIO_FORMAT_PCM_16_BIT;
1138 break;
1139 case SNDRV_PCM_FORMAT_S24_3LE:
1140 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1141 break;
1142 case SNDRV_PCM_FORMAT_S24_LE:
1143 format = AUDIO_FORMAT_PCM_8_24_BIT;
1144 break;
1145 case SNDRV_PCM_FORMAT_S32_LE:
1146 format = AUDIO_FORMAT_PCM_32_BIT;
1147 break;
1148 default:
1149 ALOGW("Incorrect ALSA format");
1150 format = AUDIO_FORMAT_INVALID;
1151 }
1152 return format;
1153}
1154
1155/*Translates hal format (AOSP) to alsa formats*/
1156uint32_t hal_format_to_alsa(audio_format_t hal_format)
1157{
1158 uint32_t alsa_format;
1159
1160 switch (hal_format) {
1161 case AUDIO_FORMAT_PCM_32_BIT: {
1162 if (platform_supports_true_32bit())
1163 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1164 else
1165 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1166 }
1167 break;
1168 case AUDIO_FORMAT_PCM_8_BIT:
1169 alsa_format = SNDRV_PCM_FORMAT_S8;
1170 break;
1171 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1172 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1173 break;
1174 case AUDIO_FORMAT_PCM_8_24_BIT: {
1175 if (platform_supports_true_32bit())
1176 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1177 else
1178 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1179 }
1180 break;
1181 case AUDIO_FORMAT_PCM_FLOAT:
1182 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1183 break;
1184 default:
1185 case AUDIO_FORMAT_PCM_16_BIT:
1186 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1187 break;
1188 }
1189 return alsa_format;
1190}
1191
Ashish Jain83a6cc22016-06-28 14:34:17 +05301192/*Translates PCM formats to AOSP formats*/
1193audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1194{
1195 audio_format_t format = AUDIO_FORMAT_INVALID;
1196
1197 switch(pcm_format) {
1198 case PCM_FORMAT_S16_LE:
1199 format = AUDIO_FORMAT_PCM_16_BIT;
1200 break;
1201 case PCM_FORMAT_S24_3LE:
1202 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1203 break;
1204 case PCM_FORMAT_S24_LE:
1205 format = AUDIO_FORMAT_PCM_8_24_BIT;
1206 break;
1207 case PCM_FORMAT_S32_LE:
1208 format = AUDIO_FORMAT_PCM_32_BIT;
1209 break;
1210 default:
1211 ALOGW("Incorrect PCM format");
1212 format = AUDIO_FORMAT_INVALID;
1213 }
1214 return format;
1215}
1216
1217/*Translates hal format (AOSP) to alsa formats*/
1218uint32_t hal_format_to_pcm(audio_format_t hal_format)
1219{
1220 uint32_t pcm_format;
1221
1222 switch (hal_format) {
1223 case AUDIO_FORMAT_PCM_32_BIT:
1224 case AUDIO_FORMAT_PCM_8_24_BIT:
1225 case AUDIO_FORMAT_PCM_FLOAT: {
1226 if (platform_supports_true_32bit())
1227 pcm_format = PCM_FORMAT_S32_LE;
1228 else
1229 pcm_format = PCM_FORMAT_S24_3LE;
1230 }
1231 break;
1232 case AUDIO_FORMAT_PCM_8_BIT:
1233 pcm_format = PCM_FORMAT_S8;
1234 break;
1235 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1236 pcm_format = PCM_FORMAT_S24_3LE;
1237 break;
1238 default:
1239 case AUDIO_FORMAT_PCM_16_BIT:
1240 pcm_format = PCM_FORMAT_S16_LE;
1241 break;
1242 }
1243 return pcm_format;
1244}
1245
Ashish Jainf1eaa582016-05-23 20:54:24 +05301246uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1247 uint32_t sample_rate,
1248 uint32_t noOfChannels)
1249{
1250 uint32_t fragment_size = 0;
1251 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1252
1253 fragment_size = (pcm_offload_time
1254 * sample_rate
1255 * bytes_per_sample
1256 * noOfChannels)/1000;
1257 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1258 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1259 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1260 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1261 /*To have same PCM samples for all channels, the buffer size requires to
1262 *be multiple of (number of channels * bytes per sample)
1263 *For writes to succeed, the buffer must be written at address which is multiple of 32
1264 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001265 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301266
1267 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1268 return fragment_size;
1269}
1270
1271/* Calculates the fragment size required to configure compress session.
1272 * Based on the alsa format selected, decide if conversion is needed in
1273
1274 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1275 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1276 */
1277void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1278{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301279 audio_format_t dst_format = out->hal_op_format;
1280 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301281 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1282 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1283
1284 out->compr_config.fragment_size =
1285 get_alsa_fragment_size(hal_op_bytes_per_sample,
1286 out->sample_rate,
1287 popcount(out->channel_mask));
1288
1289 if ((src_format != dst_format) &&
1290 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1291
Ashish Jain83a6cc22016-06-28 14:34:17 +05301292 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301293 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1294 hal_op_bytes_per_sample);
1295 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301296 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301297 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301298 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301299 }
1300}
1301
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301302/* converts pcm format 24_8 to 8_24 inplace */
1303size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1304{
1305 size_t i = 0;
1306 int *int_buf_stream = buf;
1307
1308 if ((bytes % 4) != 0) {
1309 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1310 return -EINVAL;
1311 }
1312
1313 for (; i < (bytes / 4); i++)
1314 int_buf_stream[i] >>= 8;
1315
1316 return bytes;
1317}
1318
1319int get_snd_codec_id(audio_format_t format)
1320{
1321 int id = 0;
1322
1323 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1324 case AUDIO_FORMAT_MP3:
1325 id = SND_AUDIOCODEC_MP3;
1326 break;
1327 case AUDIO_FORMAT_AAC:
1328 id = SND_AUDIOCODEC_AAC;
1329 break;
1330 case AUDIO_FORMAT_AAC_ADTS:
1331 id = SND_AUDIOCODEC_AAC;
1332 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301333 case AUDIO_FORMAT_AAC_LATM:
1334 id = SND_AUDIOCODEC_AAC;
1335 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301336 case AUDIO_FORMAT_PCM:
1337 id = SND_AUDIOCODEC_PCM;
1338 break;
1339 case AUDIO_FORMAT_FLAC:
1340 id = SND_AUDIOCODEC_FLAC;
1341 break;
1342 case AUDIO_FORMAT_ALAC:
1343 id = SND_AUDIOCODEC_ALAC;
1344 break;
1345 case AUDIO_FORMAT_APE:
1346 id = SND_AUDIOCODEC_APE;
1347 break;
1348 case AUDIO_FORMAT_VORBIS:
1349 id = SND_AUDIOCODEC_VORBIS;
1350 break;
1351 case AUDIO_FORMAT_WMA:
1352 id = SND_AUDIOCODEC_WMA;
1353 break;
1354 case AUDIO_FORMAT_WMA_PRO:
1355 id = SND_AUDIOCODEC_WMA_PRO;
1356 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301357 case AUDIO_FORMAT_MP2:
1358 id = SND_AUDIOCODEC_MP2;
1359 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301360 case AUDIO_FORMAT_AC3:
1361 id = SND_AUDIOCODEC_AC3;
1362 break;
1363 case AUDIO_FORMAT_E_AC3:
1364 case AUDIO_FORMAT_E_AC3_JOC:
1365 id = SND_AUDIOCODEC_EAC3;
1366 break;
1367 case AUDIO_FORMAT_DTS:
1368 case AUDIO_FORMAT_DTS_HD:
1369 id = SND_AUDIOCODEC_DTS;
1370 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001371 case AUDIO_FORMAT_DOLBY_TRUEHD:
1372 id = SND_AUDIOCODEC_TRUEHD;
1373 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001374 case AUDIO_FORMAT_IEC61937:
1375 id = SND_AUDIOCODEC_IEC61937;
1376 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301377 case AUDIO_FORMAT_DSD:
1378 id = SND_AUDIOCODEC_DSD;
1379 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301380 case AUDIO_FORMAT_APTX:
1381 id = SND_AUDIOCODEC_APTX;
1382 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301383 default:
1384 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1385 }
1386
1387 return id;
1388}
1389
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001390void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1391 struct audio_usecase *usecase)
1392{
1393 int type = usecase->type;
1394
Dhananjay Kumar14245982017-01-16 20:21:00 +05301395 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001396 struct stream_out *out = usecase->stream.out;
1397 int snd_device = usecase->out_snd_device;
1398 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001399 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301400 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001401 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301402 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301403 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301404 platform_send_audio_calibration(adev->platform, usecase,
1405 usecase->stream.in->app_type_cfg.app_type,
1406 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001407 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301408 /* when app type is default. the sample rate is not used to send cal */
1409 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301410 platform_get_default_app_type_v2(adev->platform, usecase->type),
1411 48000);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301412 } else if (type == TRANSCODE_LOOPBACK && usecase->stream.inout != NULL) {
1413 int snd_device = usecase->out_snd_device;
1414 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1415 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1416 platform_send_audio_calibration(adev->platform, usecase,
1417 platform_get_default_app_type_v2(adev->platform, usecase->type),
1418 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001419 } else {
1420 /* No need to send audio calibration for voice and voip call usecases */
1421 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1422 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001423 }
1424}
1425
Ben Rombergera04fabc2014-11-14 12:16:03 -08001426// Base64 Encode and Decode
1427// Not all features supported. This must be used only with following conditions.
1428// Decode Modes: Support with and without padding
1429// CRLF not handling. So no CRLF in string to decode.
1430// Encode Modes: Supports only padding
1431int b64decode(char *inp, int ilen, uint8_t* outp)
1432{
1433 int i, j, k, ii, num;
1434 int rem, pcnt;
1435 uint32_t res=0;
1436 uint8_t getIndex[MAX_BASEINDEX_LEN];
1437 uint8_t tmp, cflag;
1438
1439 if(inp == NULL || outp == NULL || ilen <= 0) {
1440 ALOGE("[%s] received NULL pointer or zero length",__func__);
1441 return -1;
1442 }
1443
1444 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1445 for(i=0;i<BASE_TABLE_SIZE;i++) {
1446 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1447 }
1448 getIndex[(uint8_t)'=']=0;
1449
1450 j=0;k=0;
1451 num = ilen/4;
1452 rem = ilen%4;
1453 if(rem==0)
1454 num = num-1;
1455 cflag=0;
1456 for(i=0; i<num; i++) {
1457 res=0;
1458 for(ii=0;ii<4;ii++) {
1459 res = res << 6;
1460 tmp = getIndex[(uint8_t)inp[j++]];
1461 res = res | tmp;
1462 cflag = cflag | tmp;
1463 }
1464 outp[k++] = (res >> 16)&0xFF;
1465 outp[k++] = (res >> 8)&0xFF;
1466 outp[k++] = res & 0xFF;
1467 }
1468
1469 // Handle last bytes special
1470 pcnt=0;
1471 if(rem == 0) {
1472 //With padding or full data
1473 res = 0;
1474 for(ii=0;ii<4;ii++) {
1475 if(inp[j] == '=')
1476 pcnt++;
1477 res = res << 6;
1478 tmp = getIndex[(uint8_t)inp[j++]];
1479 res = res | tmp;
1480 cflag = cflag | tmp;
1481 }
1482 outp[k++] = res >> 16;
1483 if(pcnt == 2)
1484 goto done;
1485 outp[k++] = (res>>8)&0xFF;
1486 if(pcnt == 1)
1487 goto done;
1488 outp[k++] = res&0xFF;
1489 } else {
1490 //without padding
1491 res = 0;
1492 for(i=0;i<rem;i++) {
1493 res = res << 6;
1494 tmp = getIndex[(uint8_t)inp[j++]];
1495 res = res | tmp;
1496 cflag = cflag | tmp;
1497 }
1498 for(i=rem;i<4;i++) {
1499 res = res << 6;
1500 pcnt++;
1501 }
1502 outp[k++] = res >> 16;
1503 if(pcnt == 2)
1504 goto done;
1505 outp[k++] = (res>>8)&0xFF;
1506 if(pcnt == 1)
1507 goto done;
1508 outp[k++] = res&0xFF;
1509 }
1510done:
1511 if(cflag == 0xFF) {
1512 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1513 __func__, inp);
1514 return 0;
1515 }
1516 return k;
1517}
1518
1519int b64encode(uint8_t *inp, int ilen, char* outp)
1520{
1521 int i,j,k, num;
1522 int rem=0;
1523 uint32_t res=0;
1524
1525 if(inp == NULL || outp == NULL || ilen<=0) {
1526 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1527 return -1;
1528 }
1529
1530 num = ilen/3;
1531 rem = ilen%3;
1532 j=0;k=0;
1533 for(i=0; i<num; i++) {
1534 //prepare index
1535 res = inp[j++]<<16;
1536 res = res | inp[j++]<<8;
1537 res = res | inp[j++];
1538 //get output map from index
1539 outp[k++] = (char) bTable[(res>>18)&0x3F];
1540 outp[k++] = (char) bTable[(res>>12)&0x3F];
1541 outp[k++] = (char) bTable[(res>>6)&0x3F];
1542 outp[k++] = (char) bTable[res&0x3F];
1543 }
1544
1545 switch(rem) {
1546 case 1:
1547 res = inp[j++]<<16;
1548 outp[k++] = (char) bTable[res>>18];
1549 outp[k++] = (char) bTable[(res>>12)&0x3F];
1550 //outp[k++] = '=';
1551 //outp[k++] = '=';
1552 break;
1553 case 2:
1554 res = inp[j++]<<16;
1555 res = res | inp[j++]<<8;
1556 outp[k++] = (char) bTable[res>>18];
1557 outp[k++] = (char) bTable[(res>>12)&0x3F];
1558 outp[k++] = (char) bTable[(res>>6)&0x3F];
1559 //outp[k++] = '=';
1560 break;
1561 default:
1562 break;
1563 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001564 outp[k] = '\0';
1565 return k;
1566}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301567
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301568
1569int audio_extn_utils_get_codec_version(const char *snd_card_name,
1570 int card_num,
1571 char *codec_version)
1572{
1573 char procfs_path[50];
1574 FILE *fp;
1575
1576 if (strstr(snd_card_name, "tasha")) {
1577 snprintf(procfs_path, sizeof(procfs_path),
1578 "/proc/asound/card%d/codecs/tasha/version", card_num);
1579 if ((fp = fopen(procfs_path, "r")) != NULL) {
1580 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1581 fclose(fp);
1582 } else {
1583 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1584 return -ENOENT;
1585 }
1586 ALOGD("%s: codec version %s", __func__, codec_version);
1587 }
1588
1589 return 0;
1590}
1591
1592
Ashish Jain81eb2a82015-05-13 10:52:34 +05301593#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1594
1595void get_default_compressed_channel_status(
1596 unsigned char *channel_status)
1597{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301598 memset(channel_status,0,24);
1599
1600 /* block start bit in preamble bit 3 */
1601 channel_status[0] |= PROFESSIONAL;
1602 //compre out
1603 channel_status[0] |= NON_LPCM;
1604 // sample rate; fixed 48K for default/transcode
1605 channel_status[3] |= SR_48000;
1606}
1607
1608#ifdef HDMI_PASSTHROUGH_ENABLED
1609int32_t get_compressed_channel_status(void *audio_stream_data,
1610 uint32_t audio_frame_size,
1611 unsigned char *channel_status,
1612 enum audio_parser_code_type codec_type)
1613 // codec_type - AUDIO_PARSER_CODEC_AC3
1614 // - AUDIO_PARSER_CODEC_DTS
1615{
1616 unsigned char *stream;
1617 int ret = 0;
1618 stream = (unsigned char *)audio_stream_data;
1619
1620 if (audio_stream_data == NULL || audio_frame_size == 0) {
1621 ALOGW("no buffer to get channel status, return default for compress");
1622 get_default_compressed_channel_status(channel_status);
1623 return ret;
1624 }
1625
1626 memset(channel_status,0,24);
1627 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1628 {
1629 ALOGE("init audio parser failed");
1630 return -1;
1631 }
1632 ret = get_channel_status(channel_status, codec_type);
1633 return ret;
1634
1635}
1636
1637#endif
1638
1639void get_lpcm_channel_status(uint32_t sampleRate,
1640 unsigned char *channel_status)
1641{
1642 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301643 memset(channel_status,0,24);
1644 /* block start bit in preamble bit 3 */
1645 channel_status[0] |= PROFESSIONAL;
1646 //LPCM OUT
1647 channel_status[0] &= ~NON_LPCM;
1648
1649 switch (sampleRate) {
1650 case 8000:
1651 case 11025:
1652 case 12000:
1653 case 16000:
1654 case 22050:
1655 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301656 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301657 case 24000:
1658 channel_status[3] |= SR_24000;
1659 break;
1660 case 32000:
1661 channel_status[3] |= SR_32000;
1662 break;
1663 case 44100:
1664 channel_status[3] |= SR_44100;
1665 break;
1666 case 48000:
1667 channel_status[3] |= SR_48000;
1668 break;
1669 case 88200:
1670 channel_status[3] |= SR_88200;
1671 break;
1672 case 96000:
1673 channel_status[3] |= SR_96000;
1674 break;
1675 case 176400:
1676 channel_status[3] |= SR_176400;
1677 break;
1678 case 192000:
1679 channel_status[3] |= SR_192000;
1680 break;
1681 default:
1682 ALOGV("Invalid sample_rate %u\n", sampleRate);
1683 status = -1;
1684 break;
1685 }
1686}
1687
1688void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1689{
1690 unsigned char channel_status[24]={0};
1691 struct snd_aes_iec958 iec958;
1692 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1693 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301694 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301695#ifdef HDMI_PASSTHROUGH_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301696 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301697 /*TODO:Extend code to support DTS passthrough*/
1698 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301699 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301700 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1701 } else
1702#endif
1703 {
1704 /*set channel status bit for LPCM*/
1705 get_lpcm_channel_status(out->sample_rate, channel_status);
1706 }
1707
1708 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1709 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1710 if (!ctl) {
1711 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1712 __func__, mixer_ctl_name);
1713 return;
1714 }
1715 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1716 ALOGE("%s: Could not set channel status for ext HDMI ",
1717 __func__);
1718 return;
1719 }
1720
1721}
1722#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301723
1724int audio_extn_utils_get_avt_device_drift(
1725 struct audio_usecase *usecase,
1726 struct audio_avt_device_drift_param *drift_param)
1727{
1728 int ret = 0, count = 0;
1729 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301730 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301731 struct mixer_ctl *ctl = NULL;
1732 struct audio_avt_device_drift_stats drift_stats;
1733 struct audio_device *adev = NULL;
1734
1735 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301736 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1737 if (!backend) {
1738 ALOGE("%s: Unsupported device %d", __func__,
1739 usecase->stream.out->devices);
1740 ret = -EINVAL;
1741 goto done;
1742 }
1743 strlcpy(avt_device_drift_mixer_ctl_name,
1744 backend,
1745 MIXER_PATH_MAX_LENGTH);
1746
1747 count = strlen(backend);
1748 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1749 strlcat(&avt_device_drift_mixer_ctl_name[count],
1750 " DRIFT",
1751 MIXER_PATH_MAX_LENGTH - count);
1752 } else {
1753 ret = -EINVAL;
1754 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301755 }
1756 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301757 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301758 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301759 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301760 }
1761
Naresh Tanniru6160c712017-04-17 15:43:48 +05301762 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301763 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1764 if (!ctl) {
1765 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1766 __func__, avt_device_drift_mixer_ctl_name);
1767
1768 ret = -EINVAL;
1769 goto done;
1770 }
1771
1772 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1773 avt_device_drift_mixer_ctl_name);
1774
1775 mixer_ctl_update(ctl);
1776 count = mixer_ctl_get_num_values(ctl);
1777 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1778 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1779 __func__);
1780
1781 ret = -EINVAL;
1782 goto done;
1783 }
1784
1785 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1786 if (ret != 0) {
1787 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1788 __func__);
1789
1790 ret = -EINVAL;
1791 goto done;
1792 }
1793 memcpy(drift_param, &drift_stats.drift_param,
1794 sizeof(struct audio_avt_device_drift_param));
1795done:
1796 return ret;
1797}
Manish Dewangan07de2142017-02-27 19:27:20 +05301798
1799#ifdef SNDRV_COMPRESS_PATH_DELAY
1800int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1801{
1802 int ret = -EINVAL;
1803 struct snd_compr_metadata metadata;
1804 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1805
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001806 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301807 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1808 if (!(is_offload_usecase(out->usecase))) {
1809 ALOGE("%s:: not supported for non offload session", __func__);
1810 goto exit;
1811 }
1812
1813 if (!out->compr) {
1814 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1815 __func__);
1816 goto exit;
1817 }
1818
1819 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1820 ret = compress_get_metadata(out->compr, &metadata);
1821 if(ret) {
1822 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1823 goto exit;
1824 }
1825 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1826 } else {
1827 ALOGD("%s:: Using Fix DSP delay",__func__);
1828 }
1829
1830exit:
1831 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1832 return delay_ms;
1833}
1834#else
1835int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1836{
1837 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1838}
1839#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301840
1841#ifdef SNDRV_COMPRESS_RENDER_MODE
1842int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1843{
1844 struct snd_compr_metadata metadata;
1845 int ret = -EINVAL;
1846
1847 if (!(is_offload_usecase(out->usecase))) {
1848 ALOGE("%s:: not supported for non offload session", __func__);
1849 goto exit;
1850 }
1851
1852 if (!out->compr) {
1853 ALOGD("%s:: Invalid compress handle",
1854 __func__);
1855 goto exit;
1856 }
1857
1858 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1859
1860 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1861 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1862 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1863 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1864 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1865 } else {
1866 ret = 0;
1867 goto exit;
1868 }
1869 ret = compress_set_metadata(out->compr, &metadata);
1870 if(ret) {
1871 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1872 }
1873exit:
1874 return ret;
1875}
1876#else
1877int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1878{
1879 ALOGD("%s:: configuring render mode not supported", __func__);
1880 return 0;
1881}
1882#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301883
1884#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1885int audio_extn_utils_compress_set_clk_rec_mode(
1886 struct audio_usecase *usecase)
1887{
1888 struct snd_compr_metadata metadata;
1889 struct stream_out *out = NULL;
1890 int ret = -EINVAL;
1891
Naresh Tanniru7586e292017-04-13 10:38:13 +05301892 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301893 ALOGE("%s:: Invalid use case", __func__);
1894 goto exit;
1895 }
1896
1897 out = usecase->stream.out;
1898 if (!out) {
1899 ALOGE("%s:: invalid stream", __func__);
1900 goto exit;
1901 }
1902
1903 if (!is_offload_usecase(out->usecase)) {
1904 ALOGE("%s:: not supported for non offload session", __func__);
1905 goto exit;
1906 }
1907
1908 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1909 ALOGD("%s:: clk recovery is only supported in STC render mode",
1910 __func__);
1911 ret = 0;
1912 goto exit;
1913 }
1914
1915 if (!out->compr) {
1916 ALOGD("%s:: Invalid compress handle",
1917 __func__);
1918 goto exit;
1919 }
1920 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1921 switch(usecase->out_snd_device) {
1922 case SND_DEVICE_OUT_HDMI:
1923 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1924 case SND_DEVICE_OUT_DISPLAY_PORT:
1925 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1926 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1927 break;
1928 default:
1929 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1930 break;
1931 }
1932
1933 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1934
1935 ret = compress_set_metadata(out->compr, &metadata);
1936 if(ret) {
1937 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1938 }
1939
1940exit:
1941 return ret;
1942}
1943#else
1944int audio_extn_utils_compress_set_clk_rec_mode(
1945 struct audio_usecase *usecase __unused)
1946{
1947 ALOGD("%s:: configuring render mode not supported", __func__);
1948 return 0;
1949}
1950#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301951
1952#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1953int audio_extn_utils_compress_set_render_window(
1954 struct stream_out *out,
1955 struct audio_out_render_window_param *render_window)
1956{
1957 struct snd_compr_metadata metadata;
1958 int ret = -EINVAL;
1959
Manish Dewangan27346042017-03-01 12:56:12 +05301960 if(render_window == NULL) {
1961 ALOGE("%s:: Invalid render_window", __func__);
1962 goto exit;
1963 }
1964
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001965 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1966 __func__,render_window->render_ws, render_window->render_we);
1967
Manish Dewangan27346042017-03-01 12:56:12 +05301968 if (!is_offload_usecase(out->usecase)) {
1969 ALOGE("%s:: not supported for non offload session", __func__);
1970 goto exit;
1971 }
1972
Naresh Tanniru6160c712017-04-17 15:43:48 +05301973 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1974 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301975 ALOGD("%s:: only supported in timestamp mode, current "
1976 "render mode mode %d", __func__, out->render_mode);
1977 goto exit;
1978 }
1979
1980 if (!out->compr) {
1981 ALOGW("%s:: offload session not yet opened,"
1982 "render window will be configure later", __func__);
1983 /* store render window to reconfigure in start_output_stream() */
1984 goto exit;
1985 }
1986
1987 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1988 /*render window start value */
1989 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1990 metadata.value[1] = \
1991 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1992 /*render window end value */
1993 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1994 metadata.value[3] = \
1995 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1996
1997 ret = compress_set_metadata(out->compr, &metadata);
1998 if(ret) {
1999 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2000 }
2001
2002exit:
2003 return ret;
2004}
2005#else
2006int audio_extn_utils_compress_set_render_window(
2007 struct stream_out *out __unused,
2008 struct audio_out_render_window_param *render_window __unused)
2009{
2010 ALOGD("%s:: configuring render window not supported", __func__);
2011 return 0;
2012}
2013#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302014
2015#ifdef SNDRV_COMPRESS_START_DELAY
2016int audio_extn_utils_compress_set_start_delay(
2017 struct stream_out *out,
2018 struct audio_out_start_delay_param *delay_param)
2019{
2020 struct snd_compr_metadata metadata;
2021 int ret = -EINVAL;
2022
2023 if(delay_param == NULL) {
2024 ALOGE("%s:: Invalid delay_param", __func__);
2025 goto exit;
2026 }
2027
2028 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2029 delay_param->start_delay);
2030
2031 if (!is_offload_usecase(out->usecase)) {
2032 ALOGE("%s:: not supported for non offload session", __func__);
2033 goto exit;
2034 }
2035
Naresh Tanniru6160c712017-04-17 15:43:48 +05302036 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2037 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302038 ALOGD("%s:: only supported in timestamp mode, current "
2039 "render mode mode %d", __func__, out->render_mode);
2040 goto exit;
2041 }
2042
2043 if (!out->compr) {
2044 ALOGW("%s:: offload session not yet opened,"
2045 "start delay will be configure later", __func__);
2046 goto exit;
2047 }
2048
2049 metadata.key = SNDRV_COMPRESS_START_DELAY;
2050 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2051 metadata.value[1] = \
2052 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2053
2054 ret = compress_set_metadata(out->compr, &metadata);
2055 if(ret) {
2056 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2057 }
2058
2059exit:
2060 return ret;
2061}
2062#else
2063int audio_extn_utils_compress_set_start_delay(
2064 struct stream_out *out __unused,
2065 struct audio_out_start_delay_param *delay_param __unused)
2066{
2067 ALOGD("%s:: configuring render window not supported", __func__);
2068 return 0;
2069}
2070#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002071
2072#define MAX_SND_CARD 8
2073#define RETRY_US 500000
2074#define RETRY_NUMBER 10
2075
2076int audio_extn_utils_get_snd_card_num()
2077{
2078
2079 void *hw_info = NULL;
2080 struct mixer *mixer = NULL;
2081 int retry_num = 0;
2082 int snd_card_num = 0;
2083 char* snd_card_name = NULL;
2084
2085 while (snd_card_num < MAX_SND_CARD) {
2086 mixer = mixer_open(snd_card_num);
2087
2088 while (!mixer && retry_num < RETRY_NUMBER) {
2089 usleep(RETRY_US);
2090 mixer = mixer_open(snd_card_num);
2091 retry_num++;
2092 }
2093
2094 if (!mixer) {
2095 ALOGE("%s: Unable to open the mixer card: %d", __func__,
2096 snd_card_num);
2097 retry_num = 0;
2098 snd_card_num++;
2099 continue;
2100 }
2101
2102 snd_card_name = strdup(mixer_get_name(mixer));
2103 if (!snd_card_name) {
2104 ALOGE("failed to allocate memory for snd_card_name\n");
2105 mixer_close(mixer);
2106 return -1;
2107 }
2108 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2109
2110 hw_info = hw_info_init(snd_card_name);
2111 if (hw_info) {
2112 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2113 break;
2114 }
2115 ALOGE("%s: Failed to init hardware info", __func__);
2116 retry_num = 0;
2117 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002118
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302119 free(snd_card_name);
2120 snd_card_name = NULL;
2121
2122 mixer_close(mixer);
2123 mixer = NULL;
2124 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002125 if (snd_card_name)
2126 free(snd_card_name);
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302127 if (mixer)
2128 mixer_close(mixer);
2129 if (hw_info)
2130 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002131
2132 if (snd_card_num >= MAX_SND_CARD) {
2133 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2134 return -1;
2135 }
2136
2137 return snd_card_num;
2138}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302139
2140#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2141int audio_extn_utils_compress_enable_drift_correction(
2142 struct stream_out *out,
2143 struct audio_out_enable_drift_correction *drift)
2144{
2145 struct snd_compr_metadata metadata;
2146 int ret = -EINVAL;
2147
2148 if(drift == NULL) {
2149 ALOGE("%s:: Invalid param", __func__);
2150 goto exit;
2151 }
2152
2153 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2154
2155 if (!is_offload_usecase(out->usecase)) {
2156 ALOGE("%s:: not supported for non offload session", __func__);
2157 goto exit;
2158 }
2159
2160 if (!out->compr) {
2161 ALOGW("%s:: offload session not yet opened,"
2162 "start delay will be configure later", __func__);
2163 goto exit;
2164 }
2165
2166 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2167 metadata.value[0] = drift->enable;
2168 out->drift_correction_enabled = drift->enable;
2169
2170 ret = compress_set_metadata(out->compr, &metadata);
2171 if(ret) {
2172 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2173 out->drift_correction_enabled = false;
2174 }
2175
2176exit:
2177 return ret;
2178}
2179#else
2180int audio_extn_utils_compress_enable_drift_correction(
2181 struct stream_out *out __unused,
2182 struct audio_out_enable_drift_correction *drift __unused)
2183{
2184 ALOGD("%s:: configuring drift enablement not supported", __func__);
2185 return 0;
2186}
2187#endif
2188
2189#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2190int audio_extn_utils_compress_correct_drift(
2191 struct stream_out *out,
2192 struct audio_out_correct_drift *drift_param)
2193{
2194 struct snd_compr_metadata metadata;
2195 int ret = -EINVAL;
2196
2197 if (drift_param == NULL) {
2198 ALOGE("%s:: Invalid drift_param", __func__);
2199 goto exit;
2200 }
2201
2202 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2203 drift_param->adjust_time);
2204
2205 if (!is_offload_usecase(out->usecase)) {
2206 ALOGE("%s:: not supported for non offload session", __func__);
2207 goto exit;
2208 }
2209
2210 if (!out->compr) {
2211 ALOGW("%s:: offload session not yet opened", __func__);
2212 goto exit;
2213 }
2214
2215 if (!out->drift_correction_enabled) {
2216 ALOGE("%s:: drift correction not enabled", __func__);
2217 goto exit;
2218 }
2219
2220 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2221 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2222 metadata.value[1] = \
2223 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2224
2225 ret = compress_set_metadata(out->compr, &metadata);
2226 if(ret)
2227 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2228exit:
2229 return ret;
2230}
2231#else
2232int audio_extn_utils_compress_correct_drift(
2233 struct stream_out *out __unused,
2234 struct audio_out_correct_drift *drift_param __unused)
2235{
2236 ALOGD("%s:: setting adjust clock not supported", __func__);
2237 return 0;
2238}
2239#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302240
2241int audio_extn_utils_set_channel_map(
2242 struct stream_out *out,
2243 struct audio_out_channel_map_param *channel_map_param)
2244{
2245 int ret = -EINVAL, i = 0;
2246 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2247
2248 if (channel_map_param == NULL) {
2249 ALOGE("%s:: Invalid channel_map", __func__);
2250 goto exit;
2251 }
2252
2253 if (channel_map_param->channels != channels) {
2254 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2255 __func__, channel_map_param->channels, channels);
2256 goto exit;
2257 }
2258
2259 for ( i = 0; i < channels; i++) {
2260 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2261 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2262 }
2263 ret = 0;
2264exit:
2265 return ret;
2266}
Varun Balaraje49253e2017-07-06 19:48:56 +05302267
2268int audio_extn_utils_set_pan_scale_params(
2269 struct stream_out *out,
2270 struct mix_matrix_params *mm_params)
2271{
2272 int ret = -EINVAL, i = 0, j = 0;
2273
Varun Balaraj003ee752017-08-07 17:54:55 +05302274 if (mm_params == NULL || out == NULL) {
2275 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302276 goto exit;
2277 }
2278
2279 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2280 mm_params->num_output_channels <= 0 ||
2281 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2282 mm_params->num_input_channels <= 0)
2283 goto exit;
2284
2285 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2286 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2287 out->pan_scale_params.has_output_channel_map =
2288 mm_params->has_output_channel_map;
2289 for (i = 0; i < mm_params->num_output_channels; i++)
2290 out->pan_scale_params.output_channel_map[i] =
2291 mm_params->output_channel_map[i];
2292
2293 out->pan_scale_params.has_input_channel_map =
2294 mm_params->has_input_channel_map;
2295 for (i = 0; i < mm_params->num_input_channels; i++)
2296 out->pan_scale_params.input_channel_map[i] =
2297 mm_params->input_channel_map[i];
2298
2299 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2300 for (i = 0; i < mm_params->num_output_channels; i++)
2301 for (j = 0; j < mm_params->num_input_channels; j++) {
2302 //Convert the channel coefficient gains in Q14 format
2303 out->pan_scale_params.mixer_coeffs[i][j] =
2304 mm_params->mixer_coeffs[i][j] * (2 << 13);
2305 }
2306
2307 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2308 out->pcm_device_id,
2309 out->pan_scale_params);
2310
2311exit:
2312 return ret;
2313}
2314
2315int audio_extn_utils_set_downmix_params(
2316 struct stream_out *out,
2317 struct mix_matrix_params *mm_params)
2318{
2319 int ret = -EINVAL, i = 0, j = 0;
2320 struct audio_usecase *usecase = NULL;
2321
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002322 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302323 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302324 goto exit;
2325 }
2326
2327 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2328 mm_params->num_output_channels <= 0 ||
2329 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2330 mm_params->num_input_channels <= 0)
2331 goto exit;
2332
2333 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302334 if (!usecase) {
2335 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002336 goto exit;
2337 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302338 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2339 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2340
2341 out->downmix_params.has_output_channel_map =
2342 mm_params->has_output_channel_map;
2343 for (i = 0; i < mm_params->num_output_channels; i++) {
2344 out->downmix_params.output_channel_map[i] =
2345 mm_params->output_channel_map[i];
2346 }
2347
2348 out->downmix_params.has_input_channel_map =
2349 mm_params->has_input_channel_map;
2350 for (i = 0; i < mm_params->num_input_channels; i++)
2351 out->downmix_params.input_channel_map[i] =
2352 mm_params->input_channel_map[i];
2353
2354 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2355 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302356 for (j = 0; j < mm_params->num_input_channels; j++) {
2357 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302358 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302359 mm_params->mixer_coeffs[i][j] * (2 << 13);
2360 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302361
2362 ret = platform_set_stream_downmix_params(out->dev->platform,
2363 out->pcm_device_id,
2364 usecase->out_snd_device,
2365 out->downmix_params);
2366
2367exit:
2368 return ret;
2369}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302370
2371bool audio_extn_utils_is_dolby_format(audio_format_t format)
2372{
2373 if (format == AUDIO_FORMAT_AC3 ||
2374 format == AUDIO_FORMAT_E_AC3 ||
2375 format == AUDIO_FORMAT_E_AC3_JOC)
2376 return true;
2377 else
2378 return false;
2379}
2380
`Deeraj Soman676c2702017-09-18 19:25:53 +05302381int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2382{
2383 int i;
2384 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2385 {"S32_LE", 32},
2386 {"S24_LE", 24},
2387 {"S16_LE", 16}};
2388 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302389
`Deeraj Soman676c2702017-09-18 19:25:53 +05302390 for (i = 0; i < num_configs; i++) {
2391 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2392 return mixer_bitwidth_config[i].value;
2393 }
2394
2395 return -EINVAL;
2396}
2397
2398int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2399{
2400 int i;
2401 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2402 {"KHZ_48", 48000},
2403 {"KHZ_96", 96000},
2404 {"KHZ_144", 144000},
2405 {"KHZ_192", 192000},
2406 {"KHZ_384", 384000},
2407 {"KHZ_44P1", 44100},
2408 {"KHZ_88P2", 88200},
2409 {"KHZ_176P4", 176400},
2410 {"KHZ_352P8", 352800}};
2411 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2412
2413 for (i = 0; i < num_configs; i++) {
2414 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2415 return mixer_samplerate_config[i].value;
2416 }
2417
2418 return -EINVAL;
2419}
2420
2421int audio_extn_utils_get_channels_from_string(const char *id_string)
2422{
2423 int i;
2424 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2425 {"Two", 2},
2426 {"Three",3},
2427 {"Four", 4},
2428 {"Five", 5},
2429 {"Six", 6},
2430 {"Seven", 7},
2431 {"Eight", 8}};
2432 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2433
2434 for (i = 0; i < num_configs; i++) {
2435 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2436 return mixer_channels_config[i].value;
2437 }
2438
2439 return -EINVAL;
2440}