blob: 0d43e021e918b5cb2df1771f3ffcb463d3398479 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002 * Copyright (c) 2014-2019, 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>
Aalique Grahame22e49102018-12-18 14:23:57 -080030#include <log/log.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070031#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053032#include <unistd.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070033
Manish Dewangan07de2142017-02-27 19:27:20 +053034
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070035#include "audio_hw.h"
36#include "platform.h"
37#include "platform_api.h"
38#include "audio_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080039#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053040#include <sound/compress_params.h>
41#include <sound/compress_offload.h>
42#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053043
44#ifdef DYNAMIC_LOG_ENABLED
45#include <log_xml_parser.h>
46#define LOG_MASK HAL_MOD_FILE_UTILS
47#include <log_utils.h>
48#endif
49
Ashish Jain81eb2a82015-05-13 10:52:34 +053050#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
51#ifdef HDMI_PASSTHROUGH_ENABLED
52#include "audio_parsers.h"
53#endif
54#endif
55
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053056#ifdef LINUX_ENABLED
57#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053058#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053059#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070060#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053061#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053062#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070063
64#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053065#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066
67#define DYNAMIC_VALUE_TAG "dynamic"
68#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053069#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070070#define FORMATS_TAG "formats"
71#define SAMPLING_RATES_TAG "sampling_rates"
72#define BIT_WIDTH_TAG "bit_width"
73#define APP_TYPE_TAG "app_type"
74
75#define STRING_TO_ENUM(string) { #string, string }
76#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
77
Ben Rombergera04fabc2014-11-14 12:16:03 -080078#define BASE_TABLE_SIZE 64
79#define MAX_BASEINDEX_LEN 256
80
Ben Romberger1aaaf862017-04-06 17:49:46 -070081#ifndef SND_AUDIOCODEC_TRUEHD
82#define SND_AUDIOCODEC_TRUEHD 0x00000023
83#endif
84
Vikram Panduranga93f080e2017-06-07 18:16:14 -070085#define APP_TYPE_VOIP_AUDIO 0x1113A
86
Ashish Jain81eb2a82015-05-13 10:52:34 +053087#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
88#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
89#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
90#define SR_44100 (0<<0) /* 44.1kHz */
91#define SR_NOTID (1<<0) /* non indicated */
92#define SR_48000 (2<<0) /* 48kHz */
93#define SR_32000 (3<<0) /* 32kHz */
94#define SR_22050 (4<<0) /* 22.05kHz */
95#define SR_24000 (6<<0) /* 24kHz */
96#define SR_88200 (8<<0) /* 88.2kHz */
97#define SR_96000 (10<<0) /* 96kHz */
98#define SR_176400 (12<<0) /* 176.4kHz */
99#define SR_192000 (14<<0) /* 192kHz */
100
101#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530102
103/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800104#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
105#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
106#define PCM_OFFLOAD_PLAYBACK_LATENCY \
107 (PCM_OFFLOAD_BUFFER_DURATION + PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY)
Manish Dewangan07de2142017-02-27 19:27:20 +0530108
Varun Balaraje49253e2017-07-06 19:48:56 +0530109#ifndef MAX_CHANNELS_SUPPORTED
110#define MAX_CHANNELS_SUPPORTED 8
111#endif
112
`Deeraj Soman676c2702017-09-18 19:25:53 +0530113typedef struct {
114 const char *id_string;
115 const int value;
116} mixer_config_lookup;
117
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700118struct string_to_enum {
119 const char *name;
120 uint32_t value;
121};
122
123const struct string_to_enum s_flag_name_to_enum_table[] = {
124 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
125 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
126 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800127 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
129 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
130 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700131 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700132#ifdef INCALL_MUSIC_ENABLED
133 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
134#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530140 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
141 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
142 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
143 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
144 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530145 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530146 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200147 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700148};
149
150const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530151 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700152 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530153 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
154 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530155 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700156 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
157 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
158 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700159 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
160 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700161 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700162 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700163 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530164 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700165 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700166 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530167 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700168 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
169 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
170 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
172 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
173 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
174 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
175 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
176 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
177 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530179 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
180 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800181 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
182 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
183 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530184 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530185 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
186 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
187 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530188 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530189 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
190 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
191 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
192 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530193 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700194};
195
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530196/* payload structure avt_device drift query */
197struct audio_avt_device_drift_stats {
198 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530199
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530200 /* Indicates the device interface direction as either
201 * source (Tx) or sink (Rx).
202 */
203 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530204
205 /* Reference timer for drift accumulation and time stamp information.
206 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
207 */
208 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530209 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530210} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530211
Ben Rombergera04fabc2014-11-14 12:16:03 -0800212static char bTable[BASE_TABLE_SIZE] = {
213 'A','B','C','D','E','F','G','H','I','J','K','L',
214 'M','N','O','P','Q','R','S','T','U','V','W','X',
215 'Y','Z','a','b','c','d','e','f','g','h','i','j',
216 'k','l','m','n','o','p','q','r','s','t','u','v',
217 'w','x','y','z','0','1','2','3','4','5','6','7',
218 '8','9','+','/'
219};
220
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700221static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
222 const char *name)
223{
224 size_t i;
225 for (i = 0; i < size; i++) {
226 if (strcmp(table[i].name, name) == 0) {
227 ALOGV("%s found %s", __func__, table[i].name);
228 return table[i].value;
229 }
230 }
231 return 0;
232}
233
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530234static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700235{
236 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530237 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800238 char *last_r;
239 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700240 while (flag_name != NULL) {
241 if (strlen(flag_name) != 0) {
242 flag |= string_to_enum(s_flag_name_to_enum_table,
243 ARRAY_SIZE(s_flag_name_to_enum_table),
244 flag_name);
245 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800246 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700247 }
248
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800249 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530250 io_flags.in_flags = (audio_input_flags_t)flag;
251 io_flags.out_flags = (audio_output_flags_t)flag;
252 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700253}
254
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530255static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700256{
257 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800258 char *last_r;
259 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700260
261 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
262 return;
263
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530264 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700265 while (str != NULL) {
266 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
267 ARRAY_SIZE(s_format_name_to_enum_table), str);
268 ALOGV("%s: format - %d", __func__, format);
269 if (format != 0) {
270 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700271 if (sf_info == NULL)
272 break; /* return whatever was parsed */
273
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700274 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530275 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700276 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800277 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700278 }
279}
280
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530281static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700282{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700283 struct stream_sample_rate *ss_info = NULL;
284 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800285 char *last_r;
286 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700287
Amit Shekhar6f461b12014-08-01 14:52:58 -0700288 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
289 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700290
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530291 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700292 while (str != NULL) {
293 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
294 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
295 if (0 != sample_rate) {
296 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800297 if (!ss_info) {
298 ALOGE("%s: memory allocation failure", __func__);
299 return;
300 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700301 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530302 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700303 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800304 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700305 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700306}
307
308static int parse_bit_width_names(char *name)
309{
310 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800311 char *last_r;
312 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700313
314 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
315 bit_width = (int)strtol(str, (char **)NULL, 10);
316
317 ALOGV("%s: bit_width - %d", __func__, bit_width);
318 return bit_width;
319}
320
321static int parse_app_type_names(void *platform, char *name)
322{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700323 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800324 char *last_r;
325 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700326
327 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
328 app_type = (int)strtol(str, (char **)NULL, 10);
329
330 ALOGV("%s: app_type - %d", __func__, app_type);
331 return app_type;
332}
333
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530334static void update_streams_cfg_list(cnode *root, void *platform,
335 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700336{
337 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530338 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700339
340 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530341 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700342
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530343 if (!s_info) {
344 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700345 return;
346 }
347
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700348 while (node) {
349 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530350 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530351 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
352 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700353 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530354 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700355 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530356 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
357 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700358 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530359 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700360 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530361 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700362 }
363 node = node->next;
364 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700366}
367
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530368static void load_cfg_list(cnode *root, void *platform,
369 struct listnode *streams_output_cfg_list,
370 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700371{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530372 cnode *node = NULL;
373
374 node = config_find(root, OUTPUTS_TAG);
375 if (node != NULL) {
376 node = node->first_child;
377 while (node) {
378 ALOGV("%s: loading output %s", __func__, node->name);
379 update_streams_cfg_list(node, platform, streams_output_cfg_list);
380 node = node->next;
381 }
382 } else {
383 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700384 }
385
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530386 node = config_find(root, INPUTS_TAG);
387 if (node != NULL) {
388 node = node->first_child;
389 while (node) {
390 ALOGV("%s: loading input %s", __func__, node->name);
391 update_streams_cfg_list(node, platform, streams_input_cfg_list);
392 node = node->next;
393 }
394 } else {
395 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700396 }
397}
398
399static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530400 struct listnode *streams_output_cfg_list,
401 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700402{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530403 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700404 int length = 0, i, num_app_types = 0;
405 struct listnode *node;
406 bool update;
407 struct mixer_ctl *ctl = NULL;
408 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530409 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800410 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700411
412 if (!mixer) {
413 ALOGE("%s: mixer is null",__func__);
414 return;
415 }
416 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
417 if (!ctl) {
418 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
419 return;
420 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530421 app_type_cfg[length++] = num_app_types;
422
423 if (list_empty(streams_output_cfg_list)) {
424 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700425 app_type_cfg[length++] = 48000;
426 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530427 num_app_types += 1;
428 }
429 if (list_empty(streams_input_cfg_list)) {
430 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
431 app_type_cfg[length++] = 48000;
432 app_type_cfg[length++] = 16;
433 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700434 }
435
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800436 /* get target bit width for ADM enforce mode */
437 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
438
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700439 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530440 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700441 update = true;
442 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530443 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700444 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530445 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530446 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530447 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530448 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530449 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800450 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
451 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
452 (target_bit_width > app_type_cfg[i+3]))
453 app_type_cfg[i+3] = target_bit_width;
454
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700455 update = false;
456 break;
457 }
458 }
459 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 num_app_types += 1;
461 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
462 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800463 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
464 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
465 (target_bit_width > app_type_cfg[length]))
466 app_type_cfg[length] = target_bit_width;
467
468 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530469 }
470 }
471 list_for_each(node, streams_input_cfg_list) {
472 s_info = node_to_item(node, struct streams_io_cfg, list);
473 update = true;
474 for (i=0; i<length; i=i+3) {
475 if (app_type_cfg[i+1] == 0)
476 break;
477 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530478 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530479 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530480 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530481 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
482 update = false;
483 break;
484 }
485 }
486 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
487 num_app_types += 1;
488 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
489 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
490 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700491 }
492 }
493 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
494 if (num_app_types) {
495 app_type_cfg[0] = num_app_types;
496 mixer_ctl_set_array(ctl, app_type_cfg, length);
497 }
498}
499
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530500void audio_extn_utils_update_streams_cfg_lists(void *platform,
501 struct mixer *mixer,
502 struct listnode *streams_output_cfg_list,
503 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700504{
505 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530506 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700507
508 ALOGV("%s", __func__);
509 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530510 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700511
512 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700513 if (root == NULL) {
514 ALOGE("cfg_list, NULL config root");
515 return;
516 }
517
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530518 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
519 if (data == NULL) {
520 ALOGD("%s: failed to open io config file(%s), trying older config file",
521 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
522 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
523 if (data == NULL) {
524 send_app_type_cfg(platform, mixer,
525 streams_output_cfg_list,
526 streams_input_cfg_list);
527 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800528 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530529 return;
530 }
531 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700532
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530533 config_load(root, data);
534 load_cfg_list(root, platform, streams_output_cfg_list,
535 streams_input_cfg_list);
536
537 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
538 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800539
540 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800541 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800542 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700543}
544
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530545static void audio_extn_utils_dump_streams_cfg_list(
546 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700547{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700548 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700550 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700551 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530552
553 list_for_each(node_i, streams_cfg_list) {
554 s_info = node_to_item(node_i, struct streams_io_cfg, list);
555 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
556 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
557 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
558 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700559 sf_info = node_to_item(node_j, struct stream_format, list);
560 ALOGV("format-%x", sf_info->format);
561 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530562 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700563 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
564 ALOGV("sample rate-%d", ss_info->sample_rate);
565 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700566 }
567}
568
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530569void audio_extn_utils_dump_streams_cfg_lists(
570 struct listnode *streams_output_cfg_list,
571 struct listnode *streams_input_cfg_list)
572{
573 ALOGV("%s", __func__);
574 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
575 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
576}
577
578static void audio_extn_utils_release_streams_cfg_list(
579 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700580{
581 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530582 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700583
584 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530585
586 while (!list_empty(streams_cfg_list)) {
587 node_i = list_head(streams_cfg_list);
588 s_info = node_to_item(node_i, struct streams_io_cfg, list);
589 while (!list_empty(&s_info->format_list)) {
590 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700591 list_remove(node_j);
592 free(node_to_item(node_j, struct stream_format, list));
593 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530594 while (!list_empty(&s_info->sample_rate_list)) {
595 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700596 list_remove(node_j);
597 free(node_to_item(node_j, struct stream_sample_rate, list));
598 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700599 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700601 }
602}
603
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530604void audio_extn_utils_release_streams_cfg_lists(
605 struct listnode *streams_output_cfg_list,
606 struct listnode *streams_input_cfg_list)
607{
608 ALOGV("%s", __func__);
609 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
610 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
611}
612
613static bool set_app_type_cfg(struct streams_io_cfg *s_info,
614 struct stream_app_type_cfg *app_type_cfg,
615 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700616 {
617 struct listnode *node_i;
618 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530619 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700620 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
621 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530622 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700623
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530624 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700625 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530626 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700627 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
628 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
629 return true;
630 }
631 }
632 /*
633 * Reiterate through the list assuming dafault sample rate.
634 * Handles scenario where input sample rate is higher
635 * than all sample rates in list for the input bit width.
636 */
637 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800638
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530639 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700640 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
641 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530642 (bit_width == s_info->app_type_cfg.bit_width)) {
643 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700644 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530645 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800646 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 -0700647 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
648 return true;
649 }
650 }
651 return false;
652}
653
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530654void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
655 struct listnode *streams_input_cfg_list,
656 audio_devices_t devices __unused,
657 audio_input_flags_t flags,
658 audio_format_t format,
659 uint32_t sample_rate,
660 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530661 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530662 struct stream_app_type_cfg *app_type_cfg)
663{
664 struct listnode *node_i, *node_j;
665 struct streams_io_cfg *s_info;
666 struct stream_format *sf_info;
667
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530668 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
669 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530670
671 list_for_each(node_i, streams_input_cfg_list) {
672 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530673 /* Along with flags do profile matching if set at either end.*/
674 if (s_info->flags.in_flags == flags &&
675 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
676 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530677 list_for_each(node_j, &s_info->format_list) {
678 sf_info = node_to_item(node_j, struct stream_format, list);
679 if (sf_info->format == format) {
680 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
681 return;
682 }
683 }
684 }
685 }
686 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
687 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
688 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
689 app_type_cfg->bit_width = 16;
690}
691
692void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700693 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700694 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700695 audio_output_flags_t flags,
696 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700697 uint32_t sample_rate,
698 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530699 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530700 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700701 struct stream_app_type_cfg *app_type_cfg)
702{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530703 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530704 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700705 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530706 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700707
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530708 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
709 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
710 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700711 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530712 else if (-ENOSYS == bw)
713 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700714 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530715 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700716 }
717
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700718 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530719 if (!strncmp("true", value, sizeof("true"))) {
720 if ((popcount(channel_mask) > 2) &&
721 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700722 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530723 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
724 ALOGD("%s: MCH session defaulting sample rate to %d",
725 __func__, sample_rate);
726 }
727 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530728
729 /* Set sampling rate to 176.4 for DSD64
730 * and 352.8Khz for DSD128.
731 * Set Bit Width to 16. output will be 16 bit
732 * post DoP in ASM.
733 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700734 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530735 (format == AUDIO_FORMAT_DSD)) {
736 bit_width = 16;
737 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
738 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
739 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
740 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
741 }
742
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530743
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800744 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
745 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700746 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530747 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530748 /* Along with flags do profile matching if set at either end.*/
749 if (s_info->flags.out_flags == flags &&
750 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
751 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530752 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700753 sf_info = node_to_item(node_j, struct stream_format, list);
754 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530755 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700756 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700757 }
758 }
759 }
760 }
761 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530762 s_info = node_to_item(node_i, struct streams_io_cfg, list);
763 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700764 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530765 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
766 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
767 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700768 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530769 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700770 return;
771 }
772 }
773 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700774 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700775 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700776 app_type_cfg->bit_width = 16;
777}
778
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530779static bool audio_is_this_native_usecase(struct audio_usecase *uc)
780{
781 bool native_usecase = false;
782 struct stream_out *out = (struct stream_out*) uc->stream.out;
783
784 if (PCM_PLAYBACK == uc->type && out != NULL &&
785 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
786 is_offload_usecase(uc->id) &&
787 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
788 native_usecase = true;
789
790 return native_usecase;
791}
792
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800793bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
794{
795 /* DSP bitwidth enforce mode for ADM and AFE:
796 * includes:
797 * deep buffer, low latency, direct pcm and offload.
798 * excludes:
799 * ull(raw+fast), VOIP.
800 */
801 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
802 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
803 (flags & AUDIO_OUTPUT_FLAG_FAST)))
804 return false;
805
806
807 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
808 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
809 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
810 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
811 return true;
812 else
813 return false;
814}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800815
816static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
817{
818 return adev->vr_audio_mode_enabled;
819}
820
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530821void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
822 struct audio_device *adev,
823 struct audio_usecase *usecase)
824{
825 ALOGV("%s", __func__);
826
827 switch(usecase->type) {
828 case PCM_PLAYBACK:
829 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
830 &adev->streams_output_cfg_list,
831 usecase->stream.out->devices,
832 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700833 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530834 usecase->stream.out->sample_rate,
835 usecase->stream.out->bit_width,
836 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530837 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530838 &usecase->stream.out->app_type_cfg);
839 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
840 break;
841 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700842 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
843 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
844 else
845 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530846 &adev->streams_input_cfg_list,
847 usecase->stream.in->device,
848 usecase->stream.in->flags,
849 usecase->stream.in->format,
850 usecase->stream.in->sample_rate,
851 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530852 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530853 &usecase->stream.in->app_type_cfg);
854 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
855 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530856 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530857 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
858 &adev->streams_output_cfg_list,
859 usecase->stream.inout->out_config.devices,
860 0,
861 usecase->stream.inout->out_config.format,
862 usecase->stream.inout->out_config.sample_rate,
863 usecase->stream.inout->out_config.bit_width,
864 usecase->stream.inout->out_config.channel_mask,
865 usecase->stream.inout->profile,
866 &usecase->stream.inout->out_app_type_cfg);
867 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
868 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530869 default:
870 ALOGE("%s: app type cfg not supported for usecase type (%d)",
871 __func__, usecase->type);
872 }
873}
874
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530875void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
876{
877 switch (snd_device) {
878 case SND_DEVICE_OUT_BT_SCO:
879 case SND_DEVICE_IN_BT_SCO_MIC:
880 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
881 *sample_rate = 8000;
882 break;
883 case SND_DEVICE_OUT_BT_SCO_WB:
884 case SND_DEVICE_IN_BT_SCO_MIC_WB:
885 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
886 *sample_rate = 16000;
887 break;
888 default:
889 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
890 break;
891 }
892}
893
Aalique Grahame22e49102018-12-18 14:23:57 -0800894static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
895 int pcm_device_id, int app_type,
896 int acdb_dev_id, int sample_rate,
897 int stream_type,
898 snd_device_t snd_device)
899{
900
901 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
902 struct mixer_ctl *ctl;
903 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
904 int snd_device_be_idx = -1;
905
906 if (stream_type == PCM_PLAYBACK) {
907 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
908 "Audio Stream %d App Type Cfg", pcm_device_id);
909 } else if (stream_type == PCM_CAPTURE) {
910 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
911 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
912 }
913
914 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
915 if (!ctl) {
916 ALOGE("%s: Could not get ctl for mixer cmd - %s",
917 __func__, mixer_ctl_name);
918 rc = -EINVAL;
919 goto exit;
920 }
921 app_type_cfg[len++] = app_type;
922 app_type_cfg[len++] = acdb_dev_id;
923 app_type_cfg[len++] = sample_rate;
924
925 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
926 if (snd_device_be_idx > 0)
927 app_type_cfg[len++] = snd_device_be_idx;
928 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
929 "sample rate %d, snd_device_be_idx %d",
930 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
931 snd_device_be_idx);
932 mixer_ctl_set_array(ctl, app_type_cfg, len);
933
934exit:
935 return rc;
936}
937
938static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
939 struct audio_usecase *usecase)
940{
941 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
942 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
943 int app_type = 0, rc = 0;
944
945 ALOGV("%s", __func__);
946
947 if (usecase->type != PCM_HFP_CALL) {
948 ALOGV("%s: not a playback or HFP path, no need to cfg app type", __func__);
949 rc = 0;
950 goto exit_send_app_type_cfg;
951 }
952 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
953 (usecase->id != USECASE_AUDIO_HFP_SCO_WB)) {
954 ALOGV("%s: a playback path where app type cfg is not required", __func__);
955 rc = 0;
956 goto exit_send_app_type_cfg;
957 }
958
959 snd_device = usecase->out_snd_device;
960 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
961
962 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
963 if (acdb_dev_id < 0) {
964 ALOGE("%s: Couldn't get the acdb dev id", __func__);
965 rc = -EINVAL;
966 goto exit_send_app_type_cfg;
967 }
968
969 if (usecase->type == PCM_HFP_CALL) {
970
971 /* config HFP session:1 playback path */
972 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
973 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
974 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
975 acdb_dev_id, sample_rate,
976 PCM_PLAYBACK,
977 SND_DEVICE_NONE); // use legacy behavior
978 if (rc < 0)
979 goto exit_send_app_type_cfg;
980
981 /* config HFP session:1 capture path */
982 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
983 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
984 acdb_dev_id, sample_rate,
985 PCM_CAPTURE,
986 SND_DEVICE_NONE);
987 if (rc < 0)
988 goto exit_send_app_type_cfg;
989
990 /* config HFP session:2 capture path */
991 pcm_device_id = HFP_ASM_RX_TX;
992 snd_device = usecase->in_snd_device;
993 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
994 if (acdb_dev_id <= 0) {
995 ALOGE("%s: Couldn't get the acdb dev id", __func__);
996 rc = -EINVAL;
997 goto exit_send_app_type_cfg;
998 }
999 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1000 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1001 acdb_dev_id, sample_rate, PCM_CAPTURE,
1002 SND_DEVICE_NONE);
1003 if (rc < 0)
1004 goto exit_send_app_type_cfg;
1005
1006 /* config HFP session:2 playback path */
1007 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1008 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1009 acdb_dev_id, sample_rate,
1010 PCM_PLAYBACK, SND_DEVICE_NONE);
1011 if (rc < 0)
1012 goto exit_send_app_type_cfg;
1013 }
1014
1015 rc = 0;
1016exit_send_app_type_cfg:
1017 return rc;
1018}
1019
Siena Richard7c2db772016-12-21 11:32:34 -08001020static int send_app_type_cfg_for_device(struct audio_device *adev,
1021 struct audio_usecase *usecase,
1022 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001023{
1024 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301025 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1026 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001027 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001028 int pcm_device_id = 0, acdb_dev_id, app_type;
1029 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301030 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +05301031 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +05301032 struct streams_io_cfg *s_info = NULL;
1033 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301034 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001035
Siena Richard7c2db772016-12-21 11:32:34 -08001036 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1037 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1038 platform_get_snd_device_name(usecase->in_snd_device),
1039 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001040
Siddartha Shaik343abc62017-08-08 11:15:25 +05301041 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301042 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301043 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001044 rc = 0;
1045 goto exit_send_app_type_cfg;
1046 }
1047 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
1048 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1049 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001050 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001051 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301052 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301053 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301054 (!is_offload_usecase(usecase->id)) &&
1055 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301056 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 -07001057 rc = 0;
1058 goto exit_send_app_type_cfg;
1059 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001060
1061 //if VR is active then only send the mixer control
1062 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1063 ALOGI("ULL doesnt need sending app type cfg, returning");
1064 rc = 0;
1065 goto exit_send_app_type_cfg;
1066 }
1067
Surendar Karka93cd25a2018-08-28 14:21:37 +05301068 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001069 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301070 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1071 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001072 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001073 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301074 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1075 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301076 }
Siena Richard7c2db772016-12-21 11:32:34 -08001077
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001078 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1079 if (!ctl) {
1080 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1081 mixer_ctl_name);
1082 rc = -EINVAL;
1083 goto exit_send_app_type_cfg;
1084 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301085 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301086 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1087 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1088 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1089 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1090 } else {
1091 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1092 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301093 if (acdb_dev_id <= 0) {
1094 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1095 rc = -EINVAL;
1096 goto exit_send_app_type_cfg;
1097 }
1098
Siena Richard7c2db772016-12-21 11:32:34 -08001099 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1100 if (snd_device_be_idx < 0) {
1101 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1102 __func__, platform_get_snd_device_name(snd_device),
1103 snd_device_be_idx);
1104 }
1105
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301106 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +05301107
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001108 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301109 if (!strncmp("true", value, sizeof("true"))) {
1110 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1111 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001112 !(usecase->stream.out->flags &
1113 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301114 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1115 }
Ashish Jainc02430d2016-01-04 10:42:43 +05301116
kunleizcdf25942017-09-20 14:01:21 +08001117 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1118 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
1119 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Dieter Luecking5d57def2018-09-07 14:23:37 +02001120 if (platform_spkr_use_default_sample_rate(adev->platform)) {
1121 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1122 } else {
1123 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1124 usecase->stream.out->sample_rate,
1125 &usecase->stream.out->app_type_cfg.sample_rate);
1126 }
1127
Ashish Jaina052e572016-11-07 16:41:07 +05301128 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1129 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1130 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1131 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1132 /*
1133 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1134 * configured device sample rate, if not update the COPP rate to be equal to the
1135 * device sample rate, else open COPP at stream sample rate
1136 */
1137 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1138 usecase->stream.out->sample_rate,
1139 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05301140 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1141 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -07001142 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1143 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +05301144 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -07001145 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301146 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1147 /*
1148 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1149 * for bit width use the stream param only.
1150 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001151 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301152 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1153 __func__, usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001154 }
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301155 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001156 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1157
Varun Balaraje49253e2017-07-06 19:48:56 +05301158 /* Interactive streams are supported with only direct app type id.
1159 * Get Direct profile app type and use it for interactive streams
1160 */
1161 list_for_each(node, &adev->streams_output_cfg_list) {
1162 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001163 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301164 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1165 AUDIO_OUTPUT_FLAG_DIRECT))
1166 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301167 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001168 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301169 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301170 else
1171 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001172 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001173 app_type_cfg[len++] = acdb_dev_id;
1174 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001175 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1176 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301177 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1178 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001179
1180 sample_rate = sample_rate * 4;
1181 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1182 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001183 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001184 app_type_cfg[len++] = sample_rate;
1185
Siena Richard7c2db772016-12-21 11:32:34 -08001186 if (snd_device_be_idx > 0)
1187 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301188
Siena Richard7c2db772016-12-21 11:32:34 -08001189 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1190 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301191
1192 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001193 app_type = usecase->stream.in->app_type_cfg.app_type;
1194 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301195 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001196 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1197 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301198 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1199 audio_extn_btsco_get_sample_rate(usecase->in_snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1200 } else {
1201 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1202 }
Deeraj Somanfc791e72018-11-30 13:23:01 +05301203 if (usecase->stream.in->device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP & ~AUDIO_DEVICE_BIT_IN) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001204 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
1205 ALOGI("%s using %d sample rate rate for A2DP dec CoPP",
1206 __func__, usecase->stream.in->app_type_cfg.sample_rate);
1207 }
Siena Richard7c2db772016-12-21 11:32:34 -08001208 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1209 app_type_cfg[len++] = sample_rate;
1210 if (snd_device_be_idx > 0)
1211 app_type_cfg[len++] = snd_device_be_idx;
1212 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1213 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301214 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001215 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301216 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301217 sample_rate = usecase->stream.inout->out_config.sample_rate;
1218 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1219 }
Siena Richard7c2db772016-12-21 11:32:34 -08001220 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301221 app_type_cfg[len++] = acdb_dev_id;
1222 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001223 if (snd_device_be_idx > 0)
1224 app_type_cfg[len++] = snd_device_be_idx;
1225 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1226 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301227 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301228
Siddartha Shaik343abc62017-08-08 11:15:25 +05301229 if(ctl)
1230 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001231 rc = 0;
1232exit_send_app_type_cfg:
1233 return rc;
1234}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001235
Siena Richard7c2db772016-12-21 11:32:34 -08001236int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1237 struct audio_usecase *usecase)
1238{
1239 int i, num_devices = 0;
1240 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301241 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001242 int rc = 0;
1243
Aalique Grahame22e49102018-12-18 14:23:57 -08001244 if (usecase->type == PCM_HFP_CALL) {
1245 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1246 }
1247
Siena Richard7c2db772016-12-21 11:32:34 -08001248 switch (usecase->type) {
1249 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301250 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001251 ALOGD("%s: usecase->out_snd_device %s",
1252 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1253 /* check for out combo device */
1254 if (platform_split_snd_device(adev->platform,
1255 usecase->out_snd_device,
1256 &num_devices, new_snd_devices)) {
1257 new_snd_devices[0] = usecase->out_snd_device;
1258 num_devices = 1;
1259 }
1260 break;
1261 case PCM_CAPTURE:
1262 ALOGD("%s: usecase->in_snd_device %s",
1263 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301264 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1265 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1266 }
Siena Richard7c2db772016-12-21 11:32:34 -08001267 /* check for in combo device */
1268 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301269 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001270 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301271 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001272 num_devices = 1;
1273 }
1274 break;
1275 default:
1276 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1277 rc = 0;
1278 break;
1279 }
1280
1281 for (i = 0; i < num_devices; i++) {
1282 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1283 if (rc)
1284 break;
1285 }
1286
1287 return rc;
1288}
1289
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301290int read_line_from_file(const char *path, char *buf, size_t count)
1291{
1292 char * fgets_ret;
1293 FILE * fd;
1294 int rv;
1295
1296 fd = fopen(path, "r");
1297 if (fd == NULL)
1298 return -1;
1299
1300 fgets_ret = fgets(buf, (int)count, fd);
1301 if (NULL != fgets_ret) {
1302 rv = (int)strlen(buf);
1303 } else {
1304 rv = ferror(fd);
1305 }
1306 fclose(fd);
1307
1308 return rv;
1309}
1310
Ashish Jainf1eaa582016-05-23 20:54:24 +05301311/*Translates ALSA formats to AOSP PCM formats*/
1312audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1313{
1314 audio_format_t format;
1315
1316 switch(alsa_format) {
1317 case SNDRV_PCM_FORMAT_S16_LE:
1318 format = AUDIO_FORMAT_PCM_16_BIT;
1319 break;
1320 case SNDRV_PCM_FORMAT_S24_3LE:
1321 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1322 break;
1323 case SNDRV_PCM_FORMAT_S24_LE:
1324 format = AUDIO_FORMAT_PCM_8_24_BIT;
1325 break;
1326 case SNDRV_PCM_FORMAT_S32_LE:
1327 format = AUDIO_FORMAT_PCM_32_BIT;
1328 break;
1329 default:
1330 ALOGW("Incorrect ALSA format");
1331 format = AUDIO_FORMAT_INVALID;
1332 }
1333 return format;
1334}
1335
1336/*Translates hal format (AOSP) to alsa formats*/
1337uint32_t hal_format_to_alsa(audio_format_t hal_format)
1338{
1339 uint32_t alsa_format;
1340
1341 switch (hal_format) {
1342 case AUDIO_FORMAT_PCM_32_BIT: {
1343 if (platform_supports_true_32bit())
1344 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1345 else
1346 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1347 }
1348 break;
1349 case AUDIO_FORMAT_PCM_8_BIT:
1350 alsa_format = SNDRV_PCM_FORMAT_S8;
1351 break;
1352 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1353 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1354 break;
1355 case AUDIO_FORMAT_PCM_8_24_BIT: {
1356 if (platform_supports_true_32bit())
1357 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1358 else
1359 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1360 }
1361 break;
1362 case AUDIO_FORMAT_PCM_FLOAT:
1363 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1364 break;
1365 default:
1366 case AUDIO_FORMAT_PCM_16_BIT:
1367 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1368 break;
1369 }
1370 return alsa_format;
1371}
1372
Ashish Jain83a6cc22016-06-28 14:34:17 +05301373/*Translates PCM formats to AOSP formats*/
1374audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1375{
1376 audio_format_t format = AUDIO_FORMAT_INVALID;
1377
1378 switch(pcm_format) {
1379 case PCM_FORMAT_S16_LE:
1380 format = AUDIO_FORMAT_PCM_16_BIT;
1381 break;
1382 case PCM_FORMAT_S24_3LE:
1383 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1384 break;
1385 case PCM_FORMAT_S24_LE:
1386 format = AUDIO_FORMAT_PCM_8_24_BIT;
1387 break;
1388 case PCM_FORMAT_S32_LE:
1389 format = AUDIO_FORMAT_PCM_32_BIT;
1390 break;
1391 default:
1392 ALOGW("Incorrect PCM format");
1393 format = AUDIO_FORMAT_INVALID;
1394 }
1395 return format;
1396}
1397
1398/*Translates hal format (AOSP) to alsa formats*/
1399uint32_t hal_format_to_pcm(audio_format_t hal_format)
1400{
1401 uint32_t pcm_format;
1402
1403 switch (hal_format) {
1404 case AUDIO_FORMAT_PCM_32_BIT:
1405 case AUDIO_FORMAT_PCM_8_24_BIT:
1406 case AUDIO_FORMAT_PCM_FLOAT: {
1407 if (platform_supports_true_32bit())
1408 pcm_format = PCM_FORMAT_S32_LE;
1409 else
1410 pcm_format = PCM_FORMAT_S24_3LE;
1411 }
1412 break;
1413 case AUDIO_FORMAT_PCM_8_BIT:
1414 pcm_format = PCM_FORMAT_S8;
1415 break;
1416 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1417 pcm_format = PCM_FORMAT_S24_3LE;
1418 break;
1419 default:
1420 case AUDIO_FORMAT_PCM_16_BIT:
1421 pcm_format = PCM_FORMAT_S16_LE;
1422 break;
1423 }
1424 return pcm_format;
1425}
1426
Ashish Jainf1eaa582016-05-23 20:54:24 +05301427uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1428 uint32_t sample_rate,
1429 uint32_t noOfChannels)
1430{
1431 uint32_t fragment_size = 0;
1432 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1433
1434 fragment_size = (pcm_offload_time
1435 * sample_rate
1436 * bytes_per_sample
1437 * noOfChannels)/1000;
1438 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1439 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1440 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1441 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1442 /*To have same PCM samples for all channels, the buffer size requires to
1443 *be multiple of (number of channels * bytes per sample)
1444 *For writes to succeed, the buffer must be written at address which is multiple of 32
1445 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001446 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301447
1448 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1449 return fragment_size;
1450}
1451
1452/* Calculates the fragment size required to configure compress session.
1453 * Based on the alsa format selected, decide if conversion is needed in
1454
1455 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1456 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1457 */
1458void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1459{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301460 audio_format_t dst_format = out->hal_op_format;
1461 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301462 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1463 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1464
1465 out->compr_config.fragment_size =
1466 get_alsa_fragment_size(hal_op_bytes_per_sample,
1467 out->sample_rate,
1468 popcount(out->channel_mask));
1469
1470 if ((src_format != dst_format) &&
1471 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1472
Ashish Jain83a6cc22016-06-28 14:34:17 +05301473 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301474 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1475 hal_op_bytes_per_sample);
1476 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301477 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301478 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301479 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301480 }
1481}
1482
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301483/* converts pcm format 24_8 to 8_24 inplace */
1484size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1485{
1486 size_t i = 0;
1487 int *int_buf_stream = buf;
1488
1489 if ((bytes % 4) != 0) {
1490 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1491 return -EINVAL;
1492 }
1493
1494 for (; i < (bytes / 4); i++)
1495 int_buf_stream[i] >>= 8;
1496
1497 return bytes;
1498}
1499
1500int get_snd_codec_id(audio_format_t format)
1501{
1502 int id = 0;
1503
1504 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1505 case AUDIO_FORMAT_MP3:
1506 id = SND_AUDIOCODEC_MP3;
1507 break;
1508 case AUDIO_FORMAT_AAC:
1509 id = SND_AUDIOCODEC_AAC;
1510 break;
1511 case AUDIO_FORMAT_AAC_ADTS:
1512 id = SND_AUDIOCODEC_AAC;
1513 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301514 case AUDIO_FORMAT_AAC_LATM:
1515 id = SND_AUDIOCODEC_AAC;
1516 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301517 case AUDIO_FORMAT_PCM:
1518 id = SND_AUDIOCODEC_PCM;
1519 break;
1520 case AUDIO_FORMAT_FLAC:
1521 id = SND_AUDIOCODEC_FLAC;
1522 break;
1523 case AUDIO_FORMAT_ALAC:
1524 id = SND_AUDIOCODEC_ALAC;
1525 break;
1526 case AUDIO_FORMAT_APE:
1527 id = SND_AUDIOCODEC_APE;
1528 break;
1529 case AUDIO_FORMAT_VORBIS:
1530 id = SND_AUDIOCODEC_VORBIS;
1531 break;
1532 case AUDIO_FORMAT_WMA:
1533 id = SND_AUDIOCODEC_WMA;
1534 break;
1535 case AUDIO_FORMAT_WMA_PRO:
1536 id = SND_AUDIOCODEC_WMA_PRO;
1537 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301538 case AUDIO_FORMAT_MP2:
1539 id = SND_AUDIOCODEC_MP2;
1540 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301541 case AUDIO_FORMAT_AC3:
1542 id = SND_AUDIOCODEC_AC3;
1543 break;
1544 case AUDIO_FORMAT_E_AC3:
1545 case AUDIO_FORMAT_E_AC3_JOC:
1546 id = SND_AUDIOCODEC_EAC3;
1547 break;
1548 case AUDIO_FORMAT_DTS:
1549 case AUDIO_FORMAT_DTS_HD:
1550 id = SND_AUDIOCODEC_DTS;
1551 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001552 case AUDIO_FORMAT_DOLBY_TRUEHD:
1553 id = SND_AUDIOCODEC_TRUEHD;
1554 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001555 case AUDIO_FORMAT_IEC61937:
1556 id = SND_AUDIOCODEC_IEC61937;
1557 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301558 case AUDIO_FORMAT_DSD:
1559 id = SND_AUDIOCODEC_DSD;
1560 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301561 case AUDIO_FORMAT_APTX:
1562 id = SND_AUDIOCODEC_APTX;
1563 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301564 default:
1565 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1566 }
1567
1568 return id;
1569}
1570
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001571void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1572 struct audio_usecase *usecase)
1573{
1574 int type = usecase->type;
1575
Dhananjay Kumar14245982017-01-16 20:21:00 +05301576 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001577 struct stream_out *out = usecase->stream.out;
1578 int snd_device = usecase->out_snd_device;
1579 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001580 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301581 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001582 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301583 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301584 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301585 platform_send_audio_calibration(adev->platform, usecase,
1586 usecase->stream.in->app_type_cfg.app_type,
1587 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001588 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301589 /* when app type is default. the sample rate is not used to send cal */
1590 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301591 platform_get_default_app_type_v2(adev->platform, usecase->type),
1592 48000);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301593 } else if (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301594 int snd_device = usecase->out_snd_device;
1595 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1596 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1597 platform_send_audio_calibration(adev->platform, usecase,
1598 platform_get_default_app_type_v2(adev->platform, usecase->type),
1599 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001600 } else {
1601 /* No need to send audio calibration for voice and voip call usecases */
1602 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1603 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001604 }
1605}
1606
Ben Rombergera04fabc2014-11-14 12:16:03 -08001607// Base64 Encode and Decode
1608// Not all features supported. This must be used only with following conditions.
1609// Decode Modes: Support with and without padding
1610// CRLF not handling. So no CRLF in string to decode.
1611// Encode Modes: Supports only padding
1612int b64decode(char *inp, int ilen, uint8_t* outp)
1613{
1614 int i, j, k, ii, num;
1615 int rem, pcnt;
1616 uint32_t res=0;
1617 uint8_t getIndex[MAX_BASEINDEX_LEN];
1618 uint8_t tmp, cflag;
1619
1620 if(inp == NULL || outp == NULL || ilen <= 0) {
1621 ALOGE("[%s] received NULL pointer or zero length",__func__);
1622 return -1;
1623 }
1624
1625 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1626 for(i=0;i<BASE_TABLE_SIZE;i++) {
1627 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1628 }
1629 getIndex[(uint8_t)'=']=0;
1630
1631 j=0;k=0;
1632 num = ilen/4;
1633 rem = ilen%4;
1634 if(rem==0)
1635 num = num-1;
1636 cflag=0;
1637 for(i=0; i<num; i++) {
1638 res=0;
1639 for(ii=0;ii<4;ii++) {
1640 res = res << 6;
1641 tmp = getIndex[(uint8_t)inp[j++]];
1642 res = res | tmp;
1643 cflag = cflag | tmp;
1644 }
1645 outp[k++] = (res >> 16)&0xFF;
1646 outp[k++] = (res >> 8)&0xFF;
1647 outp[k++] = res & 0xFF;
1648 }
1649
1650 // Handle last bytes special
1651 pcnt=0;
1652 if(rem == 0) {
1653 //With padding or full data
1654 res = 0;
1655 for(ii=0;ii<4;ii++) {
1656 if(inp[j] == '=')
1657 pcnt++;
1658 res = res << 6;
1659 tmp = getIndex[(uint8_t)inp[j++]];
1660 res = res | tmp;
1661 cflag = cflag | tmp;
1662 }
1663 outp[k++] = res >> 16;
1664 if(pcnt == 2)
1665 goto done;
1666 outp[k++] = (res>>8)&0xFF;
1667 if(pcnt == 1)
1668 goto done;
1669 outp[k++] = res&0xFF;
1670 } else {
1671 //without padding
1672 res = 0;
1673 for(i=0;i<rem;i++) {
1674 res = res << 6;
1675 tmp = getIndex[(uint8_t)inp[j++]];
1676 res = res | tmp;
1677 cflag = cflag | tmp;
1678 }
1679 for(i=rem;i<4;i++) {
1680 res = res << 6;
1681 pcnt++;
1682 }
1683 outp[k++] = res >> 16;
1684 if(pcnt == 2)
1685 goto done;
1686 outp[k++] = (res>>8)&0xFF;
1687 if(pcnt == 1)
1688 goto done;
1689 outp[k++] = res&0xFF;
1690 }
1691done:
1692 if(cflag == 0xFF) {
1693 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1694 __func__, inp);
1695 return 0;
1696 }
1697 return k;
1698}
1699
1700int b64encode(uint8_t *inp, int ilen, char* outp)
1701{
1702 int i,j,k, num;
1703 int rem=0;
1704 uint32_t res=0;
1705
1706 if(inp == NULL || outp == NULL || ilen<=0) {
1707 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1708 return -1;
1709 }
1710
1711 num = ilen/3;
1712 rem = ilen%3;
1713 j=0;k=0;
1714 for(i=0; i<num; i++) {
1715 //prepare index
1716 res = inp[j++]<<16;
1717 res = res | inp[j++]<<8;
1718 res = res | inp[j++];
1719 //get output map from index
1720 outp[k++] = (char) bTable[(res>>18)&0x3F];
1721 outp[k++] = (char) bTable[(res>>12)&0x3F];
1722 outp[k++] = (char) bTable[(res>>6)&0x3F];
1723 outp[k++] = (char) bTable[res&0x3F];
1724 }
1725
1726 switch(rem) {
1727 case 1:
1728 res = inp[j++]<<16;
1729 outp[k++] = (char) bTable[res>>18];
1730 outp[k++] = (char) bTable[(res>>12)&0x3F];
1731 //outp[k++] = '=';
1732 //outp[k++] = '=';
1733 break;
1734 case 2:
1735 res = inp[j++]<<16;
1736 res = res | inp[j++]<<8;
1737 outp[k++] = (char) bTable[res>>18];
1738 outp[k++] = (char) bTable[(res>>12)&0x3F];
1739 outp[k++] = (char) bTable[(res>>6)&0x3F];
1740 //outp[k++] = '=';
1741 break;
1742 default:
1743 break;
1744 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001745 outp[k] = '\0';
1746 return k;
1747}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301748
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301749
1750int audio_extn_utils_get_codec_version(const char *snd_card_name,
1751 int card_num,
1752 char *codec_version)
1753{
1754 char procfs_path[50];
1755 FILE *fp;
1756
1757 if (strstr(snd_card_name, "tasha")) {
1758 snprintf(procfs_path, sizeof(procfs_path),
1759 "/proc/asound/card%d/codecs/tasha/version", card_num);
1760 if ((fp = fopen(procfs_path, "r")) != NULL) {
1761 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1762 fclose(fp);
1763 } else {
1764 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1765 return -ENOENT;
1766 }
1767 ALOGD("%s: codec version %s", __func__, codec_version);
1768 }
1769
1770 return 0;
1771}
1772
1773
Ashish Jain81eb2a82015-05-13 10:52:34 +05301774#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1775
1776void get_default_compressed_channel_status(
1777 unsigned char *channel_status)
1778{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301779 memset(channel_status,0,24);
1780
1781 /* block start bit in preamble bit 3 */
1782 channel_status[0] |= PROFESSIONAL;
1783 //compre out
1784 channel_status[0] |= NON_LPCM;
1785 // sample rate; fixed 48K for default/transcode
1786 channel_status[3] |= SR_48000;
1787}
1788
1789#ifdef HDMI_PASSTHROUGH_ENABLED
1790int32_t get_compressed_channel_status(void *audio_stream_data,
1791 uint32_t audio_frame_size,
1792 unsigned char *channel_status,
1793 enum audio_parser_code_type codec_type)
1794 // codec_type - AUDIO_PARSER_CODEC_AC3
1795 // - AUDIO_PARSER_CODEC_DTS
1796{
1797 unsigned char *stream;
1798 int ret = 0;
1799 stream = (unsigned char *)audio_stream_data;
1800
1801 if (audio_stream_data == NULL || audio_frame_size == 0) {
1802 ALOGW("no buffer to get channel status, return default for compress");
1803 get_default_compressed_channel_status(channel_status);
1804 return ret;
1805 }
1806
1807 memset(channel_status,0,24);
1808 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1809 {
1810 ALOGE("init audio parser failed");
1811 return -1;
1812 }
1813 ret = get_channel_status(channel_status, codec_type);
1814 return ret;
1815
1816}
1817
1818#endif
1819
1820void get_lpcm_channel_status(uint32_t sampleRate,
1821 unsigned char *channel_status)
1822{
1823 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301824 memset(channel_status,0,24);
1825 /* block start bit in preamble bit 3 */
1826 channel_status[0] |= PROFESSIONAL;
1827 //LPCM OUT
1828 channel_status[0] &= ~NON_LPCM;
1829
1830 switch (sampleRate) {
1831 case 8000:
1832 case 11025:
1833 case 12000:
1834 case 16000:
1835 case 22050:
1836 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301837 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301838 case 24000:
1839 channel_status[3] |= SR_24000;
1840 break;
1841 case 32000:
1842 channel_status[3] |= SR_32000;
1843 break;
1844 case 44100:
1845 channel_status[3] |= SR_44100;
1846 break;
1847 case 48000:
1848 channel_status[3] |= SR_48000;
1849 break;
1850 case 88200:
1851 channel_status[3] |= SR_88200;
1852 break;
1853 case 96000:
1854 channel_status[3] |= SR_96000;
1855 break;
1856 case 176400:
1857 channel_status[3] |= SR_176400;
1858 break;
1859 case 192000:
1860 channel_status[3] |= SR_192000;
1861 break;
1862 default:
1863 ALOGV("Invalid sample_rate %u\n", sampleRate);
1864 status = -1;
1865 break;
1866 }
1867}
1868
1869void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1870{
1871 unsigned char channel_status[24]={0};
1872 struct snd_aes_iec958 iec958;
1873 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1874 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301875 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301876#ifdef HDMI_PASSTHROUGH_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301877 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301878 /*TODO:Extend code to support DTS passthrough*/
1879 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301880 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301881 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1882 } else
1883#endif
1884 {
1885 /*set channel status bit for LPCM*/
1886 get_lpcm_channel_status(out->sample_rate, channel_status);
1887 }
1888
1889 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1890 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1891 if (!ctl) {
1892 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1893 __func__, mixer_ctl_name);
1894 return;
1895 }
1896 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1897 ALOGE("%s: Could not set channel status for ext HDMI ",
1898 __func__);
1899 return;
1900 }
1901
1902}
1903#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301904
1905int audio_extn_utils_get_avt_device_drift(
1906 struct audio_usecase *usecase,
1907 struct audio_avt_device_drift_param *drift_param)
1908{
1909 int ret = 0, count = 0;
1910 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301911 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301912 struct mixer_ctl *ctl = NULL;
1913 struct audio_avt_device_drift_stats drift_stats;
1914 struct audio_device *adev = NULL;
1915
1916 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301917 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1918 if (!backend) {
1919 ALOGE("%s: Unsupported device %d", __func__,
1920 usecase->stream.out->devices);
1921 ret = -EINVAL;
1922 goto done;
1923 }
1924 strlcpy(avt_device_drift_mixer_ctl_name,
1925 backend,
1926 MIXER_PATH_MAX_LENGTH);
1927
1928 count = strlen(backend);
1929 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1930 strlcat(&avt_device_drift_mixer_ctl_name[count],
1931 " DRIFT",
1932 MIXER_PATH_MAX_LENGTH - count);
1933 } else {
1934 ret = -EINVAL;
1935 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301936 }
1937 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301938 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301939 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301940 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301941 }
1942
Naresh Tanniru6160c712017-04-17 15:43:48 +05301943 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301944 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1945 if (!ctl) {
1946 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1947 __func__, avt_device_drift_mixer_ctl_name);
1948
1949 ret = -EINVAL;
1950 goto done;
1951 }
1952
1953 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1954 avt_device_drift_mixer_ctl_name);
1955
1956 mixer_ctl_update(ctl);
1957 count = mixer_ctl_get_num_values(ctl);
1958 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1959 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1960 __func__);
1961
1962 ret = -EINVAL;
1963 goto done;
1964 }
1965
1966 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1967 if (ret != 0) {
1968 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1969 __func__);
1970
1971 ret = -EINVAL;
1972 goto done;
1973 }
1974 memcpy(drift_param, &drift_stats.drift_param,
1975 sizeof(struct audio_avt_device_drift_param));
1976done:
1977 return ret;
1978}
Manish Dewangan07de2142017-02-27 19:27:20 +05301979
1980#ifdef SNDRV_COMPRESS_PATH_DELAY
1981int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1982{
1983 int ret = -EINVAL;
1984 struct snd_compr_metadata metadata;
1985 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1986
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08001987 /* override the latency for pcm offload use case */
1988 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
1989 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
1990 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
1991 }
1992
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001993 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301994 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1995 if (!(is_offload_usecase(out->usecase))) {
1996 ALOGE("%s:: not supported for non offload session", __func__);
1997 goto exit;
1998 }
1999
2000 if (!out->compr) {
2001 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2002 __func__);
2003 goto exit;
2004 }
2005
2006 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2007 ret = compress_get_metadata(out->compr, &metadata);
2008 if(ret) {
2009 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2010 goto exit;
2011 }
2012 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2013 } else {
2014 ALOGD("%s:: Using Fix DSP delay",__func__);
2015 }
2016
2017exit:
2018 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2019 return delay_ms;
2020}
2021#else
2022int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2023{
2024 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2025}
2026#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302027
2028#ifdef SNDRV_COMPRESS_RENDER_MODE
2029int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2030{
2031 struct snd_compr_metadata metadata;
2032 int ret = -EINVAL;
2033
2034 if (!(is_offload_usecase(out->usecase))) {
2035 ALOGE("%s:: not supported for non offload session", __func__);
2036 goto exit;
2037 }
2038
2039 if (!out->compr) {
2040 ALOGD("%s:: Invalid compress handle",
2041 __func__);
2042 goto exit;
2043 }
2044
2045 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2046
2047 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2048 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2049 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2050 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2051 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2052 } else {
2053 ret = 0;
2054 goto exit;
2055 }
2056 ret = compress_set_metadata(out->compr, &metadata);
2057 if(ret) {
2058 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2059 }
2060exit:
2061 return ret;
2062}
2063#else
2064int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2065{
2066 ALOGD("%s:: configuring render mode not supported", __func__);
2067 return 0;
2068}
2069#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302070
2071#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2072int audio_extn_utils_compress_set_clk_rec_mode(
2073 struct audio_usecase *usecase)
2074{
2075 struct snd_compr_metadata metadata;
2076 struct stream_out *out = NULL;
2077 int ret = -EINVAL;
2078
Naresh Tanniru7586e292017-04-13 10:38:13 +05302079 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302080 ALOGE("%s:: Invalid use case", __func__);
2081 goto exit;
2082 }
2083
2084 out = usecase->stream.out;
2085 if (!out) {
2086 ALOGE("%s:: invalid stream", __func__);
2087 goto exit;
2088 }
2089
2090 if (!is_offload_usecase(out->usecase)) {
2091 ALOGE("%s:: not supported for non offload session", __func__);
2092 goto exit;
2093 }
2094
2095 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2096 ALOGD("%s:: clk recovery is only supported in STC render mode",
2097 __func__);
2098 ret = 0;
2099 goto exit;
2100 }
2101
2102 if (!out->compr) {
2103 ALOGD("%s:: Invalid compress handle",
2104 __func__);
2105 goto exit;
2106 }
2107 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2108 switch(usecase->out_snd_device) {
2109 case SND_DEVICE_OUT_HDMI:
2110 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2111 case SND_DEVICE_OUT_DISPLAY_PORT:
2112 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2113 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2114 break;
2115 default:
2116 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2117 break;
2118 }
2119
2120 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2121
2122 ret = compress_set_metadata(out->compr, &metadata);
2123 if(ret) {
2124 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2125 }
2126
2127exit:
2128 return ret;
2129}
2130#else
2131int audio_extn_utils_compress_set_clk_rec_mode(
2132 struct audio_usecase *usecase __unused)
2133{
2134 ALOGD("%s:: configuring render mode not supported", __func__);
2135 return 0;
2136}
2137#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302138
2139#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2140int audio_extn_utils_compress_set_render_window(
2141 struct stream_out *out,
2142 struct audio_out_render_window_param *render_window)
2143{
2144 struct snd_compr_metadata metadata;
2145 int ret = -EINVAL;
2146
Manish Dewangan27346042017-03-01 12:56:12 +05302147 if(render_window == NULL) {
2148 ALOGE("%s:: Invalid render_window", __func__);
2149 goto exit;
2150 }
2151
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002152 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2153 __func__,render_window->render_ws, render_window->render_we);
2154
Manish Dewangan27346042017-03-01 12:56:12 +05302155 if (!is_offload_usecase(out->usecase)) {
2156 ALOGE("%s:: not supported for non offload session", __func__);
2157 goto exit;
2158 }
2159
Naresh Tanniru6160c712017-04-17 15:43:48 +05302160 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2161 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302162 ALOGD("%s:: only supported in timestamp mode, current "
2163 "render mode mode %d", __func__, out->render_mode);
2164 goto exit;
2165 }
2166
2167 if (!out->compr) {
2168 ALOGW("%s:: offload session not yet opened,"
2169 "render window will be configure later", __func__);
2170 /* store render window to reconfigure in start_output_stream() */
2171 goto exit;
2172 }
2173
2174 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2175 /*render window start value */
2176 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2177 metadata.value[1] = \
2178 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2179 /*render window end value */
2180 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2181 metadata.value[3] = \
2182 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2183
2184 ret = compress_set_metadata(out->compr, &metadata);
2185 if(ret) {
2186 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2187 }
2188
2189exit:
2190 return ret;
2191}
2192#else
2193int audio_extn_utils_compress_set_render_window(
2194 struct stream_out *out __unused,
2195 struct audio_out_render_window_param *render_window __unused)
2196{
2197 ALOGD("%s:: configuring render window not supported", __func__);
2198 return 0;
2199}
2200#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302201
2202#ifdef SNDRV_COMPRESS_START_DELAY
2203int audio_extn_utils_compress_set_start_delay(
2204 struct stream_out *out,
2205 struct audio_out_start_delay_param *delay_param)
2206{
2207 struct snd_compr_metadata metadata;
2208 int ret = -EINVAL;
2209
2210 if(delay_param == NULL) {
2211 ALOGE("%s:: Invalid delay_param", __func__);
2212 goto exit;
2213 }
2214
2215 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2216 delay_param->start_delay);
2217
2218 if (!is_offload_usecase(out->usecase)) {
2219 ALOGE("%s:: not supported for non offload session", __func__);
2220 goto exit;
2221 }
2222
Naresh Tanniru6160c712017-04-17 15:43:48 +05302223 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2224 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302225 ALOGD("%s:: only supported in timestamp mode, current "
2226 "render mode mode %d", __func__, out->render_mode);
2227 goto exit;
2228 }
2229
2230 if (!out->compr) {
2231 ALOGW("%s:: offload session not yet opened,"
2232 "start delay will be configure later", __func__);
2233 goto exit;
2234 }
2235
2236 metadata.key = SNDRV_COMPRESS_START_DELAY;
2237 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2238 metadata.value[1] = \
2239 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2240
2241 ret = compress_set_metadata(out->compr, &metadata);
2242 if(ret) {
2243 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2244 }
2245
2246exit:
2247 return ret;
2248}
2249#else
2250int audio_extn_utils_compress_set_start_delay(
2251 struct stream_out *out __unused,
2252 struct audio_out_start_delay_param *delay_param __unused)
2253{
2254 ALOGD("%s:: configuring render window not supported", __func__);
2255 return 0;
2256}
2257#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002258
2259#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302260#define RETRY_US 1000000
2261#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002262#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2263#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2264
2265#ifdef LINUX_ENABLED
2266static const char *kConfigLocationList[] =
2267 {"/etc"};
2268#else
2269static const char *kConfigLocationList[] =
2270 {"/vendor/etc"};
2271#endif
2272static const int kConfigLocationListSize =
2273 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2274
2275bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2276{
2277 char full_config_path[MIXER_PATH_MAX_LENGTH];
2278 for (int i = 0; i < kConfigLocationListSize; i++) {
2279 snprintf(full_config_path,
2280 MIXER_PATH_MAX_LENGTH,
2281 "%s/%s",
2282 kConfigLocationList[i],
2283 file_name);
2284 if (F_OK == access(full_config_path, 0)) {
2285 strcpy(file_name, full_config_path);
2286 return true;
2287 }
2288 }
2289 return false;
2290}
2291
2292/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2293int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2294{
2295 if (NULL == snd_card_name) {
2296 return -1;
2297 }
2298
2299 struct snd_card_split *snd_split_handle = NULL;
2300 int ret = 0;
2301 audio_extn_set_snd_card_split(snd_card_name);
2302 snd_split_handle = audio_extn_get_snd_card_split();
2303
2304 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2305 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2306 snd_split_handle->form_factor);
2307
2308 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2309 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2310 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2311 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2312
2313 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2314 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2315 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2316 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2317 }
2318 }
2319
2320 return ret;
2321}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002322
2323int audio_extn_utils_get_snd_card_num()
2324{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302325 int snd_card_num = 0;
2326 struct mixer *mixer = NULL;
2327
2328 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2329 if (mixer)
2330 mixer_close(mixer);
2331 return snd_card_num;
2332}
2333
2334int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2335{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002336
2337 void *hw_info = NULL;
2338 struct mixer *mixer = NULL;
2339 int retry_num = 0;
Ashish Jain30ae8942017-11-05 17:21:23 +05302340 int snd_card_num = 0, min_snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002341 char* snd_card_name = NULL;
2342
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302343 if (!mixer_handle) {
2344 ALOGE("invalid mixer handle");
2345 return -1;
2346 }
2347 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302348 /*
2349 * Try with all the sound cards ( 0 to 8 ) and if none of them were detected
2350 * sleep for 1 sec and try detections with sound card 0 again.
2351 * If sound card gets detected, check if it is relevant, if not check with the
2352 * other sound cards. To ensure that the irrelevant sound card is not check again,
2353 * we maintain it in min_snd_card_num.
2354 */
2355 while (retry_num < RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002356
Ashish Jain30ae8942017-11-05 17:21:23 +05302357 while (snd_card_num <= MAX_SND_CARD) {
2358 mixer = mixer_open(snd_card_num);
2359 if (!mixer)
2360 snd_card_num++;
2361 else
2362 break;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002363 }
2364
2365 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302366 usleep(RETRY_US);
2367 snd_card_num = min_snd_card_num;
2368 retry_num++;
2369 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002370 }
2371
2372 snd_card_name = strdup(mixer_get_name(mixer));
2373 if (!snd_card_name) {
2374 ALOGE("failed to allocate memory for snd_card_name\n");
2375 mixer_close(mixer);
2376 return -1;
2377 }
2378 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2379
2380 hw_info = hw_info_init(snd_card_name);
2381 if (hw_info) {
2382 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2383 break;
2384 }
2385 ALOGE("%s: Failed to init hardware info", __func__);
Ashish Jain30ae8942017-11-05 17:21:23 +05302386 min_snd_card_num++;
2387 snd_card_num = min_snd_card_num;
2388
2389 if (snd_card_num >= MAX_SND_CARD)
2390 break;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002391
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302392 free(snd_card_name);
2393 snd_card_name = NULL;
2394
2395 mixer_close(mixer);
2396 mixer = NULL;
2397 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002398 if (snd_card_name)
2399 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302400
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302401 if (hw_info)
2402 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002403
Ashish Jain30ae8942017-11-05 17:21:23 +05302404 if ((snd_card_num >= MAX_SND_CARD) || (retry_num >= RETRY_NUMBER)) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002405 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2406 return -1;
2407 }
2408
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302409 if (mixer)
2410 *mixer_handle = mixer;
2411
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002412 return snd_card_num;
2413}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302414
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302415void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2416{
2417 if (mixer)
2418 mixer_close(mixer);
2419}
2420
Naresh Tanniru6160c712017-04-17 15:43:48 +05302421#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2422int audio_extn_utils_compress_enable_drift_correction(
2423 struct stream_out *out,
2424 struct audio_out_enable_drift_correction *drift)
2425{
2426 struct snd_compr_metadata metadata;
2427 int ret = -EINVAL;
2428
2429 if(drift == NULL) {
2430 ALOGE("%s:: Invalid param", __func__);
2431 goto exit;
2432 }
2433
2434 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2435
2436 if (!is_offload_usecase(out->usecase)) {
2437 ALOGE("%s:: not supported for non offload session", __func__);
2438 goto exit;
2439 }
2440
2441 if (!out->compr) {
2442 ALOGW("%s:: offload session not yet opened,"
2443 "start delay will be configure later", __func__);
2444 goto exit;
2445 }
2446
2447 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2448 metadata.value[0] = drift->enable;
2449 out->drift_correction_enabled = drift->enable;
2450
2451 ret = compress_set_metadata(out->compr, &metadata);
2452 if(ret) {
2453 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2454 out->drift_correction_enabled = false;
2455 }
2456
2457exit:
2458 return ret;
2459}
2460#else
2461int audio_extn_utils_compress_enable_drift_correction(
2462 struct stream_out *out __unused,
2463 struct audio_out_enable_drift_correction *drift __unused)
2464{
2465 ALOGD("%s:: configuring drift enablement not supported", __func__);
2466 return 0;
2467}
2468#endif
2469
2470#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2471int audio_extn_utils_compress_correct_drift(
2472 struct stream_out *out,
2473 struct audio_out_correct_drift *drift_param)
2474{
2475 struct snd_compr_metadata metadata;
2476 int ret = -EINVAL;
2477
2478 if (drift_param == NULL) {
2479 ALOGE("%s:: Invalid drift_param", __func__);
2480 goto exit;
2481 }
2482
2483 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2484 drift_param->adjust_time);
2485
2486 if (!is_offload_usecase(out->usecase)) {
2487 ALOGE("%s:: not supported for non offload session", __func__);
2488 goto exit;
2489 }
2490
2491 if (!out->compr) {
2492 ALOGW("%s:: offload session not yet opened", __func__);
2493 goto exit;
2494 }
2495
2496 if (!out->drift_correction_enabled) {
2497 ALOGE("%s:: drift correction not enabled", __func__);
2498 goto exit;
2499 }
2500
2501 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2502 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2503 metadata.value[1] = \
2504 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2505
2506 ret = compress_set_metadata(out->compr, &metadata);
2507 if(ret)
2508 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2509exit:
2510 return ret;
2511}
2512#else
2513int audio_extn_utils_compress_correct_drift(
2514 struct stream_out *out __unused,
2515 struct audio_out_correct_drift *drift_param __unused)
2516{
2517 ALOGD("%s:: setting adjust clock not supported", __func__);
2518 return 0;
2519}
2520#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302521
2522int audio_extn_utils_set_channel_map(
2523 struct stream_out *out,
2524 struct audio_out_channel_map_param *channel_map_param)
2525{
2526 int ret = -EINVAL, i = 0;
2527 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2528
2529 if (channel_map_param == NULL) {
2530 ALOGE("%s:: Invalid channel_map", __func__);
2531 goto exit;
2532 }
2533
2534 if (channel_map_param->channels != channels) {
2535 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2536 __func__, channel_map_param->channels, channels);
2537 goto exit;
2538 }
2539
2540 for ( i = 0; i < channels; i++) {
2541 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2542 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2543 }
2544 ret = 0;
2545exit:
2546 return ret;
2547}
Varun Balaraje49253e2017-07-06 19:48:56 +05302548
2549int audio_extn_utils_set_pan_scale_params(
2550 struct stream_out *out,
2551 struct mix_matrix_params *mm_params)
2552{
2553 int ret = -EINVAL, i = 0, j = 0;
2554
Varun Balaraj003ee752017-08-07 17:54:55 +05302555 if (mm_params == NULL || out == NULL) {
2556 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302557 goto exit;
2558 }
2559
2560 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2561 mm_params->num_output_channels <= 0 ||
2562 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2563 mm_params->num_input_channels <= 0)
2564 goto exit;
2565
2566 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2567 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2568 out->pan_scale_params.has_output_channel_map =
2569 mm_params->has_output_channel_map;
2570 for (i = 0; i < mm_params->num_output_channels; i++)
2571 out->pan_scale_params.output_channel_map[i] =
2572 mm_params->output_channel_map[i];
2573
2574 out->pan_scale_params.has_input_channel_map =
2575 mm_params->has_input_channel_map;
2576 for (i = 0; i < mm_params->num_input_channels; i++)
2577 out->pan_scale_params.input_channel_map[i] =
2578 mm_params->input_channel_map[i];
2579
2580 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2581 for (i = 0; i < mm_params->num_output_channels; i++)
2582 for (j = 0; j < mm_params->num_input_channels; j++) {
2583 //Convert the channel coefficient gains in Q14 format
2584 out->pan_scale_params.mixer_coeffs[i][j] =
2585 mm_params->mixer_coeffs[i][j] * (2 << 13);
2586 }
2587
2588 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2589 out->pcm_device_id,
2590 out->pan_scale_params);
2591
2592exit:
2593 return ret;
2594}
2595
2596int audio_extn_utils_set_downmix_params(
2597 struct stream_out *out,
2598 struct mix_matrix_params *mm_params)
2599{
2600 int ret = -EINVAL, i = 0, j = 0;
2601 struct audio_usecase *usecase = NULL;
2602
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002603 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302604 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302605 goto exit;
2606 }
2607
2608 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2609 mm_params->num_output_channels <= 0 ||
2610 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2611 mm_params->num_input_channels <= 0)
2612 goto exit;
2613
2614 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302615 if (!usecase) {
2616 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002617 goto exit;
2618 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302619 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2620 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2621
2622 out->downmix_params.has_output_channel_map =
2623 mm_params->has_output_channel_map;
2624 for (i = 0; i < mm_params->num_output_channels; i++) {
2625 out->downmix_params.output_channel_map[i] =
2626 mm_params->output_channel_map[i];
2627 }
2628
2629 out->downmix_params.has_input_channel_map =
2630 mm_params->has_input_channel_map;
2631 for (i = 0; i < mm_params->num_input_channels; i++)
2632 out->downmix_params.input_channel_map[i] =
2633 mm_params->input_channel_map[i];
2634
2635 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2636 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302637 for (j = 0; j < mm_params->num_input_channels; j++) {
2638 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302639 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302640 mm_params->mixer_coeffs[i][j] * (2 << 13);
2641 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302642
2643 ret = platform_set_stream_downmix_params(out->dev->platform,
2644 out->pcm_device_id,
2645 usecase->out_snd_device,
2646 out->downmix_params);
2647
2648exit:
2649 return ret;
2650}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302651
2652bool audio_extn_utils_is_dolby_format(audio_format_t format)
2653{
2654 if (format == AUDIO_FORMAT_AC3 ||
2655 format == AUDIO_FORMAT_E_AC3 ||
2656 format == AUDIO_FORMAT_E_AC3_JOC)
2657 return true;
2658 else
2659 return false;
2660}
2661
`Deeraj Soman676c2702017-09-18 19:25:53 +05302662int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2663{
2664 int i;
2665 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2666 {"S32_LE", 32},
2667 {"S24_LE", 24},
2668 {"S16_LE", 16}};
2669 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302670
`Deeraj Soman676c2702017-09-18 19:25:53 +05302671 for (i = 0; i < num_configs; i++) {
2672 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2673 return mixer_bitwidth_config[i].value;
2674 }
2675
2676 return -EINVAL;
2677}
2678
2679int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2680{
2681 int i;
2682 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2683 {"KHZ_48", 48000},
2684 {"KHZ_96", 96000},
2685 {"KHZ_144", 144000},
2686 {"KHZ_192", 192000},
2687 {"KHZ_384", 384000},
2688 {"KHZ_44P1", 44100},
2689 {"KHZ_88P2", 88200},
2690 {"KHZ_176P4", 176400},
2691 {"KHZ_352P8", 352800}};
2692 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2693
2694 for (i = 0; i < num_configs; i++) {
2695 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2696 return mixer_samplerate_config[i].value;
2697 }
2698
2699 return -EINVAL;
2700}
2701
2702int audio_extn_utils_get_channels_from_string(const char *id_string)
2703{
2704 int i;
2705 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2706 {"Two", 2},
2707 {"Three",3},
2708 {"Four", 4},
2709 {"Five", 5},
2710 {"Six", 6},
2711 {"Seven", 7},
2712 {"Eight", 8}};
2713 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2714
2715 for (i = 0; i < num_configs; i++) {
2716 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2717 return mixer_channels_config[i].value;
2718 }
2719
2720 return -EINVAL;
2721}
Surendar karka30569792018-05-08 12:02:21 +05302722
2723int audio_extn_utils_get_license_params
2724(
2725const struct audio_device *adev,
2726struct audio_license_params *license_params
2727)
2728{
2729 if(!license_params)
2730 return -EINVAL;
2731 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2732}
2733
Aalique Grahame22e49102018-12-18 14:23:57 -08002734int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2735 int app_type,
2736 int *gain)
2737{
2738 int gain_cfg[4];
2739 const char *mixer_ctl_name = "App Type Gain";
2740 struct mixer_ctl *ctl;
2741 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2742 if (!ctl) {
2743 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2744 mixer_ctl_name);
2745 return -EINVAL;
2746 }
2747 gain_cfg[0] = 0;
2748 gain_cfg[1] = app_type;
2749 gain_cfg[2] = gain[0];
2750 gain_cfg[3] = gain[1];
2751 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2752 return mixer_ctl_set_array(ctl, gain_cfg,
2753 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2754}