blob: b06276edfa52c7d8b6d1731a50ccb5ff4143f644 [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
Ashish Jain81eb2a82015-05-13 10:52:34 +053051#include "audio_parsers.h"
52#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053053
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053054#ifdef LINUX_ENABLED
55#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053056#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053057#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070058#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053059#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053060#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070061
62#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053063#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070064
65#define DYNAMIC_VALUE_TAG "dynamic"
66#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053067#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070068#define FORMATS_TAG "formats"
69#define SAMPLING_RATES_TAG "sampling_rates"
70#define BIT_WIDTH_TAG "bit_width"
71#define APP_TYPE_TAG "app_type"
72
73#define STRING_TO_ENUM(string) { #string, string }
74#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
75
Ben Rombergera04fabc2014-11-14 12:16:03 -080076#define BASE_TABLE_SIZE 64
77#define MAX_BASEINDEX_LEN 256
78
Ben Romberger1aaaf862017-04-06 17:49:46 -070079#ifndef SND_AUDIOCODEC_TRUEHD
80#define SND_AUDIOCODEC_TRUEHD 0x00000023
81#endif
82
Vikram Panduranga93f080e2017-06-07 18:16:14 -070083#define APP_TYPE_VOIP_AUDIO 0x1113A
84
Ashish Jain81eb2a82015-05-13 10:52:34 +053085#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
86#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
87#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
88#define SR_44100 (0<<0) /* 44.1kHz */
89#define SR_NOTID (1<<0) /* non indicated */
90#define SR_48000 (2<<0) /* 48kHz */
91#define SR_32000 (3<<0) /* 32kHz */
92#define SR_22050 (4<<0) /* 22.05kHz */
93#define SR_24000 (6<<0) /* 24kHz */
94#define SR_88200 (8<<0) /* 88.2kHz */
95#define SR_96000 (10<<0) /* 96kHz */
96#define SR_176400 (12<<0) /* 176.4kHz */
97#define SR_192000 (14<<0) /* 192kHz */
98
99#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530100
101/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800102#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
103#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
104#define PCM_OFFLOAD_PLAYBACK_LATENCY \
105 (PCM_OFFLOAD_BUFFER_DURATION + PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY)
Manish Dewangan07de2142017-02-27 19:27:20 +0530106
Varun Balaraje49253e2017-07-06 19:48:56 +0530107#ifndef MAX_CHANNELS_SUPPORTED
108#define MAX_CHANNELS_SUPPORTED 8
109#endif
110
`Deeraj Soman676c2702017-09-18 19:25:53 +0530111typedef struct {
112 const char *id_string;
113 const int value;
114} mixer_config_lookup;
115
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700116struct string_to_enum {
117 const char *name;
118 uint32_t value;
119};
120
121const struct string_to_enum s_flag_name_to_enum_table[] = {
122 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
123 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
124 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800125 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700126 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
127 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
128 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700129 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700130 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700131 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530132 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700133 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700134 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530136 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
137 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
138 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
139 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
140 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530141 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530142 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200143 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700144};
145
146const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530147 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700148 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530149 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
150 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530151 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700152 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
153 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
154 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700155 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
156 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700157 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700158 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700159 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530160 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700161 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700162 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530163 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700164 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
165 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
166 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
168 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
169 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
170 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
171 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
172 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
173 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530175 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
176 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800177 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
178 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
179 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530180 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530181 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
182 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
183 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530184 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530185 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
186 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
187 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
188 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530189 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700190};
191
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530192/* payload structure avt_device drift query */
193struct audio_avt_device_drift_stats {
194 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530195
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530196 /* Indicates the device interface direction as either
197 * source (Tx) or sink (Rx).
198 */
199 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530200
201 /* Reference timer for drift accumulation and time stamp information.
202 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
203 */
204 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530205 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530206} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530207
Ben Rombergera04fabc2014-11-14 12:16:03 -0800208static char bTable[BASE_TABLE_SIZE] = {
209 'A','B','C','D','E','F','G','H','I','J','K','L',
210 'M','N','O','P','Q','R','S','T','U','V','W','X',
211 'Y','Z','a','b','c','d','e','f','g','h','i','j',
212 'k','l','m','n','o','p','q','r','s','t','u','v',
213 'w','x','y','z','0','1','2','3','4','5','6','7',
214 '8','9','+','/'
215};
216
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700217static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
218 const char *name)
219{
220 size_t i;
221 for (i = 0; i < size; i++) {
222 if (strcmp(table[i].name, name) == 0) {
223 ALOGV("%s found %s", __func__, table[i].name);
224 return table[i].value;
225 }
226 }
227 return 0;
228}
229
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530230static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700231{
232 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530233 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800234 char *last_r;
235 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700236 while (flag_name != NULL) {
237 if (strlen(flag_name) != 0) {
238 flag |= string_to_enum(s_flag_name_to_enum_table,
239 ARRAY_SIZE(s_flag_name_to_enum_table),
240 flag_name);
241 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800242 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700243 }
244
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800245 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530246 io_flags.in_flags = (audio_input_flags_t)flag;
247 io_flags.out_flags = (audio_output_flags_t)flag;
248 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700249}
250
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530251static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700252{
253 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800254 char *last_r;
255 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700256
257 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
258 return;
259
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530260 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700261 while (str != NULL) {
262 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
263 ARRAY_SIZE(s_format_name_to_enum_table), str);
264 ALOGV("%s: format - %d", __func__, format);
265 if (format != 0) {
266 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700267 if (sf_info == NULL)
268 break; /* return whatever was parsed */
269
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700270 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530271 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700272 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800273 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700274 }
275}
276
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530277static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700278{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700279 struct stream_sample_rate *ss_info = NULL;
280 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800281 char *last_r;
282 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700283
Amit Shekhar6f461b12014-08-01 14:52:58 -0700284 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
285 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700286
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530287 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700288 while (str != NULL) {
289 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
290 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
291 if (0 != sample_rate) {
292 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800293 if (!ss_info) {
294 ALOGE("%s: memory allocation failure", __func__);
295 return;
296 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700297 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530298 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700299 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800300 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700301 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302}
303
304static int parse_bit_width_names(char *name)
305{
306 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800307 char *last_r;
308 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700309
310 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
311 bit_width = (int)strtol(str, (char **)NULL, 10);
312
313 ALOGV("%s: bit_width - %d", __func__, bit_width);
314 return bit_width;
315}
316
317static int parse_app_type_names(void *platform, char *name)
318{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700319 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800320 char *last_r;
321 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700322
323 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
324 app_type = (int)strtol(str, (char **)NULL, 10);
325
326 ALOGV("%s: app_type - %d", __func__, app_type);
327 return app_type;
328}
329
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530330static void update_streams_cfg_list(cnode *root, void *platform,
331 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700332{
333 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530334 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700335
336 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530337 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700338
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530339 if (!s_info) {
340 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700341 return;
342 }
343
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700344 while (node) {
345 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530346 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530347 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
348 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700349 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530350 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700351 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530352 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
353 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530355 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700356 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530357 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700358 }
359 node = node->next;
360 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530361 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700362}
363
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530364static void load_cfg_list(cnode *root, void *platform,
365 struct listnode *streams_output_cfg_list,
366 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700367{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530368 cnode *node = NULL;
369
370 node = config_find(root, OUTPUTS_TAG);
371 if (node != NULL) {
372 node = node->first_child;
373 while (node) {
374 ALOGV("%s: loading output %s", __func__, node->name);
375 update_streams_cfg_list(node, platform, streams_output_cfg_list);
376 node = node->next;
377 }
378 } else {
379 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700380 }
381
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530382 node = config_find(root, INPUTS_TAG);
383 if (node != NULL) {
384 node = node->first_child;
385 while (node) {
386 ALOGV("%s: loading input %s", __func__, node->name);
387 update_streams_cfg_list(node, platform, streams_input_cfg_list);
388 node = node->next;
389 }
390 } else {
391 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700392 }
393}
394
395static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530396 struct listnode *streams_output_cfg_list,
397 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700398{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530399 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700400 int length = 0, i, num_app_types = 0;
401 struct listnode *node;
402 bool update;
403 struct mixer_ctl *ctl = NULL;
404 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530405 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800406 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700407
408 if (!mixer) {
409 ALOGE("%s: mixer is null",__func__);
410 return;
411 }
412 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
413 if (!ctl) {
414 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
415 return;
416 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530417 app_type_cfg[length++] = num_app_types;
418
419 if (list_empty(streams_output_cfg_list)) {
420 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700421 app_type_cfg[length++] = 48000;
422 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530423 num_app_types += 1;
424 }
425 if (list_empty(streams_input_cfg_list)) {
426 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
427 app_type_cfg[length++] = 48000;
428 app_type_cfg[length++] = 16;
429 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700430 }
431
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800432 /* get target bit width for ADM enforce mode */
433 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
434
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700435 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530436 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700437 update = true;
438 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530439 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700440 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530441 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530442 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530443 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530444 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530445 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800446 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
447 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
448 (target_bit_width > app_type_cfg[i+3]))
449 app_type_cfg[i+3] = target_bit_width;
450
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700451 update = false;
452 break;
453 }
454 }
455 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530456 num_app_types += 1;
457 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
458 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800459 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
460 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
461 (target_bit_width > app_type_cfg[length]))
462 app_type_cfg[length] = target_bit_width;
463
464 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530465 }
466 }
467 list_for_each(node, streams_input_cfg_list) {
468 s_info = node_to_item(node, struct streams_io_cfg, list);
469 update = true;
470 for (i=0; i<length; i=i+3) {
471 if (app_type_cfg[i+1] == 0)
472 break;
473 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530474 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530476 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530477 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
478 update = false;
479 break;
480 }
481 }
482 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
483 num_app_types += 1;
484 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
485 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
486 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700487 }
488 }
489 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
490 if (num_app_types) {
491 app_type_cfg[0] = num_app_types;
492 mixer_ctl_set_array(ctl, app_type_cfg, length);
493 }
494}
495
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530496void audio_extn_utils_update_streams_cfg_lists(void *platform,
497 struct mixer *mixer,
498 struct listnode *streams_output_cfg_list,
499 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700500{
501 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530502 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700503
504 ALOGV("%s", __func__);
505 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530506 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700507
508 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700509 if (root == NULL) {
510 ALOGE("cfg_list, NULL config root");
511 return;
512 }
513
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530514 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
515 if (data == NULL) {
516 ALOGD("%s: failed to open io config file(%s), trying older config file",
517 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
518 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
519 if (data == NULL) {
520 send_app_type_cfg(platform, mixer,
521 streams_output_cfg_list,
522 streams_input_cfg_list);
523 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800524 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530525 return;
526 }
527 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700528
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530529 config_load(root, data);
530 load_cfg_list(root, platform, streams_output_cfg_list,
531 streams_input_cfg_list);
532
533 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
534 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800535
536 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800537 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800538 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700539}
540
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530541static void audio_extn_utils_dump_streams_cfg_list(
542 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700543{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700544 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530545 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700546 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700547 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530548
549 list_for_each(node_i, streams_cfg_list) {
550 s_info = node_to_item(node_i, struct streams_io_cfg, list);
551 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
552 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
553 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
554 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700555 sf_info = node_to_item(node_j, struct stream_format, list);
556 ALOGV("format-%x", sf_info->format);
557 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530558 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700559 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
560 ALOGV("sample rate-%d", ss_info->sample_rate);
561 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700562 }
563}
564
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530565void audio_extn_utils_dump_streams_cfg_lists(
566 struct listnode *streams_output_cfg_list,
567 struct listnode *streams_input_cfg_list)
568{
569 ALOGV("%s", __func__);
570 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
571 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
572}
573
574static void audio_extn_utils_release_streams_cfg_list(
575 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700576{
577 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530578 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700579
580 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530581
582 while (!list_empty(streams_cfg_list)) {
583 node_i = list_head(streams_cfg_list);
584 s_info = node_to_item(node_i, struct streams_io_cfg, list);
585 while (!list_empty(&s_info->format_list)) {
586 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700587 list_remove(node_j);
588 free(node_to_item(node_j, struct stream_format, list));
589 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530590 while (!list_empty(&s_info->sample_rate_list)) {
591 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700592 list_remove(node_j);
593 free(node_to_item(node_j, struct stream_sample_rate, list));
594 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700595 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530596 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700597 }
598}
599
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600void audio_extn_utils_release_streams_cfg_lists(
601 struct listnode *streams_output_cfg_list,
602 struct listnode *streams_input_cfg_list)
603{
604 ALOGV("%s", __func__);
605 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
606 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
607}
608
609static bool set_app_type_cfg(struct streams_io_cfg *s_info,
610 struct stream_app_type_cfg *app_type_cfg,
611 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700612 {
613 struct listnode *node_i;
614 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530615 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700616 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
617 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530618 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700619
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530620 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700621 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530622 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700623 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
624 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
625 return true;
626 }
627 }
628 /*
629 * Reiterate through the list assuming dafault sample rate.
630 * Handles scenario where input sample rate is higher
631 * than all sample rates in list for the input bit width.
632 */
633 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800634
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530635 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700636 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
637 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530638 (bit_width == s_info->app_type_cfg.bit_width)) {
639 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700640 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530641 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800642 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 -0700643 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
644 return true;
645 }
646 }
647 return false;
648}
649
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530650void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
651 struct listnode *streams_input_cfg_list,
652 audio_devices_t devices __unused,
653 audio_input_flags_t flags,
654 audio_format_t format,
655 uint32_t sample_rate,
656 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530657 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530658 struct stream_app_type_cfg *app_type_cfg)
659{
660 struct listnode *node_i, *node_j;
661 struct streams_io_cfg *s_info;
662 struct stream_format *sf_info;
663
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530664 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
665 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530666
667 list_for_each(node_i, streams_input_cfg_list) {
668 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530669 /* Along with flags do profile matching if set at either end.*/
670 if (s_info->flags.in_flags == flags &&
671 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
672 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530673 list_for_each(node_j, &s_info->format_list) {
674 sf_info = node_to_item(node_j, struct stream_format, list);
675 if (sf_info->format == format) {
676 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
677 return;
678 }
679 }
680 }
681 }
682 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
683 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
684 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
685 app_type_cfg->bit_width = 16;
686}
687
688void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700689 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700690 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700691 audio_output_flags_t flags,
692 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700693 uint32_t sample_rate,
694 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530695 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530696 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700697 struct stream_app_type_cfg *app_type_cfg)
698{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530699 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530700 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700701 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530702 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700703
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530704 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
705 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
706 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700707 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530708 else if (-ENOSYS == bw)
709 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700710 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530711 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700712 }
713
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700714 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530715 if (!strncmp("true", value, sizeof("true"))) {
716 if ((popcount(channel_mask) > 2) &&
717 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700718 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530719 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
720 ALOGD("%s: MCH session defaulting sample rate to %d",
721 __func__, sample_rate);
722 }
723 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530724
725 /* Set sampling rate to 176.4 for DSD64
726 * and 352.8Khz for DSD128.
727 * Set Bit Width to 16. output will be 16 bit
728 * post DoP in ASM.
729 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700730 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530731 (format == AUDIO_FORMAT_DSD)) {
732 bit_width = 16;
733 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
734 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
735 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
736 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
737 }
738
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530739
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800740 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
741 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700742 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530743 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530744 /* Along with flags do profile matching if set at either end.*/
745 if (s_info->flags.out_flags == flags &&
746 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
747 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530748 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700749 sf_info = node_to_item(node_j, struct stream_format, list);
750 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530751 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700752 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700753 }
754 }
755 }
756 }
757 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530758 s_info = node_to_item(node_i, struct streams_io_cfg, list);
759 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700760 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530761 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
762 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
763 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700764 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530765 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700766 return;
767 }
768 }
769 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700770 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700771 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700772 app_type_cfg->bit_width = 16;
773}
774
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530775static bool audio_is_this_native_usecase(struct audio_usecase *uc)
776{
777 bool native_usecase = false;
778 struct stream_out *out = (struct stream_out*) uc->stream.out;
779
780 if (PCM_PLAYBACK == uc->type && out != NULL &&
781 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
782 is_offload_usecase(uc->id) &&
783 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
784 native_usecase = true;
785
786 return native_usecase;
787}
788
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800789bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
790{
791 /* DSP bitwidth enforce mode for ADM and AFE:
792 * includes:
793 * deep buffer, low latency, direct pcm and offload.
794 * excludes:
795 * ull(raw+fast), VOIP.
796 */
797 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
798 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
799 (flags & AUDIO_OUTPUT_FLAG_FAST)))
800 return false;
801
802
803 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
804 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
805 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
806 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
807 return true;
808 else
809 return false;
810}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800811
812static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
813{
814 return adev->vr_audio_mode_enabled;
815}
816
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530817void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
818 struct audio_device *adev,
819 struct audio_usecase *usecase)
820{
821 ALOGV("%s", __func__);
822
823 switch(usecase->type) {
824 case PCM_PLAYBACK:
825 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
826 &adev->streams_output_cfg_list,
827 usecase->stream.out->devices,
828 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700829 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530830 usecase->stream.out->sample_rate,
831 usecase->stream.out->bit_width,
832 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530833 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530834 &usecase->stream.out->app_type_cfg);
835 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
836 break;
837 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700838 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
839 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
840 else
841 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530842 &adev->streams_input_cfg_list,
843 usecase->stream.in->device,
844 usecase->stream.in->flags,
845 usecase->stream.in->format,
846 usecase->stream.in->sample_rate,
847 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530848 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530849 &usecase->stream.in->app_type_cfg);
850 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
851 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530852 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530853 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
854 &adev->streams_output_cfg_list,
855 usecase->stream.inout->out_config.devices,
856 0,
857 usecase->stream.inout->out_config.format,
858 usecase->stream.inout->out_config.sample_rate,
859 usecase->stream.inout->out_config.bit_width,
860 usecase->stream.inout->out_config.channel_mask,
861 usecase->stream.inout->profile,
862 &usecase->stream.inout->out_app_type_cfg);
863 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
864 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530865 default:
866 ALOGE("%s: app type cfg not supported for usecase type (%d)",
867 __func__, usecase->type);
868 }
869}
870
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530871void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
872{
873 switch (snd_device) {
874 case SND_DEVICE_OUT_BT_SCO:
875 case SND_DEVICE_IN_BT_SCO_MIC:
876 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
877 *sample_rate = 8000;
878 break;
879 case SND_DEVICE_OUT_BT_SCO_WB:
880 case SND_DEVICE_IN_BT_SCO_MIC_WB:
881 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
882 *sample_rate = 16000;
883 break;
884 default:
885 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
886 break;
887 }
888}
889
Aalique Grahame22e49102018-12-18 14:23:57 -0800890static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
891 int pcm_device_id, int app_type,
892 int acdb_dev_id, int sample_rate,
893 int stream_type,
894 snd_device_t snd_device)
895{
896
897 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
898 struct mixer_ctl *ctl;
899 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
900 int snd_device_be_idx = -1;
901
902 if (stream_type == PCM_PLAYBACK) {
903 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
904 "Audio Stream %d App Type Cfg", pcm_device_id);
905 } else if (stream_type == PCM_CAPTURE) {
906 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
907 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
908 }
909
910 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
911 if (!ctl) {
912 ALOGE("%s: Could not get ctl for mixer cmd - %s",
913 __func__, mixer_ctl_name);
914 rc = -EINVAL;
915 goto exit;
916 }
917 app_type_cfg[len++] = app_type;
918 app_type_cfg[len++] = acdb_dev_id;
919 app_type_cfg[len++] = sample_rate;
920
921 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
922 if (snd_device_be_idx > 0)
923 app_type_cfg[len++] = snd_device_be_idx;
924 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
925 "sample rate %d, snd_device_be_idx %d",
926 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
927 snd_device_be_idx);
928 mixer_ctl_set_array(ctl, app_type_cfg, len);
929
930exit:
931 return rc;
932}
933
934static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
935 struct audio_usecase *usecase)
936{
937 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
938 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
939 int app_type = 0, rc = 0;
940
941 ALOGV("%s", __func__);
942
943 if (usecase->type != PCM_HFP_CALL) {
944 ALOGV("%s: not a playback or HFP path, no need to cfg app type", __func__);
945 rc = 0;
946 goto exit_send_app_type_cfg;
947 }
948 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
949 (usecase->id != USECASE_AUDIO_HFP_SCO_WB)) {
950 ALOGV("%s: a playback path where app type cfg is not required", __func__);
951 rc = 0;
952 goto exit_send_app_type_cfg;
953 }
954
955 snd_device = usecase->out_snd_device;
956 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
957
958 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
959 if (acdb_dev_id < 0) {
960 ALOGE("%s: Couldn't get the acdb dev id", __func__);
961 rc = -EINVAL;
962 goto exit_send_app_type_cfg;
963 }
964
965 if (usecase->type == PCM_HFP_CALL) {
966
967 /* config HFP session:1 playback path */
968 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
969 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
970 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
971 acdb_dev_id, sample_rate,
972 PCM_PLAYBACK,
973 SND_DEVICE_NONE); // use legacy behavior
974 if (rc < 0)
975 goto exit_send_app_type_cfg;
976
977 /* config HFP session:1 capture path */
978 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
979 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
980 acdb_dev_id, sample_rate,
981 PCM_CAPTURE,
982 SND_DEVICE_NONE);
983 if (rc < 0)
984 goto exit_send_app_type_cfg;
985
986 /* config HFP session:2 capture path */
987 pcm_device_id = HFP_ASM_RX_TX;
988 snd_device = usecase->in_snd_device;
989 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
990 if (acdb_dev_id <= 0) {
991 ALOGE("%s: Couldn't get the acdb dev id", __func__);
992 rc = -EINVAL;
993 goto exit_send_app_type_cfg;
994 }
995 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
996 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
997 acdb_dev_id, sample_rate, PCM_CAPTURE,
998 SND_DEVICE_NONE);
999 if (rc < 0)
1000 goto exit_send_app_type_cfg;
1001
1002 /* config HFP session:2 playback path */
1003 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1004 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1005 acdb_dev_id, sample_rate,
1006 PCM_PLAYBACK, SND_DEVICE_NONE);
1007 if (rc < 0)
1008 goto exit_send_app_type_cfg;
1009 }
1010
1011 rc = 0;
1012exit_send_app_type_cfg:
1013 return rc;
1014}
1015
Siena Richard7c2db772016-12-21 11:32:34 -08001016static int send_app_type_cfg_for_device(struct audio_device *adev,
1017 struct audio_usecase *usecase,
1018 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001019{
1020 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301021 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1022 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001023 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001024 int pcm_device_id = 0, acdb_dev_id, app_type;
1025 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301026 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +05301027 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +05301028 struct streams_io_cfg *s_info = NULL;
1029 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301030 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001031
Siena Richard7c2db772016-12-21 11:32:34 -08001032 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1033 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1034 platform_get_snd_device_name(usecase->in_snd_device),
1035 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001036
Siddartha Shaik343abc62017-08-08 11:15:25 +05301037 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301038 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301039 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001040 rc = 0;
1041 goto exit_send_app_type_cfg;
1042 }
1043 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
1044 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1045 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001046 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001047 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301048 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301049 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301050 (!is_offload_usecase(usecase->id)) &&
1051 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301052 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 -07001053 rc = 0;
1054 goto exit_send_app_type_cfg;
1055 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001056
1057 //if VR is active then only send the mixer control
1058 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1059 ALOGI("ULL doesnt need sending app type cfg, returning");
1060 rc = 0;
1061 goto exit_send_app_type_cfg;
1062 }
1063
Surendar Karka93cd25a2018-08-28 14:21:37 +05301064 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001065 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301066 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1067 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001068 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001069 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301070 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1071 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301072 }
Siena Richard7c2db772016-12-21 11:32:34 -08001073
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001074 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1075 if (!ctl) {
1076 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1077 mixer_ctl_name);
1078 rc = -EINVAL;
1079 goto exit_send_app_type_cfg;
1080 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301081 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301082 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1083 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1084 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1085 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1086 } else {
1087 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1088 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301089 if (acdb_dev_id <= 0) {
1090 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1091 rc = -EINVAL;
1092 goto exit_send_app_type_cfg;
1093 }
1094
Siena Richard7c2db772016-12-21 11:32:34 -08001095 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1096 if (snd_device_be_idx < 0) {
1097 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1098 __func__, platform_get_snd_device_name(snd_device),
1099 snd_device_be_idx);
1100 }
1101
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301102 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +05301103
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001104 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301105 if (!strncmp("true", value, sizeof("true"))) {
1106 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1107 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001108 !(usecase->stream.out->flags &
1109 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301110 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1111 }
Ashish Jainc02430d2016-01-04 10:42:43 +05301112
kunleizcdf25942017-09-20 14:01:21 +08001113 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1114 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
1115 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Dieter Luecking5d57def2018-09-07 14:23:37 +02001116 if (platform_spkr_use_default_sample_rate(adev->platform)) {
1117 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1118 } else {
1119 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1120 usecase->stream.out->sample_rate,
1121 &usecase->stream.out->app_type_cfg.sample_rate);
1122 }
1123
Ashish Jaina052e572016-11-07 16:41:07 +05301124 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1125 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1126 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1127 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1128 /*
1129 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1130 * configured device sample rate, if not update the COPP rate to be equal to the
1131 * device sample rate, else open COPP at stream sample rate
1132 */
1133 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1134 usecase->stream.out->sample_rate,
1135 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05301136 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1137 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -07001138 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1139 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +05301140 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -07001141 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301142 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1143 /*
1144 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1145 * for bit width use the stream param only.
1146 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001147 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301148 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1149 __func__, usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001150 }
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301151 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001152 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1153
Varun Balaraje49253e2017-07-06 19:48:56 +05301154 /* Interactive streams are supported with only direct app type id.
1155 * Get Direct profile app type and use it for interactive streams
1156 */
1157 list_for_each(node, &adev->streams_output_cfg_list) {
1158 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001159 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301160 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1161 AUDIO_OUTPUT_FLAG_DIRECT))
1162 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301163 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001164 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301165 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301166 else
1167 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001168 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001169 app_type_cfg[len++] = acdb_dev_id;
1170 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001171 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1172 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301173 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1174 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001175
1176 sample_rate = sample_rate * 4;
1177 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1178 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001179 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001180 app_type_cfg[len++] = sample_rate;
1181
Siena Richard7c2db772016-12-21 11:32:34 -08001182 if (snd_device_be_idx > 0)
1183 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301184
Siena Richard7c2db772016-12-21 11:32:34 -08001185 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1186 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301187
1188 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001189 app_type = usecase->stream.in->app_type_cfg.app_type;
1190 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301191 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001192 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1193 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301194 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1195 audio_extn_btsco_get_sample_rate(usecase->in_snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1196 } else {
1197 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1198 }
Deeraj Somanfc791e72018-11-30 13:23:01 +05301199 if (usecase->stream.in->device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP & ~AUDIO_DEVICE_BIT_IN) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001200 audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
1201 ALOGI("%s using %d sample rate rate for A2DP dec CoPP",
1202 __func__, usecase->stream.in->app_type_cfg.sample_rate);
1203 }
Siena Richard7c2db772016-12-21 11:32:34 -08001204 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1205 app_type_cfg[len++] = sample_rate;
1206 if (snd_device_be_idx > 0)
1207 app_type_cfg[len++] = snd_device_be_idx;
1208 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1209 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301210 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001211 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301212 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301213 sample_rate = usecase->stream.inout->out_config.sample_rate;
1214 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1215 }
Siena Richard7c2db772016-12-21 11:32:34 -08001216 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301217 app_type_cfg[len++] = acdb_dev_id;
1218 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001219 if (snd_device_be_idx > 0)
1220 app_type_cfg[len++] = snd_device_be_idx;
1221 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1222 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301223 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301224
Siddartha Shaik343abc62017-08-08 11:15:25 +05301225 if(ctl)
1226 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001227 rc = 0;
1228exit_send_app_type_cfg:
1229 return rc;
1230}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001231
Siena Richard7c2db772016-12-21 11:32:34 -08001232int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1233 struct audio_usecase *usecase)
1234{
1235 int i, num_devices = 0;
1236 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301237 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001238 int rc = 0;
1239
Aalique Grahame22e49102018-12-18 14:23:57 -08001240 if (usecase->type == PCM_HFP_CALL) {
1241 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1242 }
1243
Siena Richard7c2db772016-12-21 11:32:34 -08001244 switch (usecase->type) {
1245 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301246 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001247 ALOGD("%s: usecase->out_snd_device %s",
1248 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1249 /* check for out combo device */
1250 if (platform_split_snd_device(adev->platform,
1251 usecase->out_snd_device,
1252 &num_devices, new_snd_devices)) {
1253 new_snd_devices[0] = usecase->out_snd_device;
1254 num_devices = 1;
1255 }
1256 break;
1257 case PCM_CAPTURE:
1258 ALOGD("%s: usecase->in_snd_device %s",
1259 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301260 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1261 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1262 }
Siena Richard7c2db772016-12-21 11:32:34 -08001263 /* check for in combo device */
1264 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301265 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001266 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301267 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001268 num_devices = 1;
1269 }
1270 break;
1271 default:
1272 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1273 rc = 0;
1274 break;
1275 }
1276
1277 for (i = 0; i < num_devices; i++) {
1278 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1279 if (rc)
1280 break;
1281 }
1282
1283 return rc;
1284}
1285
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301286int read_line_from_file(const char *path, char *buf, size_t count)
1287{
1288 char * fgets_ret;
1289 FILE * fd;
1290 int rv;
1291
1292 fd = fopen(path, "r");
1293 if (fd == NULL)
1294 return -1;
1295
1296 fgets_ret = fgets(buf, (int)count, fd);
1297 if (NULL != fgets_ret) {
1298 rv = (int)strlen(buf);
1299 } else {
1300 rv = ferror(fd);
1301 }
1302 fclose(fd);
1303
1304 return rv;
1305}
1306
Ashish Jainf1eaa582016-05-23 20:54:24 +05301307/*Translates ALSA formats to AOSP PCM formats*/
1308audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1309{
1310 audio_format_t format;
1311
1312 switch(alsa_format) {
1313 case SNDRV_PCM_FORMAT_S16_LE:
1314 format = AUDIO_FORMAT_PCM_16_BIT;
1315 break;
1316 case SNDRV_PCM_FORMAT_S24_3LE:
1317 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1318 break;
1319 case SNDRV_PCM_FORMAT_S24_LE:
1320 format = AUDIO_FORMAT_PCM_8_24_BIT;
1321 break;
1322 case SNDRV_PCM_FORMAT_S32_LE:
1323 format = AUDIO_FORMAT_PCM_32_BIT;
1324 break;
1325 default:
1326 ALOGW("Incorrect ALSA format");
1327 format = AUDIO_FORMAT_INVALID;
1328 }
1329 return format;
1330}
1331
1332/*Translates hal format (AOSP) to alsa formats*/
1333uint32_t hal_format_to_alsa(audio_format_t hal_format)
1334{
1335 uint32_t alsa_format;
1336
1337 switch (hal_format) {
1338 case AUDIO_FORMAT_PCM_32_BIT: {
1339 if (platform_supports_true_32bit())
1340 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1341 else
1342 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1343 }
1344 break;
1345 case AUDIO_FORMAT_PCM_8_BIT:
1346 alsa_format = SNDRV_PCM_FORMAT_S8;
1347 break;
1348 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1349 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1350 break;
1351 case AUDIO_FORMAT_PCM_8_24_BIT: {
1352 if (platform_supports_true_32bit())
1353 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1354 else
1355 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1356 }
1357 break;
1358 case AUDIO_FORMAT_PCM_FLOAT:
1359 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1360 break;
1361 default:
1362 case AUDIO_FORMAT_PCM_16_BIT:
1363 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1364 break;
1365 }
1366 return alsa_format;
1367}
1368
Ashish Jain83a6cc22016-06-28 14:34:17 +05301369/*Translates PCM formats to AOSP formats*/
1370audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1371{
1372 audio_format_t format = AUDIO_FORMAT_INVALID;
1373
1374 switch(pcm_format) {
1375 case PCM_FORMAT_S16_LE:
1376 format = AUDIO_FORMAT_PCM_16_BIT;
1377 break;
1378 case PCM_FORMAT_S24_3LE:
1379 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1380 break;
1381 case PCM_FORMAT_S24_LE:
1382 format = AUDIO_FORMAT_PCM_8_24_BIT;
1383 break;
1384 case PCM_FORMAT_S32_LE:
1385 format = AUDIO_FORMAT_PCM_32_BIT;
1386 break;
1387 default:
1388 ALOGW("Incorrect PCM format");
1389 format = AUDIO_FORMAT_INVALID;
1390 }
1391 return format;
1392}
1393
1394/*Translates hal format (AOSP) to alsa formats*/
1395uint32_t hal_format_to_pcm(audio_format_t hal_format)
1396{
1397 uint32_t pcm_format;
1398
1399 switch (hal_format) {
1400 case AUDIO_FORMAT_PCM_32_BIT:
1401 case AUDIO_FORMAT_PCM_8_24_BIT:
1402 case AUDIO_FORMAT_PCM_FLOAT: {
1403 if (platform_supports_true_32bit())
1404 pcm_format = PCM_FORMAT_S32_LE;
1405 else
1406 pcm_format = PCM_FORMAT_S24_3LE;
1407 }
1408 break;
1409 case AUDIO_FORMAT_PCM_8_BIT:
1410 pcm_format = PCM_FORMAT_S8;
1411 break;
1412 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1413 pcm_format = PCM_FORMAT_S24_3LE;
1414 break;
1415 default:
1416 case AUDIO_FORMAT_PCM_16_BIT:
1417 pcm_format = PCM_FORMAT_S16_LE;
1418 break;
1419 }
1420 return pcm_format;
1421}
1422
Ashish Jainf1eaa582016-05-23 20:54:24 +05301423uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1424 uint32_t sample_rate,
1425 uint32_t noOfChannels)
1426{
1427 uint32_t fragment_size = 0;
1428 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1429
1430 fragment_size = (pcm_offload_time
1431 * sample_rate
1432 * bytes_per_sample
1433 * noOfChannels)/1000;
1434 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1435 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1436 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1437 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1438 /*To have same PCM samples for all channels, the buffer size requires to
1439 *be multiple of (number of channels * bytes per sample)
1440 *For writes to succeed, the buffer must be written at address which is multiple of 32
1441 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001442 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301443
1444 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1445 return fragment_size;
1446}
1447
1448/* Calculates the fragment size required to configure compress session.
1449 * Based on the alsa format selected, decide if conversion is needed in
1450
1451 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1452 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1453 */
1454void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1455{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301456 audio_format_t dst_format = out->hal_op_format;
1457 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301458 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1459 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1460
1461 out->compr_config.fragment_size =
1462 get_alsa_fragment_size(hal_op_bytes_per_sample,
1463 out->sample_rate,
1464 popcount(out->channel_mask));
1465
1466 if ((src_format != dst_format) &&
1467 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1468
Ashish Jain83a6cc22016-06-28 14:34:17 +05301469 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301470 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1471 hal_op_bytes_per_sample);
1472 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301473 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301474 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301475 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301476 }
1477}
1478
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301479/* converts pcm format 24_8 to 8_24 inplace */
1480size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1481{
1482 size_t i = 0;
1483 int *int_buf_stream = buf;
1484
1485 if ((bytes % 4) != 0) {
1486 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1487 return -EINVAL;
1488 }
1489
1490 for (; i < (bytes / 4); i++)
1491 int_buf_stream[i] >>= 8;
1492
1493 return bytes;
1494}
1495
1496int get_snd_codec_id(audio_format_t format)
1497{
1498 int id = 0;
1499
1500 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1501 case AUDIO_FORMAT_MP3:
1502 id = SND_AUDIOCODEC_MP3;
1503 break;
1504 case AUDIO_FORMAT_AAC:
1505 id = SND_AUDIOCODEC_AAC;
1506 break;
1507 case AUDIO_FORMAT_AAC_ADTS:
1508 id = SND_AUDIOCODEC_AAC;
1509 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301510 case AUDIO_FORMAT_AAC_LATM:
1511 id = SND_AUDIOCODEC_AAC;
1512 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301513 case AUDIO_FORMAT_PCM:
1514 id = SND_AUDIOCODEC_PCM;
1515 break;
1516 case AUDIO_FORMAT_FLAC:
1517 id = SND_AUDIOCODEC_FLAC;
1518 break;
1519 case AUDIO_FORMAT_ALAC:
1520 id = SND_AUDIOCODEC_ALAC;
1521 break;
1522 case AUDIO_FORMAT_APE:
1523 id = SND_AUDIOCODEC_APE;
1524 break;
1525 case AUDIO_FORMAT_VORBIS:
1526 id = SND_AUDIOCODEC_VORBIS;
1527 break;
1528 case AUDIO_FORMAT_WMA:
1529 id = SND_AUDIOCODEC_WMA;
1530 break;
1531 case AUDIO_FORMAT_WMA_PRO:
1532 id = SND_AUDIOCODEC_WMA_PRO;
1533 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301534 case AUDIO_FORMAT_MP2:
1535 id = SND_AUDIOCODEC_MP2;
1536 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301537 case AUDIO_FORMAT_AC3:
1538 id = SND_AUDIOCODEC_AC3;
1539 break;
1540 case AUDIO_FORMAT_E_AC3:
1541 case AUDIO_FORMAT_E_AC3_JOC:
1542 id = SND_AUDIOCODEC_EAC3;
1543 break;
1544 case AUDIO_FORMAT_DTS:
1545 case AUDIO_FORMAT_DTS_HD:
1546 id = SND_AUDIOCODEC_DTS;
1547 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001548 case AUDIO_FORMAT_DOLBY_TRUEHD:
1549 id = SND_AUDIOCODEC_TRUEHD;
1550 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001551 case AUDIO_FORMAT_IEC61937:
1552 id = SND_AUDIOCODEC_IEC61937;
1553 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301554 case AUDIO_FORMAT_DSD:
1555 id = SND_AUDIOCODEC_DSD;
1556 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301557 case AUDIO_FORMAT_APTX:
1558 id = SND_AUDIOCODEC_APTX;
1559 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301560 default:
1561 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1562 }
1563
1564 return id;
1565}
1566
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001567void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1568 struct audio_usecase *usecase)
1569{
1570 int type = usecase->type;
1571
Dhananjay Kumar14245982017-01-16 20:21:00 +05301572 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001573 struct stream_out *out = usecase->stream.out;
1574 int snd_device = usecase->out_snd_device;
1575 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001576 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301577 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001578 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301579 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301580 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301581 platform_send_audio_calibration(adev->platform, usecase,
1582 usecase->stream.in->app_type_cfg.app_type,
1583 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001584 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301585 /* when app type is default. the sample rate is not used to send cal */
1586 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301587 platform_get_default_app_type_v2(adev->platform, usecase->type),
1588 48000);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301589 } else if (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301590 int snd_device = usecase->out_snd_device;
1591 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1592 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1593 platform_send_audio_calibration(adev->platform, usecase,
1594 platform_get_default_app_type_v2(adev->platform, usecase->type),
1595 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001596 } else {
1597 /* No need to send audio calibration for voice and voip call usecases */
1598 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1599 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001600 }
1601}
1602
Ben Rombergera04fabc2014-11-14 12:16:03 -08001603// Base64 Encode and Decode
1604// Not all features supported. This must be used only with following conditions.
1605// Decode Modes: Support with and without padding
1606// CRLF not handling. So no CRLF in string to decode.
1607// Encode Modes: Supports only padding
1608int b64decode(char *inp, int ilen, uint8_t* outp)
1609{
1610 int i, j, k, ii, num;
1611 int rem, pcnt;
1612 uint32_t res=0;
1613 uint8_t getIndex[MAX_BASEINDEX_LEN];
1614 uint8_t tmp, cflag;
1615
1616 if(inp == NULL || outp == NULL || ilen <= 0) {
1617 ALOGE("[%s] received NULL pointer or zero length",__func__);
1618 return -1;
1619 }
1620
1621 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1622 for(i=0;i<BASE_TABLE_SIZE;i++) {
1623 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1624 }
1625 getIndex[(uint8_t)'=']=0;
1626
1627 j=0;k=0;
1628 num = ilen/4;
1629 rem = ilen%4;
1630 if(rem==0)
1631 num = num-1;
1632 cflag=0;
1633 for(i=0; i<num; i++) {
1634 res=0;
1635 for(ii=0;ii<4;ii++) {
1636 res = res << 6;
1637 tmp = getIndex[(uint8_t)inp[j++]];
1638 res = res | tmp;
1639 cflag = cflag | tmp;
1640 }
1641 outp[k++] = (res >> 16)&0xFF;
1642 outp[k++] = (res >> 8)&0xFF;
1643 outp[k++] = res & 0xFF;
1644 }
1645
1646 // Handle last bytes special
1647 pcnt=0;
1648 if(rem == 0) {
1649 //With padding or full data
1650 res = 0;
1651 for(ii=0;ii<4;ii++) {
1652 if(inp[j] == '=')
1653 pcnt++;
1654 res = res << 6;
1655 tmp = getIndex[(uint8_t)inp[j++]];
1656 res = res | tmp;
1657 cflag = cflag | tmp;
1658 }
1659 outp[k++] = res >> 16;
1660 if(pcnt == 2)
1661 goto done;
1662 outp[k++] = (res>>8)&0xFF;
1663 if(pcnt == 1)
1664 goto done;
1665 outp[k++] = res&0xFF;
1666 } else {
1667 //without padding
1668 res = 0;
1669 for(i=0;i<rem;i++) {
1670 res = res << 6;
1671 tmp = getIndex[(uint8_t)inp[j++]];
1672 res = res | tmp;
1673 cflag = cflag | tmp;
1674 }
1675 for(i=rem;i<4;i++) {
1676 res = res << 6;
1677 pcnt++;
1678 }
1679 outp[k++] = res >> 16;
1680 if(pcnt == 2)
1681 goto done;
1682 outp[k++] = (res>>8)&0xFF;
1683 if(pcnt == 1)
1684 goto done;
1685 outp[k++] = res&0xFF;
1686 }
1687done:
1688 if(cflag == 0xFF) {
1689 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1690 __func__, inp);
1691 return 0;
1692 }
1693 return k;
1694}
1695
1696int b64encode(uint8_t *inp, int ilen, char* outp)
1697{
1698 int i,j,k, num;
1699 int rem=0;
1700 uint32_t res=0;
1701
1702 if(inp == NULL || outp == NULL || ilen<=0) {
1703 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1704 return -1;
1705 }
1706
1707 num = ilen/3;
1708 rem = ilen%3;
1709 j=0;k=0;
1710 for(i=0; i<num; i++) {
1711 //prepare index
1712 res = inp[j++]<<16;
1713 res = res | inp[j++]<<8;
1714 res = res | inp[j++];
1715 //get output map from index
1716 outp[k++] = (char) bTable[(res>>18)&0x3F];
1717 outp[k++] = (char) bTable[(res>>12)&0x3F];
1718 outp[k++] = (char) bTable[(res>>6)&0x3F];
1719 outp[k++] = (char) bTable[res&0x3F];
1720 }
1721
1722 switch(rem) {
1723 case 1:
1724 res = inp[j++]<<16;
1725 outp[k++] = (char) bTable[res>>18];
1726 outp[k++] = (char) bTable[(res>>12)&0x3F];
1727 //outp[k++] = '=';
1728 //outp[k++] = '=';
1729 break;
1730 case 2:
1731 res = inp[j++]<<16;
1732 res = res | inp[j++]<<8;
1733 outp[k++] = (char) bTable[res>>18];
1734 outp[k++] = (char) bTable[(res>>12)&0x3F];
1735 outp[k++] = (char) bTable[(res>>6)&0x3F];
1736 //outp[k++] = '=';
1737 break;
1738 default:
1739 break;
1740 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001741 outp[k] = '\0';
1742 return k;
1743}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301744
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301745
1746int audio_extn_utils_get_codec_version(const char *snd_card_name,
1747 int card_num,
1748 char *codec_version)
1749{
1750 char procfs_path[50];
1751 FILE *fp;
1752
1753 if (strstr(snd_card_name, "tasha")) {
1754 snprintf(procfs_path, sizeof(procfs_path),
1755 "/proc/asound/card%d/codecs/tasha/version", card_num);
1756 if ((fp = fopen(procfs_path, "r")) != NULL) {
1757 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1758 fclose(fp);
1759 } else {
1760 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1761 return -ENOENT;
1762 }
1763 ALOGD("%s: codec version %s", __func__, codec_version);
1764 }
1765
1766 return 0;
1767}
1768
1769
Ashish Jain81eb2a82015-05-13 10:52:34 +05301770#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1771
1772void get_default_compressed_channel_status(
1773 unsigned char *channel_status)
1774{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301775 memset(channel_status,0,24);
1776
1777 /* block start bit in preamble bit 3 */
1778 channel_status[0] |= PROFESSIONAL;
1779 //compre out
1780 channel_status[0] |= NON_LPCM;
1781 // sample rate; fixed 48K for default/transcode
1782 channel_status[3] |= SR_48000;
1783}
1784
Ashish Jain81eb2a82015-05-13 10:52:34 +05301785int32_t get_compressed_channel_status(void *audio_stream_data,
1786 uint32_t audio_frame_size,
1787 unsigned char *channel_status,
1788 enum audio_parser_code_type codec_type)
1789 // codec_type - AUDIO_PARSER_CODEC_AC3
1790 // - AUDIO_PARSER_CODEC_DTS
1791{
1792 unsigned char *stream;
1793 int ret = 0;
1794 stream = (unsigned char *)audio_stream_data;
1795
1796 if (audio_stream_data == NULL || audio_frame_size == 0) {
1797 ALOGW("no buffer to get channel status, return default for compress");
1798 get_default_compressed_channel_status(channel_status);
1799 return ret;
1800 }
1801
1802 memset(channel_status,0,24);
1803 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1804 {
1805 ALOGE("init audio parser failed");
1806 return -1;
1807 }
1808 ret = get_channel_status(channel_status, codec_type);
1809 return ret;
1810
1811}
1812
Ashish Jain81eb2a82015-05-13 10:52:34 +05301813void get_lpcm_channel_status(uint32_t sampleRate,
1814 unsigned char *channel_status)
1815{
1816 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301817 memset(channel_status,0,24);
1818 /* block start bit in preamble bit 3 */
1819 channel_status[0] |= PROFESSIONAL;
1820 //LPCM OUT
1821 channel_status[0] &= ~NON_LPCM;
1822
1823 switch (sampleRate) {
1824 case 8000:
1825 case 11025:
1826 case 12000:
1827 case 16000:
1828 case 22050:
1829 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301830 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301831 case 24000:
1832 channel_status[3] |= SR_24000;
1833 break;
1834 case 32000:
1835 channel_status[3] |= SR_32000;
1836 break;
1837 case 44100:
1838 channel_status[3] |= SR_44100;
1839 break;
1840 case 48000:
1841 channel_status[3] |= SR_48000;
1842 break;
1843 case 88200:
1844 channel_status[3] |= SR_88200;
1845 break;
1846 case 96000:
1847 channel_status[3] |= SR_96000;
1848 break;
1849 case 176400:
1850 channel_status[3] |= SR_176400;
1851 break;
1852 case 192000:
1853 channel_status[3] |= SR_192000;
1854 break;
1855 default:
1856 ALOGV("Invalid sample_rate %u\n", sampleRate);
1857 status = -1;
1858 break;
1859 }
1860}
1861
1862void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1863{
1864 unsigned char channel_status[24]={0};
1865 struct snd_aes_iec958 iec958;
1866 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1867 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301868 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001869
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301870 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301871 /*TODO:Extend code to support DTS passthrough*/
1872 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07001873 audio_extn_passthru_is_passthrough_stream(out) &&
1874 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301875 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001876 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301877 /*set channel status bit for LPCM*/
1878 get_lpcm_channel_status(out->sample_rate, channel_status);
1879 }
1880
1881 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1882 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1883 if (!ctl) {
1884 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1885 __func__, mixer_ctl_name);
1886 return;
1887 }
1888 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1889 ALOGE("%s: Could not set channel status for ext HDMI ",
1890 __func__);
1891 return;
1892 }
1893
1894}
1895#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301896
1897int audio_extn_utils_get_avt_device_drift(
1898 struct audio_usecase *usecase,
1899 struct audio_avt_device_drift_param *drift_param)
1900{
1901 int ret = 0, count = 0;
1902 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301903 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301904 struct mixer_ctl *ctl = NULL;
1905 struct audio_avt_device_drift_stats drift_stats;
1906 struct audio_device *adev = NULL;
1907
1908 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301909 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1910 if (!backend) {
1911 ALOGE("%s: Unsupported device %d", __func__,
1912 usecase->stream.out->devices);
1913 ret = -EINVAL;
1914 goto done;
1915 }
1916 strlcpy(avt_device_drift_mixer_ctl_name,
1917 backend,
1918 MIXER_PATH_MAX_LENGTH);
1919
1920 count = strlen(backend);
1921 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1922 strlcat(&avt_device_drift_mixer_ctl_name[count],
1923 " DRIFT",
1924 MIXER_PATH_MAX_LENGTH - count);
1925 } else {
1926 ret = -EINVAL;
1927 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301928 }
1929 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301930 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301931 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301932 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301933 }
1934
Naresh Tanniru6160c712017-04-17 15:43:48 +05301935 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301936 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1937 if (!ctl) {
1938 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1939 __func__, avt_device_drift_mixer_ctl_name);
1940
1941 ret = -EINVAL;
1942 goto done;
1943 }
1944
1945 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1946 avt_device_drift_mixer_ctl_name);
1947
1948 mixer_ctl_update(ctl);
1949 count = mixer_ctl_get_num_values(ctl);
1950 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1951 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1952 __func__);
1953
1954 ret = -EINVAL;
1955 goto done;
1956 }
1957
1958 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1959 if (ret != 0) {
1960 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1961 __func__);
1962
1963 ret = -EINVAL;
1964 goto done;
1965 }
1966 memcpy(drift_param, &drift_stats.drift_param,
1967 sizeof(struct audio_avt_device_drift_param));
1968done:
1969 return ret;
1970}
Manish Dewangan07de2142017-02-27 19:27:20 +05301971
1972#ifdef SNDRV_COMPRESS_PATH_DELAY
1973int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1974{
1975 int ret = -EINVAL;
1976 struct snd_compr_metadata metadata;
1977 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1978
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08001979 /* override the latency for pcm offload use case */
1980 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
1981 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
1982 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
1983 }
1984
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001985 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301986 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1987 if (!(is_offload_usecase(out->usecase))) {
1988 ALOGE("%s:: not supported for non offload session", __func__);
1989 goto exit;
1990 }
1991
1992 if (!out->compr) {
1993 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1994 __func__);
1995 goto exit;
1996 }
1997
1998 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1999 ret = compress_get_metadata(out->compr, &metadata);
2000 if(ret) {
2001 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2002 goto exit;
2003 }
2004 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2005 } else {
2006 ALOGD("%s:: Using Fix DSP delay",__func__);
2007 }
2008
2009exit:
2010 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2011 return delay_ms;
2012}
2013#else
2014int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2015{
2016 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2017}
2018#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302019
2020#ifdef SNDRV_COMPRESS_RENDER_MODE
2021int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2022{
2023 struct snd_compr_metadata metadata;
2024 int ret = -EINVAL;
2025
2026 if (!(is_offload_usecase(out->usecase))) {
2027 ALOGE("%s:: not supported for non offload session", __func__);
2028 goto exit;
2029 }
2030
2031 if (!out->compr) {
2032 ALOGD("%s:: Invalid compress handle",
2033 __func__);
2034 goto exit;
2035 }
2036
2037 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2038
2039 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2040 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2041 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2042 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2043 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2044 } else {
2045 ret = 0;
2046 goto exit;
2047 }
2048 ret = compress_set_metadata(out->compr, &metadata);
2049 if(ret) {
2050 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2051 }
2052exit:
2053 return ret;
2054}
2055#else
2056int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2057{
2058 ALOGD("%s:: configuring render mode not supported", __func__);
2059 return 0;
2060}
2061#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302062
2063#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2064int audio_extn_utils_compress_set_clk_rec_mode(
2065 struct audio_usecase *usecase)
2066{
2067 struct snd_compr_metadata metadata;
2068 struct stream_out *out = NULL;
2069 int ret = -EINVAL;
2070
Naresh Tanniru7586e292017-04-13 10:38:13 +05302071 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302072 ALOGE("%s:: Invalid use case", __func__);
2073 goto exit;
2074 }
2075
2076 out = usecase->stream.out;
2077 if (!out) {
2078 ALOGE("%s:: invalid stream", __func__);
2079 goto exit;
2080 }
2081
2082 if (!is_offload_usecase(out->usecase)) {
2083 ALOGE("%s:: not supported for non offload session", __func__);
2084 goto exit;
2085 }
2086
2087 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2088 ALOGD("%s:: clk recovery is only supported in STC render mode",
2089 __func__);
2090 ret = 0;
2091 goto exit;
2092 }
2093
2094 if (!out->compr) {
2095 ALOGD("%s:: Invalid compress handle",
2096 __func__);
2097 goto exit;
2098 }
2099 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2100 switch(usecase->out_snd_device) {
2101 case SND_DEVICE_OUT_HDMI:
2102 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2103 case SND_DEVICE_OUT_DISPLAY_PORT:
2104 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2105 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2106 break;
2107 default:
2108 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2109 break;
2110 }
2111
2112 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2113
2114 ret = compress_set_metadata(out->compr, &metadata);
2115 if(ret) {
2116 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2117 }
2118
2119exit:
2120 return ret;
2121}
2122#else
2123int audio_extn_utils_compress_set_clk_rec_mode(
2124 struct audio_usecase *usecase __unused)
2125{
2126 ALOGD("%s:: configuring render mode not supported", __func__);
2127 return 0;
2128}
2129#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302130
2131#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2132int audio_extn_utils_compress_set_render_window(
2133 struct stream_out *out,
2134 struct audio_out_render_window_param *render_window)
2135{
2136 struct snd_compr_metadata metadata;
2137 int ret = -EINVAL;
2138
Manish Dewangan27346042017-03-01 12:56:12 +05302139 if(render_window == NULL) {
2140 ALOGE("%s:: Invalid render_window", __func__);
2141 goto exit;
2142 }
2143
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002144 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2145 __func__,render_window->render_ws, render_window->render_we);
2146
Manish Dewangan27346042017-03-01 12:56:12 +05302147 if (!is_offload_usecase(out->usecase)) {
2148 ALOGE("%s:: not supported for non offload session", __func__);
2149 goto exit;
2150 }
2151
Naresh Tanniru6160c712017-04-17 15:43:48 +05302152 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2153 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302154 ALOGD("%s:: only supported in timestamp mode, current "
2155 "render mode mode %d", __func__, out->render_mode);
2156 goto exit;
2157 }
2158
2159 if (!out->compr) {
2160 ALOGW("%s:: offload session not yet opened,"
2161 "render window will be configure later", __func__);
2162 /* store render window to reconfigure in start_output_stream() */
2163 goto exit;
2164 }
2165
2166 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2167 /*render window start value */
2168 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2169 metadata.value[1] = \
2170 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2171 /*render window end value */
2172 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2173 metadata.value[3] = \
2174 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2175
2176 ret = compress_set_metadata(out->compr, &metadata);
2177 if(ret) {
2178 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2179 }
2180
2181exit:
2182 return ret;
2183}
2184#else
2185int audio_extn_utils_compress_set_render_window(
2186 struct stream_out *out __unused,
2187 struct audio_out_render_window_param *render_window __unused)
2188{
2189 ALOGD("%s:: configuring render window not supported", __func__);
2190 return 0;
2191}
2192#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302193
2194#ifdef SNDRV_COMPRESS_START_DELAY
2195int audio_extn_utils_compress_set_start_delay(
2196 struct stream_out *out,
2197 struct audio_out_start_delay_param *delay_param)
2198{
2199 struct snd_compr_metadata metadata;
2200 int ret = -EINVAL;
2201
2202 if(delay_param == NULL) {
2203 ALOGE("%s:: Invalid delay_param", __func__);
2204 goto exit;
2205 }
2206
2207 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2208 delay_param->start_delay);
2209
2210 if (!is_offload_usecase(out->usecase)) {
2211 ALOGE("%s:: not supported for non offload session", __func__);
2212 goto exit;
2213 }
2214
Naresh Tanniru6160c712017-04-17 15:43:48 +05302215 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2216 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302217 ALOGD("%s:: only supported in timestamp mode, current "
2218 "render mode mode %d", __func__, out->render_mode);
2219 goto exit;
2220 }
2221
2222 if (!out->compr) {
2223 ALOGW("%s:: offload session not yet opened,"
2224 "start delay will be configure later", __func__);
2225 goto exit;
2226 }
2227
2228 metadata.key = SNDRV_COMPRESS_START_DELAY;
2229 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2230 metadata.value[1] = \
2231 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2232
2233 ret = compress_set_metadata(out->compr, &metadata);
2234 if(ret) {
2235 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2236 }
2237
2238exit:
2239 return ret;
2240}
2241#else
2242int audio_extn_utils_compress_set_start_delay(
2243 struct stream_out *out __unused,
2244 struct audio_out_start_delay_param *delay_param __unused)
2245{
2246 ALOGD("%s:: configuring render window not supported", __func__);
2247 return 0;
2248}
2249#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002250
2251#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302252#define RETRY_US 1000000
2253#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002254#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2255#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2256
2257#ifdef LINUX_ENABLED
2258static const char *kConfigLocationList[] =
2259 {"/etc"};
2260#else
2261static const char *kConfigLocationList[] =
2262 {"/vendor/etc"};
2263#endif
2264static const int kConfigLocationListSize =
2265 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2266
2267bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2268{
2269 char full_config_path[MIXER_PATH_MAX_LENGTH];
2270 for (int i = 0; i < kConfigLocationListSize; i++) {
2271 snprintf(full_config_path,
2272 MIXER_PATH_MAX_LENGTH,
2273 "%s/%s",
2274 kConfigLocationList[i],
2275 file_name);
2276 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002277 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002278 return true;
2279 }
2280 }
2281 return false;
2282}
2283
2284/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2285int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2286{
2287 if (NULL == snd_card_name) {
2288 return -1;
2289 }
2290
2291 struct snd_card_split *snd_split_handle = NULL;
2292 int ret = 0;
2293 audio_extn_set_snd_card_split(snd_card_name);
2294 snd_split_handle = audio_extn_get_snd_card_split();
2295
2296 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2297 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2298 snd_split_handle->form_factor);
2299
2300 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2301 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2302 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2303 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2304
2305 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2306 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2307 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2308 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2309 }
2310 }
2311
2312 return ret;
2313}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002314
2315int audio_extn_utils_get_snd_card_num()
2316{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302317 int snd_card_num = 0;
2318 struct mixer *mixer = NULL;
2319
2320 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2321 if (mixer)
2322 mixer_close(mixer);
2323 return snd_card_num;
2324}
2325
2326int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2327{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002328
2329 void *hw_info = NULL;
2330 struct mixer *mixer = NULL;
2331 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002332 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002333 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002334 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002335
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302336 if (!mixer_handle) {
2337 ALOGE("invalid mixer handle");
2338 return -1;
2339 }
2340 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302341 /*
mpangfaa7bae2019-01-15 16:38:13 +08002342 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302343 * sleep for 1 sec and try detections with sound card 0 again.
2344 * If sound card gets detected, check if it is relevant, if not check with the
2345 * other sound cards. To ensure that the irrelevant sound card is not check again,
2346 * we maintain it in min_snd_card_num.
2347 */
2348 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002349 snd_card_num = 0;
2350 while (snd_card_num < MAX_SND_CARD) {
2351 if (snd_card_detection_info[snd_card_num] == 0) {
2352 mixer = mixer_open(snd_card_num);
2353 if (!mixer)
2354 snd_card_num++;
2355 else
2356 break;
2357 } else
2358 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002359 }
2360
2361 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302362 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302363 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002364 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302365 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002366 }
2367
2368 snd_card_name = strdup(mixer_get_name(mixer));
2369 if (!snd_card_name) {
2370 ALOGE("failed to allocate memory for snd_card_name\n");
2371 mixer_close(mixer);
2372 return -1;
2373 }
2374 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002375 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002376 hw_info = hw_info_init(snd_card_name);
2377 if (hw_info) {
2378 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2379 break;
2380 }
mpangfaa7bae2019-01-15 16:38:13 +08002381 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002382
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302383 free(snd_card_name);
2384 snd_card_name = NULL;
2385
2386 mixer_close(mixer);
2387 mixer = NULL;
2388 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002389 if (snd_card_name)
2390 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302391
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302392 if (hw_info)
2393 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002394
mpangfaa7bae2019-01-15 16:38:13 +08002395 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002396 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2397 return -1;
2398 }
2399
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302400 if (mixer)
2401 *mixer_handle = mixer;
2402
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002403 return snd_card_num;
2404}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302405
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302406void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2407{
2408 if (mixer)
2409 mixer_close(mixer);
2410}
2411
Naresh Tanniru6160c712017-04-17 15:43:48 +05302412#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2413int audio_extn_utils_compress_enable_drift_correction(
2414 struct stream_out *out,
2415 struct audio_out_enable_drift_correction *drift)
2416{
2417 struct snd_compr_metadata metadata;
2418 int ret = -EINVAL;
2419
2420 if(drift == NULL) {
2421 ALOGE("%s:: Invalid param", __func__);
2422 goto exit;
2423 }
2424
2425 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2426
2427 if (!is_offload_usecase(out->usecase)) {
2428 ALOGE("%s:: not supported for non offload session", __func__);
2429 goto exit;
2430 }
2431
2432 if (!out->compr) {
2433 ALOGW("%s:: offload session not yet opened,"
2434 "start delay will be configure later", __func__);
2435 goto exit;
2436 }
2437
2438 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2439 metadata.value[0] = drift->enable;
2440 out->drift_correction_enabled = drift->enable;
2441
2442 ret = compress_set_metadata(out->compr, &metadata);
2443 if(ret) {
2444 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2445 out->drift_correction_enabled = false;
2446 }
2447
2448exit:
2449 return ret;
2450}
2451#else
2452int audio_extn_utils_compress_enable_drift_correction(
2453 struct stream_out *out __unused,
2454 struct audio_out_enable_drift_correction *drift __unused)
2455{
2456 ALOGD("%s:: configuring drift enablement not supported", __func__);
2457 return 0;
2458}
2459#endif
2460
2461#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2462int audio_extn_utils_compress_correct_drift(
2463 struct stream_out *out,
2464 struct audio_out_correct_drift *drift_param)
2465{
2466 struct snd_compr_metadata metadata;
2467 int ret = -EINVAL;
2468
2469 if (drift_param == NULL) {
2470 ALOGE("%s:: Invalid drift_param", __func__);
2471 goto exit;
2472 }
2473
2474 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2475 drift_param->adjust_time);
2476
2477 if (!is_offload_usecase(out->usecase)) {
2478 ALOGE("%s:: not supported for non offload session", __func__);
2479 goto exit;
2480 }
2481
2482 if (!out->compr) {
2483 ALOGW("%s:: offload session not yet opened", __func__);
2484 goto exit;
2485 }
2486
2487 if (!out->drift_correction_enabled) {
2488 ALOGE("%s:: drift correction not enabled", __func__);
2489 goto exit;
2490 }
2491
2492 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2493 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2494 metadata.value[1] = \
2495 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2496
2497 ret = compress_set_metadata(out->compr, &metadata);
2498 if(ret)
2499 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2500exit:
2501 return ret;
2502}
2503#else
2504int audio_extn_utils_compress_correct_drift(
2505 struct stream_out *out __unused,
2506 struct audio_out_correct_drift *drift_param __unused)
2507{
2508 ALOGD("%s:: setting adjust clock not supported", __func__);
2509 return 0;
2510}
2511#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302512
2513int audio_extn_utils_set_channel_map(
2514 struct stream_out *out,
2515 struct audio_out_channel_map_param *channel_map_param)
2516{
2517 int ret = -EINVAL, i = 0;
2518 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2519
2520 if (channel_map_param == NULL) {
2521 ALOGE("%s:: Invalid channel_map", __func__);
2522 goto exit;
2523 }
2524
2525 if (channel_map_param->channels != channels) {
2526 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2527 __func__, channel_map_param->channels, channels);
2528 goto exit;
2529 }
2530
2531 for ( i = 0; i < channels; i++) {
2532 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2533 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2534 }
2535 ret = 0;
2536exit:
2537 return ret;
2538}
Varun Balaraje49253e2017-07-06 19:48:56 +05302539
2540int audio_extn_utils_set_pan_scale_params(
2541 struct stream_out *out,
2542 struct mix_matrix_params *mm_params)
2543{
2544 int ret = -EINVAL, i = 0, j = 0;
2545
Varun Balaraj003ee752017-08-07 17:54:55 +05302546 if (mm_params == NULL || out == NULL) {
2547 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302548 goto exit;
2549 }
2550
2551 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2552 mm_params->num_output_channels <= 0 ||
2553 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2554 mm_params->num_input_channels <= 0)
2555 goto exit;
2556
2557 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2558 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2559 out->pan_scale_params.has_output_channel_map =
2560 mm_params->has_output_channel_map;
2561 for (i = 0; i < mm_params->num_output_channels; i++)
2562 out->pan_scale_params.output_channel_map[i] =
2563 mm_params->output_channel_map[i];
2564
2565 out->pan_scale_params.has_input_channel_map =
2566 mm_params->has_input_channel_map;
2567 for (i = 0; i < mm_params->num_input_channels; i++)
2568 out->pan_scale_params.input_channel_map[i] =
2569 mm_params->input_channel_map[i];
2570
2571 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2572 for (i = 0; i < mm_params->num_output_channels; i++)
2573 for (j = 0; j < mm_params->num_input_channels; j++) {
2574 //Convert the channel coefficient gains in Q14 format
2575 out->pan_scale_params.mixer_coeffs[i][j] =
2576 mm_params->mixer_coeffs[i][j] * (2 << 13);
2577 }
2578
2579 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2580 out->pcm_device_id,
2581 out->pan_scale_params);
2582
2583exit:
2584 return ret;
2585}
2586
2587int audio_extn_utils_set_downmix_params(
2588 struct stream_out *out,
2589 struct mix_matrix_params *mm_params)
2590{
2591 int ret = -EINVAL, i = 0, j = 0;
2592 struct audio_usecase *usecase = NULL;
2593
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002594 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302595 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302596 goto exit;
2597 }
2598
2599 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2600 mm_params->num_output_channels <= 0 ||
2601 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2602 mm_params->num_input_channels <= 0)
2603 goto exit;
2604
2605 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302606 if (!usecase) {
2607 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002608 goto exit;
2609 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302610 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2611 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2612
2613 out->downmix_params.has_output_channel_map =
2614 mm_params->has_output_channel_map;
2615 for (i = 0; i < mm_params->num_output_channels; i++) {
2616 out->downmix_params.output_channel_map[i] =
2617 mm_params->output_channel_map[i];
2618 }
2619
2620 out->downmix_params.has_input_channel_map =
2621 mm_params->has_input_channel_map;
2622 for (i = 0; i < mm_params->num_input_channels; i++)
2623 out->downmix_params.input_channel_map[i] =
2624 mm_params->input_channel_map[i];
2625
2626 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2627 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302628 for (j = 0; j < mm_params->num_input_channels; j++) {
2629 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302630 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302631 mm_params->mixer_coeffs[i][j] * (2 << 13);
2632 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302633
2634 ret = platform_set_stream_downmix_params(out->dev->platform,
2635 out->pcm_device_id,
2636 usecase->out_snd_device,
2637 out->downmix_params);
2638
2639exit:
2640 return ret;
2641}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302642
2643bool audio_extn_utils_is_dolby_format(audio_format_t format)
2644{
2645 if (format == AUDIO_FORMAT_AC3 ||
2646 format == AUDIO_FORMAT_E_AC3 ||
2647 format == AUDIO_FORMAT_E_AC3_JOC)
2648 return true;
2649 else
2650 return false;
2651}
2652
`Deeraj Soman676c2702017-09-18 19:25:53 +05302653int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2654{
2655 int i;
2656 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2657 {"S32_LE", 32},
2658 {"S24_LE", 24},
2659 {"S16_LE", 16}};
2660 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302661
`Deeraj Soman676c2702017-09-18 19:25:53 +05302662 for (i = 0; i < num_configs; i++) {
2663 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2664 return mixer_bitwidth_config[i].value;
2665 }
2666
2667 return -EINVAL;
2668}
2669
2670int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2671{
2672 int i;
2673 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2674 {"KHZ_48", 48000},
2675 {"KHZ_96", 96000},
2676 {"KHZ_144", 144000},
2677 {"KHZ_192", 192000},
2678 {"KHZ_384", 384000},
2679 {"KHZ_44P1", 44100},
2680 {"KHZ_88P2", 88200},
2681 {"KHZ_176P4", 176400},
2682 {"KHZ_352P8", 352800}};
2683 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2684
2685 for (i = 0; i < num_configs; i++) {
2686 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2687 return mixer_samplerate_config[i].value;
2688 }
2689
2690 return -EINVAL;
2691}
2692
2693int audio_extn_utils_get_channels_from_string(const char *id_string)
2694{
2695 int i;
2696 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2697 {"Two", 2},
2698 {"Three",3},
2699 {"Four", 4},
2700 {"Five", 5},
2701 {"Six", 6},
2702 {"Seven", 7},
2703 {"Eight", 8}};
2704 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2705
2706 for (i = 0; i < num_configs; i++) {
2707 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2708 return mixer_channels_config[i].value;
2709 }
2710
2711 return -EINVAL;
2712}
Surendar karka30569792018-05-08 12:02:21 +05302713
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002714void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2715{
2716 audio_extn_dev_arbi_release(snd_device);
2717 audio_extn_sound_trigger_update_device_status(snd_device,
2718 ST_EVENT_SND_DEVICE_FREE);
2719 audio_extn_listen_update_device_status(snd_device,
2720 LISTEN_EVENT_SND_DEVICE_FREE);
2721}
2722
Surendar karka30569792018-05-08 12:02:21 +05302723int audio_extn_utils_get_license_params
2724(
2725const struct audio_device *adev,
2726struct audio_license_params *license_params
2727)
2728{
2729 if(!license_params)
2730 return -EINVAL;
2731 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2732}
2733
Aalique Grahame22e49102018-12-18 14:23:57 -08002734int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2735 int app_type,
2736 int *gain)
2737{
2738 int gain_cfg[4];
2739 const char *mixer_ctl_name = "App Type Gain";
2740 struct mixer_ctl *ctl;
2741 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2742 if (!ctl) {
2743 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2744 mixer_ctl_name);
2745 return -EINVAL;
2746 }
2747 gain_cfg[0] = 0;
2748 gain_cfg[1] = app_type;
2749 gain_cfg[2] = gain[0];
2750 gain_cfg[3] = gain[1];
2751 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2752 return mixer_ctl_set_array(ctl, gain_cfg,
2753 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2754}