blob: 30bc10de1ebc9731d340a58de78a57562c04b52a [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>
Surendar Karka287348c2019-04-10 18:31:46 +053033#include <sys/ioctl.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034
Manish Dewangan07de2142017-02-27 19:27:20 +053035
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
39#include "audio_extn.h"
Deeraj Soman14230922019-01-30 16:39:30 +053040#include "voice_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080041#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053042#include <sound/compress_params.h>
43#include <sound/compress_offload.h>
Surendar Karka287348c2019-04-10 18:31:46 +053044#include <sound/devdep_params.h>
Manish Dewangan07de2142017-02-27 19:27:20 +053045#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046
47#ifdef DYNAMIC_LOG_ENABLED
48#include <log_xml_parser.h>
49#define LOG_MASK HAL_MOD_FILE_UTILS
50#include <log_utils.h>
51#endif
52
Ashish Jain81eb2a82015-05-13 10:52:34 +053053#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
Ashish Jain81eb2a82015-05-13 10:52:34 +053054#include "audio_parsers.h"
55#endif
Ashish Jain81eb2a82015-05-13 10:52:34 +053056
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053057#ifdef LINUX_ENABLED
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053059#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053060#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070061#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053062#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053063#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070064
65#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053066#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070067
68#define DYNAMIC_VALUE_TAG "dynamic"
69#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053070#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070071#define FORMATS_TAG "formats"
72#define SAMPLING_RATES_TAG "sampling_rates"
73#define BIT_WIDTH_TAG "bit_width"
74#define APP_TYPE_TAG "app_type"
75
76#define STRING_TO_ENUM(string) { #string, string }
77#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
78
Ben Rombergera04fabc2014-11-14 12:16:03 -080079#define BASE_TABLE_SIZE 64
80#define MAX_BASEINDEX_LEN 256
81
Ben Romberger1aaaf862017-04-06 17:49:46 -070082#ifndef SND_AUDIOCODEC_TRUEHD
83#define SND_AUDIOCODEC_TRUEHD 0x00000023
84#endif
85
Vikram Panduranga93f080e2017-06-07 18:16:14 -070086#define APP_TYPE_VOIP_AUDIO 0x1113A
87
Ashish Jain81eb2a82015-05-13 10:52:34 +053088#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
89#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
90#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
91#define SR_44100 (0<<0) /* 44.1kHz */
92#define SR_NOTID (1<<0) /* non indicated */
93#define SR_48000 (2<<0) /* 48kHz */
94#define SR_32000 (3<<0) /* 32kHz */
95#define SR_22050 (4<<0) /* 22.05kHz */
96#define SR_24000 (6<<0) /* 24kHz */
97#define SR_88200 (8<<0) /* 88.2kHz */
98#define SR_96000 (10<<0) /* 96kHz */
99#define SR_176400 (12<<0) /* 176.4kHz */
100#define SR_192000 (14<<0) /* 192kHz */
101
102#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
104/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800105#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
106#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
107#define PCM_OFFLOAD_PLAYBACK_LATENCY \
108 (PCM_OFFLOAD_BUFFER_DURATION + PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY)
Manish Dewangan07de2142017-02-27 19:27:20 +0530109
Varun Balaraje49253e2017-07-06 19:48:56 +0530110#ifndef MAX_CHANNELS_SUPPORTED
111#define MAX_CHANNELS_SUPPORTED 8
112#endif
113
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700114#ifdef __LP64__
115#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
116#else
117#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
118#endif
119
120static void *vndk_fwk_lib_handle = NULL;
121typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
122static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
123
`Deeraj Soman676c2702017-09-18 19:25:53 +0530124typedef struct {
125 const char *id_string;
126 const int value;
127} mixer_config_lookup;
128
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129struct string_to_enum {
130 const char *name;
131 uint32_t value;
132};
133
134const struct string_to_enum s_flag_name_to_enum_table[] = {
135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530149 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
150 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
151 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
152 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
153 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530154 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700157};
158
159const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530160 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700161 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530162 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
163 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530164 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700165 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
166 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
167 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700168 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
169 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700170 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700172 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530173 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700174 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700175 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530176 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700177 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
178 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
179 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
181 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
182 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
183 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
184 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
185 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
186 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700187 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530188 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
189 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800190 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
191 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
192 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530193 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530194 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
195 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
196 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530197 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530198 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
199 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
201 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530202 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700203};
204
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530205/* payload structure avt_device drift query */
206struct audio_avt_device_drift_stats {
207 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530208
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530209 /* Indicates the device interface direction as either
210 * source (Tx) or sink (Rx).
211 */
212 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530213
214 /* Reference timer for drift accumulation and time stamp information.
215 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
216 */
217 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530218 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530219} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530220
Ben Rombergera04fabc2014-11-14 12:16:03 -0800221static char bTable[BASE_TABLE_SIZE] = {
222 'A','B','C','D','E','F','G','H','I','J','K','L',
223 'M','N','O','P','Q','R','S','T','U','V','W','X',
224 'Y','Z','a','b','c','d','e','f','g','h','i','j',
225 'k','l','m','n','o','p','q','r','s','t','u','v',
226 'w','x','y','z','0','1','2','3','4','5','6','7',
227 '8','9','+','/'
228};
229
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700230static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
231 const char *name)
232{
233 size_t i;
234 for (i = 0; i < size; i++) {
235 if (strcmp(table[i].name, name) == 0) {
236 ALOGV("%s found %s", __func__, table[i].name);
237 return table[i].value;
238 }
239 }
240 return 0;
241}
242
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530243static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700244{
245 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530246 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800247 char *last_r;
248 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700249 while (flag_name != NULL) {
250 if (strlen(flag_name) != 0) {
251 flag |= string_to_enum(s_flag_name_to_enum_table,
252 ARRAY_SIZE(s_flag_name_to_enum_table),
253 flag_name);
254 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800255 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700256 }
257
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800258 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530259 io_flags.in_flags = (audio_input_flags_t)flag;
260 io_flags.out_flags = (audio_output_flags_t)flag;
261 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700262}
263
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530264static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700265{
266 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800267 char *last_r;
268 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700269
270 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
271 return;
272
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530273 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700274 while (str != NULL) {
275 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
276 ARRAY_SIZE(s_format_name_to_enum_table), str);
277 ALOGV("%s: format - %d", __func__, format);
278 if (format != 0) {
279 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700280 if (sf_info == NULL)
281 break; /* return whatever was parsed */
282
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700283 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700285 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800286 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700287 }
288}
289
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530290static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700291{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700292 struct stream_sample_rate *ss_info = NULL;
293 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800294 char *last_r;
295 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296
Amit Shekhar6f461b12014-08-01 14:52:58 -0700297 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
298 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530300 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700301 while (str != NULL) {
302 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
303 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
304 if (0 != sample_rate) {
305 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800306 if (!ss_info) {
307 ALOGE("%s: memory allocation failure", __func__);
308 return;
309 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700310 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530311 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700312 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800313 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700314 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700315}
316
317static int parse_bit_width_names(char *name)
318{
319 int bit_width = 16;
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 bit_width = (int)strtol(str, (char **)NULL, 10);
325
326 ALOGV("%s: bit_width - %d", __func__, bit_width);
327 return bit_width;
328}
329
330static int parse_app_type_names(void *platform, char *name)
331{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700332 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800333 char *last_r;
334 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700335
336 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
337 app_type = (int)strtol(str, (char **)NULL, 10);
338
339 ALOGV("%s: app_type - %d", __func__, app_type);
340 return app_type;
341}
342
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530343static void update_streams_cfg_list(cnode *root, void *platform,
344 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700345{
346 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530347 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700348
349 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530350 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700351
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530352 if (!s_info) {
353 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700354 return;
355 }
356
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700357 while (node) {
358 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530359 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530360 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
361 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700362 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530363 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700364 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
366 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700367 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530368 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700369 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530370 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700371 }
372 node = node->next;
373 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700375}
376
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530377static void load_cfg_list(cnode *root, void *platform,
378 struct listnode *streams_output_cfg_list,
379 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700380{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530381 cnode *node = NULL;
382
383 node = config_find(root, OUTPUTS_TAG);
384 if (node != NULL) {
385 node = node->first_child;
386 while (node) {
387 ALOGV("%s: loading output %s", __func__, node->name);
388 update_streams_cfg_list(node, platform, streams_output_cfg_list);
389 node = node->next;
390 }
391 } else {
392 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700393 }
394
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530395 node = config_find(root, INPUTS_TAG);
396 if (node != NULL) {
397 node = node->first_child;
398 while (node) {
399 ALOGV("%s: loading input %s", __func__, node->name);
400 update_streams_cfg_list(node, platform, streams_input_cfg_list);
401 node = node->next;
402 }
403 } else {
404 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700405 }
406}
407
408static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530409 struct listnode *streams_output_cfg_list,
410 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700411{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530412 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700413 int length = 0, i, num_app_types = 0;
414 struct listnode *node;
415 bool update;
416 struct mixer_ctl *ctl = NULL;
417 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530418 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800419 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700420
421 if (!mixer) {
422 ALOGE("%s: mixer is null",__func__);
423 return;
424 }
425 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
426 if (!ctl) {
427 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
428 return;
429 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530430 app_type_cfg[length++] = num_app_types;
431
432 if (list_empty(streams_output_cfg_list)) {
433 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700434 app_type_cfg[length++] = 48000;
435 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530436 num_app_types += 1;
437 }
438 if (list_empty(streams_input_cfg_list)) {
439 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
440 app_type_cfg[length++] = 48000;
441 app_type_cfg[length++] = 16;
442 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700443 }
444
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800445 /* get target bit width for ADM enforce mode */
446 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
447
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700448 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530449 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700450 update = true;
451 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530452 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700453 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530454 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530455 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530456 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530457 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530458 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800459 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
460 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
461 (target_bit_width > app_type_cfg[i+3]))
462 app_type_cfg[i+3] = target_bit_width;
463
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700464 update = false;
465 break;
466 }
467 }
468 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530469 num_app_types += 1;
470 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
471 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800472 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
473 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
474 (target_bit_width > app_type_cfg[length]))
475 app_type_cfg[length] = target_bit_width;
476
477 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530478 }
479 }
480 list_for_each(node, streams_input_cfg_list) {
481 s_info = node_to_item(node, struct streams_io_cfg, list);
482 update = true;
483 for (i=0; i<length; i=i+3) {
484 if (app_type_cfg[i+1] == 0)
485 break;
486 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530487 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530488 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530489 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530490 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
491 update = false;
492 break;
493 }
494 }
495 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
496 num_app_types += 1;
497 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
498 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
499 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700500 }
501 }
502 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
503 if (num_app_types) {
504 app_type_cfg[0] = num_app_types;
505 mixer_ctl_set_array(ctl, app_type_cfg, length);
506 }
507}
508
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530509void audio_extn_utils_update_streams_cfg_lists(void *platform,
510 struct mixer *mixer,
511 struct listnode *streams_output_cfg_list,
512 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700513{
514 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530515 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700516
517 ALOGV("%s", __func__);
518 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530519 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700520
521 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700522 if (root == NULL) {
523 ALOGE("cfg_list, NULL config root");
524 return;
525 }
526
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530527 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
528 if (data == NULL) {
529 ALOGD("%s: failed to open io config file(%s), trying older config file",
530 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
531 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
532 if (data == NULL) {
533 send_app_type_cfg(platform, mixer,
534 streams_output_cfg_list,
535 streams_input_cfg_list);
536 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800537 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530538 return;
539 }
540 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700541
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530542 config_load(root, data);
543 load_cfg_list(root, platform, streams_output_cfg_list,
544 streams_input_cfg_list);
545
546 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
547 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800548
549 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800550 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800551 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700552}
553
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530554static void audio_extn_utils_dump_streams_cfg_list(
555 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700556{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700557 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530558 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700559 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700560 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530561
562 list_for_each(node_i, streams_cfg_list) {
563 s_info = node_to_item(node_i, struct streams_io_cfg, list);
564 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
565 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
566 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
567 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700568 sf_info = node_to_item(node_j, struct stream_format, list);
569 ALOGV("format-%x", sf_info->format);
570 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530571 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700572 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
573 ALOGV("sample rate-%d", ss_info->sample_rate);
574 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700575 }
576}
577
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530578void audio_extn_utils_dump_streams_cfg_lists(
579 struct listnode *streams_output_cfg_list,
580 struct listnode *streams_input_cfg_list)
581{
582 ALOGV("%s", __func__);
583 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
584 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
585}
586
587static void audio_extn_utils_release_streams_cfg_list(
588 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700589{
590 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530591 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700592
593 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530594
595 while (!list_empty(streams_cfg_list)) {
596 node_i = list_head(streams_cfg_list);
597 s_info = node_to_item(node_i, struct streams_io_cfg, list);
598 while (!list_empty(&s_info->format_list)) {
599 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700600 list_remove(node_j);
601 free(node_to_item(node_j, struct stream_format, list));
602 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530603 while (!list_empty(&s_info->sample_rate_list)) {
604 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700605 list_remove(node_j);
606 free(node_to_item(node_j, struct stream_sample_rate, list));
607 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700608 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530609 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700610 }
611}
612
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530613void audio_extn_utils_release_streams_cfg_lists(
614 struct listnode *streams_output_cfg_list,
615 struct listnode *streams_input_cfg_list)
616{
617 ALOGV("%s", __func__);
618 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
619 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
620}
621
622static bool set_app_type_cfg(struct streams_io_cfg *s_info,
623 struct stream_app_type_cfg *app_type_cfg,
624 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700625 {
626 struct listnode *node_i;
627 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530628 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700629 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
630 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530631 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700632
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530633 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700634 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530635 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700636 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
637 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
638 return true;
639 }
640 }
641 /*
642 * Reiterate through the list assuming dafault sample rate.
643 * Handles scenario where input sample rate is higher
644 * than all sample rates in list for the input bit width.
645 */
646 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800647
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530648 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700649 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
650 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530651 (bit_width == s_info->app_type_cfg.bit_width)) {
652 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700653 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530654 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800655 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 -0700656 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
657 return true;
658 }
659 }
660 return false;
661}
662
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530663void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
664 struct listnode *streams_input_cfg_list,
665 audio_devices_t devices __unused,
666 audio_input_flags_t flags,
667 audio_format_t format,
668 uint32_t sample_rate,
669 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530670 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530671 struct stream_app_type_cfg *app_type_cfg)
672{
673 struct listnode *node_i, *node_j;
674 struct streams_io_cfg *s_info;
675 struct stream_format *sf_info;
676
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530677 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
678 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530679
680 list_for_each(node_i, streams_input_cfg_list) {
681 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530682 /* Along with flags do profile matching if set at either end.*/
683 if (s_info->flags.in_flags == flags &&
684 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
685 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530686 list_for_each(node_j, &s_info->format_list) {
687 sf_info = node_to_item(node_j, struct stream_format, list);
688 if (sf_info->format == format) {
689 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
690 return;
691 }
692 }
693 }
694 }
695 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
696 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
697 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
698 app_type_cfg->bit_width = 16;
699}
700
701void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700702 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700703 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700704 audio_output_flags_t flags,
705 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700706 uint32_t sample_rate,
707 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530708 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530709 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700710 struct stream_app_type_cfg *app_type_cfg)
711{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530712 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530713 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700714 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530715 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700716
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530717 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
718 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
719 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700720 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530721 else if (-ENOSYS == bw)
722 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700723 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530724 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700725 }
726
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700727 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530728 if (!strncmp("true", value, sizeof("true"))) {
729 if ((popcount(channel_mask) > 2) &&
730 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700731 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530732 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
733 ALOGD("%s: MCH session defaulting sample rate to %d",
734 __func__, sample_rate);
735 }
736 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530737
738 /* Set sampling rate to 176.4 for DSD64
739 * and 352.8Khz for DSD128.
740 * Set Bit Width to 16. output will be 16 bit
741 * post DoP in ASM.
742 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700743 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530744 (format == AUDIO_FORMAT_DSD)) {
745 bit_width = 16;
746 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
747 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
748 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
749 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
750 }
751
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530752
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800753 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
754 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700755 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530756 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530757 /* Along with flags do profile matching if set at either end.*/
758 if (s_info->flags.out_flags == flags &&
759 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
760 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530761 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700762 sf_info = node_to_item(node_j, struct stream_format, list);
763 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530764 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700765 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700766 }
767 }
768 }
769 }
770 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530771 s_info = node_to_item(node_i, struct streams_io_cfg, list);
772 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700773 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530774 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
775 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
776 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700777 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530778 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700779 return;
780 }
781 }
782 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700783 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700784 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700785 app_type_cfg->bit_width = 16;
786}
787
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530788static bool audio_is_this_native_usecase(struct audio_usecase *uc)
789{
790 bool native_usecase = false;
791 struct stream_out *out = (struct stream_out*) uc->stream.out;
792
793 if (PCM_PLAYBACK == uc->type && out != NULL &&
794 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
795 is_offload_usecase(uc->id) &&
796 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
797 native_usecase = true;
798
799 return native_usecase;
800}
801
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800802bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
803{
804 /* DSP bitwidth enforce mode for ADM and AFE:
805 * includes:
806 * deep buffer, low latency, direct pcm and offload.
807 * excludes:
808 * ull(raw+fast), VOIP.
809 */
810 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
811 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
812 (flags & AUDIO_OUTPUT_FLAG_FAST)))
813 return false;
814
815
816 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
817 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
818 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
819 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
820 return true;
821 else
822 return false;
823}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800824
825static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
826{
827 return adev->vr_audio_mode_enabled;
828}
829
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530830void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
831 struct audio_device *adev,
832 struct audio_usecase *usecase)
833{
834 ALOGV("%s", __func__);
835
836 switch(usecase->type) {
837 case PCM_PLAYBACK:
838 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
839 &adev->streams_output_cfg_list,
840 usecase->stream.out->devices,
841 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700842 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530843 usecase->stream.out->sample_rate,
844 usecase->stream.out->bit_width,
845 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530846 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530847 &usecase->stream.out->app_type_cfg);
848 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
849 break;
850 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700851 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
852 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
853 else
854 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530855 &adev->streams_input_cfg_list,
856 usecase->stream.in->device,
857 usecase->stream.in->flags,
858 usecase->stream.in->format,
859 usecase->stream.in->sample_rate,
860 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530861 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530862 &usecase->stream.in->app_type_cfg);
863 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
864 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530865 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530866 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
867 &adev->streams_output_cfg_list,
868 usecase->stream.inout->out_config.devices,
869 0,
870 usecase->stream.inout->out_config.format,
871 usecase->stream.inout->out_config.sample_rate,
872 usecase->stream.inout->out_config.bit_width,
873 usecase->stream.inout->out_config.channel_mask,
874 usecase->stream.inout->profile,
875 &usecase->stream.inout->out_app_type_cfg);
876 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
877 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530878 default:
879 ALOGE("%s: app type cfg not supported for usecase type (%d)",
880 __func__, usecase->type);
881 }
882}
883
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530884void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
885{
886 switch (snd_device) {
887 case SND_DEVICE_OUT_BT_SCO:
888 case SND_DEVICE_IN_BT_SCO_MIC:
889 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
890 *sample_rate = 8000;
891 break;
892 case SND_DEVICE_OUT_BT_SCO_WB:
893 case SND_DEVICE_IN_BT_SCO_MIC_WB:
894 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
895 *sample_rate = 16000;
896 break;
897 default:
898 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
899 break;
900 }
901}
902
Aalique Grahame22e49102018-12-18 14:23:57 -0800903static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
904 int pcm_device_id, int app_type,
905 int acdb_dev_id, int sample_rate,
906 int stream_type,
907 snd_device_t snd_device)
908{
909
910 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
911 struct mixer_ctl *ctl;
912 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
913 int snd_device_be_idx = -1;
914
915 if (stream_type == PCM_PLAYBACK) {
916 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
917 "Audio Stream %d App Type Cfg", pcm_device_id);
918 } else if (stream_type == PCM_CAPTURE) {
919 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
920 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
921 }
922
923 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
924 if (!ctl) {
925 ALOGE("%s: Could not get ctl for mixer cmd - %s",
926 __func__, mixer_ctl_name);
927 rc = -EINVAL;
928 goto exit;
929 }
930 app_type_cfg[len++] = app_type;
931 app_type_cfg[len++] = acdb_dev_id;
932 app_type_cfg[len++] = sample_rate;
933
934 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
935 if (snd_device_be_idx > 0)
936 app_type_cfg[len++] = snd_device_be_idx;
937 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
938 "sample rate %d, snd_device_be_idx %d",
939 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
940 snd_device_be_idx);
941 mixer_ctl_set_array(ctl, app_type_cfg, len);
942
943exit:
944 return rc;
945}
946
947static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
948 struct audio_usecase *usecase)
949{
950 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
951 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
952 int app_type = 0, rc = 0;
953
954 ALOGV("%s", __func__);
955
956 if (usecase->type != PCM_HFP_CALL) {
957 ALOGV("%s: not a playback or HFP path, no need to cfg app type", __func__);
958 rc = 0;
959 goto exit_send_app_type_cfg;
960 }
961 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
962 (usecase->id != USECASE_AUDIO_HFP_SCO_WB)) {
963 ALOGV("%s: a playback path where app type cfg is not required", __func__);
964 rc = 0;
965 goto exit_send_app_type_cfg;
966 }
967
968 snd_device = usecase->out_snd_device;
969 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
970
971 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
972 if (acdb_dev_id < 0) {
973 ALOGE("%s: Couldn't get the acdb dev id", __func__);
974 rc = -EINVAL;
975 goto exit_send_app_type_cfg;
976 }
977
978 if (usecase->type == PCM_HFP_CALL) {
979
980 /* config HFP session:1 playback path */
981 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
982 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
983 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
984 acdb_dev_id, sample_rate,
985 PCM_PLAYBACK,
986 SND_DEVICE_NONE); // use legacy behavior
987 if (rc < 0)
988 goto exit_send_app_type_cfg;
989
990 /* config HFP session:1 capture path */
991 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
992 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
993 acdb_dev_id, sample_rate,
994 PCM_CAPTURE,
995 SND_DEVICE_NONE);
996 if (rc < 0)
997 goto exit_send_app_type_cfg;
998
999 /* config HFP session:2 capture path */
1000 pcm_device_id = HFP_ASM_RX_TX;
1001 snd_device = usecase->in_snd_device;
1002 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1003 if (acdb_dev_id <= 0) {
1004 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1005 rc = -EINVAL;
1006 goto exit_send_app_type_cfg;
1007 }
1008 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1009 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1010 acdb_dev_id, sample_rate, PCM_CAPTURE,
1011 SND_DEVICE_NONE);
1012 if (rc < 0)
1013 goto exit_send_app_type_cfg;
1014
1015 /* config HFP session:2 playback path */
1016 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1017 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1018 acdb_dev_id, sample_rate,
1019 PCM_PLAYBACK, SND_DEVICE_NONE);
1020 if (rc < 0)
1021 goto exit_send_app_type_cfg;
1022 }
1023
1024 rc = 0;
1025exit_send_app_type_cfg:
1026 return rc;
1027}
1028
Zhou Song06761dd2019-04-28 18:08:17 +08001029int audio_extn_utils_get_app_sample_rate_for_device(
1030 struct audio_device *adev,
1031 struct audio_usecase *usecase, int snd_device)
1032{
1033 char value[PROPERTY_VALUE_MAX] = {0};
1034 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1035
1036 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1037 property_get("vendor.audio.playback.mch.downsample",value,"");
1038 if (!strncmp("true", value, sizeof("true"))) {
1039 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1040 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1041 !(usecase->stream.out->flags &
1042 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1043 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1044 }
1045
1046 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1047 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
1048 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
1049 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1050 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1051 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1052 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1053 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1054 /*
1055 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1056 * configured device sample rate, if not update the COPP rate to be equal to the
1057 * device sample rate, else open COPP at stream sample rate
1058 */
1059 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1060 usecase->stream.out->sample_rate,
1061 &usecase->stream.out->app_type_cfg.sample_rate);
1062 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1063 !audio_is_this_native_usecase(usecase)) &&
1064 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1065 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1066 /* Reset to default if no native stream is active*/
1067 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1068 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1069 /*
1070 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1071 * for bit width use the stream param only.
1072 */
1073 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1074 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1075 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1076 }
1077 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1078 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1079
1080 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1081 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1082 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1083 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1084 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1085 sample_rate = sample_rate * 4;
1086 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1087 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1088 }
1089 } else if (usecase->type == PCM_CAPTURE) {
1090 if (usecase->stream.in != NULL) {
1091 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1092 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1093 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1094 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1095 &usecase->stream.in->app_type_cfg.sample_rate);
1096 } else {
1097 audio_extn_btsco_get_sample_rate(snd_device,
1098 &usecase->stream.in->app_type_cfg.sample_rate);
1099 }
1100 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1101 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
1102 sample_rate = SAMPLE_RATE_8000;
1103 }
1104 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1105 sample_rate = usecase->stream.inout->out_config.sample_rate;
1106 }
1107 return sample_rate;
1108}
1109
Siena Richard7c2db772016-12-21 11:32:34 -08001110static int send_app_type_cfg_for_device(struct audio_device *adev,
1111 struct audio_usecase *usecase,
1112 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001113{
1114 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301115 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1116 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001117 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001118 int pcm_device_id = 0, acdb_dev_id, app_type;
1119 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301120 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301121 struct streams_io_cfg *s_info = NULL;
1122 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301123 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001124
Siena Richard7c2db772016-12-21 11:32:34 -08001125 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1126 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1127 platform_get_snd_device_name(usecase->in_snd_device),
1128 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001129
Siddartha Shaik343abc62017-08-08 11:15:25 +05301130 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301131 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301132 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001133 rc = 0;
1134 goto exit_send_app_type_cfg;
1135 }
1136 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
1137 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1138 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001139 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001140 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301141 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301142 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301143 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001144 (usecase->type != PCM_CAPTURE) &&
1145 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301146 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 -07001147 rc = 0;
1148 goto exit_send_app_type_cfg;
1149 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001150
1151 //if VR is active then only send the mixer control
1152 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1153 ALOGI("ULL doesnt need sending app type cfg, returning");
1154 rc = 0;
1155 goto exit_send_app_type_cfg;
1156 }
1157
Surendar Karka93cd25a2018-08-28 14:21:37 +05301158 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001159 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301160 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1161 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001162 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001163 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301164 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1165 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301166 }
Siena Richard7c2db772016-12-21 11:32:34 -08001167
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001168 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1169 if (!ctl) {
1170 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1171 mixer_ctl_name);
1172 rc = -EINVAL;
1173 goto exit_send_app_type_cfg;
1174 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301175 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301176 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1177 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1178 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1179 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1180 } else {
1181 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1182 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301183 if (acdb_dev_id <= 0) {
1184 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1185 rc = -EINVAL;
1186 goto exit_send_app_type_cfg;
1187 }
1188
Siena Richard7c2db772016-12-21 11:32:34 -08001189 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1190 if (snd_device_be_idx < 0) {
1191 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1192 __func__, platform_get_snd_device_name(snd_device),
1193 snd_device_be_idx);
1194 }
1195
Zhou Song06761dd2019-04-28 18:08:17 +08001196 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1197
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301198 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301199 /* Interactive streams are supported with only direct app type id.
1200 * Get Direct profile app type and use it for interactive streams
1201 */
1202 list_for_each(node, &adev->streams_output_cfg_list) {
1203 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001204 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301205 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1206 AUDIO_OUTPUT_FLAG_DIRECT))
1207 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301208 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001209 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301210 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301211 else
1212 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001213 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001214 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001215 app_type_cfg[len++] = sample_rate;
1216
Siena Richard7c2db772016-12-21 11:32:34 -08001217 if (snd_device_be_idx > 0)
1218 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301219
Siena Richard7c2db772016-12-21 11:32:34 -08001220 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1221 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301222
1223 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001224 app_type = usecase->stream.in->app_type_cfg.app_type;
1225 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301226 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001227 app_type_cfg[len++] = sample_rate;
1228 if (snd_device_be_idx > 0)
1229 app_type_cfg[len++] = snd_device_be_idx;
1230 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1231 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301232 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001233 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301234 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301235 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1236 }
Siena Richard7c2db772016-12-21 11:32:34 -08001237 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301238 app_type_cfg[len++] = acdb_dev_id;
1239 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001240 if (snd_device_be_idx > 0)
1241 app_type_cfg[len++] = snd_device_be_idx;
1242 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1243 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301244 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301245
Siddartha Shaik343abc62017-08-08 11:15:25 +05301246 if(ctl)
1247 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001248 rc = 0;
1249exit_send_app_type_cfg:
1250 return rc;
1251}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001252
Deeraj Soman14230922019-01-30 16:39:30 +05301253static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1254 audio_format_t format,
1255 int channel_count)
1256{
1257 int ret = 0;
1258
1259 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1260 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1261 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1262 !voice_extn_compress_voip_is_format_supported(format) &&
1263 !audio_extn_compr_cap_format_supported(format) &&
1264 !audio_extn_cin_format_supported(format))
1265 ret = -EINVAL;
1266
1267 switch (channel_count) {
1268 case 1:
1269 case 2:
1270 case 3:
1271 case 4:
1272 case 6:
1273 case 8:
1274 break;
1275 default:
1276 ret = -EINVAL;
1277 }
1278
1279 switch (sample_rate) {
1280 case 8000:
1281 case 11025:
1282 case 12000:
1283 case 16000:
1284 case 22050:
1285 case 24000:
1286 case 32000:
1287 case 44100:
1288 case 48000:
1289 case 88200:
1290 case 96000:
1291 case 176400:
1292 case 192000:
1293 break;
1294 default:
1295 ret = -EINVAL;
1296 }
1297
1298 return ret;
1299}
1300
1301static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1302{
1303 uint32_t remainder = 0;
1304
1305 if (!multiplier)
1306 return num;
1307
1308 remainder = num % multiplier;
1309 if (remainder)
1310 num += (multiplier - remainder);
1311
1312 return num;
1313}
1314
1315static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1316{
1317 uint32_t high = num1, low = num2, temp = 0;
1318
1319 if (!num1 || !num2)
1320 return 0;
1321
1322 if (num1 < num2) {
1323 high = num2;
1324 low = num1;
1325 }
1326
1327 while (low != 0) {
1328 temp = low;
1329 low = high % low;
1330 high = temp;
1331 }
1332 return (num1 * num2)/high;
1333}
1334
Siena Richard7c2db772016-12-21 11:32:34 -08001335int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1336 struct audio_usecase *usecase)
1337{
1338 int i, num_devices = 0;
1339 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301340 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001341 int rc = 0;
1342
Aalique Grahame22e49102018-12-18 14:23:57 -08001343 if (usecase->type == PCM_HFP_CALL) {
1344 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1345 }
1346
Siena Richard7c2db772016-12-21 11:32:34 -08001347 switch (usecase->type) {
1348 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301349 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001350 ALOGD("%s: usecase->out_snd_device %s",
1351 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1352 /* check for out combo device */
1353 if (platform_split_snd_device(adev->platform,
1354 usecase->out_snd_device,
1355 &num_devices, new_snd_devices)) {
1356 new_snd_devices[0] = usecase->out_snd_device;
1357 num_devices = 1;
1358 }
1359 break;
1360 case PCM_CAPTURE:
1361 ALOGD("%s: usecase->in_snd_device %s",
1362 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301363 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1364 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1365 }
Siena Richard7c2db772016-12-21 11:32:34 -08001366 /* check for in combo device */
1367 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301368 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001369 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301370 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001371 num_devices = 1;
1372 }
1373 break;
1374 default:
1375 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1376 rc = 0;
1377 break;
1378 }
1379
1380 for (i = 0; i < num_devices; i++) {
1381 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1382 if (rc)
1383 break;
1384 }
1385
1386 return rc;
1387}
1388
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301389int read_line_from_file(const char *path, char *buf, size_t count)
1390{
1391 char * fgets_ret;
1392 FILE * fd;
1393 int rv;
1394
1395 fd = fopen(path, "r");
1396 if (fd == NULL)
1397 return -1;
1398
1399 fgets_ret = fgets(buf, (int)count, fd);
1400 if (NULL != fgets_ret) {
1401 rv = (int)strlen(buf);
1402 } else {
1403 rv = ferror(fd);
1404 }
1405 fclose(fd);
1406
1407 return rv;
1408}
1409
Ashish Jainf1eaa582016-05-23 20:54:24 +05301410/*Translates ALSA formats to AOSP PCM formats*/
1411audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1412{
1413 audio_format_t format;
1414
1415 switch(alsa_format) {
1416 case SNDRV_PCM_FORMAT_S16_LE:
1417 format = AUDIO_FORMAT_PCM_16_BIT;
1418 break;
1419 case SNDRV_PCM_FORMAT_S24_3LE:
1420 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1421 break;
1422 case SNDRV_PCM_FORMAT_S24_LE:
1423 format = AUDIO_FORMAT_PCM_8_24_BIT;
1424 break;
1425 case SNDRV_PCM_FORMAT_S32_LE:
1426 format = AUDIO_FORMAT_PCM_32_BIT;
1427 break;
1428 default:
1429 ALOGW("Incorrect ALSA format");
1430 format = AUDIO_FORMAT_INVALID;
1431 }
1432 return format;
1433}
1434
1435/*Translates hal format (AOSP) to alsa formats*/
1436uint32_t hal_format_to_alsa(audio_format_t hal_format)
1437{
1438 uint32_t alsa_format;
1439
1440 switch (hal_format) {
1441 case AUDIO_FORMAT_PCM_32_BIT: {
1442 if (platform_supports_true_32bit())
1443 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1444 else
1445 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1446 }
1447 break;
1448 case AUDIO_FORMAT_PCM_8_BIT:
1449 alsa_format = SNDRV_PCM_FORMAT_S8;
1450 break;
1451 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1452 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1453 break;
1454 case AUDIO_FORMAT_PCM_8_24_BIT: {
1455 if (platform_supports_true_32bit())
1456 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1457 else
1458 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1459 }
1460 break;
1461 case AUDIO_FORMAT_PCM_FLOAT:
1462 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1463 break;
1464 default:
1465 case AUDIO_FORMAT_PCM_16_BIT:
1466 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1467 break;
1468 }
1469 return alsa_format;
1470}
1471
Ashish Jain83a6cc22016-06-28 14:34:17 +05301472/*Translates PCM formats to AOSP formats*/
1473audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1474{
1475 audio_format_t format = AUDIO_FORMAT_INVALID;
1476
1477 switch(pcm_format) {
1478 case PCM_FORMAT_S16_LE:
1479 format = AUDIO_FORMAT_PCM_16_BIT;
1480 break;
1481 case PCM_FORMAT_S24_3LE:
1482 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1483 break;
1484 case PCM_FORMAT_S24_LE:
1485 format = AUDIO_FORMAT_PCM_8_24_BIT;
1486 break;
1487 case PCM_FORMAT_S32_LE:
1488 format = AUDIO_FORMAT_PCM_32_BIT;
1489 break;
1490 default:
1491 ALOGW("Incorrect PCM format");
1492 format = AUDIO_FORMAT_INVALID;
1493 }
1494 return format;
1495}
1496
1497/*Translates hal format (AOSP) to alsa formats*/
1498uint32_t hal_format_to_pcm(audio_format_t hal_format)
1499{
1500 uint32_t pcm_format;
1501
1502 switch (hal_format) {
1503 case AUDIO_FORMAT_PCM_32_BIT:
1504 case AUDIO_FORMAT_PCM_8_24_BIT:
1505 case AUDIO_FORMAT_PCM_FLOAT: {
1506 if (platform_supports_true_32bit())
1507 pcm_format = PCM_FORMAT_S32_LE;
1508 else
1509 pcm_format = PCM_FORMAT_S24_3LE;
1510 }
1511 break;
1512 case AUDIO_FORMAT_PCM_8_BIT:
1513 pcm_format = PCM_FORMAT_S8;
1514 break;
1515 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1516 pcm_format = PCM_FORMAT_S24_3LE;
1517 break;
1518 default:
1519 case AUDIO_FORMAT_PCM_16_BIT:
1520 pcm_format = PCM_FORMAT_S16_LE;
1521 break;
1522 }
1523 return pcm_format;
1524}
1525
Ashish Jainf1eaa582016-05-23 20:54:24 +05301526uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1527 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301528 uint32_t noOfChannels,
1529 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301530{
1531 uint32_t fragment_size = 0;
1532 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1533
Deeraj Soman65358ab2019-02-07 15:40:49 +05301534 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1535 pcm_offload_time = duration_ms;
1536
Ashish Jainf1eaa582016-05-23 20:54:24 +05301537 fragment_size = (pcm_offload_time
1538 * sample_rate
1539 * bytes_per_sample
1540 * noOfChannels)/1000;
1541 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1542 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1543 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1544 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1545 /*To have same PCM samples for all channels, the buffer size requires to
1546 *be multiple of (number of channels * bytes per sample)
1547 *For writes to succeed, the buffer must be written at address which is multiple of 32
1548 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001549 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301550
1551 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1552 return fragment_size;
1553}
1554
1555/* Calculates the fragment size required to configure compress session.
1556 * Based on the alsa format selected, decide if conversion is needed in
1557
1558 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1559 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1560 */
1561void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1562{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301563 audio_format_t dst_format = out->hal_op_format;
1564 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301565 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1566 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1567
1568 out->compr_config.fragment_size =
1569 get_alsa_fragment_size(hal_op_bytes_per_sample,
1570 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301571 popcount(out->channel_mask),
1572 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301573
1574 if ((src_format != dst_format) &&
1575 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1576
Ashish Jain83a6cc22016-06-28 14:34:17 +05301577 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301578 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1579 hal_op_bytes_per_sample);
1580 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301581 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301582 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301583 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301584 }
1585}
1586
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301587/* converts pcm format 24_8 to 8_24 inplace */
1588size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1589{
1590 size_t i = 0;
1591 int *int_buf_stream = buf;
1592
1593 if ((bytes % 4) != 0) {
1594 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1595 return -EINVAL;
1596 }
1597
1598 for (; i < (bytes / 4); i++)
1599 int_buf_stream[i] >>= 8;
1600
1601 return bytes;
1602}
1603
1604int get_snd_codec_id(audio_format_t format)
1605{
1606 int id = 0;
1607
1608 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1609 case AUDIO_FORMAT_MP3:
1610 id = SND_AUDIOCODEC_MP3;
1611 break;
1612 case AUDIO_FORMAT_AAC:
1613 id = SND_AUDIOCODEC_AAC;
1614 break;
1615 case AUDIO_FORMAT_AAC_ADTS:
1616 id = SND_AUDIOCODEC_AAC;
1617 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301618 case AUDIO_FORMAT_AAC_LATM:
1619 id = SND_AUDIOCODEC_AAC;
1620 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301621 case AUDIO_FORMAT_PCM:
1622 id = SND_AUDIOCODEC_PCM;
1623 break;
1624 case AUDIO_FORMAT_FLAC:
1625 id = SND_AUDIOCODEC_FLAC;
1626 break;
1627 case AUDIO_FORMAT_ALAC:
1628 id = SND_AUDIOCODEC_ALAC;
1629 break;
1630 case AUDIO_FORMAT_APE:
1631 id = SND_AUDIOCODEC_APE;
1632 break;
1633 case AUDIO_FORMAT_VORBIS:
1634 id = SND_AUDIOCODEC_VORBIS;
1635 break;
1636 case AUDIO_FORMAT_WMA:
1637 id = SND_AUDIOCODEC_WMA;
1638 break;
1639 case AUDIO_FORMAT_WMA_PRO:
1640 id = SND_AUDIOCODEC_WMA_PRO;
1641 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301642 case AUDIO_FORMAT_MP2:
1643 id = SND_AUDIOCODEC_MP2;
1644 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301645 case AUDIO_FORMAT_AC3:
1646 id = SND_AUDIOCODEC_AC3;
1647 break;
1648 case AUDIO_FORMAT_E_AC3:
1649 case AUDIO_FORMAT_E_AC3_JOC:
1650 id = SND_AUDIOCODEC_EAC3;
1651 break;
1652 case AUDIO_FORMAT_DTS:
1653 case AUDIO_FORMAT_DTS_HD:
1654 id = SND_AUDIOCODEC_DTS;
1655 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001656 case AUDIO_FORMAT_DOLBY_TRUEHD:
1657 id = SND_AUDIOCODEC_TRUEHD;
1658 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001659 case AUDIO_FORMAT_IEC61937:
1660 id = SND_AUDIOCODEC_IEC61937;
1661 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301662 case AUDIO_FORMAT_DSD:
1663 id = SND_AUDIOCODEC_DSD;
1664 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301665 case AUDIO_FORMAT_APTX:
1666 id = SND_AUDIOCODEC_APTX;
1667 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301668 default:
1669 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1670 }
1671
1672 return id;
1673}
1674
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001675void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1676 struct audio_usecase *usecase)
1677{
1678 int type = usecase->type;
1679
Dhananjay Kumar14245982017-01-16 20:21:00 +05301680 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301681 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001682 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301683 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301684 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001685 usecase->stream.in->app_type_cfg.app_type);
1686 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1687 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301688 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001689 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001690 } else {
1691 /* No need to send audio calibration for voice and voip call usecases */
1692 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1693 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001694 }
1695}
1696
Ben Rombergera04fabc2014-11-14 12:16:03 -08001697// Base64 Encode and Decode
1698// Not all features supported. This must be used only with following conditions.
1699// Decode Modes: Support with and without padding
1700// CRLF not handling. So no CRLF in string to decode.
1701// Encode Modes: Supports only padding
1702int b64decode(char *inp, int ilen, uint8_t* outp)
1703{
1704 int i, j, k, ii, num;
1705 int rem, pcnt;
1706 uint32_t res=0;
1707 uint8_t getIndex[MAX_BASEINDEX_LEN];
1708 uint8_t tmp, cflag;
1709
1710 if(inp == NULL || outp == NULL || ilen <= 0) {
1711 ALOGE("[%s] received NULL pointer or zero length",__func__);
1712 return -1;
1713 }
1714
1715 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1716 for(i=0;i<BASE_TABLE_SIZE;i++) {
1717 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1718 }
1719 getIndex[(uint8_t)'=']=0;
1720
1721 j=0;k=0;
1722 num = ilen/4;
1723 rem = ilen%4;
1724 if(rem==0)
1725 num = num-1;
1726 cflag=0;
1727 for(i=0; i<num; i++) {
1728 res=0;
1729 for(ii=0;ii<4;ii++) {
1730 res = res << 6;
1731 tmp = getIndex[(uint8_t)inp[j++]];
1732 res = res | tmp;
1733 cflag = cflag | tmp;
1734 }
1735 outp[k++] = (res >> 16)&0xFF;
1736 outp[k++] = (res >> 8)&0xFF;
1737 outp[k++] = res & 0xFF;
1738 }
1739
1740 // Handle last bytes special
1741 pcnt=0;
1742 if(rem == 0) {
1743 //With padding or full data
1744 res = 0;
1745 for(ii=0;ii<4;ii++) {
1746 if(inp[j] == '=')
1747 pcnt++;
1748 res = res << 6;
1749 tmp = getIndex[(uint8_t)inp[j++]];
1750 res = res | tmp;
1751 cflag = cflag | tmp;
1752 }
1753 outp[k++] = res >> 16;
1754 if(pcnt == 2)
1755 goto done;
1756 outp[k++] = (res>>8)&0xFF;
1757 if(pcnt == 1)
1758 goto done;
1759 outp[k++] = res&0xFF;
1760 } else {
1761 //without padding
1762 res = 0;
1763 for(i=0;i<rem;i++) {
1764 res = res << 6;
1765 tmp = getIndex[(uint8_t)inp[j++]];
1766 res = res | tmp;
1767 cflag = cflag | tmp;
1768 }
1769 for(i=rem;i<4;i++) {
1770 res = res << 6;
1771 pcnt++;
1772 }
1773 outp[k++] = res >> 16;
1774 if(pcnt == 2)
1775 goto done;
1776 outp[k++] = (res>>8)&0xFF;
1777 if(pcnt == 1)
1778 goto done;
1779 outp[k++] = res&0xFF;
1780 }
1781done:
1782 if(cflag == 0xFF) {
1783 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1784 __func__, inp);
1785 return 0;
1786 }
1787 return k;
1788}
1789
1790int b64encode(uint8_t *inp, int ilen, char* outp)
1791{
1792 int i,j,k, num;
1793 int rem=0;
1794 uint32_t res=0;
1795
1796 if(inp == NULL || outp == NULL || ilen<=0) {
1797 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1798 return -1;
1799 }
1800
1801 num = ilen/3;
1802 rem = ilen%3;
1803 j=0;k=0;
1804 for(i=0; i<num; i++) {
1805 //prepare index
1806 res = inp[j++]<<16;
1807 res = res | inp[j++]<<8;
1808 res = res | inp[j++];
1809 //get output map from index
1810 outp[k++] = (char) bTable[(res>>18)&0x3F];
1811 outp[k++] = (char) bTable[(res>>12)&0x3F];
1812 outp[k++] = (char) bTable[(res>>6)&0x3F];
1813 outp[k++] = (char) bTable[res&0x3F];
1814 }
1815
1816 switch(rem) {
1817 case 1:
1818 res = inp[j++]<<16;
1819 outp[k++] = (char) bTable[res>>18];
1820 outp[k++] = (char) bTable[(res>>12)&0x3F];
1821 //outp[k++] = '=';
1822 //outp[k++] = '=';
1823 break;
1824 case 2:
1825 res = inp[j++]<<16;
1826 res = res | inp[j++]<<8;
1827 outp[k++] = (char) bTable[res>>18];
1828 outp[k++] = (char) bTable[(res>>12)&0x3F];
1829 outp[k++] = (char) bTable[(res>>6)&0x3F];
1830 //outp[k++] = '=';
1831 break;
1832 default:
1833 break;
1834 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001835 outp[k] = '\0';
1836 return k;
1837}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301838
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301839
1840int audio_extn_utils_get_codec_version(const char *snd_card_name,
1841 int card_num,
1842 char *codec_version)
1843{
1844 char procfs_path[50];
1845 FILE *fp;
1846
1847 if (strstr(snd_card_name, "tasha")) {
1848 snprintf(procfs_path, sizeof(procfs_path),
1849 "/proc/asound/card%d/codecs/tasha/version", card_num);
1850 if ((fp = fopen(procfs_path, "r")) != NULL) {
1851 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1852 fclose(fp);
1853 } else {
1854 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1855 return -ENOENT;
1856 }
1857 ALOGD("%s: codec version %s", __func__, codec_version);
1858 }
1859
1860 return 0;
1861}
1862
1863
Ashish Jain81eb2a82015-05-13 10:52:34 +05301864#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1865
1866void get_default_compressed_channel_status(
1867 unsigned char *channel_status)
1868{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301869 memset(channel_status,0,24);
1870
1871 /* block start bit in preamble bit 3 */
1872 channel_status[0] |= PROFESSIONAL;
1873 //compre out
1874 channel_status[0] |= NON_LPCM;
1875 // sample rate; fixed 48K for default/transcode
1876 channel_status[3] |= SR_48000;
1877}
1878
Ashish Jain81eb2a82015-05-13 10:52:34 +05301879int32_t get_compressed_channel_status(void *audio_stream_data,
1880 uint32_t audio_frame_size,
1881 unsigned char *channel_status,
1882 enum audio_parser_code_type codec_type)
1883 // codec_type - AUDIO_PARSER_CODEC_AC3
1884 // - AUDIO_PARSER_CODEC_DTS
1885{
1886 unsigned char *stream;
1887 int ret = 0;
1888 stream = (unsigned char *)audio_stream_data;
1889
1890 if (audio_stream_data == NULL || audio_frame_size == 0) {
1891 ALOGW("no buffer to get channel status, return default for compress");
1892 get_default_compressed_channel_status(channel_status);
1893 return ret;
1894 }
1895
1896 memset(channel_status,0,24);
1897 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1898 {
1899 ALOGE("init audio parser failed");
1900 return -1;
1901 }
1902 ret = get_channel_status(channel_status, codec_type);
1903 return ret;
1904
1905}
1906
Ashish Jain81eb2a82015-05-13 10:52:34 +05301907void get_lpcm_channel_status(uint32_t sampleRate,
1908 unsigned char *channel_status)
1909{
1910 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301911 memset(channel_status,0,24);
1912 /* block start bit in preamble bit 3 */
1913 channel_status[0] |= PROFESSIONAL;
1914 //LPCM OUT
1915 channel_status[0] &= ~NON_LPCM;
1916
1917 switch (sampleRate) {
1918 case 8000:
1919 case 11025:
1920 case 12000:
1921 case 16000:
1922 case 22050:
1923 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301924 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301925 case 24000:
1926 channel_status[3] |= SR_24000;
1927 break;
1928 case 32000:
1929 channel_status[3] |= SR_32000;
1930 break;
1931 case 44100:
1932 channel_status[3] |= SR_44100;
1933 break;
1934 case 48000:
1935 channel_status[3] |= SR_48000;
1936 break;
1937 case 88200:
1938 channel_status[3] |= SR_88200;
1939 break;
1940 case 96000:
1941 channel_status[3] |= SR_96000;
1942 break;
1943 case 176400:
1944 channel_status[3] |= SR_176400;
1945 break;
1946 case 192000:
1947 channel_status[3] |= SR_192000;
1948 break;
1949 default:
1950 ALOGV("Invalid sample_rate %u\n", sampleRate);
1951 status = -1;
1952 break;
1953 }
1954}
1955
1956void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1957{
1958 unsigned char channel_status[24]={0};
1959 struct snd_aes_iec958 iec958;
1960 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1961 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301962 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001963
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301964 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301965 /*TODO:Extend code to support DTS passthrough*/
1966 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07001967 audio_extn_passthru_is_passthrough_stream(out) &&
1968 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301969 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001970 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301971 /*set channel status bit for LPCM*/
1972 get_lpcm_channel_status(out->sample_rate, channel_status);
1973 }
1974
1975 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1976 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1977 if (!ctl) {
1978 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1979 __func__, mixer_ctl_name);
1980 return;
1981 }
1982 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1983 ALOGE("%s: Could not set channel status for ext HDMI ",
1984 __func__);
1985 return;
1986 }
1987
1988}
1989#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301990
1991int audio_extn_utils_get_avt_device_drift(
1992 struct audio_usecase *usecase,
1993 struct audio_avt_device_drift_param *drift_param)
1994{
1995 int ret = 0, count = 0;
1996 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301997 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301998 struct mixer_ctl *ctl = NULL;
1999 struct audio_avt_device_drift_stats drift_stats;
2000 struct audio_device *adev = NULL;
2001
2002 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302003 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2004 if (!backend) {
2005 ALOGE("%s: Unsupported device %d", __func__,
2006 usecase->stream.out->devices);
2007 ret = -EINVAL;
2008 goto done;
2009 }
2010 strlcpy(avt_device_drift_mixer_ctl_name,
2011 backend,
2012 MIXER_PATH_MAX_LENGTH);
2013
2014 count = strlen(backend);
2015 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2016 strlcat(&avt_device_drift_mixer_ctl_name[count],
2017 " DRIFT",
2018 MIXER_PATH_MAX_LENGTH - count);
2019 } else {
2020 ret = -EINVAL;
2021 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302022 }
2023 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302024 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302025 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302026 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302027 }
2028
Naresh Tanniru6160c712017-04-17 15:43:48 +05302029 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302030 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2031 if (!ctl) {
2032 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2033 __func__, avt_device_drift_mixer_ctl_name);
2034
2035 ret = -EINVAL;
2036 goto done;
2037 }
2038
2039 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2040 avt_device_drift_mixer_ctl_name);
2041
2042 mixer_ctl_update(ctl);
2043 count = mixer_ctl_get_num_values(ctl);
2044 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2045 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2046 __func__);
2047
2048 ret = -EINVAL;
2049 goto done;
2050 }
2051
2052 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2053 if (ret != 0) {
2054 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2055 __func__);
2056
2057 ret = -EINVAL;
2058 goto done;
2059 }
2060 memcpy(drift_param, &drift_stats.drift_param,
2061 sizeof(struct audio_avt_device_drift_param));
2062done:
2063 return ret;
2064}
Manish Dewangan07de2142017-02-27 19:27:20 +05302065
2066#ifdef SNDRV_COMPRESS_PATH_DELAY
2067int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2068{
2069 int ret = -EINVAL;
2070 struct snd_compr_metadata metadata;
2071 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2072
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002073 /* override the latency for pcm offload use case */
2074 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2075 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2076 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2077 }
2078
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002079 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302080 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2081 if (!(is_offload_usecase(out->usecase))) {
2082 ALOGE("%s:: not supported for non offload session", __func__);
2083 goto exit;
2084 }
2085
2086 if (!out->compr) {
2087 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2088 __func__);
2089 goto exit;
2090 }
2091
2092 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2093 ret = compress_get_metadata(out->compr, &metadata);
2094 if(ret) {
2095 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2096 goto exit;
2097 }
2098 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2099 } else {
2100 ALOGD("%s:: Using Fix DSP delay",__func__);
2101 }
2102
2103exit:
2104 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2105 return delay_ms;
2106}
2107#else
2108int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2109{
2110 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2111}
2112#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302113
2114#ifdef SNDRV_COMPRESS_RENDER_MODE
2115int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2116{
2117 struct snd_compr_metadata metadata;
2118 int ret = -EINVAL;
2119
2120 if (!(is_offload_usecase(out->usecase))) {
2121 ALOGE("%s:: not supported for non offload session", __func__);
2122 goto exit;
2123 }
2124
2125 if (!out->compr) {
2126 ALOGD("%s:: Invalid compress handle",
2127 __func__);
2128 goto exit;
2129 }
2130
2131 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2132
2133 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2134 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2135 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2136 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2137 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2138 } else {
2139 ret = 0;
2140 goto exit;
2141 }
2142 ret = compress_set_metadata(out->compr, &metadata);
2143 if(ret) {
2144 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2145 }
2146exit:
2147 return ret;
2148}
2149#else
2150int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2151{
2152 ALOGD("%s:: configuring render mode not supported", __func__);
2153 return 0;
2154}
2155#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302156
2157#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2158int audio_extn_utils_compress_set_clk_rec_mode(
2159 struct audio_usecase *usecase)
2160{
2161 struct snd_compr_metadata metadata;
2162 struct stream_out *out = NULL;
2163 int ret = -EINVAL;
2164
Naresh Tanniru7586e292017-04-13 10:38:13 +05302165 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302166 ALOGE("%s:: Invalid use case", __func__);
2167 goto exit;
2168 }
2169
2170 out = usecase->stream.out;
2171 if (!out) {
2172 ALOGE("%s:: invalid stream", __func__);
2173 goto exit;
2174 }
2175
2176 if (!is_offload_usecase(out->usecase)) {
2177 ALOGE("%s:: not supported for non offload session", __func__);
2178 goto exit;
2179 }
2180
2181 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2182 ALOGD("%s:: clk recovery is only supported in STC render mode",
2183 __func__);
2184 ret = 0;
2185 goto exit;
2186 }
2187
2188 if (!out->compr) {
2189 ALOGD("%s:: Invalid compress handle",
2190 __func__);
2191 goto exit;
2192 }
2193 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2194 switch(usecase->out_snd_device) {
2195 case SND_DEVICE_OUT_HDMI:
2196 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2197 case SND_DEVICE_OUT_DISPLAY_PORT:
2198 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2199 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2200 break;
2201 default:
2202 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2203 break;
2204 }
2205
2206 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2207
2208 ret = compress_set_metadata(out->compr, &metadata);
2209 if(ret) {
2210 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2211 }
2212
2213exit:
2214 return ret;
2215}
2216#else
2217int audio_extn_utils_compress_set_clk_rec_mode(
2218 struct audio_usecase *usecase __unused)
2219{
2220 ALOGD("%s:: configuring render mode not supported", __func__);
2221 return 0;
2222}
2223#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302224
2225#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2226int audio_extn_utils_compress_set_render_window(
2227 struct stream_out *out,
2228 struct audio_out_render_window_param *render_window)
2229{
2230 struct snd_compr_metadata metadata;
2231 int ret = -EINVAL;
2232
Manish Dewangan27346042017-03-01 12:56:12 +05302233 if(render_window == NULL) {
2234 ALOGE("%s:: Invalid render_window", __func__);
2235 goto exit;
2236 }
2237
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002238 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2239 __func__,render_window->render_ws, render_window->render_we);
2240
Manish Dewangan27346042017-03-01 12:56:12 +05302241 if (!is_offload_usecase(out->usecase)) {
2242 ALOGE("%s:: not supported for non offload session", __func__);
2243 goto exit;
2244 }
2245
Naresh Tanniru6160c712017-04-17 15:43:48 +05302246 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2247 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302248 ALOGD("%s:: only supported in timestamp mode, current "
2249 "render mode mode %d", __func__, out->render_mode);
2250 goto exit;
2251 }
2252
2253 if (!out->compr) {
2254 ALOGW("%s:: offload session not yet opened,"
2255 "render window will be configure later", __func__);
2256 /* store render window to reconfigure in start_output_stream() */
2257 goto exit;
2258 }
2259
2260 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2261 /*render window start value */
2262 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2263 metadata.value[1] = \
2264 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2265 /*render window end value */
2266 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2267 metadata.value[3] = \
2268 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2269
2270 ret = compress_set_metadata(out->compr, &metadata);
2271 if(ret) {
2272 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2273 }
2274
2275exit:
2276 return ret;
2277}
2278#else
2279int audio_extn_utils_compress_set_render_window(
2280 struct stream_out *out __unused,
2281 struct audio_out_render_window_param *render_window __unused)
2282{
2283 ALOGD("%s:: configuring render window not supported", __func__);
2284 return 0;
2285}
2286#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302287
2288#ifdef SNDRV_COMPRESS_START_DELAY
2289int audio_extn_utils_compress_set_start_delay(
2290 struct stream_out *out,
2291 struct audio_out_start_delay_param *delay_param)
2292{
2293 struct snd_compr_metadata metadata;
2294 int ret = -EINVAL;
2295
2296 if(delay_param == NULL) {
2297 ALOGE("%s:: Invalid delay_param", __func__);
2298 goto exit;
2299 }
2300
2301 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2302 delay_param->start_delay);
2303
2304 if (!is_offload_usecase(out->usecase)) {
2305 ALOGE("%s:: not supported for non offload session", __func__);
2306 goto exit;
2307 }
2308
Naresh Tanniru6160c712017-04-17 15:43:48 +05302309 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2310 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302311 ALOGD("%s:: only supported in timestamp mode, current "
2312 "render mode mode %d", __func__, out->render_mode);
2313 goto exit;
2314 }
2315
2316 if (!out->compr) {
2317 ALOGW("%s:: offload session not yet opened,"
2318 "start delay will be configure later", __func__);
2319 goto exit;
2320 }
2321
2322 metadata.key = SNDRV_COMPRESS_START_DELAY;
2323 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2324 metadata.value[1] = \
2325 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2326
2327 ret = compress_set_metadata(out->compr, &metadata);
2328 if(ret) {
2329 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2330 }
2331
2332exit:
2333 return ret;
2334}
2335#else
2336int audio_extn_utils_compress_set_start_delay(
2337 struct stream_out *out __unused,
2338 struct audio_out_start_delay_param *delay_param __unused)
2339{
2340 ALOGD("%s:: configuring render window not supported", __func__);
2341 return 0;
2342}
2343#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002344
Surendar Karka287348c2019-04-10 18:31:46 +05302345#ifdef SNDRV_COMPRESS_DSP_POSITION
2346int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2347 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2348{
2349 int ret = -EINVAL;
2350 uint64_t *val = NULL;
2351 uint64_t time = 0;
2352 struct snd_compr_metadata metadata;
2353
2354 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2355 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2356 metadata.value[0] = clock_id;
2357 ret = compress_get_metadata(out->compr, &metadata);
2358 if (ret) {
2359 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2360 ret = -errno;
2361 goto exit;
2362 }
2363 val = (uint64_t *)&metadata.value[1];
2364 *frames = *val;
2365 time = *(val + 1);
2366 timestamp->tv_sec = time / 1000000;
2367 timestamp->tv_nsec = (time % 1000000)*1000;
2368
2369exit:
2370 return ret;
2371}
2372#else
2373int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2374 uint64_t *frames __unused, struct timespec *timestamp __unused,
2375 int32_t clock_id __unused)
2376{
2377 ALOGD("%s:: dsp presentation position not supported", __func__);
2378 return 0;
2379
2380}
2381#endif
2382
2383#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2384int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2385 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2386{
2387 int ret = -EINVAL;
2388 uint64_t time = 0;
2389 struct snd_pcm_prsnt_position prsnt_position;
2390
2391 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2392 prsnt_position.clock_id = clock_id;
2393 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2394 if (ret) {
2395 ALOGE("%s::error %d", __func__, ret);
2396 ret = -EIO;
2397 goto exit;
2398 }
2399
2400 *frames = prsnt_position.frames;
2401 time = prsnt_position.timestamp;
2402 timestamp->tv_sec = time / 1000000;
2403 timestamp->tv_nsec = (time % 1000000)*1000;
2404
2405exit:
2406 return ret;
2407}
2408#else
2409int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2410 uint64_t *frames __unused, struct timespec *timestamp __unused,
2411 int32_t clock_id __unused)
2412{
2413 ALOGD("%s:: dsp presentation position not supported", __func__);
2414 return 0;
2415
2416}
2417#endif
2418
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002419#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302420#define RETRY_US 1000000
2421#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002422#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2423#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2424
2425#ifdef LINUX_ENABLED
2426static const char *kConfigLocationList[] =
2427 {"/etc"};
2428#else
2429static const char *kConfigLocationList[] =
2430 {"/vendor/etc"};
2431#endif
2432static const int kConfigLocationListSize =
2433 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2434
2435bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2436{
2437 char full_config_path[MIXER_PATH_MAX_LENGTH];
2438 for (int i = 0; i < kConfigLocationListSize; i++) {
2439 snprintf(full_config_path,
2440 MIXER_PATH_MAX_LENGTH,
2441 "%s/%s",
2442 kConfigLocationList[i],
2443 file_name);
2444 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002445 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002446 return true;
2447 }
2448 }
2449 return false;
2450}
2451
2452/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2453int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2454{
2455 if (NULL == snd_card_name) {
2456 return -1;
2457 }
2458
2459 struct snd_card_split *snd_split_handle = NULL;
2460 int ret = 0;
2461 audio_extn_set_snd_card_split(snd_card_name);
2462 snd_split_handle = audio_extn_get_snd_card_split();
2463
2464 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2465 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2466 snd_split_handle->form_factor);
2467
2468 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2469 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2470 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2471 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2472
2473 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2474 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2475 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2476 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2477 }
2478 }
2479
2480 return ret;
2481}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002482
2483int audio_extn_utils_get_snd_card_num()
2484{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302485 int snd_card_num = 0;
2486 struct mixer *mixer = NULL;
2487
2488 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2489 if (mixer)
2490 mixer_close(mixer);
2491 return snd_card_num;
2492}
2493
2494int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2495{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002496
2497 void *hw_info = NULL;
2498 struct mixer *mixer = NULL;
2499 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002500 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002501 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002502 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002503
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302504 if (!mixer_handle) {
2505 ALOGE("invalid mixer handle");
2506 return -1;
2507 }
2508 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302509 /*
mpangfaa7bae2019-01-15 16:38:13 +08002510 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302511 * sleep for 1 sec and try detections with sound card 0 again.
2512 * If sound card gets detected, check if it is relevant, if not check with the
2513 * other sound cards. To ensure that the irrelevant sound card is not check again,
2514 * we maintain it in min_snd_card_num.
2515 */
2516 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002517 snd_card_num = 0;
2518 while (snd_card_num < MAX_SND_CARD) {
2519 if (snd_card_detection_info[snd_card_num] == 0) {
2520 mixer = mixer_open(snd_card_num);
2521 if (!mixer)
2522 snd_card_num++;
2523 else
2524 break;
2525 } else
2526 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002527 }
2528
2529 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302530 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302531 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002532 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302533 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002534 }
2535
2536 snd_card_name = strdup(mixer_get_name(mixer));
2537 if (!snd_card_name) {
2538 ALOGE("failed to allocate memory for snd_card_name\n");
2539 mixer_close(mixer);
2540 return -1;
2541 }
2542 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002543 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002544 hw_info = hw_info_init(snd_card_name);
2545 if (hw_info) {
2546 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2547 break;
2548 }
mpangfaa7bae2019-01-15 16:38:13 +08002549 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002550
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302551 free(snd_card_name);
2552 snd_card_name = NULL;
2553
2554 mixer_close(mixer);
2555 mixer = NULL;
2556 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002557 if (snd_card_name)
2558 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302559
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302560 if (hw_info)
2561 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002562
mpangfaa7bae2019-01-15 16:38:13 +08002563 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002564 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2565 return -1;
2566 }
2567
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302568 if (mixer)
2569 *mixer_handle = mixer;
2570
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002571 return snd_card_num;
2572}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302573
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302574void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2575{
2576 if (mixer)
2577 mixer_close(mixer);
2578}
2579
Naresh Tanniru6160c712017-04-17 15:43:48 +05302580#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2581int audio_extn_utils_compress_enable_drift_correction(
2582 struct stream_out *out,
2583 struct audio_out_enable_drift_correction *drift)
2584{
2585 struct snd_compr_metadata metadata;
2586 int ret = -EINVAL;
2587
2588 if(drift == NULL) {
2589 ALOGE("%s:: Invalid param", __func__);
2590 goto exit;
2591 }
2592
2593 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2594
2595 if (!is_offload_usecase(out->usecase)) {
2596 ALOGE("%s:: not supported for non offload session", __func__);
2597 goto exit;
2598 }
2599
2600 if (!out->compr) {
2601 ALOGW("%s:: offload session not yet opened,"
2602 "start delay will be configure later", __func__);
2603 goto exit;
2604 }
2605
2606 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2607 metadata.value[0] = drift->enable;
2608 out->drift_correction_enabled = drift->enable;
2609
2610 ret = compress_set_metadata(out->compr, &metadata);
2611 if(ret) {
2612 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2613 out->drift_correction_enabled = false;
2614 }
2615
2616exit:
2617 return ret;
2618}
2619#else
2620int audio_extn_utils_compress_enable_drift_correction(
2621 struct stream_out *out __unused,
2622 struct audio_out_enable_drift_correction *drift __unused)
2623{
2624 ALOGD("%s:: configuring drift enablement not supported", __func__);
2625 return 0;
2626}
2627#endif
2628
2629#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2630int audio_extn_utils_compress_correct_drift(
2631 struct stream_out *out,
2632 struct audio_out_correct_drift *drift_param)
2633{
2634 struct snd_compr_metadata metadata;
2635 int ret = -EINVAL;
2636
2637 if (drift_param == NULL) {
2638 ALOGE("%s:: Invalid drift_param", __func__);
2639 goto exit;
2640 }
2641
2642 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2643 drift_param->adjust_time);
2644
2645 if (!is_offload_usecase(out->usecase)) {
2646 ALOGE("%s:: not supported for non offload session", __func__);
2647 goto exit;
2648 }
2649
2650 if (!out->compr) {
2651 ALOGW("%s:: offload session not yet opened", __func__);
2652 goto exit;
2653 }
2654
2655 if (!out->drift_correction_enabled) {
2656 ALOGE("%s:: drift correction not enabled", __func__);
2657 goto exit;
2658 }
2659
2660 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2661 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2662 metadata.value[1] = \
2663 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2664
2665 ret = compress_set_metadata(out->compr, &metadata);
2666 if(ret)
2667 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2668exit:
2669 return ret;
2670}
2671#else
2672int audio_extn_utils_compress_correct_drift(
2673 struct stream_out *out __unused,
2674 struct audio_out_correct_drift *drift_param __unused)
2675{
2676 ALOGD("%s:: setting adjust clock not supported", __func__);
2677 return 0;
2678}
2679#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302680
2681int audio_extn_utils_set_channel_map(
2682 struct stream_out *out,
2683 struct audio_out_channel_map_param *channel_map_param)
2684{
2685 int ret = -EINVAL, i = 0;
2686 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2687
2688 if (channel_map_param == NULL) {
2689 ALOGE("%s:: Invalid channel_map", __func__);
2690 goto exit;
2691 }
2692
2693 if (channel_map_param->channels != channels) {
2694 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2695 __func__, channel_map_param->channels, channels);
2696 goto exit;
2697 }
2698
2699 for ( i = 0; i < channels; i++) {
2700 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2701 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2702 }
2703 ret = 0;
2704exit:
2705 return ret;
2706}
Varun Balaraje49253e2017-07-06 19:48:56 +05302707
2708int audio_extn_utils_set_pan_scale_params(
2709 struct stream_out *out,
2710 struct mix_matrix_params *mm_params)
2711{
2712 int ret = -EINVAL, i = 0, j = 0;
2713
Varun Balaraj003ee752017-08-07 17:54:55 +05302714 if (mm_params == NULL || out == NULL) {
2715 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302716 goto exit;
2717 }
2718
2719 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2720 mm_params->num_output_channels <= 0 ||
2721 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2722 mm_params->num_input_channels <= 0)
2723 goto exit;
2724
2725 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2726 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2727 out->pan_scale_params.has_output_channel_map =
2728 mm_params->has_output_channel_map;
2729 for (i = 0; i < mm_params->num_output_channels; i++)
2730 out->pan_scale_params.output_channel_map[i] =
2731 mm_params->output_channel_map[i];
2732
2733 out->pan_scale_params.has_input_channel_map =
2734 mm_params->has_input_channel_map;
2735 for (i = 0; i < mm_params->num_input_channels; i++)
2736 out->pan_scale_params.input_channel_map[i] =
2737 mm_params->input_channel_map[i];
2738
2739 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2740 for (i = 0; i < mm_params->num_output_channels; i++)
2741 for (j = 0; j < mm_params->num_input_channels; j++) {
2742 //Convert the channel coefficient gains in Q14 format
2743 out->pan_scale_params.mixer_coeffs[i][j] =
2744 mm_params->mixer_coeffs[i][j] * (2 << 13);
2745 }
2746
2747 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2748 out->pcm_device_id,
2749 out->pan_scale_params);
2750
2751exit:
2752 return ret;
2753}
2754
2755int audio_extn_utils_set_downmix_params(
2756 struct stream_out *out,
2757 struct mix_matrix_params *mm_params)
2758{
2759 int ret = -EINVAL, i = 0, j = 0;
2760 struct audio_usecase *usecase = NULL;
2761
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002762 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302763 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302764 goto exit;
2765 }
2766
2767 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2768 mm_params->num_output_channels <= 0 ||
2769 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2770 mm_params->num_input_channels <= 0)
2771 goto exit;
2772
2773 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302774 if (!usecase) {
2775 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002776 goto exit;
2777 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302778 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2779 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2780
2781 out->downmix_params.has_output_channel_map =
2782 mm_params->has_output_channel_map;
2783 for (i = 0; i < mm_params->num_output_channels; i++) {
2784 out->downmix_params.output_channel_map[i] =
2785 mm_params->output_channel_map[i];
2786 }
2787
2788 out->downmix_params.has_input_channel_map =
2789 mm_params->has_input_channel_map;
2790 for (i = 0; i < mm_params->num_input_channels; i++)
2791 out->downmix_params.input_channel_map[i] =
2792 mm_params->input_channel_map[i];
2793
2794 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2795 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302796 for (j = 0; j < mm_params->num_input_channels; j++) {
2797 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302798 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302799 mm_params->mixer_coeffs[i][j] * (2 << 13);
2800 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302801
2802 ret = platform_set_stream_downmix_params(out->dev->platform,
2803 out->pcm_device_id,
2804 usecase->out_snd_device,
2805 out->downmix_params);
2806
2807exit:
2808 return ret;
2809}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302810
2811bool audio_extn_utils_is_dolby_format(audio_format_t format)
2812{
2813 if (format == AUDIO_FORMAT_AC3 ||
2814 format == AUDIO_FORMAT_E_AC3 ||
2815 format == AUDIO_FORMAT_E_AC3_JOC)
2816 return true;
2817 else
2818 return false;
2819}
2820
`Deeraj Soman676c2702017-09-18 19:25:53 +05302821int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2822{
2823 int i;
2824 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2825 {"S32_LE", 32},
2826 {"S24_LE", 24},
2827 {"S16_LE", 16}};
2828 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302829
`Deeraj Soman676c2702017-09-18 19:25:53 +05302830 for (i = 0; i < num_configs; i++) {
2831 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2832 return mixer_bitwidth_config[i].value;
2833 }
2834
2835 return -EINVAL;
2836}
2837
2838int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2839{
2840 int i;
2841 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2842 {"KHZ_48", 48000},
2843 {"KHZ_96", 96000},
2844 {"KHZ_144", 144000},
2845 {"KHZ_192", 192000},
2846 {"KHZ_384", 384000},
2847 {"KHZ_44P1", 44100},
2848 {"KHZ_88P2", 88200},
2849 {"KHZ_176P4", 176400},
2850 {"KHZ_352P8", 352800}};
2851 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2852
2853 for (i = 0; i < num_configs; i++) {
2854 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2855 return mixer_samplerate_config[i].value;
2856 }
2857
2858 return -EINVAL;
2859}
2860
2861int audio_extn_utils_get_channels_from_string(const char *id_string)
2862{
2863 int i;
2864 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2865 {"Two", 2},
2866 {"Three",3},
2867 {"Four", 4},
2868 {"Five", 5},
2869 {"Six", 6},
2870 {"Seven", 7},
2871 {"Eight", 8}};
2872 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2873
2874 for (i = 0; i < num_configs; i++) {
2875 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2876 return mixer_channels_config[i].value;
2877 }
2878
2879 return -EINVAL;
2880}
Surendar karka30569792018-05-08 12:02:21 +05302881
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002882void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2883{
2884 audio_extn_dev_arbi_release(snd_device);
2885 audio_extn_sound_trigger_update_device_status(snd_device,
2886 ST_EVENT_SND_DEVICE_FREE);
2887 audio_extn_listen_update_device_status(snd_device,
2888 LISTEN_EVENT_SND_DEVICE_FREE);
2889}
2890
Surendar karka30569792018-05-08 12:02:21 +05302891int audio_extn_utils_get_license_params
2892(
2893const struct audio_device *adev,
2894struct audio_license_params *license_params
2895)
2896{
2897 if(!license_params)
2898 return -EINVAL;
2899 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2900}
2901
Aalique Grahame22e49102018-12-18 14:23:57 -08002902int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2903 int app_type,
2904 int *gain)
2905{
2906 int gain_cfg[4];
2907 const char *mixer_ctl_name = "App Type Gain";
2908 struct mixer_ctl *ctl;
2909 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2910 if (!ctl) {
2911 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2912 mixer_ctl_name);
2913 return -EINVAL;
2914 }
2915 gain_cfg[0] = 0;
2916 gain_cfg[1] = app_type;
2917 gain_cfg[2] = gain[0];
2918 gain_cfg[3] = gain[1];
2919 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2920 return mixer_ctl_set_array(ctl, gain_cfg,
2921 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2922}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002923
2924int audio_extn_utils_is_vendor_enhanced_fwk()
2925{
2926 static int is_running_with_enhanced_fwk = -EINVAL;
2927
2928 if (is_running_with_enhanced_fwk == -EINVAL) {
2929 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
2930 if (vndk_fwk_lib_handle != NULL) {
2931 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
2932 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
2933 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
2934 ALOGW("%s: dlsym failed, defaulting to enhanced_fwk configuration",
2935 __func__);
2936 is_running_with_enhanced_fwk = 1;
2937 } else {
2938 is_running_with_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
2939 }
2940 dlclose(vndk_fwk_lib_handle);
2941 vndk_fwk_lib_handle = NULL;
2942 } else {
2943 ALOGW("%s: VNDK_FWK_LIB not found, setting stock configuration", __func__);
2944 is_running_with_enhanced_fwk = 0;
2945 }
2946 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_running_with_enhanced_fwk);
2947 }
2948
2949 return is_running_with_enhanced_fwk;
2950}
Deeraj Soman14230922019-01-30 16:39:30 +05302951
Deeraj Somanfa377bf2019-02-06 12:57:59 +05302952int audio_extn_utils_get_perf_mode_flag(void)
2953{
2954#ifdef COMPRESSED_PERF_MODE_FLAG
2955 return COMPRESSED_PERF_MODE_FLAG;
2956#else
2957 return 0;
2958#endif
2959}
2960
Deeraj Soman14230922019-01-30 16:39:30 +05302961size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
2962 audio_format_t format,
2963 int channel_count,
2964 int64_t duration_ms,
2965 bool is_low_latency)
2966{
2967 size_t size = 0;
2968 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
2969 uint32_t bytes_per_period_sample = 0;
2970
2971
2972 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
2973 return 0;
2974
2975 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
2976 capture_duration = duration_ms;
2977
2978 size = (sample_rate * capture_duration) / 1000;
2979 if (is_low_latency)
2980 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
2981
2982
2983 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
2984 size *= bytes_per_period_sample;
2985
2986 /* make sure the size is multiple of 32 bytes and additionally multiple of
2987 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
2988 * At 48 kHz mono 16-bit PCM:
2989 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
2990 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
2991 *
2992 * The loop reaches result within 32 iterations, as initial size is
2993 * already a multiple of frame_size
2994 */
2995 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
2996
2997 return size;
2998}