blob: ad4e4b81dedd15776a0680aef4189b09144d2f2d [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#ifdef AUDIO_EXTN_FORMATS_ENABLED
168 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700169 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
170 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
171 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700172 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
173 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
174 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
175 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
176 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
177 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
178 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700179 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530180 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
181 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700182 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800183 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
184 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
185 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530186 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530187 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
188 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
189 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530190 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530191 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
192 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
193 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
194 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530195 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700196#endif
197};
198
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530199/* payload structure avt_device drift query */
200struct audio_avt_device_drift_stats {
201 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530202
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530203 /* Indicates the device interface direction as either
204 * source (Tx) or sink (Rx).
205 */
206 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530207
208 /* Reference timer for drift accumulation and time stamp information.
209 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
210 */
211 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530212 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530213} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530214
Ben Rombergera04fabc2014-11-14 12:16:03 -0800215static char bTable[BASE_TABLE_SIZE] = {
216 'A','B','C','D','E','F','G','H','I','J','K','L',
217 'M','N','O','P','Q','R','S','T','U','V','W','X',
218 'Y','Z','a','b','c','d','e','f','g','h','i','j',
219 'k','l','m','n','o','p','q','r','s','t','u','v',
220 'w','x','y','z','0','1','2','3','4','5','6','7',
221 '8','9','+','/'
222};
223
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700224static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
225 const char *name)
226{
227 size_t i;
228 for (i = 0; i < size; i++) {
229 if (strcmp(table[i].name, name) == 0) {
230 ALOGV("%s found %s", __func__, table[i].name);
231 return table[i].value;
232 }
233 }
234 return 0;
235}
236
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530237static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700238{
239 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530240 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800241 char *last_r;
242 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700243 while (flag_name != NULL) {
244 if (strlen(flag_name) != 0) {
245 flag |= string_to_enum(s_flag_name_to_enum_table,
246 ARRAY_SIZE(s_flag_name_to_enum_table),
247 flag_name);
248 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800249 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700250 }
251
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800252 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530253 io_flags.in_flags = (audio_input_flags_t)flag;
254 io_flags.out_flags = (audio_output_flags_t)flag;
255 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700256}
257
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530258static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700259{
260 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800261 char *last_r;
262 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700263
264 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
265 return;
266
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530267 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268 while (str != NULL) {
269 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
270 ARRAY_SIZE(s_format_name_to_enum_table), str);
271 ALOGV("%s: format - %d", __func__, format);
272 if (format != 0) {
273 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700274 if (sf_info == NULL)
275 break; /* return whatever was parsed */
276
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700277 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530278 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700279 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800280 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700281 }
282}
283
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700285{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700286 struct stream_sample_rate *ss_info = NULL;
287 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800288 char *last_r;
289 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700290
Amit Shekhar6f461b12014-08-01 14:52:58 -0700291 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
292 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700293
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530294 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700295 while (str != NULL) {
296 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
297 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
298 if (0 != sample_rate) {
299 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800300 if (!ss_info) {
301 ALOGE("%s: memory allocation failure", __func__);
302 return;
303 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700304 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530305 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700306 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800307 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700308 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700309}
310
311static int parse_bit_width_names(char *name)
312{
313 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800314 char *last_r;
315 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700316
317 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
318 bit_width = (int)strtol(str, (char **)NULL, 10);
319
320 ALOGV("%s: bit_width - %d", __func__, bit_width);
321 return bit_width;
322}
323
324static int parse_app_type_names(void *platform, char *name)
325{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700326 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800327 char *last_r;
328 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700329
330 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
331 app_type = (int)strtol(str, (char **)NULL, 10);
332
333 ALOGV("%s: app_type - %d", __func__, app_type);
334 return app_type;
335}
336
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530337static void update_streams_cfg_list(cnode *root, void *platform,
338 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700339{
340 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530341 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700342
343 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530344 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700345
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530346 if (!s_info) {
347 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700348 return;
349 }
350
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700351 while (node) {
352 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530353 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530354 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
355 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700356 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530357 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700358 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530359 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
360 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700361 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530362 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700363 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530364 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700365 }
366 node = node->next;
367 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530368 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700369}
370
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530371static void load_cfg_list(cnode *root, void *platform,
372 struct listnode *streams_output_cfg_list,
373 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530375 cnode *node = NULL;
376
377 node = config_find(root, OUTPUTS_TAG);
378 if (node != NULL) {
379 node = node->first_child;
380 while (node) {
381 ALOGV("%s: loading output %s", __func__, node->name);
382 update_streams_cfg_list(node, platform, streams_output_cfg_list);
383 node = node->next;
384 }
385 } else {
386 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700387 }
388
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530389 node = config_find(root, INPUTS_TAG);
390 if (node != NULL) {
391 node = node->first_child;
392 while (node) {
393 ALOGV("%s: loading input %s", __func__, node->name);
394 update_streams_cfg_list(node, platform, streams_input_cfg_list);
395 node = node->next;
396 }
397 } else {
398 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700399 }
400}
401
402static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530403 struct listnode *streams_output_cfg_list,
404 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700405{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530406 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700407 int length = 0, i, num_app_types = 0;
408 struct listnode *node;
409 bool update;
410 struct mixer_ctl *ctl = NULL;
411 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530412 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800413 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700414
415 if (!mixer) {
416 ALOGE("%s: mixer is null",__func__);
417 return;
418 }
419 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
420 if (!ctl) {
421 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
422 return;
423 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530424 app_type_cfg[length++] = num_app_types;
425
426 if (list_empty(streams_output_cfg_list)) {
427 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700428 app_type_cfg[length++] = 48000;
429 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530430 num_app_types += 1;
431 }
432 if (list_empty(streams_input_cfg_list)) {
433 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
434 app_type_cfg[length++] = 48000;
435 app_type_cfg[length++] = 16;
436 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700437 }
438
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800439 /* get target bit width for ADM enforce mode */
440 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
441
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700442 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530443 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700444 update = true;
445 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530446 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700447 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530448 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530449 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530450 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530451 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530452 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800453 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
454 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
455 (target_bit_width > app_type_cfg[i+3]))
456 app_type_cfg[i+3] = target_bit_width;
457
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700458 update = false;
459 break;
460 }
461 }
462 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530463 num_app_types += 1;
464 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
465 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800466 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
467 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
468 (target_bit_width > app_type_cfg[length]))
469 app_type_cfg[length] = target_bit_width;
470
471 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530472 }
473 }
474 list_for_each(node, streams_input_cfg_list) {
475 s_info = node_to_item(node, struct streams_io_cfg, list);
476 update = true;
477 for (i=0; i<length; i=i+3) {
478 if (app_type_cfg[i+1] == 0)
479 break;
480 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530481 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530482 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530483 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530484 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
485 update = false;
486 break;
487 }
488 }
489 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
490 num_app_types += 1;
491 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
492 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
493 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700494 }
495 }
496 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
497 if (num_app_types) {
498 app_type_cfg[0] = num_app_types;
499 mixer_ctl_set_array(ctl, app_type_cfg, length);
500 }
501}
502
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530503void audio_extn_utils_update_streams_cfg_lists(void *platform,
504 struct mixer *mixer,
505 struct listnode *streams_output_cfg_list,
506 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700507{
508 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530509 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700510
511 ALOGV("%s", __func__);
512 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530513 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700514
515 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700516 if (root == NULL) {
517 ALOGE("cfg_list, NULL config root");
518 return;
519 }
520
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530521 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
522 if (data == NULL) {
523 ALOGD("%s: failed to open io config file(%s), trying older config file",
524 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
525 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
526 if (data == NULL) {
527 send_app_type_cfg(platform, mixer,
528 streams_output_cfg_list,
529 streams_input_cfg_list);
530 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800531 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530532 return;
533 }
534 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700535
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530536 config_load(root, data);
537 load_cfg_list(root, platform, streams_output_cfg_list,
538 streams_input_cfg_list);
539
540 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
541 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800542
543 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800544 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800545 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700546}
547
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530548static void audio_extn_utils_dump_streams_cfg_list(
549 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700550{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700551 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530552 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700553 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700554 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530555
556 list_for_each(node_i, streams_cfg_list) {
557 s_info = node_to_item(node_i, struct streams_io_cfg, list);
558 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
559 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
560 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
561 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700562 sf_info = node_to_item(node_j, struct stream_format, list);
563 ALOGV("format-%x", sf_info->format);
564 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530565 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700566 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
567 ALOGV("sample rate-%d", ss_info->sample_rate);
568 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700569 }
570}
571
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530572void audio_extn_utils_dump_streams_cfg_lists(
573 struct listnode *streams_output_cfg_list,
574 struct listnode *streams_input_cfg_list)
575{
576 ALOGV("%s", __func__);
577 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
578 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
579}
580
581static void audio_extn_utils_release_streams_cfg_list(
582 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700583{
584 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530585 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700586
587 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530588
589 while (!list_empty(streams_cfg_list)) {
590 node_i = list_head(streams_cfg_list);
591 s_info = node_to_item(node_i, struct streams_io_cfg, list);
592 while (!list_empty(&s_info->format_list)) {
593 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700594 list_remove(node_j);
595 free(node_to_item(node_j, struct stream_format, list));
596 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530597 while (!list_empty(&s_info->sample_rate_list)) {
598 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700599 list_remove(node_j);
600 free(node_to_item(node_j, struct stream_sample_rate, list));
601 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700602 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530603 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700604 }
605}
606
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530607void audio_extn_utils_release_streams_cfg_lists(
608 struct listnode *streams_output_cfg_list,
609 struct listnode *streams_input_cfg_list)
610{
611 ALOGV("%s", __func__);
612 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
613 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
614}
615
616static bool set_app_type_cfg(struct streams_io_cfg *s_info,
617 struct stream_app_type_cfg *app_type_cfg,
618 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700619 {
620 struct listnode *node_i;
621 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530622 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700623 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
624 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530625 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700626
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530627 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700628 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530629 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700630 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
631 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
632 return true;
633 }
634 }
635 /*
636 * Reiterate through the list assuming dafault sample rate.
637 * Handles scenario where input sample rate is higher
638 * than all sample rates in list for the input bit width.
639 */
640 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800641
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530642 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700643 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
644 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530645 (bit_width == s_info->app_type_cfg.bit_width)) {
646 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700647 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530648 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800649 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 -0700650 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
651 return true;
652 }
653 }
654 return false;
655}
656
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530657void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
658 struct listnode *streams_input_cfg_list,
659 audio_devices_t devices __unused,
660 audio_input_flags_t flags,
661 audio_format_t format,
662 uint32_t sample_rate,
663 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530664 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530665 struct stream_app_type_cfg *app_type_cfg)
666{
667 struct listnode *node_i, *node_j;
668 struct streams_io_cfg *s_info;
669 struct stream_format *sf_info;
670
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530671 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
672 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530673
674 list_for_each(node_i, streams_input_cfg_list) {
675 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530676 /* Along with flags do profile matching if set at either end.*/
677 if (s_info->flags.in_flags == flags &&
678 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
679 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530680 list_for_each(node_j, &s_info->format_list) {
681 sf_info = node_to_item(node_j, struct stream_format, list);
682 if (sf_info->format == format) {
683 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
684 return;
685 }
686 }
687 }
688 }
689 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
690 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
691 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
692 app_type_cfg->bit_width = 16;
693}
694
695void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700696 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700697 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700698 audio_output_flags_t flags,
699 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700700 uint32_t sample_rate,
701 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530702 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530703 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700704 struct stream_app_type_cfg *app_type_cfg)
705{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530706 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530707 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700708 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530709 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700710
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530711 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
712 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
713 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700714 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530715 else if (-ENOSYS == bw)
716 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700717 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530718 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700719 }
720
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700721 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530722 if (!strncmp("true", value, sizeof("true"))) {
723 if ((popcount(channel_mask) > 2) &&
724 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700725 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530726 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
727 ALOGD("%s: MCH session defaulting sample rate to %d",
728 __func__, sample_rate);
729 }
730 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530731
732 /* Set sampling rate to 176.4 for DSD64
733 * and 352.8Khz for DSD128.
734 * Set Bit Width to 16. output will be 16 bit
735 * post DoP in ASM.
736 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700737 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530738 (format == AUDIO_FORMAT_DSD)) {
739 bit_width = 16;
740 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
741 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
742 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
743 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
744 }
745
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530746
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800747 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
748 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700749 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530750 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530751 /* Along with flags do profile matching if set at either end.*/
752 if (s_info->flags.out_flags == flags &&
753 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
754 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530755 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700756 sf_info = node_to_item(node_j, struct stream_format, list);
757 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530758 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700759 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700760 }
761 }
762 }
763 }
764 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530765 s_info = node_to_item(node_i, struct streams_io_cfg, list);
766 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530768 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
769 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
770 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700771 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530772 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700773 return;
774 }
775 }
776 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700777 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700778 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700779 app_type_cfg->bit_width = 16;
780}
781
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530782static bool audio_is_this_native_usecase(struct audio_usecase *uc)
783{
784 bool native_usecase = false;
785 struct stream_out *out = (struct stream_out*) uc->stream.out;
786
787 if (PCM_PLAYBACK == uc->type && out != NULL &&
788 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
789 is_offload_usecase(uc->id) &&
790 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
791 native_usecase = true;
792
793 return native_usecase;
794}
795
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800796bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
797{
798 /* DSP bitwidth enforce mode for ADM and AFE:
799 * includes:
800 * deep buffer, low latency, direct pcm and offload.
801 * excludes:
802 * ull(raw+fast), VOIP.
803 */
804 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
805 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
806 (flags & AUDIO_OUTPUT_FLAG_FAST)))
807 return false;
808
809
810 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
811 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
812 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
813 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
814 return true;
815 else
816 return false;
817}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800818
819static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
820{
821 return adev->vr_audio_mode_enabled;
822}
823
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530824void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
825 struct audio_device *adev,
826 struct audio_usecase *usecase)
827{
828 ALOGV("%s", __func__);
829
830 switch(usecase->type) {
831 case PCM_PLAYBACK:
832 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
833 &adev->streams_output_cfg_list,
834 usecase->stream.out->devices,
835 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700836 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530837 usecase->stream.out->sample_rate,
838 usecase->stream.out->bit_width,
839 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530840 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530841 &usecase->stream.out->app_type_cfg);
842 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
843 break;
844 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700845 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
846 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
847 else
848 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530849 &adev->streams_input_cfg_list,
850 usecase->stream.in->device,
851 usecase->stream.in->flags,
852 usecase->stream.in->format,
853 usecase->stream.in->sample_rate,
854 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530855 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530856 &usecase->stream.in->app_type_cfg);
857 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
858 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530859 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530860 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
861 &adev->streams_output_cfg_list,
862 usecase->stream.inout->out_config.devices,
863 0,
864 usecase->stream.inout->out_config.format,
865 usecase->stream.inout->out_config.sample_rate,
866 usecase->stream.inout->out_config.bit_width,
867 usecase->stream.inout->out_config.channel_mask,
868 usecase->stream.inout->profile,
869 &usecase->stream.inout->out_app_type_cfg);
870 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
871 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530872 default:
873 ALOGE("%s: app type cfg not supported for usecase type (%d)",
874 __func__, usecase->type);
875 }
876}
877
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530878void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
879{
880 switch (snd_device) {
881 case SND_DEVICE_OUT_BT_SCO:
882 case SND_DEVICE_IN_BT_SCO_MIC:
883 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
884 *sample_rate = 8000;
885 break;
886 case SND_DEVICE_OUT_BT_SCO_WB:
887 case SND_DEVICE_IN_BT_SCO_MIC_WB:
888 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
889 *sample_rate = 16000;
890 break;
891 default:
892 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
893 break;
894 }
895}
896
Aalique Grahame22e49102018-12-18 14:23:57 -0800897static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
898 int pcm_device_id, int app_type,
899 int acdb_dev_id, int sample_rate,
900 int stream_type,
901 snd_device_t snd_device)
902{
903
904 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
905 struct mixer_ctl *ctl;
906 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
907 int snd_device_be_idx = -1;
908
909 if (stream_type == PCM_PLAYBACK) {
910 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
911 "Audio Stream %d App Type Cfg", pcm_device_id);
912 } else if (stream_type == PCM_CAPTURE) {
913 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
914 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
915 }
916
917 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
918 if (!ctl) {
919 ALOGE("%s: Could not get ctl for mixer cmd - %s",
920 __func__, mixer_ctl_name);
921 rc = -EINVAL;
922 goto exit;
923 }
924 app_type_cfg[len++] = app_type;
925 app_type_cfg[len++] = acdb_dev_id;
926 app_type_cfg[len++] = sample_rate;
927
928 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
929 if (snd_device_be_idx > 0)
930 app_type_cfg[len++] = snd_device_be_idx;
931 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
932 "sample rate %d, snd_device_be_idx %d",
933 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
934 snd_device_be_idx);
935 mixer_ctl_set_array(ctl, app_type_cfg, len);
936
937exit:
938 return rc;
939}
940
941static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
942 struct audio_usecase *usecase)
943{
944 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
945 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
946 int app_type = 0, rc = 0;
947
948 ALOGV("%s", __func__);
949
950 if (usecase->type != PCM_HFP_CALL) {
951 ALOGV("%s: not a playback or HFP path, no need to cfg app type", __func__);
952 rc = 0;
953 goto exit_send_app_type_cfg;
954 }
955 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
956 (usecase->id != USECASE_AUDIO_HFP_SCO_WB)) {
957 ALOGV("%s: a playback path where app type cfg is not required", __func__);
958 rc = 0;
959 goto exit_send_app_type_cfg;
960 }
961
962 snd_device = usecase->out_snd_device;
963 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
964
965 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
966 if (acdb_dev_id < 0) {
967 ALOGE("%s: Couldn't get the acdb dev id", __func__);
968 rc = -EINVAL;
969 goto exit_send_app_type_cfg;
970 }
971
972 if (usecase->type == PCM_HFP_CALL) {
973
974 /* config HFP session:1 playback path */
975 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
976 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
977 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
978 acdb_dev_id, sample_rate,
979 PCM_PLAYBACK,
980 SND_DEVICE_NONE); // use legacy behavior
981 if (rc < 0)
982 goto exit_send_app_type_cfg;
983
984 /* config HFP session:1 capture path */
985 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
986 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
987 acdb_dev_id, sample_rate,
988 PCM_CAPTURE,
989 SND_DEVICE_NONE);
990 if (rc < 0)
991 goto exit_send_app_type_cfg;
992
993 /* config HFP session:2 capture path */
994 pcm_device_id = HFP_ASM_RX_TX;
995 snd_device = usecase->in_snd_device;
996 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
997 if (acdb_dev_id <= 0) {
998 ALOGE("%s: Couldn't get the acdb dev id", __func__);
999 rc = -EINVAL;
1000 goto exit_send_app_type_cfg;
1001 }
1002 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1003 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1004 acdb_dev_id, sample_rate, PCM_CAPTURE,
1005 SND_DEVICE_NONE);
1006 if (rc < 0)
1007 goto exit_send_app_type_cfg;
1008
1009 /* config HFP session:2 playback path */
1010 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1011 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1012 acdb_dev_id, sample_rate,
1013 PCM_PLAYBACK, SND_DEVICE_NONE);
1014 if (rc < 0)
1015 goto exit_send_app_type_cfg;
1016 }
1017
1018 rc = 0;
1019exit_send_app_type_cfg:
1020 return rc;
1021}
1022
Siena Richard7c2db772016-12-21 11:32:34 -08001023static int send_app_type_cfg_for_device(struct audio_device *adev,
1024 struct audio_usecase *usecase,
1025 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001026{
1027 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301028 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1029 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001030 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001031 int pcm_device_id = 0, acdb_dev_id, app_type;
1032 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301033 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +05301034 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +05301035 struct streams_io_cfg *s_info = NULL;
1036 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301037 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001038
Siena Richard7c2db772016-12-21 11:32:34 -08001039 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1040 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1041 platform_get_snd_device_name(usecase->in_snd_device),
1042 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001043
Siddartha Shaik343abc62017-08-08 11:15:25 +05301044 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301045 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301046 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001047 rc = 0;
1048 goto exit_send_app_type_cfg;
1049 }
1050 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
1051 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1052 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001053 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001054 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301055 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301056 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301057 (!is_offload_usecase(usecase->id)) &&
1058 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301059 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 -07001060 rc = 0;
1061 goto exit_send_app_type_cfg;
1062 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001063
1064 //if VR is active then only send the mixer control
1065 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1066 ALOGI("ULL doesnt need sending app type cfg, returning");
1067 rc = 0;
1068 goto exit_send_app_type_cfg;
1069 }
1070
Surendar Karka93cd25a2018-08-28 14:21:37 +05301071 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001072 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301073 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1074 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001075 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001076 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301077 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1078 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301079 }
Siena Richard7c2db772016-12-21 11:32:34 -08001080
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001081 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1082 if (!ctl) {
1083 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1084 mixer_ctl_name);
1085 rc = -EINVAL;
1086 goto exit_send_app_type_cfg;
1087 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301088 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301089 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1090 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1091 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1092 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1093 } else {
1094 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1095 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301096 if (acdb_dev_id <= 0) {
1097 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1098 rc = -EINVAL;
1099 goto exit_send_app_type_cfg;
1100 }
1101
Siena Richard7c2db772016-12-21 11:32:34 -08001102 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1103 if (snd_device_be_idx < 0) {
1104 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1105 __func__, platform_get_snd_device_name(snd_device),
1106 snd_device_be_idx);
1107 }
1108
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301109 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +05301110
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001111 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301112 if (!strncmp("true", value, sizeof("true"))) {
1113 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1114 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001115 !(usecase->stream.out->flags &
1116 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301117 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1118 }
Ashish Jainc02430d2016-01-04 10:42:43 +05301119
kunleizcdf25942017-09-20 14:01:21 +08001120 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1121 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
1122 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Dieter Luecking5d57def2018-09-07 14:23:37 +02001123 if (platform_spkr_use_default_sample_rate(adev->platform)) {
1124 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1125 } else {
1126 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1127 usecase->stream.out->sample_rate,
1128 &usecase->stream.out->app_type_cfg.sample_rate);
1129 }
1130
Ashish Jaina052e572016-11-07 16:41:07 +05301131 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1132 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1133 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1134 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1135 /*
1136 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1137 * configured device sample rate, if not update the COPP rate to be equal to the
1138 * device sample rate, else open COPP at stream sample rate
1139 */
1140 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1141 usecase->stream.out->sample_rate,
1142 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05301143 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1144 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -07001145 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1146 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +05301147 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -07001148 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301149 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1150 /*
1151 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1152 * for bit width use the stream param only.
1153 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001154 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301155 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1156 __func__, usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001157 }
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301158 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001159 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1160
Varun Balaraje49253e2017-07-06 19:48:56 +05301161 /* Interactive streams are supported with only direct app type id.
1162 * Get Direct profile app type and use it for interactive streams
1163 */
1164 list_for_each(node, &adev->streams_output_cfg_list) {
1165 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001166 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301167 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1168 AUDIO_OUTPUT_FLAG_DIRECT))
1169 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301170 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001171 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301172 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301173 else
1174 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001175 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001176 app_type_cfg[len++] = acdb_dev_id;
1177 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001178 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1179 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301180 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1181 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001182
1183 sample_rate = sample_rate * 4;
1184 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1185 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001186 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001187 app_type_cfg[len++] = sample_rate;
1188
Siena Richard7c2db772016-12-21 11:32:34 -08001189 if (snd_device_be_idx > 0)
1190 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301191
Siena Richard7c2db772016-12-21 11:32:34 -08001192 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1193 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301194
1195 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001196 app_type = usecase->stream.in->app_type_cfg.app_type;
1197 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301198 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001199 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1200 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301201 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1202 audio_extn_btsco_get_sample_rate(usecase->in_snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1203 } else {
1204 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1205 }
Deeraj Somanfc791e72018-11-30 13:23:01 +05301206 if (usecase->stream.in->device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP & ~AUDIO_DEVICE_BIT_IN) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001207 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
1208 ALOGI("%s using %d sample rate rate for A2DP dec CoPP",
1209 __func__, usecase->stream.in->app_type_cfg.sample_rate);
1210 }
Siena Richard7c2db772016-12-21 11:32:34 -08001211 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1212 app_type_cfg[len++] = sample_rate;
1213 if (snd_device_be_idx > 0)
1214 app_type_cfg[len++] = snd_device_be_idx;
1215 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1216 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301217 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001218 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301219 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301220 sample_rate = usecase->stream.inout->out_config.sample_rate;
1221 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1222 }
Siena Richard7c2db772016-12-21 11:32:34 -08001223 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301224 app_type_cfg[len++] = acdb_dev_id;
1225 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001226 if (snd_device_be_idx > 0)
1227 app_type_cfg[len++] = snd_device_be_idx;
1228 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1229 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301230 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301231
Siddartha Shaik343abc62017-08-08 11:15:25 +05301232 if(ctl)
1233 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001234 rc = 0;
1235exit_send_app_type_cfg:
1236 return rc;
1237}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001238
Siena Richard7c2db772016-12-21 11:32:34 -08001239int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1240 struct audio_usecase *usecase)
1241{
1242 int i, num_devices = 0;
1243 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301244 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001245 int rc = 0;
1246
Aalique Grahame22e49102018-12-18 14:23:57 -08001247 if (usecase->type == PCM_HFP_CALL) {
1248 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1249 }
1250
Siena Richard7c2db772016-12-21 11:32:34 -08001251 switch (usecase->type) {
1252 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301253 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001254 ALOGD("%s: usecase->out_snd_device %s",
1255 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1256 /* check for out combo device */
1257 if (platform_split_snd_device(adev->platform,
1258 usecase->out_snd_device,
1259 &num_devices, new_snd_devices)) {
1260 new_snd_devices[0] = usecase->out_snd_device;
1261 num_devices = 1;
1262 }
1263 break;
1264 case PCM_CAPTURE:
1265 ALOGD("%s: usecase->in_snd_device %s",
1266 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301267 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1268 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1269 }
Siena Richard7c2db772016-12-21 11:32:34 -08001270 /* check for in combo device */
1271 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301272 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001273 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301274 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001275 num_devices = 1;
1276 }
1277 break;
1278 default:
1279 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1280 rc = 0;
1281 break;
1282 }
1283
1284 for (i = 0; i < num_devices; i++) {
1285 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1286 if (rc)
1287 break;
1288 }
1289
1290 return rc;
1291}
1292
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301293int read_line_from_file(const char *path, char *buf, size_t count)
1294{
1295 char * fgets_ret;
1296 FILE * fd;
1297 int rv;
1298
1299 fd = fopen(path, "r");
1300 if (fd == NULL)
1301 return -1;
1302
1303 fgets_ret = fgets(buf, (int)count, fd);
1304 if (NULL != fgets_ret) {
1305 rv = (int)strlen(buf);
1306 } else {
1307 rv = ferror(fd);
1308 }
1309 fclose(fd);
1310
1311 return rv;
1312}
1313
Ashish Jainf1eaa582016-05-23 20:54:24 +05301314/*Translates ALSA formats to AOSP PCM formats*/
1315audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1316{
1317 audio_format_t format;
1318
1319 switch(alsa_format) {
1320 case SNDRV_PCM_FORMAT_S16_LE:
1321 format = AUDIO_FORMAT_PCM_16_BIT;
1322 break;
1323 case SNDRV_PCM_FORMAT_S24_3LE:
1324 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1325 break;
1326 case SNDRV_PCM_FORMAT_S24_LE:
1327 format = AUDIO_FORMAT_PCM_8_24_BIT;
1328 break;
1329 case SNDRV_PCM_FORMAT_S32_LE:
1330 format = AUDIO_FORMAT_PCM_32_BIT;
1331 break;
1332 default:
1333 ALOGW("Incorrect ALSA format");
1334 format = AUDIO_FORMAT_INVALID;
1335 }
1336 return format;
1337}
1338
1339/*Translates hal format (AOSP) to alsa formats*/
1340uint32_t hal_format_to_alsa(audio_format_t hal_format)
1341{
1342 uint32_t alsa_format;
1343
1344 switch (hal_format) {
1345 case AUDIO_FORMAT_PCM_32_BIT: {
1346 if (platform_supports_true_32bit())
1347 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1348 else
1349 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1350 }
1351 break;
1352 case AUDIO_FORMAT_PCM_8_BIT:
1353 alsa_format = SNDRV_PCM_FORMAT_S8;
1354 break;
1355 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1356 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1357 break;
1358 case AUDIO_FORMAT_PCM_8_24_BIT: {
1359 if (platform_supports_true_32bit())
1360 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1361 else
1362 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1363 }
1364 break;
1365 case AUDIO_FORMAT_PCM_FLOAT:
1366 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1367 break;
1368 default:
1369 case AUDIO_FORMAT_PCM_16_BIT:
1370 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1371 break;
1372 }
1373 return alsa_format;
1374}
1375
Ashish Jain83a6cc22016-06-28 14:34:17 +05301376/*Translates PCM formats to AOSP formats*/
1377audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1378{
1379 audio_format_t format = AUDIO_FORMAT_INVALID;
1380
1381 switch(pcm_format) {
1382 case PCM_FORMAT_S16_LE:
1383 format = AUDIO_FORMAT_PCM_16_BIT;
1384 break;
1385 case PCM_FORMAT_S24_3LE:
1386 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1387 break;
1388 case PCM_FORMAT_S24_LE:
1389 format = AUDIO_FORMAT_PCM_8_24_BIT;
1390 break;
1391 case PCM_FORMAT_S32_LE:
1392 format = AUDIO_FORMAT_PCM_32_BIT;
1393 break;
1394 default:
1395 ALOGW("Incorrect PCM format");
1396 format = AUDIO_FORMAT_INVALID;
1397 }
1398 return format;
1399}
1400
1401/*Translates hal format (AOSP) to alsa formats*/
1402uint32_t hal_format_to_pcm(audio_format_t hal_format)
1403{
1404 uint32_t pcm_format;
1405
1406 switch (hal_format) {
1407 case AUDIO_FORMAT_PCM_32_BIT:
1408 case AUDIO_FORMAT_PCM_8_24_BIT:
1409 case AUDIO_FORMAT_PCM_FLOAT: {
1410 if (platform_supports_true_32bit())
1411 pcm_format = PCM_FORMAT_S32_LE;
1412 else
1413 pcm_format = PCM_FORMAT_S24_3LE;
1414 }
1415 break;
1416 case AUDIO_FORMAT_PCM_8_BIT:
1417 pcm_format = PCM_FORMAT_S8;
1418 break;
1419 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1420 pcm_format = PCM_FORMAT_S24_3LE;
1421 break;
1422 default:
1423 case AUDIO_FORMAT_PCM_16_BIT:
1424 pcm_format = PCM_FORMAT_S16_LE;
1425 break;
1426 }
1427 return pcm_format;
1428}
1429
Ashish Jainf1eaa582016-05-23 20:54:24 +05301430uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1431 uint32_t sample_rate,
1432 uint32_t noOfChannels)
1433{
1434 uint32_t fragment_size = 0;
1435 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1436
1437 fragment_size = (pcm_offload_time
1438 * sample_rate
1439 * bytes_per_sample
1440 * noOfChannels)/1000;
1441 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1442 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1443 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1444 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1445 /*To have same PCM samples for all channels, the buffer size requires to
1446 *be multiple of (number of channels * bytes per sample)
1447 *For writes to succeed, the buffer must be written at address which is multiple of 32
1448 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001449 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301450
1451 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1452 return fragment_size;
1453}
1454
1455/* Calculates the fragment size required to configure compress session.
1456 * Based on the alsa format selected, decide if conversion is needed in
1457
1458 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1459 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1460 */
1461void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1462{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301463 audio_format_t dst_format = out->hal_op_format;
1464 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301465 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1466 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1467
1468 out->compr_config.fragment_size =
1469 get_alsa_fragment_size(hal_op_bytes_per_sample,
1470 out->sample_rate,
1471 popcount(out->channel_mask));
1472
1473 if ((src_format != dst_format) &&
1474 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1475
Ashish Jain83a6cc22016-06-28 14:34:17 +05301476 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301477 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1478 hal_op_bytes_per_sample);
1479 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301480 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301481 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301482 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301483 }
1484}
1485
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301486/* converts pcm format 24_8 to 8_24 inplace */
1487size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1488{
1489 size_t i = 0;
1490 int *int_buf_stream = buf;
1491
1492 if ((bytes % 4) != 0) {
1493 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1494 return -EINVAL;
1495 }
1496
1497 for (; i < (bytes / 4); i++)
1498 int_buf_stream[i] >>= 8;
1499
1500 return bytes;
1501}
1502
1503int get_snd_codec_id(audio_format_t format)
1504{
1505 int id = 0;
1506
1507 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1508 case AUDIO_FORMAT_MP3:
1509 id = SND_AUDIOCODEC_MP3;
1510 break;
1511 case AUDIO_FORMAT_AAC:
1512 id = SND_AUDIOCODEC_AAC;
1513 break;
1514 case AUDIO_FORMAT_AAC_ADTS:
1515 id = SND_AUDIOCODEC_AAC;
1516 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301517 case AUDIO_FORMAT_AAC_LATM:
1518 id = SND_AUDIOCODEC_AAC;
1519 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301520 case AUDIO_FORMAT_PCM:
1521 id = SND_AUDIOCODEC_PCM;
1522 break;
1523 case AUDIO_FORMAT_FLAC:
1524 id = SND_AUDIOCODEC_FLAC;
1525 break;
1526 case AUDIO_FORMAT_ALAC:
1527 id = SND_AUDIOCODEC_ALAC;
1528 break;
1529 case AUDIO_FORMAT_APE:
1530 id = SND_AUDIOCODEC_APE;
1531 break;
1532 case AUDIO_FORMAT_VORBIS:
1533 id = SND_AUDIOCODEC_VORBIS;
1534 break;
1535 case AUDIO_FORMAT_WMA:
1536 id = SND_AUDIOCODEC_WMA;
1537 break;
1538 case AUDIO_FORMAT_WMA_PRO:
1539 id = SND_AUDIOCODEC_WMA_PRO;
1540 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301541 case AUDIO_FORMAT_MP2:
1542 id = SND_AUDIOCODEC_MP2;
1543 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301544 case AUDIO_FORMAT_AC3:
1545 id = SND_AUDIOCODEC_AC3;
1546 break;
1547 case AUDIO_FORMAT_E_AC3:
1548 case AUDIO_FORMAT_E_AC3_JOC:
1549 id = SND_AUDIOCODEC_EAC3;
1550 break;
1551 case AUDIO_FORMAT_DTS:
1552 case AUDIO_FORMAT_DTS_HD:
1553 id = SND_AUDIOCODEC_DTS;
1554 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001555 case AUDIO_FORMAT_DOLBY_TRUEHD:
1556 id = SND_AUDIOCODEC_TRUEHD;
1557 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001558 case AUDIO_FORMAT_IEC61937:
1559 id = SND_AUDIOCODEC_IEC61937;
1560 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301561 case AUDIO_FORMAT_DSD:
1562 id = SND_AUDIOCODEC_DSD;
1563 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301564 case AUDIO_FORMAT_APTX:
1565 id = SND_AUDIOCODEC_APTX;
1566 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301567 default:
1568 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1569 }
1570
1571 return id;
1572}
1573
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001574void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1575 struct audio_usecase *usecase)
1576{
1577 int type = usecase->type;
1578
Dhananjay Kumar14245982017-01-16 20:21:00 +05301579 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001580 struct stream_out *out = usecase->stream.out;
1581 int snd_device = usecase->out_snd_device;
1582 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001583 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301584 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001585 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301586 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301587 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301588 platform_send_audio_calibration(adev->platform, usecase,
1589 usecase->stream.in->app_type_cfg.app_type,
1590 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001591 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301592 /* when app type is default. the sample rate is not used to send cal */
1593 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301594 platform_get_default_app_type_v2(adev->platform, usecase->type),
1595 48000);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301596 } else if (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301597 int snd_device = usecase->out_snd_device;
1598 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1599 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1600 platform_send_audio_calibration(adev->platform, usecase,
1601 platform_get_default_app_type_v2(adev->platform, usecase->type),
1602 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001603 } else {
1604 /* No need to send audio calibration for voice and voip call usecases */
1605 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1606 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001607 }
1608}
1609
Ben Rombergera04fabc2014-11-14 12:16:03 -08001610// Base64 Encode and Decode
1611// Not all features supported. This must be used only with following conditions.
1612// Decode Modes: Support with and without padding
1613// CRLF not handling. So no CRLF in string to decode.
1614// Encode Modes: Supports only padding
1615int b64decode(char *inp, int ilen, uint8_t* outp)
1616{
1617 int i, j, k, ii, num;
1618 int rem, pcnt;
1619 uint32_t res=0;
1620 uint8_t getIndex[MAX_BASEINDEX_LEN];
1621 uint8_t tmp, cflag;
1622
1623 if(inp == NULL || outp == NULL || ilen <= 0) {
1624 ALOGE("[%s] received NULL pointer or zero length",__func__);
1625 return -1;
1626 }
1627
1628 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1629 for(i=0;i<BASE_TABLE_SIZE;i++) {
1630 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1631 }
1632 getIndex[(uint8_t)'=']=0;
1633
1634 j=0;k=0;
1635 num = ilen/4;
1636 rem = ilen%4;
1637 if(rem==0)
1638 num = num-1;
1639 cflag=0;
1640 for(i=0; i<num; i++) {
1641 res=0;
1642 for(ii=0;ii<4;ii++) {
1643 res = res << 6;
1644 tmp = getIndex[(uint8_t)inp[j++]];
1645 res = res | tmp;
1646 cflag = cflag | tmp;
1647 }
1648 outp[k++] = (res >> 16)&0xFF;
1649 outp[k++] = (res >> 8)&0xFF;
1650 outp[k++] = res & 0xFF;
1651 }
1652
1653 // Handle last bytes special
1654 pcnt=0;
1655 if(rem == 0) {
1656 //With padding or full data
1657 res = 0;
1658 for(ii=0;ii<4;ii++) {
1659 if(inp[j] == '=')
1660 pcnt++;
1661 res = res << 6;
1662 tmp = getIndex[(uint8_t)inp[j++]];
1663 res = res | tmp;
1664 cflag = cflag | tmp;
1665 }
1666 outp[k++] = res >> 16;
1667 if(pcnt == 2)
1668 goto done;
1669 outp[k++] = (res>>8)&0xFF;
1670 if(pcnt == 1)
1671 goto done;
1672 outp[k++] = res&0xFF;
1673 } else {
1674 //without padding
1675 res = 0;
1676 for(i=0;i<rem;i++) {
1677 res = res << 6;
1678 tmp = getIndex[(uint8_t)inp[j++]];
1679 res = res | tmp;
1680 cflag = cflag | tmp;
1681 }
1682 for(i=rem;i<4;i++) {
1683 res = res << 6;
1684 pcnt++;
1685 }
1686 outp[k++] = res >> 16;
1687 if(pcnt == 2)
1688 goto done;
1689 outp[k++] = (res>>8)&0xFF;
1690 if(pcnt == 1)
1691 goto done;
1692 outp[k++] = res&0xFF;
1693 }
1694done:
1695 if(cflag == 0xFF) {
1696 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1697 __func__, inp);
1698 return 0;
1699 }
1700 return k;
1701}
1702
1703int b64encode(uint8_t *inp, int ilen, char* outp)
1704{
1705 int i,j,k, num;
1706 int rem=0;
1707 uint32_t res=0;
1708
1709 if(inp == NULL || outp == NULL || ilen<=0) {
1710 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1711 return -1;
1712 }
1713
1714 num = ilen/3;
1715 rem = ilen%3;
1716 j=0;k=0;
1717 for(i=0; i<num; i++) {
1718 //prepare index
1719 res = inp[j++]<<16;
1720 res = res | inp[j++]<<8;
1721 res = res | inp[j++];
1722 //get output map from index
1723 outp[k++] = (char) bTable[(res>>18)&0x3F];
1724 outp[k++] = (char) bTable[(res>>12)&0x3F];
1725 outp[k++] = (char) bTable[(res>>6)&0x3F];
1726 outp[k++] = (char) bTable[res&0x3F];
1727 }
1728
1729 switch(rem) {
1730 case 1:
1731 res = inp[j++]<<16;
1732 outp[k++] = (char) bTable[res>>18];
1733 outp[k++] = (char) bTable[(res>>12)&0x3F];
1734 //outp[k++] = '=';
1735 //outp[k++] = '=';
1736 break;
1737 case 2:
1738 res = inp[j++]<<16;
1739 res = res | inp[j++]<<8;
1740 outp[k++] = (char) bTable[res>>18];
1741 outp[k++] = (char) bTable[(res>>12)&0x3F];
1742 outp[k++] = (char) bTable[(res>>6)&0x3F];
1743 //outp[k++] = '=';
1744 break;
1745 default:
1746 break;
1747 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001748 outp[k] = '\0';
1749 return k;
1750}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301751
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301752
1753int audio_extn_utils_get_codec_version(const char *snd_card_name,
1754 int card_num,
1755 char *codec_version)
1756{
1757 char procfs_path[50];
1758 FILE *fp;
1759
1760 if (strstr(snd_card_name, "tasha")) {
1761 snprintf(procfs_path, sizeof(procfs_path),
1762 "/proc/asound/card%d/codecs/tasha/version", card_num);
1763 if ((fp = fopen(procfs_path, "r")) != NULL) {
1764 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1765 fclose(fp);
1766 } else {
1767 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1768 return -ENOENT;
1769 }
1770 ALOGD("%s: codec version %s", __func__, codec_version);
1771 }
1772
1773 return 0;
1774}
1775
1776
Ashish Jain81eb2a82015-05-13 10:52:34 +05301777#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1778
1779void get_default_compressed_channel_status(
1780 unsigned char *channel_status)
1781{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301782 memset(channel_status,0,24);
1783
1784 /* block start bit in preamble bit 3 */
1785 channel_status[0] |= PROFESSIONAL;
1786 //compre out
1787 channel_status[0] |= NON_LPCM;
1788 // sample rate; fixed 48K for default/transcode
1789 channel_status[3] |= SR_48000;
1790}
1791
1792#ifdef HDMI_PASSTHROUGH_ENABLED
1793int32_t get_compressed_channel_status(void *audio_stream_data,
1794 uint32_t audio_frame_size,
1795 unsigned char *channel_status,
1796 enum audio_parser_code_type codec_type)
1797 // codec_type - AUDIO_PARSER_CODEC_AC3
1798 // - AUDIO_PARSER_CODEC_DTS
1799{
1800 unsigned char *stream;
1801 int ret = 0;
1802 stream = (unsigned char *)audio_stream_data;
1803
1804 if (audio_stream_data == NULL || audio_frame_size == 0) {
1805 ALOGW("no buffer to get channel status, return default for compress");
1806 get_default_compressed_channel_status(channel_status);
1807 return ret;
1808 }
1809
1810 memset(channel_status,0,24);
1811 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1812 {
1813 ALOGE("init audio parser failed");
1814 return -1;
1815 }
1816 ret = get_channel_status(channel_status, codec_type);
1817 return ret;
1818
1819}
1820
1821#endif
1822
1823void get_lpcm_channel_status(uint32_t sampleRate,
1824 unsigned char *channel_status)
1825{
1826 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301827 memset(channel_status,0,24);
1828 /* block start bit in preamble bit 3 */
1829 channel_status[0] |= PROFESSIONAL;
1830 //LPCM OUT
1831 channel_status[0] &= ~NON_LPCM;
1832
1833 switch (sampleRate) {
1834 case 8000:
1835 case 11025:
1836 case 12000:
1837 case 16000:
1838 case 22050:
1839 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301840 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301841 case 24000:
1842 channel_status[3] |= SR_24000;
1843 break;
1844 case 32000:
1845 channel_status[3] |= SR_32000;
1846 break;
1847 case 44100:
1848 channel_status[3] |= SR_44100;
1849 break;
1850 case 48000:
1851 channel_status[3] |= SR_48000;
1852 break;
1853 case 88200:
1854 channel_status[3] |= SR_88200;
1855 break;
1856 case 96000:
1857 channel_status[3] |= SR_96000;
1858 break;
1859 case 176400:
1860 channel_status[3] |= SR_176400;
1861 break;
1862 case 192000:
1863 channel_status[3] |= SR_192000;
1864 break;
1865 default:
1866 ALOGV("Invalid sample_rate %u\n", sampleRate);
1867 status = -1;
1868 break;
1869 }
1870}
1871
1872void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1873{
1874 unsigned char channel_status[24]={0};
1875 struct snd_aes_iec958 iec958;
1876 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1877 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301878 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301879#ifdef HDMI_PASSTHROUGH_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301880 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301881 /*TODO:Extend code to support DTS passthrough*/
1882 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301883 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301884 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1885 } else
1886#endif
1887 {
1888 /*set channel status bit for LPCM*/
1889 get_lpcm_channel_status(out->sample_rate, channel_status);
1890 }
1891
1892 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1893 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1894 if (!ctl) {
1895 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1896 __func__, mixer_ctl_name);
1897 return;
1898 }
1899 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1900 ALOGE("%s: Could not set channel status for ext HDMI ",
1901 __func__);
1902 return;
1903 }
1904
1905}
1906#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301907
1908int audio_extn_utils_get_avt_device_drift(
1909 struct audio_usecase *usecase,
1910 struct audio_avt_device_drift_param *drift_param)
1911{
1912 int ret = 0, count = 0;
1913 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301914 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301915 struct mixer_ctl *ctl = NULL;
1916 struct audio_avt_device_drift_stats drift_stats;
1917 struct audio_device *adev = NULL;
1918
1919 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301920 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1921 if (!backend) {
1922 ALOGE("%s: Unsupported device %d", __func__,
1923 usecase->stream.out->devices);
1924 ret = -EINVAL;
1925 goto done;
1926 }
1927 strlcpy(avt_device_drift_mixer_ctl_name,
1928 backend,
1929 MIXER_PATH_MAX_LENGTH);
1930
1931 count = strlen(backend);
1932 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1933 strlcat(&avt_device_drift_mixer_ctl_name[count],
1934 " DRIFT",
1935 MIXER_PATH_MAX_LENGTH - count);
1936 } else {
1937 ret = -EINVAL;
1938 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301939 }
1940 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301941 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301942 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301943 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301944 }
1945
Naresh Tanniru6160c712017-04-17 15:43:48 +05301946 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301947 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1948 if (!ctl) {
1949 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1950 __func__, avt_device_drift_mixer_ctl_name);
1951
1952 ret = -EINVAL;
1953 goto done;
1954 }
1955
1956 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1957 avt_device_drift_mixer_ctl_name);
1958
1959 mixer_ctl_update(ctl);
1960 count = mixer_ctl_get_num_values(ctl);
1961 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1962 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1963 __func__);
1964
1965 ret = -EINVAL;
1966 goto done;
1967 }
1968
1969 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1970 if (ret != 0) {
1971 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1972 __func__);
1973
1974 ret = -EINVAL;
1975 goto done;
1976 }
1977 memcpy(drift_param, &drift_stats.drift_param,
1978 sizeof(struct audio_avt_device_drift_param));
1979done:
1980 return ret;
1981}
Manish Dewangan07de2142017-02-27 19:27:20 +05301982
1983#ifdef SNDRV_COMPRESS_PATH_DELAY
1984int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1985{
1986 int ret = -EINVAL;
1987 struct snd_compr_metadata metadata;
1988 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1989
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08001990 /* override the latency for pcm offload use case */
1991 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
1992 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
1993 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
1994 }
1995
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001996 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301997 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1998 if (!(is_offload_usecase(out->usecase))) {
1999 ALOGE("%s:: not supported for non offload session", __func__);
2000 goto exit;
2001 }
2002
2003 if (!out->compr) {
2004 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2005 __func__);
2006 goto exit;
2007 }
2008
2009 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2010 ret = compress_get_metadata(out->compr, &metadata);
2011 if(ret) {
2012 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2013 goto exit;
2014 }
2015 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2016 } else {
2017 ALOGD("%s:: Using Fix DSP delay",__func__);
2018 }
2019
2020exit:
2021 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2022 return delay_ms;
2023}
2024#else
2025int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2026{
2027 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2028}
2029#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302030
2031#ifdef SNDRV_COMPRESS_RENDER_MODE
2032int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2033{
2034 struct snd_compr_metadata metadata;
2035 int ret = -EINVAL;
2036
2037 if (!(is_offload_usecase(out->usecase))) {
2038 ALOGE("%s:: not supported for non offload session", __func__);
2039 goto exit;
2040 }
2041
2042 if (!out->compr) {
2043 ALOGD("%s:: Invalid compress handle",
2044 __func__);
2045 goto exit;
2046 }
2047
2048 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2049
2050 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2051 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2052 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2053 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2054 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2055 } else {
2056 ret = 0;
2057 goto exit;
2058 }
2059 ret = compress_set_metadata(out->compr, &metadata);
2060 if(ret) {
2061 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2062 }
2063exit:
2064 return ret;
2065}
2066#else
2067int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2068{
2069 ALOGD("%s:: configuring render mode not supported", __func__);
2070 return 0;
2071}
2072#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302073
2074#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2075int audio_extn_utils_compress_set_clk_rec_mode(
2076 struct audio_usecase *usecase)
2077{
2078 struct snd_compr_metadata metadata;
2079 struct stream_out *out = NULL;
2080 int ret = -EINVAL;
2081
Naresh Tanniru7586e292017-04-13 10:38:13 +05302082 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302083 ALOGE("%s:: Invalid use case", __func__);
2084 goto exit;
2085 }
2086
2087 out = usecase->stream.out;
2088 if (!out) {
2089 ALOGE("%s:: invalid stream", __func__);
2090 goto exit;
2091 }
2092
2093 if (!is_offload_usecase(out->usecase)) {
2094 ALOGE("%s:: not supported for non offload session", __func__);
2095 goto exit;
2096 }
2097
2098 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2099 ALOGD("%s:: clk recovery is only supported in STC render mode",
2100 __func__);
2101 ret = 0;
2102 goto exit;
2103 }
2104
2105 if (!out->compr) {
2106 ALOGD("%s:: Invalid compress handle",
2107 __func__);
2108 goto exit;
2109 }
2110 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2111 switch(usecase->out_snd_device) {
2112 case SND_DEVICE_OUT_HDMI:
2113 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2114 case SND_DEVICE_OUT_DISPLAY_PORT:
2115 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2116 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2117 break;
2118 default:
2119 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2120 break;
2121 }
2122
2123 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2124
2125 ret = compress_set_metadata(out->compr, &metadata);
2126 if(ret) {
2127 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2128 }
2129
2130exit:
2131 return ret;
2132}
2133#else
2134int audio_extn_utils_compress_set_clk_rec_mode(
2135 struct audio_usecase *usecase __unused)
2136{
2137 ALOGD("%s:: configuring render mode not supported", __func__);
2138 return 0;
2139}
2140#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302141
2142#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2143int audio_extn_utils_compress_set_render_window(
2144 struct stream_out *out,
2145 struct audio_out_render_window_param *render_window)
2146{
2147 struct snd_compr_metadata metadata;
2148 int ret = -EINVAL;
2149
Manish Dewangan27346042017-03-01 12:56:12 +05302150 if(render_window == NULL) {
2151 ALOGE("%s:: Invalid render_window", __func__);
2152 goto exit;
2153 }
2154
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002155 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2156 __func__,render_window->render_ws, render_window->render_we);
2157
Manish Dewangan27346042017-03-01 12:56:12 +05302158 if (!is_offload_usecase(out->usecase)) {
2159 ALOGE("%s:: not supported for non offload session", __func__);
2160 goto exit;
2161 }
2162
Naresh Tanniru6160c712017-04-17 15:43:48 +05302163 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2164 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302165 ALOGD("%s:: only supported in timestamp mode, current "
2166 "render mode mode %d", __func__, out->render_mode);
2167 goto exit;
2168 }
2169
2170 if (!out->compr) {
2171 ALOGW("%s:: offload session not yet opened,"
2172 "render window will be configure later", __func__);
2173 /* store render window to reconfigure in start_output_stream() */
2174 goto exit;
2175 }
2176
2177 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2178 /*render window start value */
2179 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2180 metadata.value[1] = \
2181 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2182 /*render window end value */
2183 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2184 metadata.value[3] = \
2185 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2186
2187 ret = compress_set_metadata(out->compr, &metadata);
2188 if(ret) {
2189 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2190 }
2191
2192exit:
2193 return ret;
2194}
2195#else
2196int audio_extn_utils_compress_set_render_window(
2197 struct stream_out *out __unused,
2198 struct audio_out_render_window_param *render_window __unused)
2199{
2200 ALOGD("%s:: configuring render window not supported", __func__);
2201 return 0;
2202}
2203#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302204
2205#ifdef SNDRV_COMPRESS_START_DELAY
2206int audio_extn_utils_compress_set_start_delay(
2207 struct stream_out *out,
2208 struct audio_out_start_delay_param *delay_param)
2209{
2210 struct snd_compr_metadata metadata;
2211 int ret = -EINVAL;
2212
2213 if(delay_param == NULL) {
2214 ALOGE("%s:: Invalid delay_param", __func__);
2215 goto exit;
2216 }
2217
2218 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2219 delay_param->start_delay);
2220
2221 if (!is_offload_usecase(out->usecase)) {
2222 ALOGE("%s:: not supported for non offload session", __func__);
2223 goto exit;
2224 }
2225
Naresh Tanniru6160c712017-04-17 15:43:48 +05302226 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2227 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302228 ALOGD("%s:: only supported in timestamp mode, current "
2229 "render mode mode %d", __func__, out->render_mode);
2230 goto exit;
2231 }
2232
2233 if (!out->compr) {
2234 ALOGW("%s:: offload session not yet opened,"
2235 "start delay will be configure later", __func__);
2236 goto exit;
2237 }
2238
2239 metadata.key = SNDRV_COMPRESS_START_DELAY;
2240 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2241 metadata.value[1] = \
2242 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2243
2244 ret = compress_set_metadata(out->compr, &metadata);
2245 if(ret) {
2246 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2247 }
2248
2249exit:
2250 return ret;
2251}
2252#else
2253int audio_extn_utils_compress_set_start_delay(
2254 struct stream_out *out __unused,
2255 struct audio_out_start_delay_param *delay_param __unused)
2256{
2257 ALOGD("%s:: configuring render window not supported", __func__);
2258 return 0;
2259}
2260#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002261
2262#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302263#define RETRY_US 1000000
2264#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002265#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2266#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2267
2268#ifdef LINUX_ENABLED
2269static const char *kConfigLocationList[] =
2270 {"/etc"};
2271#else
2272static const char *kConfigLocationList[] =
2273 {"/vendor/etc"};
2274#endif
2275static const int kConfigLocationListSize =
2276 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2277
2278bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2279{
2280 char full_config_path[MIXER_PATH_MAX_LENGTH];
2281 for (int i = 0; i < kConfigLocationListSize; i++) {
2282 snprintf(full_config_path,
2283 MIXER_PATH_MAX_LENGTH,
2284 "%s/%s",
2285 kConfigLocationList[i],
2286 file_name);
2287 if (F_OK == access(full_config_path, 0)) {
2288 strcpy(file_name, full_config_path);
2289 return true;
2290 }
2291 }
2292 return false;
2293}
2294
2295/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2296int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2297{
2298 if (NULL == snd_card_name) {
2299 return -1;
2300 }
2301
2302 struct snd_card_split *snd_split_handle = NULL;
2303 int ret = 0;
2304 audio_extn_set_snd_card_split(snd_card_name);
2305 snd_split_handle = audio_extn_get_snd_card_split();
2306
2307 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2308 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2309 snd_split_handle->form_factor);
2310
2311 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2312 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2313 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2314 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2315
2316 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2317 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2318 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2319 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2320 }
2321 }
2322
2323 return ret;
2324}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002325
2326int audio_extn_utils_get_snd_card_num()
2327{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302328 int snd_card_num = 0;
2329 struct mixer *mixer = NULL;
2330
2331 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2332 if (mixer)
2333 mixer_close(mixer);
2334 return snd_card_num;
2335}
2336
2337int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2338{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002339
2340 void *hw_info = NULL;
2341 struct mixer *mixer = NULL;
2342 int retry_num = 0;
Ashish Jain30ae8942017-11-05 17:21:23 +05302343 int snd_card_num = 0, min_snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002344 char* snd_card_name = NULL;
2345
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302346 if (!mixer_handle) {
2347 ALOGE("invalid mixer handle");
2348 return -1;
2349 }
2350 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302351 /*
2352 * Try with all the sound cards ( 0 to 8 ) and if none of them were detected
2353 * sleep for 1 sec and try detections with sound card 0 again.
2354 * If sound card gets detected, check if it is relevant, if not check with the
2355 * other sound cards. To ensure that the irrelevant sound card is not check again,
2356 * we maintain it in min_snd_card_num.
2357 */
2358 while (retry_num < RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002359
Ashish Jain30ae8942017-11-05 17:21:23 +05302360 while (snd_card_num <= MAX_SND_CARD) {
2361 mixer = mixer_open(snd_card_num);
2362 if (!mixer)
2363 snd_card_num++;
2364 else
2365 break;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002366 }
2367
2368 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302369 usleep(RETRY_US);
2370 snd_card_num = min_snd_card_num;
2371 retry_num++;
2372 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002373 }
2374
2375 snd_card_name = strdup(mixer_get_name(mixer));
2376 if (!snd_card_name) {
2377 ALOGE("failed to allocate memory for snd_card_name\n");
2378 mixer_close(mixer);
2379 return -1;
2380 }
2381 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2382
2383 hw_info = hw_info_init(snd_card_name);
2384 if (hw_info) {
2385 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2386 break;
2387 }
2388 ALOGE("%s: Failed to init hardware info", __func__);
Ashish Jain30ae8942017-11-05 17:21:23 +05302389 min_snd_card_num++;
2390 snd_card_num = min_snd_card_num;
2391
2392 if (snd_card_num >= MAX_SND_CARD)
2393 break;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002394
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302395 free(snd_card_name);
2396 snd_card_name = NULL;
2397
2398 mixer_close(mixer);
2399 mixer = NULL;
2400 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002401 if (snd_card_name)
2402 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302403
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302404 if (hw_info)
2405 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002406
Ashish Jain30ae8942017-11-05 17:21:23 +05302407 if ((snd_card_num >= MAX_SND_CARD) || (retry_num >= RETRY_NUMBER)) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002408 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2409 return -1;
2410 }
2411
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302412 if (mixer)
2413 *mixer_handle = mixer;
2414
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002415 return snd_card_num;
2416}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302417
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302418void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2419{
2420 if (mixer)
2421 mixer_close(mixer);
2422}
2423
Naresh Tanniru6160c712017-04-17 15:43:48 +05302424#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2425int audio_extn_utils_compress_enable_drift_correction(
2426 struct stream_out *out,
2427 struct audio_out_enable_drift_correction *drift)
2428{
2429 struct snd_compr_metadata metadata;
2430 int ret = -EINVAL;
2431
2432 if(drift == NULL) {
2433 ALOGE("%s:: Invalid param", __func__);
2434 goto exit;
2435 }
2436
2437 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2438
2439 if (!is_offload_usecase(out->usecase)) {
2440 ALOGE("%s:: not supported for non offload session", __func__);
2441 goto exit;
2442 }
2443
2444 if (!out->compr) {
2445 ALOGW("%s:: offload session not yet opened,"
2446 "start delay will be configure later", __func__);
2447 goto exit;
2448 }
2449
2450 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2451 metadata.value[0] = drift->enable;
2452 out->drift_correction_enabled = drift->enable;
2453
2454 ret = compress_set_metadata(out->compr, &metadata);
2455 if(ret) {
2456 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2457 out->drift_correction_enabled = false;
2458 }
2459
2460exit:
2461 return ret;
2462}
2463#else
2464int audio_extn_utils_compress_enable_drift_correction(
2465 struct stream_out *out __unused,
2466 struct audio_out_enable_drift_correction *drift __unused)
2467{
2468 ALOGD("%s:: configuring drift enablement not supported", __func__);
2469 return 0;
2470}
2471#endif
2472
2473#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2474int audio_extn_utils_compress_correct_drift(
2475 struct stream_out *out,
2476 struct audio_out_correct_drift *drift_param)
2477{
2478 struct snd_compr_metadata metadata;
2479 int ret = -EINVAL;
2480
2481 if (drift_param == NULL) {
2482 ALOGE("%s:: Invalid drift_param", __func__);
2483 goto exit;
2484 }
2485
2486 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2487 drift_param->adjust_time);
2488
2489 if (!is_offload_usecase(out->usecase)) {
2490 ALOGE("%s:: not supported for non offload session", __func__);
2491 goto exit;
2492 }
2493
2494 if (!out->compr) {
2495 ALOGW("%s:: offload session not yet opened", __func__);
2496 goto exit;
2497 }
2498
2499 if (!out->drift_correction_enabled) {
2500 ALOGE("%s:: drift correction not enabled", __func__);
2501 goto exit;
2502 }
2503
2504 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2505 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2506 metadata.value[1] = \
2507 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2508
2509 ret = compress_set_metadata(out->compr, &metadata);
2510 if(ret)
2511 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2512exit:
2513 return ret;
2514}
2515#else
2516int audio_extn_utils_compress_correct_drift(
2517 struct stream_out *out __unused,
2518 struct audio_out_correct_drift *drift_param __unused)
2519{
2520 ALOGD("%s:: setting adjust clock not supported", __func__);
2521 return 0;
2522}
2523#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302524
2525int audio_extn_utils_set_channel_map(
2526 struct stream_out *out,
2527 struct audio_out_channel_map_param *channel_map_param)
2528{
2529 int ret = -EINVAL, i = 0;
2530 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2531
2532 if (channel_map_param == NULL) {
2533 ALOGE("%s:: Invalid channel_map", __func__);
2534 goto exit;
2535 }
2536
2537 if (channel_map_param->channels != channels) {
2538 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2539 __func__, channel_map_param->channels, channels);
2540 goto exit;
2541 }
2542
2543 for ( i = 0; i < channels; i++) {
2544 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2545 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2546 }
2547 ret = 0;
2548exit:
2549 return ret;
2550}
Varun Balaraje49253e2017-07-06 19:48:56 +05302551
2552int audio_extn_utils_set_pan_scale_params(
2553 struct stream_out *out,
2554 struct mix_matrix_params *mm_params)
2555{
2556 int ret = -EINVAL, i = 0, j = 0;
2557
Varun Balaraj003ee752017-08-07 17:54:55 +05302558 if (mm_params == NULL || out == NULL) {
2559 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302560 goto exit;
2561 }
2562
2563 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2564 mm_params->num_output_channels <= 0 ||
2565 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2566 mm_params->num_input_channels <= 0)
2567 goto exit;
2568
2569 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2570 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2571 out->pan_scale_params.has_output_channel_map =
2572 mm_params->has_output_channel_map;
2573 for (i = 0; i < mm_params->num_output_channels; i++)
2574 out->pan_scale_params.output_channel_map[i] =
2575 mm_params->output_channel_map[i];
2576
2577 out->pan_scale_params.has_input_channel_map =
2578 mm_params->has_input_channel_map;
2579 for (i = 0; i < mm_params->num_input_channels; i++)
2580 out->pan_scale_params.input_channel_map[i] =
2581 mm_params->input_channel_map[i];
2582
2583 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2584 for (i = 0; i < mm_params->num_output_channels; i++)
2585 for (j = 0; j < mm_params->num_input_channels; j++) {
2586 //Convert the channel coefficient gains in Q14 format
2587 out->pan_scale_params.mixer_coeffs[i][j] =
2588 mm_params->mixer_coeffs[i][j] * (2 << 13);
2589 }
2590
2591 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2592 out->pcm_device_id,
2593 out->pan_scale_params);
2594
2595exit:
2596 return ret;
2597}
2598
2599int audio_extn_utils_set_downmix_params(
2600 struct stream_out *out,
2601 struct mix_matrix_params *mm_params)
2602{
2603 int ret = -EINVAL, i = 0, j = 0;
2604 struct audio_usecase *usecase = NULL;
2605
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002606 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302607 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302608 goto exit;
2609 }
2610
2611 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2612 mm_params->num_output_channels <= 0 ||
2613 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2614 mm_params->num_input_channels <= 0)
2615 goto exit;
2616
2617 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302618 if (!usecase) {
2619 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002620 goto exit;
2621 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302622 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2623 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2624
2625 out->downmix_params.has_output_channel_map =
2626 mm_params->has_output_channel_map;
2627 for (i = 0; i < mm_params->num_output_channels; i++) {
2628 out->downmix_params.output_channel_map[i] =
2629 mm_params->output_channel_map[i];
2630 }
2631
2632 out->downmix_params.has_input_channel_map =
2633 mm_params->has_input_channel_map;
2634 for (i = 0; i < mm_params->num_input_channels; i++)
2635 out->downmix_params.input_channel_map[i] =
2636 mm_params->input_channel_map[i];
2637
2638 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2639 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302640 for (j = 0; j < mm_params->num_input_channels; j++) {
2641 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302642 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302643 mm_params->mixer_coeffs[i][j] * (2 << 13);
2644 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302645
2646 ret = platform_set_stream_downmix_params(out->dev->platform,
2647 out->pcm_device_id,
2648 usecase->out_snd_device,
2649 out->downmix_params);
2650
2651exit:
2652 return ret;
2653}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302654
2655bool audio_extn_utils_is_dolby_format(audio_format_t format)
2656{
2657 if (format == AUDIO_FORMAT_AC3 ||
2658 format == AUDIO_FORMAT_E_AC3 ||
2659 format == AUDIO_FORMAT_E_AC3_JOC)
2660 return true;
2661 else
2662 return false;
2663}
2664
`Deeraj Soman676c2702017-09-18 19:25:53 +05302665int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2666{
2667 int i;
2668 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2669 {"S32_LE", 32},
2670 {"S24_LE", 24},
2671 {"S16_LE", 16}};
2672 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302673
`Deeraj Soman676c2702017-09-18 19:25:53 +05302674 for (i = 0; i < num_configs; i++) {
2675 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2676 return mixer_bitwidth_config[i].value;
2677 }
2678
2679 return -EINVAL;
2680}
2681
2682int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2683{
2684 int i;
2685 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2686 {"KHZ_48", 48000},
2687 {"KHZ_96", 96000},
2688 {"KHZ_144", 144000},
2689 {"KHZ_192", 192000},
2690 {"KHZ_384", 384000},
2691 {"KHZ_44P1", 44100},
2692 {"KHZ_88P2", 88200},
2693 {"KHZ_176P4", 176400},
2694 {"KHZ_352P8", 352800}};
2695 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2696
2697 for (i = 0; i < num_configs; i++) {
2698 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2699 return mixer_samplerate_config[i].value;
2700 }
2701
2702 return -EINVAL;
2703}
2704
2705int audio_extn_utils_get_channels_from_string(const char *id_string)
2706{
2707 int i;
2708 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2709 {"Two", 2},
2710 {"Three",3},
2711 {"Four", 4},
2712 {"Five", 5},
2713 {"Six", 6},
2714 {"Seven", 7},
2715 {"Eight", 8}};
2716 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2717
2718 for (i = 0; i < num_configs; i++) {
2719 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2720 return mixer_channels_config[i].value;
2721 }
2722
2723 return -EINVAL;
2724}
Surendar karka30569792018-05-08 12:02:21 +05302725
2726int audio_extn_utils_get_license_params
2727(
2728const struct audio_device *adev,
2729struct audio_license_params *license_params
2730)
2731{
2732 if(!license_params)
2733 return -EINVAL;
2734 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2735}
2736
Aalique Grahame22e49102018-12-18 14:23:57 -08002737int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2738 int app_type,
2739 int *gain)
2740{
2741 int gain_cfg[4];
2742 const char *mixer_ctl_name = "App Type Gain";
2743 struct mixer_ctl *ctl;
2744 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2745 if (!ctl) {
2746 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2747 mixer_ctl_name);
2748 return -EINVAL;
2749 }
2750 gain_cfg[0] = 0;
2751 gain_cfg[1] = app_type;
2752 gain_cfg[2] = gain[0];
2753 gain_cfg[3] = gain[1];
2754 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2755 return mixer_ctl_set_array(ctl, gain_cfg,
2756 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2757}