blob: 171f69e532f3a30f5af0dd26e927c933373746d5 [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,
1528 uint32_t noOfChannels)
1529{
1530 uint32_t fragment_size = 0;
1531 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1532
1533 fragment_size = (pcm_offload_time
1534 * sample_rate
1535 * bytes_per_sample
1536 * noOfChannels)/1000;
1537 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1538 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1539 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1540 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1541 /*To have same PCM samples for all channels, the buffer size requires to
1542 *be multiple of (number of channels * bytes per sample)
1543 *For writes to succeed, the buffer must be written at address which is multiple of 32
1544 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001545 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301546
1547 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1548 return fragment_size;
1549}
1550
1551/* Calculates the fragment size required to configure compress session.
1552 * Based on the alsa format selected, decide if conversion is needed in
1553
1554 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1555 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1556 */
1557void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1558{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301559 audio_format_t dst_format = out->hal_op_format;
1560 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301561 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1562 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1563
1564 out->compr_config.fragment_size =
1565 get_alsa_fragment_size(hal_op_bytes_per_sample,
1566 out->sample_rate,
1567 popcount(out->channel_mask));
1568
1569 if ((src_format != dst_format) &&
1570 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1571
Ashish Jain83a6cc22016-06-28 14:34:17 +05301572 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301573 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1574 hal_op_bytes_per_sample);
1575 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301576 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301577 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301578 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301579 }
1580}
1581
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301582/* converts pcm format 24_8 to 8_24 inplace */
1583size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1584{
1585 size_t i = 0;
1586 int *int_buf_stream = buf;
1587
1588 if ((bytes % 4) != 0) {
1589 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1590 return -EINVAL;
1591 }
1592
1593 for (; i < (bytes / 4); i++)
1594 int_buf_stream[i] >>= 8;
1595
1596 return bytes;
1597}
1598
1599int get_snd_codec_id(audio_format_t format)
1600{
1601 int id = 0;
1602
1603 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1604 case AUDIO_FORMAT_MP3:
1605 id = SND_AUDIOCODEC_MP3;
1606 break;
1607 case AUDIO_FORMAT_AAC:
1608 id = SND_AUDIOCODEC_AAC;
1609 break;
1610 case AUDIO_FORMAT_AAC_ADTS:
1611 id = SND_AUDIOCODEC_AAC;
1612 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301613 case AUDIO_FORMAT_AAC_LATM:
1614 id = SND_AUDIOCODEC_AAC;
1615 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301616 case AUDIO_FORMAT_PCM:
1617 id = SND_AUDIOCODEC_PCM;
1618 break;
1619 case AUDIO_FORMAT_FLAC:
1620 id = SND_AUDIOCODEC_FLAC;
1621 break;
1622 case AUDIO_FORMAT_ALAC:
1623 id = SND_AUDIOCODEC_ALAC;
1624 break;
1625 case AUDIO_FORMAT_APE:
1626 id = SND_AUDIOCODEC_APE;
1627 break;
1628 case AUDIO_FORMAT_VORBIS:
1629 id = SND_AUDIOCODEC_VORBIS;
1630 break;
1631 case AUDIO_FORMAT_WMA:
1632 id = SND_AUDIOCODEC_WMA;
1633 break;
1634 case AUDIO_FORMAT_WMA_PRO:
1635 id = SND_AUDIOCODEC_WMA_PRO;
1636 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301637 case AUDIO_FORMAT_MP2:
1638 id = SND_AUDIOCODEC_MP2;
1639 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301640 case AUDIO_FORMAT_AC3:
1641 id = SND_AUDIOCODEC_AC3;
1642 break;
1643 case AUDIO_FORMAT_E_AC3:
1644 case AUDIO_FORMAT_E_AC3_JOC:
1645 id = SND_AUDIOCODEC_EAC3;
1646 break;
1647 case AUDIO_FORMAT_DTS:
1648 case AUDIO_FORMAT_DTS_HD:
1649 id = SND_AUDIOCODEC_DTS;
1650 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001651 case AUDIO_FORMAT_DOLBY_TRUEHD:
1652 id = SND_AUDIOCODEC_TRUEHD;
1653 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001654 case AUDIO_FORMAT_IEC61937:
1655 id = SND_AUDIOCODEC_IEC61937;
1656 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301657 case AUDIO_FORMAT_DSD:
1658 id = SND_AUDIOCODEC_DSD;
1659 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301660 case AUDIO_FORMAT_APTX:
1661 id = SND_AUDIOCODEC_APTX;
1662 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301663 default:
1664 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1665 }
1666
1667 return id;
1668}
1669
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001670void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1671 struct audio_usecase *usecase)
1672{
1673 int type = usecase->type;
1674
Dhananjay Kumar14245982017-01-16 20:21:00 +05301675 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301676 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001677 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301678 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301679 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001680 usecase->stream.in->app_type_cfg.app_type);
1681 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1682 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301683 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001684 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001685 } else {
1686 /* No need to send audio calibration for voice and voip call usecases */
1687 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1688 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001689 }
1690}
1691
Ben Rombergera04fabc2014-11-14 12:16:03 -08001692// Base64 Encode and Decode
1693// Not all features supported. This must be used only with following conditions.
1694// Decode Modes: Support with and without padding
1695// CRLF not handling. So no CRLF in string to decode.
1696// Encode Modes: Supports only padding
1697int b64decode(char *inp, int ilen, uint8_t* outp)
1698{
1699 int i, j, k, ii, num;
1700 int rem, pcnt;
1701 uint32_t res=0;
1702 uint8_t getIndex[MAX_BASEINDEX_LEN];
1703 uint8_t tmp, cflag;
1704
1705 if(inp == NULL || outp == NULL || ilen <= 0) {
1706 ALOGE("[%s] received NULL pointer or zero length",__func__);
1707 return -1;
1708 }
1709
1710 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1711 for(i=0;i<BASE_TABLE_SIZE;i++) {
1712 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1713 }
1714 getIndex[(uint8_t)'=']=0;
1715
1716 j=0;k=0;
1717 num = ilen/4;
1718 rem = ilen%4;
1719 if(rem==0)
1720 num = num-1;
1721 cflag=0;
1722 for(i=0; i<num; i++) {
1723 res=0;
1724 for(ii=0;ii<4;ii++) {
1725 res = res << 6;
1726 tmp = getIndex[(uint8_t)inp[j++]];
1727 res = res | tmp;
1728 cflag = cflag | tmp;
1729 }
1730 outp[k++] = (res >> 16)&0xFF;
1731 outp[k++] = (res >> 8)&0xFF;
1732 outp[k++] = res & 0xFF;
1733 }
1734
1735 // Handle last bytes special
1736 pcnt=0;
1737 if(rem == 0) {
1738 //With padding or full data
1739 res = 0;
1740 for(ii=0;ii<4;ii++) {
1741 if(inp[j] == '=')
1742 pcnt++;
1743 res = res << 6;
1744 tmp = getIndex[(uint8_t)inp[j++]];
1745 res = res | tmp;
1746 cflag = cflag | tmp;
1747 }
1748 outp[k++] = res >> 16;
1749 if(pcnt == 2)
1750 goto done;
1751 outp[k++] = (res>>8)&0xFF;
1752 if(pcnt == 1)
1753 goto done;
1754 outp[k++] = res&0xFF;
1755 } else {
1756 //without padding
1757 res = 0;
1758 for(i=0;i<rem;i++) {
1759 res = res << 6;
1760 tmp = getIndex[(uint8_t)inp[j++]];
1761 res = res | tmp;
1762 cflag = cflag | tmp;
1763 }
1764 for(i=rem;i<4;i++) {
1765 res = res << 6;
1766 pcnt++;
1767 }
1768 outp[k++] = res >> 16;
1769 if(pcnt == 2)
1770 goto done;
1771 outp[k++] = (res>>8)&0xFF;
1772 if(pcnt == 1)
1773 goto done;
1774 outp[k++] = res&0xFF;
1775 }
1776done:
1777 if(cflag == 0xFF) {
1778 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1779 __func__, inp);
1780 return 0;
1781 }
1782 return k;
1783}
1784
1785int b64encode(uint8_t *inp, int ilen, char* outp)
1786{
1787 int i,j,k, num;
1788 int rem=0;
1789 uint32_t res=0;
1790
1791 if(inp == NULL || outp == NULL || ilen<=0) {
1792 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1793 return -1;
1794 }
1795
1796 num = ilen/3;
1797 rem = ilen%3;
1798 j=0;k=0;
1799 for(i=0; i<num; i++) {
1800 //prepare index
1801 res = inp[j++]<<16;
1802 res = res | inp[j++]<<8;
1803 res = res | inp[j++];
1804 //get output map from index
1805 outp[k++] = (char) bTable[(res>>18)&0x3F];
1806 outp[k++] = (char) bTable[(res>>12)&0x3F];
1807 outp[k++] = (char) bTable[(res>>6)&0x3F];
1808 outp[k++] = (char) bTable[res&0x3F];
1809 }
1810
1811 switch(rem) {
1812 case 1:
1813 res = inp[j++]<<16;
1814 outp[k++] = (char) bTable[res>>18];
1815 outp[k++] = (char) bTable[(res>>12)&0x3F];
1816 //outp[k++] = '=';
1817 //outp[k++] = '=';
1818 break;
1819 case 2:
1820 res = inp[j++]<<16;
1821 res = res | inp[j++]<<8;
1822 outp[k++] = (char) bTable[res>>18];
1823 outp[k++] = (char) bTable[(res>>12)&0x3F];
1824 outp[k++] = (char) bTable[(res>>6)&0x3F];
1825 //outp[k++] = '=';
1826 break;
1827 default:
1828 break;
1829 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001830 outp[k] = '\0';
1831 return k;
1832}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301833
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301834
1835int audio_extn_utils_get_codec_version(const char *snd_card_name,
1836 int card_num,
1837 char *codec_version)
1838{
1839 char procfs_path[50];
1840 FILE *fp;
1841
1842 if (strstr(snd_card_name, "tasha")) {
1843 snprintf(procfs_path, sizeof(procfs_path),
1844 "/proc/asound/card%d/codecs/tasha/version", card_num);
1845 if ((fp = fopen(procfs_path, "r")) != NULL) {
1846 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1847 fclose(fp);
1848 } else {
1849 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1850 return -ENOENT;
1851 }
1852 ALOGD("%s: codec version %s", __func__, codec_version);
1853 }
1854
1855 return 0;
1856}
1857
1858
Ashish Jain81eb2a82015-05-13 10:52:34 +05301859#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1860
1861void get_default_compressed_channel_status(
1862 unsigned char *channel_status)
1863{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301864 memset(channel_status,0,24);
1865
1866 /* block start bit in preamble bit 3 */
1867 channel_status[0] |= PROFESSIONAL;
1868 //compre out
1869 channel_status[0] |= NON_LPCM;
1870 // sample rate; fixed 48K for default/transcode
1871 channel_status[3] |= SR_48000;
1872}
1873
Ashish Jain81eb2a82015-05-13 10:52:34 +05301874int32_t get_compressed_channel_status(void *audio_stream_data,
1875 uint32_t audio_frame_size,
1876 unsigned char *channel_status,
1877 enum audio_parser_code_type codec_type)
1878 // codec_type - AUDIO_PARSER_CODEC_AC3
1879 // - AUDIO_PARSER_CODEC_DTS
1880{
1881 unsigned char *stream;
1882 int ret = 0;
1883 stream = (unsigned char *)audio_stream_data;
1884
1885 if (audio_stream_data == NULL || audio_frame_size == 0) {
1886 ALOGW("no buffer to get channel status, return default for compress");
1887 get_default_compressed_channel_status(channel_status);
1888 return ret;
1889 }
1890
1891 memset(channel_status,0,24);
1892 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1893 {
1894 ALOGE("init audio parser failed");
1895 return -1;
1896 }
1897 ret = get_channel_status(channel_status, codec_type);
1898 return ret;
1899
1900}
1901
Ashish Jain81eb2a82015-05-13 10:52:34 +05301902void get_lpcm_channel_status(uint32_t sampleRate,
1903 unsigned char *channel_status)
1904{
1905 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301906 memset(channel_status,0,24);
1907 /* block start bit in preamble bit 3 */
1908 channel_status[0] |= PROFESSIONAL;
1909 //LPCM OUT
1910 channel_status[0] &= ~NON_LPCM;
1911
1912 switch (sampleRate) {
1913 case 8000:
1914 case 11025:
1915 case 12000:
1916 case 16000:
1917 case 22050:
1918 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301919 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301920 case 24000:
1921 channel_status[3] |= SR_24000;
1922 break;
1923 case 32000:
1924 channel_status[3] |= SR_32000;
1925 break;
1926 case 44100:
1927 channel_status[3] |= SR_44100;
1928 break;
1929 case 48000:
1930 channel_status[3] |= SR_48000;
1931 break;
1932 case 88200:
1933 channel_status[3] |= SR_88200;
1934 break;
1935 case 96000:
1936 channel_status[3] |= SR_96000;
1937 break;
1938 case 176400:
1939 channel_status[3] |= SR_176400;
1940 break;
1941 case 192000:
1942 channel_status[3] |= SR_192000;
1943 break;
1944 default:
1945 ALOGV("Invalid sample_rate %u\n", sampleRate);
1946 status = -1;
1947 break;
1948 }
1949}
1950
1951void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1952{
1953 unsigned char channel_status[24]={0};
1954 struct snd_aes_iec958 iec958;
1955 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1956 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301957 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001958
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301959 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301960 /*TODO:Extend code to support DTS passthrough*/
1961 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07001962 audio_extn_passthru_is_passthrough_stream(out) &&
1963 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301964 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07001965 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05301966 /*set channel status bit for LPCM*/
1967 get_lpcm_channel_status(out->sample_rate, channel_status);
1968 }
1969
1970 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1971 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1972 if (!ctl) {
1973 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1974 __func__, mixer_ctl_name);
1975 return;
1976 }
1977 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1978 ALOGE("%s: Could not set channel status for ext HDMI ",
1979 __func__);
1980 return;
1981 }
1982
1983}
1984#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301985
1986int audio_extn_utils_get_avt_device_drift(
1987 struct audio_usecase *usecase,
1988 struct audio_avt_device_drift_param *drift_param)
1989{
1990 int ret = 0, count = 0;
1991 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301992 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301993 struct mixer_ctl *ctl = NULL;
1994 struct audio_avt_device_drift_stats drift_stats;
1995 struct audio_device *adev = NULL;
1996
1997 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301998 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1999 if (!backend) {
2000 ALOGE("%s: Unsupported device %d", __func__,
2001 usecase->stream.out->devices);
2002 ret = -EINVAL;
2003 goto done;
2004 }
2005 strlcpy(avt_device_drift_mixer_ctl_name,
2006 backend,
2007 MIXER_PATH_MAX_LENGTH);
2008
2009 count = strlen(backend);
2010 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2011 strlcat(&avt_device_drift_mixer_ctl_name[count],
2012 " DRIFT",
2013 MIXER_PATH_MAX_LENGTH - count);
2014 } else {
2015 ret = -EINVAL;
2016 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302017 }
2018 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302019 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302020 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302021 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302022 }
2023
Naresh Tanniru6160c712017-04-17 15:43:48 +05302024 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302025 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2026 if (!ctl) {
2027 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2028 __func__, avt_device_drift_mixer_ctl_name);
2029
2030 ret = -EINVAL;
2031 goto done;
2032 }
2033
2034 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2035 avt_device_drift_mixer_ctl_name);
2036
2037 mixer_ctl_update(ctl);
2038 count = mixer_ctl_get_num_values(ctl);
2039 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2040 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2041 __func__);
2042
2043 ret = -EINVAL;
2044 goto done;
2045 }
2046
2047 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2048 if (ret != 0) {
2049 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2050 __func__);
2051
2052 ret = -EINVAL;
2053 goto done;
2054 }
2055 memcpy(drift_param, &drift_stats.drift_param,
2056 sizeof(struct audio_avt_device_drift_param));
2057done:
2058 return ret;
2059}
Manish Dewangan07de2142017-02-27 19:27:20 +05302060
2061#ifdef SNDRV_COMPRESS_PATH_DELAY
2062int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2063{
2064 int ret = -EINVAL;
2065 struct snd_compr_metadata metadata;
2066 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2067
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002068 /* override the latency for pcm offload use case */
2069 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2070 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2071 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2072 }
2073
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002074 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302075 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2076 if (!(is_offload_usecase(out->usecase))) {
2077 ALOGE("%s:: not supported for non offload session", __func__);
2078 goto exit;
2079 }
2080
2081 if (!out->compr) {
2082 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2083 __func__);
2084 goto exit;
2085 }
2086
2087 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2088 ret = compress_get_metadata(out->compr, &metadata);
2089 if(ret) {
2090 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2091 goto exit;
2092 }
2093 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2094 } else {
2095 ALOGD("%s:: Using Fix DSP delay",__func__);
2096 }
2097
2098exit:
2099 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2100 return delay_ms;
2101}
2102#else
2103int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2104{
2105 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2106}
2107#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302108
2109#ifdef SNDRV_COMPRESS_RENDER_MODE
2110int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2111{
2112 struct snd_compr_metadata metadata;
2113 int ret = -EINVAL;
2114
2115 if (!(is_offload_usecase(out->usecase))) {
2116 ALOGE("%s:: not supported for non offload session", __func__);
2117 goto exit;
2118 }
2119
2120 if (!out->compr) {
2121 ALOGD("%s:: Invalid compress handle",
2122 __func__);
2123 goto exit;
2124 }
2125
2126 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2127
2128 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2129 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2130 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2131 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2132 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2133 } else {
2134 ret = 0;
2135 goto exit;
2136 }
2137 ret = compress_set_metadata(out->compr, &metadata);
2138 if(ret) {
2139 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2140 }
2141exit:
2142 return ret;
2143}
2144#else
2145int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2146{
2147 ALOGD("%s:: configuring render mode not supported", __func__);
2148 return 0;
2149}
2150#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302151
2152#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2153int audio_extn_utils_compress_set_clk_rec_mode(
2154 struct audio_usecase *usecase)
2155{
2156 struct snd_compr_metadata metadata;
2157 struct stream_out *out = NULL;
2158 int ret = -EINVAL;
2159
Naresh Tanniru7586e292017-04-13 10:38:13 +05302160 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302161 ALOGE("%s:: Invalid use case", __func__);
2162 goto exit;
2163 }
2164
2165 out = usecase->stream.out;
2166 if (!out) {
2167 ALOGE("%s:: invalid stream", __func__);
2168 goto exit;
2169 }
2170
2171 if (!is_offload_usecase(out->usecase)) {
2172 ALOGE("%s:: not supported for non offload session", __func__);
2173 goto exit;
2174 }
2175
2176 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2177 ALOGD("%s:: clk recovery is only supported in STC render mode",
2178 __func__);
2179 ret = 0;
2180 goto exit;
2181 }
2182
2183 if (!out->compr) {
2184 ALOGD("%s:: Invalid compress handle",
2185 __func__);
2186 goto exit;
2187 }
2188 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2189 switch(usecase->out_snd_device) {
2190 case SND_DEVICE_OUT_HDMI:
2191 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2192 case SND_DEVICE_OUT_DISPLAY_PORT:
2193 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2194 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2195 break;
2196 default:
2197 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2198 break;
2199 }
2200
2201 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2202
2203 ret = compress_set_metadata(out->compr, &metadata);
2204 if(ret) {
2205 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2206 }
2207
2208exit:
2209 return ret;
2210}
2211#else
2212int audio_extn_utils_compress_set_clk_rec_mode(
2213 struct audio_usecase *usecase __unused)
2214{
2215 ALOGD("%s:: configuring render mode not supported", __func__);
2216 return 0;
2217}
2218#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302219
2220#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2221int audio_extn_utils_compress_set_render_window(
2222 struct stream_out *out,
2223 struct audio_out_render_window_param *render_window)
2224{
2225 struct snd_compr_metadata metadata;
2226 int ret = -EINVAL;
2227
Manish Dewangan27346042017-03-01 12:56:12 +05302228 if(render_window == NULL) {
2229 ALOGE("%s:: Invalid render_window", __func__);
2230 goto exit;
2231 }
2232
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002233 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2234 __func__,render_window->render_ws, render_window->render_we);
2235
Manish Dewangan27346042017-03-01 12:56:12 +05302236 if (!is_offload_usecase(out->usecase)) {
2237 ALOGE("%s:: not supported for non offload session", __func__);
2238 goto exit;
2239 }
2240
Naresh Tanniru6160c712017-04-17 15:43:48 +05302241 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2242 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302243 ALOGD("%s:: only supported in timestamp mode, current "
2244 "render mode mode %d", __func__, out->render_mode);
2245 goto exit;
2246 }
2247
2248 if (!out->compr) {
2249 ALOGW("%s:: offload session not yet opened,"
2250 "render window will be configure later", __func__);
2251 /* store render window to reconfigure in start_output_stream() */
2252 goto exit;
2253 }
2254
2255 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2256 /*render window start value */
2257 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2258 metadata.value[1] = \
2259 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2260 /*render window end value */
2261 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2262 metadata.value[3] = \
2263 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2264
2265 ret = compress_set_metadata(out->compr, &metadata);
2266 if(ret) {
2267 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2268 }
2269
2270exit:
2271 return ret;
2272}
2273#else
2274int audio_extn_utils_compress_set_render_window(
2275 struct stream_out *out __unused,
2276 struct audio_out_render_window_param *render_window __unused)
2277{
2278 ALOGD("%s:: configuring render window not supported", __func__);
2279 return 0;
2280}
2281#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302282
2283#ifdef SNDRV_COMPRESS_START_DELAY
2284int audio_extn_utils_compress_set_start_delay(
2285 struct stream_out *out,
2286 struct audio_out_start_delay_param *delay_param)
2287{
2288 struct snd_compr_metadata metadata;
2289 int ret = -EINVAL;
2290
2291 if(delay_param == NULL) {
2292 ALOGE("%s:: Invalid delay_param", __func__);
2293 goto exit;
2294 }
2295
2296 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2297 delay_param->start_delay);
2298
2299 if (!is_offload_usecase(out->usecase)) {
2300 ALOGE("%s:: not supported for non offload session", __func__);
2301 goto exit;
2302 }
2303
Naresh Tanniru6160c712017-04-17 15:43:48 +05302304 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2305 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302306 ALOGD("%s:: only supported in timestamp mode, current "
2307 "render mode mode %d", __func__, out->render_mode);
2308 goto exit;
2309 }
2310
2311 if (!out->compr) {
2312 ALOGW("%s:: offload session not yet opened,"
2313 "start delay will be configure later", __func__);
2314 goto exit;
2315 }
2316
2317 metadata.key = SNDRV_COMPRESS_START_DELAY;
2318 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2319 metadata.value[1] = \
2320 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2321
2322 ret = compress_set_metadata(out->compr, &metadata);
2323 if(ret) {
2324 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2325 }
2326
2327exit:
2328 return ret;
2329}
2330#else
2331int audio_extn_utils_compress_set_start_delay(
2332 struct stream_out *out __unused,
2333 struct audio_out_start_delay_param *delay_param __unused)
2334{
2335 ALOGD("%s:: configuring render window not supported", __func__);
2336 return 0;
2337}
2338#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002339
Surendar Karka287348c2019-04-10 18:31:46 +05302340#ifdef SNDRV_COMPRESS_DSP_POSITION
2341int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2342 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2343{
2344 int ret = -EINVAL;
2345 uint64_t *val = NULL;
2346 uint64_t time = 0;
2347 struct snd_compr_metadata metadata;
2348
2349 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2350 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2351 metadata.value[0] = clock_id;
2352 ret = compress_get_metadata(out->compr, &metadata);
2353 if (ret) {
2354 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2355 ret = -errno;
2356 goto exit;
2357 }
2358 val = (uint64_t *)&metadata.value[1];
2359 *frames = *val;
2360 time = *(val + 1);
2361 timestamp->tv_sec = time / 1000000;
2362 timestamp->tv_nsec = (time % 1000000)*1000;
2363
2364exit:
2365 return ret;
2366}
2367#else
2368int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2369 uint64_t *frames __unused, struct timespec *timestamp __unused,
2370 int32_t clock_id __unused)
2371{
2372 ALOGD("%s:: dsp presentation position not supported", __func__);
2373 return 0;
2374
2375}
2376#endif
2377
2378#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2379int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2380 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2381{
2382 int ret = -EINVAL;
2383 uint64_t time = 0;
2384 struct snd_pcm_prsnt_position prsnt_position;
2385
2386 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2387 prsnt_position.clock_id = clock_id;
2388 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2389 if (ret) {
2390 ALOGE("%s::error %d", __func__, ret);
2391 ret = -EIO;
2392 goto exit;
2393 }
2394
2395 *frames = prsnt_position.frames;
2396 time = prsnt_position.timestamp;
2397 timestamp->tv_sec = time / 1000000;
2398 timestamp->tv_nsec = (time % 1000000)*1000;
2399
2400exit:
2401 return ret;
2402}
2403#else
2404int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2405 uint64_t *frames __unused, struct timespec *timestamp __unused,
2406 int32_t clock_id __unused)
2407{
2408 ALOGD("%s:: dsp presentation position not supported", __func__);
2409 return 0;
2410
2411}
2412#endif
2413
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002414#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302415#define RETRY_US 1000000
2416#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002417#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2418#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2419
2420#ifdef LINUX_ENABLED
2421static const char *kConfigLocationList[] =
2422 {"/etc"};
2423#else
2424static const char *kConfigLocationList[] =
2425 {"/vendor/etc"};
2426#endif
2427static const int kConfigLocationListSize =
2428 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2429
2430bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2431{
2432 char full_config_path[MIXER_PATH_MAX_LENGTH];
2433 for (int i = 0; i < kConfigLocationListSize; i++) {
2434 snprintf(full_config_path,
2435 MIXER_PATH_MAX_LENGTH,
2436 "%s/%s",
2437 kConfigLocationList[i],
2438 file_name);
2439 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002440 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002441 return true;
2442 }
2443 }
2444 return false;
2445}
2446
2447/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2448int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2449{
2450 if (NULL == snd_card_name) {
2451 return -1;
2452 }
2453
2454 struct snd_card_split *snd_split_handle = NULL;
2455 int ret = 0;
2456 audio_extn_set_snd_card_split(snd_card_name);
2457 snd_split_handle = audio_extn_get_snd_card_split();
2458
2459 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2460 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2461 snd_split_handle->form_factor);
2462
2463 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2464 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2465 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2466 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2467
2468 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2469 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2470 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2471 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2472 }
2473 }
2474
2475 return ret;
2476}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002477
2478int audio_extn_utils_get_snd_card_num()
2479{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302480 int snd_card_num = 0;
2481 struct mixer *mixer = NULL;
2482
2483 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2484 if (mixer)
2485 mixer_close(mixer);
2486 return snd_card_num;
2487}
2488
2489int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2490{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002491
2492 void *hw_info = NULL;
2493 struct mixer *mixer = NULL;
2494 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002495 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002496 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002497 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002498
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302499 if (!mixer_handle) {
2500 ALOGE("invalid mixer handle");
2501 return -1;
2502 }
2503 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302504 /*
mpangfaa7bae2019-01-15 16:38:13 +08002505 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302506 * sleep for 1 sec and try detections with sound card 0 again.
2507 * If sound card gets detected, check if it is relevant, if not check with the
2508 * other sound cards. To ensure that the irrelevant sound card is not check again,
2509 * we maintain it in min_snd_card_num.
2510 */
2511 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002512 snd_card_num = 0;
2513 while (snd_card_num < MAX_SND_CARD) {
2514 if (snd_card_detection_info[snd_card_num] == 0) {
2515 mixer = mixer_open(snd_card_num);
2516 if (!mixer)
2517 snd_card_num++;
2518 else
2519 break;
2520 } else
2521 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002522 }
2523
2524 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302525 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302526 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002527 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302528 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002529 }
2530
2531 snd_card_name = strdup(mixer_get_name(mixer));
2532 if (!snd_card_name) {
2533 ALOGE("failed to allocate memory for snd_card_name\n");
2534 mixer_close(mixer);
2535 return -1;
2536 }
2537 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002538 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002539 hw_info = hw_info_init(snd_card_name);
2540 if (hw_info) {
2541 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2542 break;
2543 }
mpangfaa7bae2019-01-15 16:38:13 +08002544 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002545
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302546 free(snd_card_name);
2547 snd_card_name = NULL;
2548
2549 mixer_close(mixer);
2550 mixer = NULL;
2551 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002552 if (snd_card_name)
2553 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302554
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302555 if (hw_info)
2556 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002557
mpangfaa7bae2019-01-15 16:38:13 +08002558 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002559 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2560 return -1;
2561 }
2562
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302563 if (mixer)
2564 *mixer_handle = mixer;
2565
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002566 return snd_card_num;
2567}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302568
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302569void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2570{
2571 if (mixer)
2572 mixer_close(mixer);
2573}
2574
Naresh Tanniru6160c712017-04-17 15:43:48 +05302575#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2576int audio_extn_utils_compress_enable_drift_correction(
2577 struct stream_out *out,
2578 struct audio_out_enable_drift_correction *drift)
2579{
2580 struct snd_compr_metadata metadata;
2581 int ret = -EINVAL;
2582
2583 if(drift == NULL) {
2584 ALOGE("%s:: Invalid param", __func__);
2585 goto exit;
2586 }
2587
2588 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2589
2590 if (!is_offload_usecase(out->usecase)) {
2591 ALOGE("%s:: not supported for non offload session", __func__);
2592 goto exit;
2593 }
2594
2595 if (!out->compr) {
2596 ALOGW("%s:: offload session not yet opened,"
2597 "start delay will be configure later", __func__);
2598 goto exit;
2599 }
2600
2601 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2602 metadata.value[0] = drift->enable;
2603 out->drift_correction_enabled = drift->enable;
2604
2605 ret = compress_set_metadata(out->compr, &metadata);
2606 if(ret) {
2607 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2608 out->drift_correction_enabled = false;
2609 }
2610
2611exit:
2612 return ret;
2613}
2614#else
2615int audio_extn_utils_compress_enable_drift_correction(
2616 struct stream_out *out __unused,
2617 struct audio_out_enable_drift_correction *drift __unused)
2618{
2619 ALOGD("%s:: configuring drift enablement not supported", __func__);
2620 return 0;
2621}
2622#endif
2623
2624#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2625int audio_extn_utils_compress_correct_drift(
2626 struct stream_out *out,
2627 struct audio_out_correct_drift *drift_param)
2628{
2629 struct snd_compr_metadata metadata;
2630 int ret = -EINVAL;
2631
2632 if (drift_param == NULL) {
2633 ALOGE("%s:: Invalid drift_param", __func__);
2634 goto exit;
2635 }
2636
2637 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2638 drift_param->adjust_time);
2639
2640 if (!is_offload_usecase(out->usecase)) {
2641 ALOGE("%s:: not supported for non offload session", __func__);
2642 goto exit;
2643 }
2644
2645 if (!out->compr) {
2646 ALOGW("%s:: offload session not yet opened", __func__);
2647 goto exit;
2648 }
2649
2650 if (!out->drift_correction_enabled) {
2651 ALOGE("%s:: drift correction not enabled", __func__);
2652 goto exit;
2653 }
2654
2655 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2656 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2657 metadata.value[1] = \
2658 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2659
2660 ret = compress_set_metadata(out->compr, &metadata);
2661 if(ret)
2662 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2663exit:
2664 return ret;
2665}
2666#else
2667int audio_extn_utils_compress_correct_drift(
2668 struct stream_out *out __unused,
2669 struct audio_out_correct_drift *drift_param __unused)
2670{
2671 ALOGD("%s:: setting adjust clock not supported", __func__);
2672 return 0;
2673}
2674#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302675
2676int audio_extn_utils_set_channel_map(
2677 struct stream_out *out,
2678 struct audio_out_channel_map_param *channel_map_param)
2679{
2680 int ret = -EINVAL, i = 0;
2681 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2682
2683 if (channel_map_param == NULL) {
2684 ALOGE("%s:: Invalid channel_map", __func__);
2685 goto exit;
2686 }
2687
2688 if (channel_map_param->channels != channels) {
2689 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2690 __func__, channel_map_param->channels, channels);
2691 goto exit;
2692 }
2693
2694 for ( i = 0; i < channels; i++) {
2695 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2696 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2697 }
2698 ret = 0;
2699exit:
2700 return ret;
2701}
Varun Balaraje49253e2017-07-06 19:48:56 +05302702
2703int audio_extn_utils_set_pan_scale_params(
2704 struct stream_out *out,
2705 struct mix_matrix_params *mm_params)
2706{
2707 int ret = -EINVAL, i = 0, j = 0;
2708
Varun Balaraj003ee752017-08-07 17:54:55 +05302709 if (mm_params == NULL || out == NULL) {
2710 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302711 goto exit;
2712 }
2713
2714 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2715 mm_params->num_output_channels <= 0 ||
2716 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2717 mm_params->num_input_channels <= 0)
2718 goto exit;
2719
2720 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2721 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2722 out->pan_scale_params.has_output_channel_map =
2723 mm_params->has_output_channel_map;
2724 for (i = 0; i < mm_params->num_output_channels; i++)
2725 out->pan_scale_params.output_channel_map[i] =
2726 mm_params->output_channel_map[i];
2727
2728 out->pan_scale_params.has_input_channel_map =
2729 mm_params->has_input_channel_map;
2730 for (i = 0; i < mm_params->num_input_channels; i++)
2731 out->pan_scale_params.input_channel_map[i] =
2732 mm_params->input_channel_map[i];
2733
2734 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2735 for (i = 0; i < mm_params->num_output_channels; i++)
2736 for (j = 0; j < mm_params->num_input_channels; j++) {
2737 //Convert the channel coefficient gains in Q14 format
2738 out->pan_scale_params.mixer_coeffs[i][j] =
2739 mm_params->mixer_coeffs[i][j] * (2 << 13);
2740 }
2741
2742 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2743 out->pcm_device_id,
2744 out->pan_scale_params);
2745
2746exit:
2747 return ret;
2748}
2749
2750int audio_extn_utils_set_downmix_params(
2751 struct stream_out *out,
2752 struct mix_matrix_params *mm_params)
2753{
2754 int ret = -EINVAL, i = 0, j = 0;
2755 struct audio_usecase *usecase = NULL;
2756
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002757 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302758 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302759 goto exit;
2760 }
2761
2762 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2763 mm_params->num_output_channels <= 0 ||
2764 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2765 mm_params->num_input_channels <= 0)
2766 goto exit;
2767
2768 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302769 if (!usecase) {
2770 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002771 goto exit;
2772 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302773 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2774 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2775
2776 out->downmix_params.has_output_channel_map =
2777 mm_params->has_output_channel_map;
2778 for (i = 0; i < mm_params->num_output_channels; i++) {
2779 out->downmix_params.output_channel_map[i] =
2780 mm_params->output_channel_map[i];
2781 }
2782
2783 out->downmix_params.has_input_channel_map =
2784 mm_params->has_input_channel_map;
2785 for (i = 0; i < mm_params->num_input_channels; i++)
2786 out->downmix_params.input_channel_map[i] =
2787 mm_params->input_channel_map[i];
2788
2789 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2790 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302791 for (j = 0; j < mm_params->num_input_channels; j++) {
2792 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302793 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302794 mm_params->mixer_coeffs[i][j] * (2 << 13);
2795 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302796
2797 ret = platform_set_stream_downmix_params(out->dev->platform,
2798 out->pcm_device_id,
2799 usecase->out_snd_device,
2800 out->downmix_params);
2801
2802exit:
2803 return ret;
2804}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302805
2806bool audio_extn_utils_is_dolby_format(audio_format_t format)
2807{
2808 if (format == AUDIO_FORMAT_AC3 ||
2809 format == AUDIO_FORMAT_E_AC3 ||
2810 format == AUDIO_FORMAT_E_AC3_JOC)
2811 return true;
2812 else
2813 return false;
2814}
2815
`Deeraj Soman676c2702017-09-18 19:25:53 +05302816int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2817{
2818 int i;
2819 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2820 {"S32_LE", 32},
2821 {"S24_LE", 24},
2822 {"S16_LE", 16}};
2823 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302824
`Deeraj Soman676c2702017-09-18 19:25:53 +05302825 for (i = 0; i < num_configs; i++) {
2826 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2827 return mixer_bitwidth_config[i].value;
2828 }
2829
2830 return -EINVAL;
2831}
2832
2833int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2834{
2835 int i;
2836 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2837 {"KHZ_48", 48000},
2838 {"KHZ_96", 96000},
2839 {"KHZ_144", 144000},
2840 {"KHZ_192", 192000},
2841 {"KHZ_384", 384000},
2842 {"KHZ_44P1", 44100},
2843 {"KHZ_88P2", 88200},
2844 {"KHZ_176P4", 176400},
2845 {"KHZ_352P8", 352800}};
2846 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2847
2848 for (i = 0; i < num_configs; i++) {
2849 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2850 return mixer_samplerate_config[i].value;
2851 }
2852
2853 return -EINVAL;
2854}
2855
2856int audio_extn_utils_get_channels_from_string(const char *id_string)
2857{
2858 int i;
2859 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2860 {"Two", 2},
2861 {"Three",3},
2862 {"Four", 4},
2863 {"Five", 5},
2864 {"Six", 6},
2865 {"Seven", 7},
2866 {"Eight", 8}};
2867 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2868
2869 for (i = 0; i < num_configs; i++) {
2870 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2871 return mixer_channels_config[i].value;
2872 }
2873
2874 return -EINVAL;
2875}
Surendar karka30569792018-05-08 12:02:21 +05302876
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002877void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2878{
2879 audio_extn_dev_arbi_release(snd_device);
2880 audio_extn_sound_trigger_update_device_status(snd_device,
2881 ST_EVENT_SND_DEVICE_FREE);
2882 audio_extn_listen_update_device_status(snd_device,
2883 LISTEN_EVENT_SND_DEVICE_FREE);
2884}
2885
Surendar karka30569792018-05-08 12:02:21 +05302886int audio_extn_utils_get_license_params
2887(
2888const struct audio_device *adev,
2889struct audio_license_params *license_params
2890)
2891{
2892 if(!license_params)
2893 return -EINVAL;
2894 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2895}
2896
Aalique Grahame22e49102018-12-18 14:23:57 -08002897int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
2898 int app_type,
2899 int *gain)
2900{
2901 int gain_cfg[4];
2902 const char *mixer_ctl_name = "App Type Gain";
2903 struct mixer_ctl *ctl;
2904 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2905 if (!ctl) {
2906 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
2907 mixer_ctl_name);
2908 return -EINVAL;
2909 }
2910 gain_cfg[0] = 0;
2911 gain_cfg[1] = app_type;
2912 gain_cfg[2] = gain[0];
2913 gain_cfg[3] = gain[1];
2914 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
2915 return mixer_ctl_set_array(ctl, gain_cfg,
2916 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
2917}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07002918
2919int audio_extn_utils_is_vendor_enhanced_fwk()
2920{
2921 static int is_running_with_enhanced_fwk = -EINVAL;
2922
2923 if (is_running_with_enhanced_fwk == -EINVAL) {
2924 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
2925 if (vndk_fwk_lib_handle != NULL) {
2926 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
2927 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
2928 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
2929 ALOGW("%s: dlsym failed, defaulting to enhanced_fwk configuration",
2930 __func__);
2931 is_running_with_enhanced_fwk = 1;
2932 } else {
2933 is_running_with_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
2934 }
2935 dlclose(vndk_fwk_lib_handle);
2936 vndk_fwk_lib_handle = NULL;
2937 } else {
2938 ALOGW("%s: VNDK_FWK_LIB not found, setting stock configuration", __func__);
2939 is_running_with_enhanced_fwk = 0;
2940 }
2941 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_running_with_enhanced_fwk);
2942 }
2943
2944 return is_running_with_enhanced_fwk;
2945}
Deeraj Soman14230922019-01-30 16:39:30 +05302946
2947size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
2948 audio_format_t format,
2949 int channel_count,
2950 int64_t duration_ms,
2951 bool is_low_latency)
2952{
2953 size_t size = 0;
2954 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
2955 uint32_t bytes_per_period_sample = 0;
2956
2957
2958 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
2959 return 0;
2960
2961 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
2962 capture_duration = duration_ms;
2963
2964 size = (sample_rate * capture_duration) / 1000;
2965 if (is_low_latency)
2966 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
2967
2968
2969 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
2970 size *= bytes_per_period_sample;
2971
2972 /* make sure the size is multiple of 32 bytes and additionally multiple of
2973 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
2974 * At 48 kHz mono 16-bit PCM:
2975 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
2976 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
2977 *
2978 * The loop reaches result within 32 iterations, as initial size is
2979 * already a multiple of frame_size
2980 */
2981 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
2982
2983 return size;
2984}