blob: bb3388562d3403515c25787c04c078550fbfea59 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07002 * Copyright (c) 2014-2020, 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
Samyak Jain2546f8a2019-08-29 19:24:21 +0530107#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530108
Varun Balaraje49253e2017-07-06 19:48:56 +0530109#ifndef MAX_CHANNELS_SUPPORTED
110#define MAX_CHANNELS_SUPPORTED 8
111#endif
112
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700113#ifdef __LP64__
114#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
115#else
116#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
117#endif
118
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800119typedef struct vndkfwk_s {
120 void *lib_handle;
121 int (*isVendorEnhancedFwk)(void);
122 int (*getVendorEnhancedInfo)(void);
123 const char *lib_name;
124} vndkfwk_t;
125
126static vndkfwk_t mVndkFwk = {
127 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700128
`Deeraj Soman676c2702017-09-18 19:25:53 +0530129typedef struct {
130 const char *id_string;
131 const int value;
132} mixer_config_lookup;
133
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700134struct string_to_enum {
135 const char *name;
136 uint32_t value;
137};
138
139const struct string_to_enum s_flag_name_to_enum_table[] = {
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
155 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
156 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
157 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Derek Chendf05eea2019-08-01 13:57:49 -0700158 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
163 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530164 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530165 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167};
168
169const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530172 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
173 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530174 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700175 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
176 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
177 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
179 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700180 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700181 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700182 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530183 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700184 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700185 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530186 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700187 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
188 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
189 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700190 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
191 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
192 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
193 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
194 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
195 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
196 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700197 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530198 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
199 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800200 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
201 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
202 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530203 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530204 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
205 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
206 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530207 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530208 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
209 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
210 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
211 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530212 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700213};
214
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530215/* payload structure avt_device drift query */
216struct audio_avt_device_drift_stats {
217 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530218
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530219 /* Indicates the device interface direction as either
220 * source (Tx) or sink (Rx).
221 */
222 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530223
224 /* Reference timer for drift accumulation and time stamp information.
225 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
226 */
227 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530228 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530229} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530230
Ben Rombergera04fabc2014-11-14 12:16:03 -0800231static char bTable[BASE_TABLE_SIZE] = {
232 'A','B','C','D','E','F','G','H','I','J','K','L',
233 'M','N','O','P','Q','R','S','T','U','V','W','X',
234 'Y','Z','a','b','c','d','e','f','g','h','i','j',
235 'k','l','m','n','o','p','q','r','s','t','u','v',
236 'w','x','y','z','0','1','2','3','4','5','6','7',
237 '8','9','+','/'
238};
239
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700240static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
241 const char *name)
242{
243 size_t i;
244 for (i = 0; i < size; i++) {
245 if (strcmp(table[i].name, name) == 0) {
246 ALOGV("%s found %s", __func__, table[i].name);
247 return table[i].value;
248 }
249 }
250 return 0;
251}
252
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530253static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700254{
255 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530256 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800257 char *last_r;
258 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700259 while (flag_name != NULL) {
260 if (strlen(flag_name) != 0) {
261 flag |= string_to_enum(s_flag_name_to_enum_table,
262 ARRAY_SIZE(s_flag_name_to_enum_table),
263 flag_name);
264 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800265 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700266 }
267
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800268 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530269 io_flags.in_flags = (audio_input_flags_t)flag;
270 io_flags.out_flags = (audio_output_flags_t)flag;
271 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700272}
273
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530274static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275{
276 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800277 char *last_r;
278 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700279
280 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
281 return;
282
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530283 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700284 while (str != NULL) {
285 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
286 ARRAY_SIZE(s_format_name_to_enum_table), str);
287 ALOGV("%s: format - %d", __func__, format);
288 if (format != 0) {
289 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700290 if (sf_info == NULL)
291 break; /* return whatever was parsed */
292
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700293 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530294 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700295 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800296 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700297 }
298}
299
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530300static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700301{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700302 struct stream_sample_rate *ss_info = NULL;
303 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800304 char *last_r;
305 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700306
Amit Shekhar6f461b12014-08-01 14:52:58 -0700307 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
308 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700309
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530310 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700311 while (str != NULL) {
312 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
313 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
314 if (0 != sample_rate) {
315 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800316 if (!ss_info) {
317 ALOGE("%s: memory allocation failure", __func__);
318 return;
319 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700320 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530321 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700322 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800323 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700324 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700325}
326
327static int parse_bit_width_names(char *name)
328{
329 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800330 char *last_r;
331 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700332
333 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
334 bit_width = (int)strtol(str, (char **)NULL, 10);
335
336 ALOGV("%s: bit_width - %d", __func__, bit_width);
337 return bit_width;
338}
339
340static int parse_app_type_names(void *platform, char *name)
341{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700342 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800343 char *last_r;
344 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700345
346 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
347 app_type = (int)strtol(str, (char **)NULL, 10);
348
349 ALOGV("%s: app_type - %d", __func__, app_type);
350 return app_type;
351}
352
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530353static void update_streams_cfg_list(cnode *root, void *platform,
354 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700355{
356 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530357 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700358
359 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530360 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700361
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530362 if (!s_info) {
363 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700364 return;
365 }
366
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700367 while (node) {
368 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530370 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
371 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700372 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530373 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530375 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
376 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700377 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530378 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530380 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381 }
382 node = node->next;
383 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530384 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700385}
386
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387static void load_cfg_list(cnode *root, void *platform,
388 struct listnode *streams_output_cfg_list,
389 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700390{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530391 cnode *node = NULL;
392
393 node = config_find(root, OUTPUTS_TAG);
394 if (node != NULL) {
395 node = node->first_child;
396 while (node) {
397 ALOGV("%s: loading output %s", __func__, node->name);
398 update_streams_cfg_list(node, platform, streams_output_cfg_list);
399 node = node->next;
400 }
401 } else {
402 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700403 }
404
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530405 node = config_find(root, INPUTS_TAG);
406 if (node != NULL) {
407 node = node->first_child;
408 while (node) {
409 ALOGV("%s: loading input %s", __func__, node->name);
410 update_streams_cfg_list(node, platform, streams_input_cfg_list);
411 node = node->next;
412 }
413 } else {
414 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700415 }
416}
417
418static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530419 struct listnode *streams_output_cfg_list,
420 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700421{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530422 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700423 int length = 0, i, num_app_types = 0;
424 struct listnode *node;
425 bool update;
426 struct mixer_ctl *ctl = NULL;
427 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530428 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800429 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700430
431 if (!mixer) {
432 ALOGE("%s: mixer is null",__func__);
433 return;
434 }
435 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
436 if (!ctl) {
437 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
438 return;
439 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530440 app_type_cfg[length++] = num_app_types;
441
442 if (list_empty(streams_output_cfg_list)) {
443 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700444 app_type_cfg[length++] = 48000;
445 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530446 num_app_types += 1;
447 }
448 if (list_empty(streams_input_cfg_list)) {
449 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
450 app_type_cfg[length++] = 48000;
451 app_type_cfg[length++] = 16;
452 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700453 }
454
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800455 /* get target bit width for ADM enforce mode */
456 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
457
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700458 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530459 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700460 update = true;
461 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700463 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530464 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530465 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530466 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530467 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530468 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800469 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
470 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
471 (target_bit_width > app_type_cfg[i+3]))
472 app_type_cfg[i+3] = target_bit_width;
473
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700474 update = false;
475 break;
476 }
477 }
478 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530479 num_app_types += 1;
480 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
481 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800482 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
483 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
484 (target_bit_width > app_type_cfg[length]))
485 app_type_cfg[length] = target_bit_width;
486
487 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530488 }
489 }
490 list_for_each(node, streams_input_cfg_list) {
491 s_info = node_to_item(node, struct streams_io_cfg, list);
492 update = true;
493 for (i=0; i<length; i=i+3) {
494 if (app_type_cfg[i+1] == 0)
495 break;
496 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530497 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530498 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530499 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530500 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
501 update = false;
502 break;
503 }
504 }
505 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
506 num_app_types += 1;
507 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
508 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
509 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700510 }
511 }
512 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
513 if (num_app_types) {
514 app_type_cfg[0] = num_app_types;
515 mixer_ctl_set_array(ctl, app_type_cfg, length);
516 }
517}
518
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530519void audio_extn_utils_update_streams_cfg_lists(void *platform,
520 struct mixer *mixer,
521 struct listnode *streams_output_cfg_list,
522 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700523{
524 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530525 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700526
527 ALOGV("%s", __func__);
528 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530529 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700530
531 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700532 if (root == NULL) {
533 ALOGE("cfg_list, NULL config root");
534 return;
535 }
536
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530537 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
538 if (data == NULL) {
539 ALOGD("%s: failed to open io config file(%s), trying older config file",
540 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
541 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
542 if (data == NULL) {
543 send_app_type_cfg(platform, mixer,
544 streams_output_cfg_list,
545 streams_input_cfg_list);
546 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800547 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530548 return;
549 }
550 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700551
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530552 config_load(root, data);
553 load_cfg_list(root, platform, streams_output_cfg_list,
554 streams_input_cfg_list);
555
556 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
557 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800558
559 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800560 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800561 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700562}
563
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530564static void audio_extn_utils_dump_streams_cfg_list(
565 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700566{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700567 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530568 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700569 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700570 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530571
572 list_for_each(node_i, streams_cfg_list) {
573 s_info = node_to_item(node_i, struct streams_io_cfg, list);
574 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
575 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
576 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
577 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700578 sf_info = node_to_item(node_j, struct stream_format, list);
579 ALOGV("format-%x", sf_info->format);
580 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530581 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700582 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
583 ALOGV("sample rate-%d", ss_info->sample_rate);
584 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700585 }
586}
587
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530588void audio_extn_utils_dump_streams_cfg_lists(
589 struct listnode *streams_output_cfg_list,
590 struct listnode *streams_input_cfg_list)
591{
592 ALOGV("%s", __func__);
593 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
594 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
595}
596
597static void audio_extn_utils_release_streams_cfg_list(
598 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700599{
600 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530601 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700602
603 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530604
605 while (!list_empty(streams_cfg_list)) {
606 node_i = list_head(streams_cfg_list);
607 s_info = node_to_item(node_i, struct streams_io_cfg, list);
608 while (!list_empty(&s_info->format_list)) {
609 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700610 list_remove(node_j);
611 free(node_to_item(node_j, struct stream_format, list));
612 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530613 while (!list_empty(&s_info->sample_rate_list)) {
614 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700615 list_remove(node_j);
616 free(node_to_item(node_j, struct stream_sample_rate, list));
617 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700618 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530619 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700620 }
621}
622
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530623void audio_extn_utils_release_streams_cfg_lists(
624 struct listnode *streams_output_cfg_list,
625 struct listnode *streams_input_cfg_list)
626{
627 ALOGV("%s", __func__);
628 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
629 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
630}
631
632static bool set_app_type_cfg(struct streams_io_cfg *s_info,
633 struct stream_app_type_cfg *app_type_cfg,
634 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700635 {
636 struct listnode *node_i;
637 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530638 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700639 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
640 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530641 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700642
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530643 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700644 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530645 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700646 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
647 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
648 return true;
649 }
650 }
651 /*
652 * Reiterate through the list assuming dafault sample rate.
653 * Handles scenario where input sample rate is higher
654 * than all sample rates in list for the input bit width.
655 */
656 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800657
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530658 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700659 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
660 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530661 (bit_width == s_info->app_type_cfg.bit_width)) {
662 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700663 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530664 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800665 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 -0700666 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
667 return true;
668 }
669 }
670 return false;
671}
672
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530673void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
674 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800675 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530676 audio_input_flags_t flags,
677 audio_format_t format,
678 uint32_t sample_rate,
679 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530680 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530681 struct stream_app_type_cfg *app_type_cfg)
682{
683 struct listnode *node_i, *node_j;
684 struct streams_io_cfg *s_info;
685 struct stream_format *sf_info;
686
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530687 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
688 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530689
690 list_for_each(node_i, streams_input_cfg_list) {
691 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530692 /* Along with flags do profile matching if set at either end.*/
693 if (s_info->flags.in_flags == flags &&
694 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
695 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530696 list_for_each(node_j, &s_info->format_list) {
697 sf_info = node_to_item(node_j, struct stream_format, list);
698 if (sf_info->format == format) {
699 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
700 return;
701 }
702 }
703 }
704 }
705 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
706 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
707 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
708 app_type_cfg->bit_width = 16;
709}
710
711void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700712 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800713 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700714 audio_output_flags_t flags,
715 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700716 uint32_t sample_rate,
717 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530718 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530719 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700720 struct stream_app_type_cfg *app_type_cfg)
721{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530722 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530723 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700724 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530725 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700726
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800727 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530728 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
729 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700730 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530731 else if (-ENOSYS == bw)
732 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700733 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530734 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700735 }
736
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700737 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530738 if (!strncmp("true", value, sizeof("true"))) {
739 if ((popcount(channel_mask) > 2) &&
740 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700741 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530742 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
743 ALOGD("%s: MCH session defaulting sample rate to %d",
744 __func__, sample_rate);
745 }
746 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530747
748 /* Set sampling rate to 176.4 for DSD64
749 * and 352.8Khz for DSD128.
750 * Set Bit Width to 16. output will be 16 bit
751 * post DoP in ASM.
752 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700753 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530754 (format == AUDIO_FORMAT_DSD)) {
755 bit_width = 16;
756 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
757 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
758 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
759 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
760 }
761
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530762
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800763 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
764 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700765 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530766 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530767 /* Along with flags do profile matching if set at either end.*/
768 if (s_info->flags.out_flags == flags &&
769 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
770 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530771 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700772 sf_info = node_to_item(node_j, struct stream_format, list);
773 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530774 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700775 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700776 }
777 }
778 }
779 }
780 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530781 s_info = node_to_item(node_i, struct streams_io_cfg, list);
782 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700783 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530784 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
785 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
786 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700787 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530788 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700789 return;
790 }
791 }
792 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700793 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700794 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700795 app_type_cfg->bit_width = 16;
796}
797
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530798static bool audio_is_this_native_usecase(struct audio_usecase *uc)
799{
800 bool native_usecase = false;
801 struct stream_out *out = (struct stream_out*) uc->stream.out;
802
803 if (PCM_PLAYBACK == uc->type && out != NULL &&
804 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
805 is_offload_usecase(uc->id) &&
806 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
807 native_usecase = true;
808
809 return native_usecase;
810}
811
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800812bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
813{
814 /* DSP bitwidth enforce mode for ADM and AFE:
815 * includes:
816 * deep buffer, low latency, direct pcm and offload.
817 * excludes:
818 * ull(raw+fast), VOIP.
819 */
820 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
821 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
822 (flags & AUDIO_OUTPUT_FLAG_FAST)))
823 return false;
824
825
826 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
827 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
828 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
829 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
830 return true;
831 else
832 return false;
833}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800834
835static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
836{
837 return adev->vr_audio_mode_enabled;
838}
839
Derek Chenf7092792017-05-23 12:23:53 -0400840static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
841{
842 switch (snd_device) {
843 case SND_DEVICE_OUT_BT_SCO:
844 case SND_DEVICE_IN_BT_SCO_MIC:
845 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
846 *sample_rate = 8000;
847 break;
848 case SND_DEVICE_OUT_BT_SCO_WB:
849 case SND_DEVICE_IN_BT_SCO_MIC_WB:
850 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
851 *sample_rate = 16000;
852 break;
853 default:
854 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
855 break;
856 }
857}
858
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530859void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
860 struct audio_device *adev,
861 struct audio_usecase *usecase)
862{
863 ALOGV("%s", __func__);
864
865 switch(usecase->type) {
866 case PCM_PLAYBACK:
867 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
868 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800869 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530870 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700871 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530872 usecase->stream.out->sample_rate,
873 usecase->stream.out->bit_width,
874 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530875 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530876 &usecase->stream.out->app_type_cfg);
877 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
878 break;
879 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700880 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
881 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
882 else
883 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530884 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800885 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530886 usecase->stream.in->flags,
887 usecase->stream.in->format,
888 usecase->stream.in->sample_rate,
889 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530890 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530891 &usecase->stream.in->app_type_cfg);
892 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
893 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530894 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530895 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
896 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800897 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530898 0,
899 usecase->stream.inout->out_config.format,
900 usecase->stream.inout->out_config.sample_rate,
901 usecase->stream.inout->out_config.bit_width,
902 usecase->stream.inout->out_config.channel_mask,
903 usecase->stream.inout->profile,
904 &usecase->stream.inout->out_app_type_cfg);
905 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
906 break;
Derek Chenf7092792017-05-23 12:23:53 -0400907 case PCM_HFP_CALL:
908 switch (usecase->id) {
909 case USECASE_AUDIO_HFP_SCO:
910 case USECASE_AUDIO_HFP_SCO_WB:
911 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
912 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500913 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400914 break;
915 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
916 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
917 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
918 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500919 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400920 break;
921 default:
922 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
923 __func__, usecase->id);
924 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
925 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
926 break;
927 }
928 /* update out_app_type_cfg */
929 usecase->out_app_type_cfg.bit_width =
930 platform_get_snd_device_bit_width(usecase->out_snd_device);
931 usecase->out_app_type_cfg.app_type =
932 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
933 /* update in_app_type_cfg */
934 usecase->in_app_type_cfg.bit_width =
935 platform_get_snd_device_bit_width(usecase->in_snd_device);
936 usecase->in_app_type_cfg.app_type =
937 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
938 ALOGV("%s Selected apptype: playback %d capture %d",
939 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
940 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530941 default:
942 ALOGE("%s: app type cfg not supported for usecase type (%d)",
943 __func__, usecase->type);
944 }
945}
946
Aalique Grahame22e49102018-12-18 14:23:57 -0800947static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
948 int pcm_device_id, int app_type,
949 int acdb_dev_id, int sample_rate,
950 int stream_type,
951 snd_device_t snd_device)
952{
953
954 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
955 struct mixer_ctl *ctl;
956 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
957 int snd_device_be_idx = -1;
958
959 if (stream_type == PCM_PLAYBACK) {
960 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
961 "Audio Stream %d App Type Cfg", pcm_device_id);
962 } else if (stream_type == PCM_CAPTURE) {
963 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
964 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
965 }
966
967 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
968 if (!ctl) {
969 ALOGE("%s: Could not get ctl for mixer cmd - %s",
970 __func__, mixer_ctl_name);
971 rc = -EINVAL;
972 goto exit;
973 }
974 app_type_cfg[len++] = app_type;
975 app_type_cfg[len++] = acdb_dev_id;
976 app_type_cfg[len++] = sample_rate;
977
978 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
979 if (snd_device_be_idx > 0)
980 app_type_cfg[len++] = snd_device_be_idx;
981 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
982 "sample rate %d, snd_device_be_idx %d",
983 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
984 snd_device_be_idx);
985 mixer_ctl_set_array(ctl, app_type_cfg, len);
986
987exit:
988 return rc;
989}
990
991static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
992 struct audio_usecase *usecase)
993{
994 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
995 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
996 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -0400997 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -0800998
999 ALOGV("%s", __func__);
1000
1001 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001002 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001003 rc = 0;
1004 goto exit_send_app_type_cfg;
1005 }
1006 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001007 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1008 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1009 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1010 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001011 rc = 0;
1012 goto exit_send_app_type_cfg;
1013 }
1014
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001015 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001016 is_bus_dev_usecase = true;
1017
Aalique Grahame22e49102018-12-18 14:23:57 -08001018 snd_device = usecase->out_snd_device;
1019 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1020
1021 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1022 if (acdb_dev_id < 0) {
1023 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1024 rc = -EINVAL;
1025 goto exit_send_app_type_cfg;
1026 }
1027
1028 if (usecase->type == PCM_HFP_CALL) {
1029
1030 /* config HFP session:1 playback path */
Derek Chenf7092792017-05-23 12:23:53 -04001031 if (is_bus_dev_usecase) {
1032 app_type = usecase->out_app_type_cfg.app_type;
1033 sample_rate= usecase->out_app_type_cfg.sample_rate;
1034 } else {
1035 snd_device = SND_DEVICE_NONE; // use legacy behavior
1036 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1037 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1038 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001039 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1040 acdb_dev_id, sample_rate,
1041 PCM_PLAYBACK,
Derek Chenf7092792017-05-23 12:23:53 -04001042 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001043 if (rc < 0)
1044 goto exit_send_app_type_cfg;
1045
1046 /* config HFP session:1 capture path */
Derek Chenf7092792017-05-23 12:23:53 -04001047 if (is_bus_dev_usecase) {
1048 snd_device = usecase->in_snd_device;
1049 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1050 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1051 if (acdb_dev_id < 0) {
1052 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1053 rc = -EINVAL;
1054 goto exit_send_app_type_cfg;
1055 }
1056 app_type = usecase->in_app_type_cfg.app_type;
1057 sample_rate= usecase->in_app_type_cfg.sample_rate;
1058 } else {
1059 snd_device = SND_DEVICE_NONE; // use legacy behavior
1060 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1061 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001062 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1063 acdb_dev_id, sample_rate,
1064 PCM_CAPTURE,
Derek Chenf7092792017-05-23 12:23:53 -04001065 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001066 if (rc < 0)
1067 goto exit_send_app_type_cfg;
1068
Derek Chenf7092792017-05-23 12:23:53 -04001069 if (is_bus_dev_usecase)
1070 goto exit_send_app_type_cfg;
1071
Aalique Grahame22e49102018-12-18 14:23:57 -08001072 /* config HFP session:2 capture path */
1073 pcm_device_id = HFP_ASM_RX_TX;
1074 snd_device = usecase->in_snd_device;
1075 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1076 if (acdb_dev_id <= 0) {
1077 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1078 rc = -EINVAL;
1079 goto exit_send_app_type_cfg;
1080 }
1081 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1082 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1083 acdb_dev_id, sample_rate, PCM_CAPTURE,
1084 SND_DEVICE_NONE);
1085 if (rc < 0)
1086 goto exit_send_app_type_cfg;
1087
1088 /* config HFP session:2 playback path */
1089 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1090 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1091 acdb_dev_id, sample_rate,
1092 PCM_PLAYBACK, SND_DEVICE_NONE);
1093 if (rc < 0)
1094 goto exit_send_app_type_cfg;
1095 }
1096
1097 rc = 0;
1098exit_send_app_type_cfg:
1099 return rc;
1100}
1101
Zhou Song06761dd2019-04-28 18:08:17 +08001102int audio_extn_utils_get_app_sample_rate_for_device(
1103 struct audio_device *adev,
1104 struct audio_usecase *usecase, int snd_device)
1105{
1106 char value[PROPERTY_VALUE_MAX] = {0};
1107 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1108
1109 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1110 property_get("vendor.audio.playback.mch.downsample",value,"");
1111 if (!strncmp("true", value, sizeof("true"))) {
1112 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1113 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1114 !(usecase->stream.out->flags &
1115 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1116 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1117 }
1118
1119 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1120 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001121 } else if (compare_device_type(&usecase->stream.out->device_list,
1122 AUDIO_DEVICE_OUT_SPEAKER)) {
Zhou Song06761dd2019-04-28 18:08:17 +08001123 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1124 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1125 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1126 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1127 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1128 /*
1129 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1130 * configured device sample rate, if not update the COPP rate to be equal to the
1131 * device sample rate, else open COPP at stream sample rate
1132 */
1133 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1134 usecase->stream.out->sample_rate,
1135 &usecase->stream.out->app_type_cfg.sample_rate);
1136 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1137 !audio_is_this_native_usecase(usecase)) &&
1138 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1139 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1140 /* Reset to default if no native stream is active*/
1141 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song9e9b3812019-12-30 17:52:10 +08001142 } else if (usecase->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
1143 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
1144 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001145 /*
1146 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1147 * for bit width use the stream param only.
1148 */
1149 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1150 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1151 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1152 }
1153 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1154 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1155
1156 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1157 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1158 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1159 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1160 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1161 sample_rate = sample_rate * 4;
1162 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1163 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1164 }
1165 } else if (usecase->type == PCM_CAPTURE) {
1166 if (usecase->stream.in != NULL) {
1167 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1168 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1169 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1170 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1171 &usecase->stream.in->app_type_cfg.sample_rate);
1172 } else {
1173 audio_extn_btsco_get_sample_rate(snd_device,
1174 &usecase->stream.in->app_type_cfg.sample_rate);
1175 }
1176 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1177 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301178 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1179 sample_rate = atoi(value);
1180 else
1181 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001182 }
1183 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1184 sample_rate = usecase->stream.inout->out_config.sample_rate;
1185 }
1186 return sample_rate;
1187}
1188
Siena Richard7c2db772016-12-21 11:32:34 -08001189static int send_app_type_cfg_for_device(struct audio_device *adev,
1190 struct audio_usecase *usecase,
1191 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001192{
1193 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301194 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1195 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001196 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001197 int pcm_device_id = 0, acdb_dev_id, app_type;
1198 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301199 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301200 struct streams_io_cfg *s_info = NULL;
1201 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301202 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001203
Siena Richard7c2db772016-12-21 11:32:34 -08001204 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1205 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1206 platform_get_snd_device_name(usecase->in_snd_device),
1207 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001208
Siddartha Shaik343abc62017-08-08 11:15:25 +05301209 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301210 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301211 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001212 rc = 0;
1213 goto exit_send_app_type_cfg;
1214 }
1215 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001216 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001217 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1218 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001219 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001220 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301221 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301222 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301223 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001224 (usecase->type != PCM_CAPTURE) &&
1225 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301226 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 -07001227 rc = 0;
1228 goto exit_send_app_type_cfg;
1229 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001230
1231 //if VR is active then only send the mixer control
1232 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1233 ALOGI("ULL doesnt need sending app type cfg, returning");
1234 rc = 0;
1235 goto exit_send_app_type_cfg;
1236 }
1237
Surendar Karka93cd25a2018-08-28 14:21:37 +05301238 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001239 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301240 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1241 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001242 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001243 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301244 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1245 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301246 }
Siena Richard7c2db772016-12-21 11:32:34 -08001247
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001248 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1249 if (!ctl) {
1250 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1251 mixer_ctl_name);
1252 rc = -EINVAL;
1253 goto exit_send_app_type_cfg;
1254 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301255 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301256 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1257 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1258 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1259 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1260 } else {
1261 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1262 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301263 if (acdb_dev_id <= 0) {
1264 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1265 rc = -EINVAL;
1266 goto exit_send_app_type_cfg;
1267 }
1268
Siena Richard7c2db772016-12-21 11:32:34 -08001269 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1270 if (snd_device_be_idx < 0) {
1271 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1272 __func__, platform_get_snd_device_name(snd_device),
1273 snd_device_be_idx);
1274 }
1275
Zhou Song06761dd2019-04-28 18:08:17 +08001276 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1277
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301278 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301279 /* Interactive streams are supported with only direct app type id.
1280 * Get Direct profile app type and use it for interactive streams
1281 */
1282 list_for_each(node, &adev->streams_output_cfg_list) {
1283 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001284 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301285 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1286 AUDIO_OUTPUT_FLAG_DIRECT))
1287 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301288 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001289 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301290 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301291 else
1292 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001293 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001294 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001295 app_type_cfg[len++] = sample_rate;
1296
Siena Richard7c2db772016-12-21 11:32:34 -08001297 if (snd_device_be_idx > 0)
1298 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301299
Siena Richard7c2db772016-12-21 11:32:34 -08001300 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1301 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301302
1303 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001304 app_type = usecase->stream.in->app_type_cfg.app_type;
1305 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301306 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001307 app_type_cfg[len++] = sample_rate;
1308 if (snd_device_be_idx > 0)
1309 app_type_cfg[len++] = snd_device_be_idx;
1310 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1311 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301312 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001313 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301314 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301315 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1316 }
Siena Richard7c2db772016-12-21 11:32:34 -08001317 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301318 app_type_cfg[len++] = acdb_dev_id;
1319 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001320 if (snd_device_be_idx > 0)
1321 app_type_cfg[len++] = snd_device_be_idx;
1322 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1323 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301324 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301325
Siddartha Shaik343abc62017-08-08 11:15:25 +05301326 if(ctl)
1327 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001328 rc = 0;
1329exit_send_app_type_cfg:
1330 return rc;
1331}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001332
Deeraj Soman14230922019-01-30 16:39:30 +05301333static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1334 audio_format_t format,
1335 int channel_count)
1336{
1337 int ret = 0;
1338
1339 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1340 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1341 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1342 !voice_extn_compress_voip_is_format_supported(format) &&
1343 !audio_extn_compr_cap_format_supported(format) &&
1344 !audio_extn_cin_format_supported(format))
1345 ret = -EINVAL;
1346
1347 switch (channel_count) {
1348 case 1:
1349 case 2:
1350 case 3:
1351 case 4:
1352 case 6:
1353 case 8:
1354 break;
1355 default:
1356 ret = -EINVAL;
1357 }
1358
1359 switch (sample_rate) {
1360 case 8000:
1361 case 11025:
1362 case 12000:
1363 case 16000:
1364 case 22050:
1365 case 24000:
1366 case 32000:
1367 case 44100:
1368 case 48000:
1369 case 88200:
1370 case 96000:
1371 case 176400:
1372 case 192000:
1373 break;
1374 default:
1375 ret = -EINVAL;
1376 }
1377
1378 return ret;
1379}
1380
1381static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1382{
1383 uint32_t remainder = 0;
1384
1385 if (!multiplier)
1386 return num;
1387
1388 remainder = num % multiplier;
1389 if (remainder)
1390 num += (multiplier - remainder);
1391
1392 return num;
1393}
1394
1395static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1396{
1397 uint32_t high = num1, low = num2, temp = 0;
1398
1399 if (!num1 || !num2)
1400 return 0;
1401
1402 if (num1 < num2) {
1403 high = num2;
1404 low = num1;
1405 }
1406
1407 while (low != 0) {
1408 temp = low;
1409 low = high % low;
1410 high = temp;
1411 }
1412 return (num1 * num2)/high;
1413}
1414
Siena Richard7c2db772016-12-21 11:32:34 -08001415int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1416 struct audio_usecase *usecase)
1417{
1418 int i, num_devices = 0;
1419 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301420 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001421 int rc = 0;
1422
Aalique Grahame22e49102018-12-18 14:23:57 -08001423 if (usecase->type == PCM_HFP_CALL) {
1424 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1425 }
1426
Siena Richard7c2db772016-12-21 11:32:34 -08001427 switch (usecase->type) {
1428 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301429 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001430 ALOGD("%s: usecase->out_snd_device %s",
1431 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1432 /* check for out combo device */
1433 if (platform_split_snd_device(adev->platform,
1434 usecase->out_snd_device,
1435 &num_devices, new_snd_devices)) {
1436 new_snd_devices[0] = usecase->out_snd_device;
1437 num_devices = 1;
1438 }
1439 break;
1440 case PCM_CAPTURE:
1441 ALOGD("%s: usecase->in_snd_device %s",
1442 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301443 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1444 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1445 }
Siena Richard7c2db772016-12-21 11:32:34 -08001446 /* check for in combo device */
1447 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301448 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001449 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301450 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001451 num_devices = 1;
1452 }
1453 break;
1454 default:
1455 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1456 rc = 0;
1457 break;
1458 }
1459
1460 for (i = 0; i < num_devices; i++) {
1461 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1462 if (rc)
1463 break;
1464 }
1465
1466 return rc;
1467}
1468
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301469int read_line_from_file(const char *path, char *buf, size_t count)
1470{
1471 char * fgets_ret;
1472 FILE * fd;
1473 int rv;
1474
1475 fd = fopen(path, "r");
1476 if (fd == NULL)
1477 return -1;
1478
1479 fgets_ret = fgets(buf, (int)count, fd);
1480 if (NULL != fgets_ret) {
1481 rv = (int)strlen(buf);
1482 } else {
1483 rv = ferror(fd);
1484 }
1485 fclose(fd);
1486
1487 return rv;
1488}
1489
Ashish Jainf1eaa582016-05-23 20:54:24 +05301490/*Translates ALSA formats to AOSP PCM formats*/
1491audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1492{
1493 audio_format_t format;
1494
1495 switch(alsa_format) {
1496 case SNDRV_PCM_FORMAT_S16_LE:
1497 format = AUDIO_FORMAT_PCM_16_BIT;
1498 break;
1499 case SNDRV_PCM_FORMAT_S24_3LE:
1500 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1501 break;
1502 case SNDRV_PCM_FORMAT_S24_LE:
1503 format = AUDIO_FORMAT_PCM_8_24_BIT;
1504 break;
1505 case SNDRV_PCM_FORMAT_S32_LE:
1506 format = AUDIO_FORMAT_PCM_32_BIT;
1507 break;
1508 default:
1509 ALOGW("Incorrect ALSA format");
1510 format = AUDIO_FORMAT_INVALID;
1511 }
1512 return format;
1513}
1514
1515/*Translates hal format (AOSP) to alsa formats*/
1516uint32_t hal_format_to_alsa(audio_format_t hal_format)
1517{
1518 uint32_t alsa_format;
1519
1520 switch (hal_format) {
1521 case AUDIO_FORMAT_PCM_32_BIT: {
1522 if (platform_supports_true_32bit())
1523 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1524 else
1525 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1526 }
1527 break;
1528 case AUDIO_FORMAT_PCM_8_BIT:
1529 alsa_format = SNDRV_PCM_FORMAT_S8;
1530 break;
1531 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1532 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1533 break;
1534 case AUDIO_FORMAT_PCM_8_24_BIT: {
1535 if (platform_supports_true_32bit())
1536 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1537 else
1538 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1539 }
1540 break;
1541 case AUDIO_FORMAT_PCM_FLOAT:
1542 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1543 break;
1544 default:
1545 case AUDIO_FORMAT_PCM_16_BIT:
1546 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1547 break;
1548 }
1549 return alsa_format;
1550}
1551
Ashish Jain83a6cc22016-06-28 14:34:17 +05301552/*Translates PCM formats to AOSP formats*/
1553audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1554{
1555 audio_format_t format = AUDIO_FORMAT_INVALID;
1556
1557 switch(pcm_format) {
1558 case PCM_FORMAT_S16_LE:
1559 format = AUDIO_FORMAT_PCM_16_BIT;
1560 break;
1561 case PCM_FORMAT_S24_3LE:
1562 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1563 break;
1564 case PCM_FORMAT_S24_LE:
1565 format = AUDIO_FORMAT_PCM_8_24_BIT;
1566 break;
1567 case PCM_FORMAT_S32_LE:
1568 format = AUDIO_FORMAT_PCM_32_BIT;
1569 break;
1570 default:
1571 ALOGW("Incorrect PCM format");
1572 format = AUDIO_FORMAT_INVALID;
1573 }
1574 return format;
1575}
1576
1577/*Translates hal format (AOSP) to alsa formats*/
1578uint32_t hal_format_to_pcm(audio_format_t hal_format)
1579{
1580 uint32_t pcm_format;
1581
1582 switch (hal_format) {
1583 case AUDIO_FORMAT_PCM_32_BIT:
1584 case AUDIO_FORMAT_PCM_8_24_BIT:
1585 case AUDIO_FORMAT_PCM_FLOAT: {
1586 if (platform_supports_true_32bit())
1587 pcm_format = PCM_FORMAT_S32_LE;
1588 else
1589 pcm_format = PCM_FORMAT_S24_3LE;
1590 }
1591 break;
1592 case AUDIO_FORMAT_PCM_8_BIT:
1593 pcm_format = PCM_FORMAT_S8;
1594 break;
1595 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1596 pcm_format = PCM_FORMAT_S24_3LE;
1597 break;
1598 default:
1599 case AUDIO_FORMAT_PCM_16_BIT:
1600 pcm_format = PCM_FORMAT_S16_LE;
1601 break;
1602 }
1603 return pcm_format;
1604}
1605
Ashish Jainf1eaa582016-05-23 20:54:24 +05301606uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1607 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301608 uint32_t noOfChannels,
1609 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301610{
1611 uint32_t fragment_size = 0;
1612 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1613
Deeraj Soman65358ab2019-02-07 15:40:49 +05301614 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1615 pcm_offload_time = duration_ms;
1616
Ashish Jainf1eaa582016-05-23 20:54:24 +05301617 fragment_size = (pcm_offload_time
1618 * sample_rate
1619 * bytes_per_sample
1620 * noOfChannels)/1000;
1621 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1622 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1623 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1624 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1625 /*To have same PCM samples for all channels, the buffer size requires to
1626 *be multiple of (number of channels * bytes per sample)
1627 *For writes to succeed, the buffer must be written at address which is multiple of 32
1628 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001629 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301630
1631 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1632 return fragment_size;
1633}
1634
1635/* Calculates the fragment size required to configure compress session.
1636 * Based on the alsa format selected, decide if conversion is needed in
1637
1638 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1639 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1640 */
1641void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1642{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301643 audio_format_t dst_format = out->hal_op_format;
1644 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301645 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1646 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1647
1648 out->compr_config.fragment_size =
1649 get_alsa_fragment_size(hal_op_bytes_per_sample,
1650 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301651 popcount(out->channel_mask),
1652 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301653
1654 if ((src_format != dst_format) &&
1655 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1656
Ashish Jain83a6cc22016-06-28 14:34:17 +05301657 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301658 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1659 hal_op_bytes_per_sample);
1660 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301661 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301662 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301663 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301664 }
1665}
1666
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301667/* converts pcm format 24_8 to 8_24 inplace */
1668size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1669{
1670 size_t i = 0;
1671 int *int_buf_stream = buf;
1672
1673 if ((bytes % 4) != 0) {
1674 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1675 return -EINVAL;
1676 }
1677
1678 for (; i < (bytes / 4); i++)
1679 int_buf_stream[i] >>= 8;
1680
1681 return bytes;
1682}
1683
1684int get_snd_codec_id(audio_format_t format)
1685{
1686 int id = 0;
1687
1688 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1689 case AUDIO_FORMAT_MP3:
1690 id = SND_AUDIOCODEC_MP3;
1691 break;
1692 case AUDIO_FORMAT_AAC:
1693 id = SND_AUDIOCODEC_AAC;
1694 break;
1695 case AUDIO_FORMAT_AAC_ADTS:
1696 id = SND_AUDIOCODEC_AAC;
1697 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301698 case AUDIO_FORMAT_AAC_LATM:
1699 id = SND_AUDIOCODEC_AAC;
1700 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301701 case AUDIO_FORMAT_PCM:
1702 id = SND_AUDIOCODEC_PCM;
1703 break;
1704 case AUDIO_FORMAT_FLAC:
1705 id = SND_AUDIOCODEC_FLAC;
1706 break;
1707 case AUDIO_FORMAT_ALAC:
1708 id = SND_AUDIOCODEC_ALAC;
1709 break;
1710 case AUDIO_FORMAT_APE:
1711 id = SND_AUDIOCODEC_APE;
1712 break;
1713 case AUDIO_FORMAT_VORBIS:
1714 id = SND_AUDIOCODEC_VORBIS;
1715 break;
1716 case AUDIO_FORMAT_WMA:
1717 id = SND_AUDIOCODEC_WMA;
1718 break;
1719 case AUDIO_FORMAT_WMA_PRO:
1720 id = SND_AUDIOCODEC_WMA_PRO;
1721 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301722 case AUDIO_FORMAT_MP2:
1723 id = SND_AUDIOCODEC_MP2;
1724 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301725 case AUDIO_FORMAT_AC3:
1726 id = SND_AUDIOCODEC_AC3;
1727 break;
1728 case AUDIO_FORMAT_E_AC3:
1729 case AUDIO_FORMAT_E_AC3_JOC:
1730 id = SND_AUDIOCODEC_EAC3;
1731 break;
1732 case AUDIO_FORMAT_DTS:
1733 case AUDIO_FORMAT_DTS_HD:
1734 id = SND_AUDIOCODEC_DTS;
1735 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001736 case AUDIO_FORMAT_DOLBY_TRUEHD:
1737 id = SND_AUDIOCODEC_TRUEHD;
1738 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001739 case AUDIO_FORMAT_IEC61937:
1740 id = SND_AUDIOCODEC_IEC61937;
1741 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301742 case AUDIO_FORMAT_DSD:
1743 id = SND_AUDIOCODEC_DSD;
1744 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301745 case AUDIO_FORMAT_APTX:
1746 id = SND_AUDIOCODEC_APTX;
1747 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301748 default:
1749 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1750 }
1751
1752 return id;
1753}
1754
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001755void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1756 struct audio_usecase *usecase)
1757{
1758 int type = usecase->type;
1759
Dhananjay Kumar14245982017-01-16 20:21:00 +05301760 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301761 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001762 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301763 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301764 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001765 usecase->stream.in->app_type_cfg.app_type);
1766 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1767 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301768 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001769 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001770 } else {
1771 /* No need to send audio calibration for voice and voip call usecases */
1772 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1773 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001774 }
1775}
1776
Ben Rombergera04fabc2014-11-14 12:16:03 -08001777// Base64 Encode and Decode
1778// Not all features supported. This must be used only with following conditions.
1779// Decode Modes: Support with and without padding
1780// CRLF not handling. So no CRLF in string to decode.
1781// Encode Modes: Supports only padding
1782int b64decode(char *inp, int ilen, uint8_t* outp)
1783{
1784 int i, j, k, ii, num;
1785 int rem, pcnt;
1786 uint32_t res=0;
1787 uint8_t getIndex[MAX_BASEINDEX_LEN];
1788 uint8_t tmp, cflag;
1789
1790 if(inp == NULL || outp == NULL || ilen <= 0) {
1791 ALOGE("[%s] received NULL pointer or zero length",__func__);
1792 return -1;
1793 }
1794
1795 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1796 for(i=0;i<BASE_TABLE_SIZE;i++) {
1797 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1798 }
1799 getIndex[(uint8_t)'=']=0;
1800
1801 j=0;k=0;
1802 num = ilen/4;
1803 rem = ilen%4;
1804 if(rem==0)
1805 num = num-1;
1806 cflag=0;
1807 for(i=0; i<num; i++) {
1808 res=0;
1809 for(ii=0;ii<4;ii++) {
1810 res = res << 6;
1811 tmp = getIndex[(uint8_t)inp[j++]];
1812 res = res | tmp;
1813 cflag = cflag | tmp;
1814 }
1815 outp[k++] = (res >> 16)&0xFF;
1816 outp[k++] = (res >> 8)&0xFF;
1817 outp[k++] = res & 0xFF;
1818 }
1819
1820 // Handle last bytes special
1821 pcnt=0;
1822 if(rem == 0) {
1823 //With padding or full data
1824 res = 0;
1825 for(ii=0;ii<4;ii++) {
1826 if(inp[j] == '=')
1827 pcnt++;
1828 res = res << 6;
1829 tmp = getIndex[(uint8_t)inp[j++]];
1830 res = res | tmp;
1831 cflag = cflag | tmp;
1832 }
1833 outp[k++] = res >> 16;
1834 if(pcnt == 2)
1835 goto done;
1836 outp[k++] = (res>>8)&0xFF;
1837 if(pcnt == 1)
1838 goto done;
1839 outp[k++] = res&0xFF;
1840 } else {
1841 //without padding
1842 res = 0;
1843 for(i=0;i<rem;i++) {
1844 res = res << 6;
1845 tmp = getIndex[(uint8_t)inp[j++]];
1846 res = res | tmp;
1847 cflag = cflag | tmp;
1848 }
1849 for(i=rem;i<4;i++) {
1850 res = res << 6;
1851 pcnt++;
1852 }
1853 outp[k++] = res >> 16;
1854 if(pcnt == 2)
1855 goto done;
1856 outp[k++] = (res>>8)&0xFF;
1857 if(pcnt == 1)
1858 goto done;
1859 outp[k++] = res&0xFF;
1860 }
1861done:
1862 if(cflag == 0xFF) {
1863 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1864 __func__, inp);
1865 return 0;
1866 }
1867 return k;
1868}
1869
1870int b64encode(uint8_t *inp, int ilen, char* outp)
1871{
1872 int i,j,k, num;
1873 int rem=0;
1874 uint32_t res=0;
1875
1876 if(inp == NULL || outp == NULL || ilen<=0) {
1877 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1878 return -1;
1879 }
1880
1881 num = ilen/3;
1882 rem = ilen%3;
1883 j=0;k=0;
1884 for(i=0; i<num; i++) {
1885 //prepare index
1886 res = inp[j++]<<16;
1887 res = res | inp[j++]<<8;
1888 res = res | inp[j++];
1889 //get output map from index
1890 outp[k++] = (char) bTable[(res>>18)&0x3F];
1891 outp[k++] = (char) bTable[(res>>12)&0x3F];
1892 outp[k++] = (char) bTable[(res>>6)&0x3F];
1893 outp[k++] = (char) bTable[res&0x3F];
1894 }
1895
1896 switch(rem) {
1897 case 1:
1898 res = inp[j++]<<16;
1899 outp[k++] = (char) bTable[res>>18];
1900 outp[k++] = (char) bTable[(res>>12)&0x3F];
1901 //outp[k++] = '=';
1902 //outp[k++] = '=';
1903 break;
1904 case 2:
1905 res = inp[j++]<<16;
1906 res = res | inp[j++]<<8;
1907 outp[k++] = (char) bTable[res>>18];
1908 outp[k++] = (char) bTable[(res>>12)&0x3F];
1909 outp[k++] = (char) bTable[(res>>6)&0x3F];
1910 //outp[k++] = '=';
1911 break;
1912 default:
1913 break;
1914 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001915 outp[k] = '\0';
1916 return k;
1917}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301918
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301919
1920int audio_extn_utils_get_codec_version(const char *snd_card_name,
1921 int card_num,
1922 char *codec_version)
1923{
1924 char procfs_path[50];
1925 FILE *fp;
1926
1927 if (strstr(snd_card_name, "tasha")) {
1928 snprintf(procfs_path, sizeof(procfs_path),
1929 "/proc/asound/card%d/codecs/tasha/version", card_num);
1930 if ((fp = fopen(procfs_path, "r")) != NULL) {
1931 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1932 fclose(fp);
1933 } else {
1934 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1935 return -ENOENT;
1936 }
1937 ALOGD("%s: codec version %s", __func__, codec_version);
1938 }
1939
1940 return 0;
1941}
1942
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05301943int audio_extn_utils_get_codec_variant(int card_num,
1944 char *codec_variant)
1945{
1946 char procfs_path[50];
1947 FILE *fp;
1948 snprintf(procfs_path, sizeof(procfs_path),
1949 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
1950 if ((fp = fopen(procfs_path, "r")) == NULL) {
1951 snprintf(procfs_path, sizeof(procfs_path),
1952 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
1953 if ((fp = fopen(procfs_path, "r")) == NULL) {
1954 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1955 return -ENOENT;
1956 }
1957 }
1958 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
1959 fclose(fp);
1960 ALOGD("%s: codec variant is %s", __func__, codec_variant);
1961 return 0;
1962}
1963
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301964
Ashish Jain81eb2a82015-05-13 10:52:34 +05301965#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1966
1967void get_default_compressed_channel_status(
1968 unsigned char *channel_status)
1969{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301970 memset(channel_status,0,24);
1971
1972 /* block start bit in preamble bit 3 */
1973 channel_status[0] |= PROFESSIONAL;
1974 //compre out
1975 channel_status[0] |= NON_LPCM;
1976 // sample rate; fixed 48K for default/transcode
1977 channel_status[3] |= SR_48000;
1978}
1979
Ashish Jain81eb2a82015-05-13 10:52:34 +05301980int32_t get_compressed_channel_status(void *audio_stream_data,
1981 uint32_t audio_frame_size,
1982 unsigned char *channel_status,
1983 enum audio_parser_code_type codec_type)
1984 // codec_type - AUDIO_PARSER_CODEC_AC3
1985 // - AUDIO_PARSER_CODEC_DTS
1986{
1987 unsigned char *stream;
1988 int ret = 0;
1989 stream = (unsigned char *)audio_stream_data;
1990
1991 if (audio_stream_data == NULL || audio_frame_size == 0) {
1992 ALOGW("no buffer to get channel status, return default for compress");
1993 get_default_compressed_channel_status(channel_status);
1994 return ret;
1995 }
1996
1997 memset(channel_status,0,24);
1998 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1999 {
2000 ALOGE("init audio parser failed");
2001 return -1;
2002 }
2003 ret = get_channel_status(channel_status, codec_type);
2004 return ret;
2005
2006}
2007
Ashish Jain81eb2a82015-05-13 10:52:34 +05302008void get_lpcm_channel_status(uint32_t sampleRate,
2009 unsigned char *channel_status)
2010{
2011 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302012 memset(channel_status,0,24);
2013 /* block start bit in preamble bit 3 */
2014 channel_status[0] |= PROFESSIONAL;
2015 //LPCM OUT
2016 channel_status[0] &= ~NON_LPCM;
2017
2018 switch (sampleRate) {
2019 case 8000:
2020 case 11025:
2021 case 12000:
2022 case 16000:
2023 case 22050:
2024 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302025 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302026 case 24000:
2027 channel_status[3] |= SR_24000;
2028 break;
2029 case 32000:
2030 channel_status[3] |= SR_32000;
2031 break;
2032 case 44100:
2033 channel_status[3] |= SR_44100;
2034 break;
2035 case 48000:
2036 channel_status[3] |= SR_48000;
2037 break;
2038 case 88200:
2039 channel_status[3] |= SR_88200;
2040 break;
2041 case 96000:
2042 channel_status[3] |= SR_96000;
2043 break;
2044 case 176400:
2045 channel_status[3] |= SR_176400;
2046 break;
2047 case 192000:
2048 channel_status[3] |= SR_192000;
2049 break;
2050 default:
2051 ALOGV("Invalid sample_rate %u\n", sampleRate);
2052 status = -1;
2053 break;
2054 }
2055}
2056
2057void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2058{
2059 unsigned char channel_status[24]={0};
2060 struct snd_aes_iec958 iec958;
2061 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2062 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302063 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002064
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302065 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302066 /*TODO:Extend code to support DTS passthrough*/
2067 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002068 audio_extn_passthru_is_passthrough_stream(out) &&
2069 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302070 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002071 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302072 /*set channel status bit for LPCM*/
2073 get_lpcm_channel_status(out->sample_rate, channel_status);
2074 }
2075
2076 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2077 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2078 if (!ctl) {
2079 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2080 __func__, mixer_ctl_name);
2081 return;
2082 }
2083 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2084 ALOGE("%s: Could not set channel status for ext HDMI ",
2085 __func__);
2086 return;
2087 }
2088
2089}
2090#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302091
2092int audio_extn_utils_get_avt_device_drift(
2093 struct audio_usecase *usecase,
2094 struct audio_avt_device_drift_param *drift_param)
2095{
2096 int ret = 0, count = 0;
2097 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302098 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302099 struct mixer_ctl *ctl = NULL;
2100 struct audio_avt_device_drift_stats drift_stats;
2101 struct audio_device *adev = NULL;
2102
2103 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302104 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2105 if (!backend) {
2106 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002107 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302108 ret = -EINVAL;
2109 goto done;
2110 }
2111 strlcpy(avt_device_drift_mixer_ctl_name,
2112 backend,
2113 MIXER_PATH_MAX_LENGTH);
2114
2115 count = strlen(backend);
2116 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2117 strlcat(&avt_device_drift_mixer_ctl_name[count],
2118 " DRIFT",
2119 MIXER_PATH_MAX_LENGTH - count);
2120 } else {
2121 ret = -EINVAL;
2122 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302123 }
2124 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302125 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302126 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302127 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302128 }
2129
Naresh Tanniru6160c712017-04-17 15:43:48 +05302130 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302131 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2132 if (!ctl) {
2133 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2134 __func__, avt_device_drift_mixer_ctl_name);
2135
2136 ret = -EINVAL;
2137 goto done;
2138 }
2139
2140 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2141 avt_device_drift_mixer_ctl_name);
2142
2143 mixer_ctl_update(ctl);
2144 count = mixer_ctl_get_num_values(ctl);
2145 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2146 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2147 __func__);
2148
2149 ret = -EINVAL;
2150 goto done;
2151 }
2152
2153 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2154 if (ret != 0) {
2155 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2156 __func__);
2157
2158 ret = -EINVAL;
2159 goto done;
2160 }
2161 memcpy(drift_param, &drift_stats.drift_param,
2162 sizeof(struct audio_avt_device_drift_param));
2163done:
2164 return ret;
2165}
Manish Dewangan07de2142017-02-27 19:27:20 +05302166
2167#ifdef SNDRV_COMPRESS_PATH_DELAY
2168int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2169{
2170 int ret = -EINVAL;
2171 struct snd_compr_metadata metadata;
2172 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2173
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002174 /* override the latency for pcm offload use case */
2175 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2176 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2177 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2178 }
2179
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002180 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302181 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2182 if (!(is_offload_usecase(out->usecase))) {
2183 ALOGE("%s:: not supported for non offload session", __func__);
2184 goto exit;
2185 }
2186
2187 if (!out->compr) {
2188 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2189 __func__);
2190 goto exit;
2191 }
2192
2193 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2194 ret = compress_get_metadata(out->compr, &metadata);
2195 if(ret) {
2196 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2197 goto exit;
2198 }
2199 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2200 } else {
2201 ALOGD("%s:: Using Fix DSP delay",__func__);
2202 }
2203
2204exit:
2205 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2206 return delay_ms;
2207}
2208#else
2209int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2210{
2211 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2212}
2213#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302214
2215#ifdef SNDRV_COMPRESS_RENDER_MODE
2216int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2217{
2218 struct snd_compr_metadata metadata;
2219 int ret = -EINVAL;
2220
2221 if (!(is_offload_usecase(out->usecase))) {
2222 ALOGE("%s:: not supported for non offload session", __func__);
2223 goto exit;
2224 }
2225
2226 if (!out->compr) {
2227 ALOGD("%s:: Invalid compress handle",
2228 __func__);
2229 goto exit;
2230 }
2231
2232 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2233
2234 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2235 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2236 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2237 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2238 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2239 } else {
2240 ret = 0;
2241 goto exit;
2242 }
2243 ret = compress_set_metadata(out->compr, &metadata);
2244 if(ret) {
2245 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2246 }
2247exit:
2248 return ret;
2249}
2250#else
2251int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2252{
2253 ALOGD("%s:: configuring render mode not supported", __func__);
2254 return 0;
2255}
2256#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302257
2258#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2259int audio_extn_utils_compress_set_clk_rec_mode(
2260 struct audio_usecase *usecase)
2261{
2262 struct snd_compr_metadata metadata;
2263 struct stream_out *out = NULL;
2264 int ret = -EINVAL;
2265
Naresh Tanniru7586e292017-04-13 10:38:13 +05302266 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302267 ALOGE("%s:: Invalid use case", __func__);
2268 goto exit;
2269 }
2270
2271 out = usecase->stream.out;
2272 if (!out) {
2273 ALOGE("%s:: invalid stream", __func__);
2274 goto exit;
2275 }
2276
2277 if (!is_offload_usecase(out->usecase)) {
2278 ALOGE("%s:: not supported for non offload session", __func__);
2279 goto exit;
2280 }
2281
2282 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2283 ALOGD("%s:: clk recovery is only supported in STC render mode",
2284 __func__);
2285 ret = 0;
2286 goto exit;
2287 }
2288
2289 if (!out->compr) {
2290 ALOGD("%s:: Invalid compress handle",
2291 __func__);
2292 goto exit;
2293 }
2294 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2295 switch(usecase->out_snd_device) {
2296 case SND_DEVICE_OUT_HDMI:
2297 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2298 case SND_DEVICE_OUT_DISPLAY_PORT:
2299 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2300 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2301 break;
2302 default:
2303 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2304 break;
2305 }
2306
2307 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2308
2309 ret = compress_set_metadata(out->compr, &metadata);
2310 if(ret) {
2311 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2312 }
2313
2314exit:
2315 return ret;
2316}
2317#else
2318int audio_extn_utils_compress_set_clk_rec_mode(
2319 struct audio_usecase *usecase __unused)
2320{
2321 ALOGD("%s:: configuring render mode not supported", __func__);
2322 return 0;
2323}
2324#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302325
2326#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2327int audio_extn_utils_compress_set_render_window(
2328 struct stream_out *out,
2329 struct audio_out_render_window_param *render_window)
2330{
2331 struct snd_compr_metadata metadata;
2332 int ret = -EINVAL;
2333
Manish Dewangan27346042017-03-01 12:56:12 +05302334 if(render_window == NULL) {
2335 ALOGE("%s:: Invalid render_window", __func__);
2336 goto exit;
2337 }
2338
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002339 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2340 __func__,render_window->render_ws, render_window->render_we);
2341
Manish Dewangan27346042017-03-01 12:56:12 +05302342 if (!is_offload_usecase(out->usecase)) {
2343 ALOGE("%s:: not supported for non offload session", __func__);
2344 goto exit;
2345 }
2346
Naresh Tanniru6160c712017-04-17 15:43:48 +05302347 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2348 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302349 ALOGD("%s:: only supported in timestamp mode, current "
2350 "render mode mode %d", __func__, out->render_mode);
2351 goto exit;
2352 }
2353
2354 if (!out->compr) {
2355 ALOGW("%s:: offload session not yet opened,"
2356 "render window will be configure later", __func__);
2357 /* store render window to reconfigure in start_output_stream() */
2358 goto exit;
2359 }
2360
2361 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2362 /*render window start value */
2363 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2364 metadata.value[1] = \
2365 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2366 /*render window end value */
2367 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2368 metadata.value[3] = \
2369 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2370
2371 ret = compress_set_metadata(out->compr, &metadata);
2372 if(ret) {
2373 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2374 }
2375
2376exit:
2377 return ret;
2378}
2379#else
2380int audio_extn_utils_compress_set_render_window(
2381 struct stream_out *out __unused,
2382 struct audio_out_render_window_param *render_window __unused)
2383{
2384 ALOGD("%s:: configuring render window not supported", __func__);
2385 return 0;
2386}
2387#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302388
2389#ifdef SNDRV_COMPRESS_START_DELAY
2390int audio_extn_utils_compress_set_start_delay(
2391 struct stream_out *out,
2392 struct audio_out_start_delay_param *delay_param)
2393{
2394 struct snd_compr_metadata metadata;
2395 int ret = -EINVAL;
2396
2397 if(delay_param == NULL) {
2398 ALOGE("%s:: Invalid delay_param", __func__);
2399 goto exit;
2400 }
2401
2402 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2403 delay_param->start_delay);
2404
2405 if (!is_offload_usecase(out->usecase)) {
2406 ALOGE("%s:: not supported for non offload session", __func__);
2407 goto exit;
2408 }
2409
Naresh Tanniru6160c712017-04-17 15:43:48 +05302410 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2411 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302412 ALOGD("%s:: only supported in timestamp mode, current "
2413 "render mode mode %d", __func__, out->render_mode);
2414 goto exit;
2415 }
2416
2417 if (!out->compr) {
2418 ALOGW("%s:: offload session not yet opened,"
2419 "start delay will be configure later", __func__);
2420 goto exit;
2421 }
2422
2423 metadata.key = SNDRV_COMPRESS_START_DELAY;
2424 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2425 metadata.value[1] = \
2426 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2427
2428 ret = compress_set_metadata(out->compr, &metadata);
2429 if(ret) {
2430 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2431 }
2432
2433exit:
2434 return ret;
2435}
2436#else
2437int audio_extn_utils_compress_set_start_delay(
2438 struct stream_out *out __unused,
2439 struct audio_out_start_delay_param *delay_param __unused)
2440{
2441 ALOGD("%s:: configuring render window not supported", __func__);
2442 return 0;
2443}
2444#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002445
Surendar Karka287348c2019-04-10 18:31:46 +05302446#ifdef SNDRV_COMPRESS_DSP_POSITION
2447int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2448 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2449{
2450 int ret = -EINVAL;
2451 uint64_t *val = NULL;
2452 uint64_t time = 0;
2453 struct snd_compr_metadata metadata;
2454
2455 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2456 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2457 metadata.value[0] = clock_id;
2458 ret = compress_get_metadata(out->compr, &metadata);
2459 if (ret) {
2460 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2461 ret = -errno;
2462 goto exit;
2463 }
2464 val = (uint64_t *)&metadata.value[1];
2465 *frames = *val;
2466 time = *(val + 1);
2467 timestamp->tv_sec = time / 1000000;
2468 timestamp->tv_nsec = (time % 1000000)*1000;
2469
2470exit:
2471 return ret;
2472}
2473#else
2474int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2475 uint64_t *frames __unused, struct timespec *timestamp __unused,
2476 int32_t clock_id __unused)
2477{
2478 ALOGD("%s:: dsp presentation position not supported", __func__);
2479 return 0;
2480
2481}
2482#endif
2483
2484#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2485int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2486 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2487{
2488 int ret = -EINVAL;
2489 uint64_t time = 0;
2490 struct snd_pcm_prsnt_position prsnt_position;
2491
2492 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2493 prsnt_position.clock_id = clock_id;
2494 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2495 if (ret) {
2496 ALOGE("%s::error %d", __func__, ret);
2497 ret = -EIO;
2498 goto exit;
2499 }
2500
2501 *frames = prsnt_position.frames;
2502 time = prsnt_position.timestamp;
2503 timestamp->tv_sec = time / 1000000;
2504 timestamp->tv_nsec = (time % 1000000)*1000;
2505
2506exit:
2507 return ret;
2508}
2509#else
2510int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2511 uint64_t *frames __unused, struct timespec *timestamp __unused,
2512 int32_t clock_id __unused)
2513{
2514 ALOGD("%s:: dsp presentation position not supported", __func__);
2515 return 0;
2516
2517}
2518#endif
2519
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002520#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302521#define RETRY_US 1000000
2522#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002523#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2524#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2525
2526#ifdef LINUX_ENABLED
2527static const char *kConfigLocationList[] =
2528 {"/etc"};
2529#else
2530static const char *kConfigLocationList[] =
2531 {"/vendor/etc"};
2532#endif
2533static const int kConfigLocationListSize =
2534 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
2535
2536bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2537{
2538 char full_config_path[MIXER_PATH_MAX_LENGTH];
2539 for (int i = 0; i < kConfigLocationListSize; i++) {
2540 snprintf(full_config_path,
2541 MIXER_PATH_MAX_LENGTH,
2542 "%s/%s",
2543 kConfigLocationList[i],
2544 file_name);
2545 if (F_OK == access(full_config_path, 0)) {
Weiyin Jiang2995f662019-04-17 14:25:12 +08002546 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
Aalique Grahame22e49102018-12-18 14:23:57 -08002547 return true;
2548 }
2549 }
2550 return false;
2551}
2552
2553/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2554int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2555{
2556 if (NULL == snd_card_name) {
2557 return -1;
2558 }
2559
2560 struct snd_card_split *snd_split_handle = NULL;
2561 int ret = 0;
2562 audio_extn_set_snd_card_split(snd_card_name);
2563 snd_split_handle = audio_extn_get_snd_card_split();
2564
2565 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2566 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2567 snd_split_handle->form_factor);
2568
2569 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2570 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2571 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2572 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2573
2574 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2575 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2576 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2577 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2578 }
2579 }
2580
2581 return ret;
2582}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002583
2584int audio_extn_utils_get_snd_card_num()
2585{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302586 int snd_card_num = 0;
2587 struct mixer *mixer = NULL;
2588
2589 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2590 if (mixer)
2591 mixer_close(mixer);
2592 return snd_card_num;
2593}
2594
2595int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2596{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002597
2598 void *hw_info = NULL;
2599 struct mixer *mixer = NULL;
2600 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002601 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002602 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002603 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002604
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302605 if (!mixer_handle) {
2606 ALOGE("invalid mixer handle");
2607 return -1;
2608 }
2609 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302610 /*
mpangfaa7bae2019-01-15 16:38:13 +08002611 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302612 * sleep for 1 sec and try detections with sound card 0 again.
2613 * If sound card gets detected, check if it is relevant, if not check with the
2614 * other sound cards. To ensure that the irrelevant sound card is not check again,
2615 * we maintain it in min_snd_card_num.
2616 */
2617 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002618 snd_card_num = 0;
2619 while (snd_card_num < MAX_SND_CARD) {
2620 if (snd_card_detection_info[snd_card_num] == 0) {
2621 mixer = mixer_open(snd_card_num);
2622 if (!mixer)
2623 snd_card_num++;
2624 else
2625 break;
2626 } else
2627 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002628 }
2629
2630 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302631 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302632 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002633 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302634 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002635 }
2636
2637 snd_card_name = strdup(mixer_get_name(mixer));
2638 if (!snd_card_name) {
2639 ALOGE("failed to allocate memory for snd_card_name\n");
2640 mixer_close(mixer);
2641 return -1;
2642 }
2643 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002644 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002645 hw_info = hw_info_init(snd_card_name);
2646 if (hw_info) {
2647 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2648 break;
2649 }
mpangfaa7bae2019-01-15 16:38:13 +08002650 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002651
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302652 free(snd_card_name);
2653 snd_card_name = NULL;
2654
2655 mixer_close(mixer);
2656 mixer = NULL;
2657 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002658 if (snd_card_name)
2659 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302660
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302661 if (hw_info)
2662 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002663
mpangfaa7bae2019-01-15 16:38:13 +08002664 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002665 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2666 return -1;
2667 }
2668
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302669 if (mixer)
2670 *mixer_handle = mixer;
2671
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002672 return snd_card_num;
2673}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302674
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302675void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2676{
2677 if (mixer)
2678 mixer_close(mixer);
2679}
2680
Naresh Tanniru6160c712017-04-17 15:43:48 +05302681#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2682int audio_extn_utils_compress_enable_drift_correction(
2683 struct stream_out *out,
2684 struct audio_out_enable_drift_correction *drift)
2685{
2686 struct snd_compr_metadata metadata;
2687 int ret = -EINVAL;
2688
2689 if(drift == NULL) {
2690 ALOGE("%s:: Invalid param", __func__);
2691 goto exit;
2692 }
2693
2694 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2695
2696 if (!is_offload_usecase(out->usecase)) {
2697 ALOGE("%s:: not supported for non offload session", __func__);
2698 goto exit;
2699 }
2700
2701 if (!out->compr) {
2702 ALOGW("%s:: offload session not yet opened,"
2703 "start delay will be configure later", __func__);
2704 goto exit;
2705 }
2706
2707 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2708 metadata.value[0] = drift->enable;
2709 out->drift_correction_enabled = drift->enable;
2710
2711 ret = compress_set_metadata(out->compr, &metadata);
2712 if(ret) {
2713 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2714 out->drift_correction_enabled = false;
2715 }
2716
2717exit:
2718 return ret;
2719}
2720#else
2721int audio_extn_utils_compress_enable_drift_correction(
2722 struct stream_out *out __unused,
2723 struct audio_out_enable_drift_correction *drift __unused)
2724{
2725 ALOGD("%s:: configuring drift enablement not supported", __func__);
2726 return 0;
2727}
2728#endif
2729
2730#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2731int audio_extn_utils_compress_correct_drift(
2732 struct stream_out *out,
2733 struct audio_out_correct_drift *drift_param)
2734{
2735 struct snd_compr_metadata metadata;
2736 int ret = -EINVAL;
2737
2738 if (drift_param == NULL) {
2739 ALOGE("%s:: Invalid drift_param", __func__);
2740 goto exit;
2741 }
2742
2743 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2744 drift_param->adjust_time);
2745
2746 if (!is_offload_usecase(out->usecase)) {
2747 ALOGE("%s:: not supported for non offload session", __func__);
2748 goto exit;
2749 }
2750
2751 if (!out->compr) {
2752 ALOGW("%s:: offload session not yet opened", __func__);
2753 goto exit;
2754 }
2755
2756 if (!out->drift_correction_enabled) {
2757 ALOGE("%s:: drift correction not enabled", __func__);
2758 goto exit;
2759 }
2760
2761 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2762 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2763 metadata.value[1] = \
2764 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2765
2766 ret = compress_set_metadata(out->compr, &metadata);
2767 if(ret)
2768 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2769exit:
2770 return ret;
2771}
2772#else
2773int audio_extn_utils_compress_correct_drift(
2774 struct stream_out *out __unused,
2775 struct audio_out_correct_drift *drift_param __unused)
2776{
2777 ALOGD("%s:: setting adjust clock not supported", __func__);
2778 return 0;
2779}
2780#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302781
2782int audio_extn_utils_set_channel_map(
2783 struct stream_out *out,
2784 struct audio_out_channel_map_param *channel_map_param)
2785{
2786 int ret = -EINVAL, i = 0;
2787 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2788
2789 if (channel_map_param == NULL) {
2790 ALOGE("%s:: Invalid channel_map", __func__);
2791 goto exit;
2792 }
2793
2794 if (channel_map_param->channels != channels) {
2795 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2796 __func__, channel_map_param->channels, channels);
2797 goto exit;
2798 }
2799
2800 for ( i = 0; i < channels; i++) {
2801 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2802 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2803 }
2804 ret = 0;
2805exit:
2806 return ret;
2807}
Varun Balaraje49253e2017-07-06 19:48:56 +05302808
2809int audio_extn_utils_set_pan_scale_params(
2810 struct stream_out *out,
2811 struct mix_matrix_params *mm_params)
2812{
2813 int ret = -EINVAL, i = 0, j = 0;
2814
Varun Balaraj003ee752017-08-07 17:54:55 +05302815 if (mm_params == NULL || out == NULL) {
2816 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302817 goto exit;
2818 }
2819
2820 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2821 mm_params->num_output_channels <= 0 ||
2822 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2823 mm_params->num_input_channels <= 0)
2824 goto exit;
2825
2826 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2827 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2828 out->pan_scale_params.has_output_channel_map =
2829 mm_params->has_output_channel_map;
2830 for (i = 0; i < mm_params->num_output_channels; i++)
2831 out->pan_scale_params.output_channel_map[i] =
2832 mm_params->output_channel_map[i];
2833
2834 out->pan_scale_params.has_input_channel_map =
2835 mm_params->has_input_channel_map;
2836 for (i = 0; i < mm_params->num_input_channels; i++)
2837 out->pan_scale_params.input_channel_map[i] =
2838 mm_params->input_channel_map[i];
2839
2840 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2841 for (i = 0; i < mm_params->num_output_channels; i++)
2842 for (j = 0; j < mm_params->num_input_channels; j++) {
2843 //Convert the channel coefficient gains in Q14 format
2844 out->pan_scale_params.mixer_coeffs[i][j] =
2845 mm_params->mixer_coeffs[i][j] * (2 << 13);
2846 }
2847
2848 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2849 out->pcm_device_id,
2850 out->pan_scale_params);
2851
2852exit:
2853 return ret;
2854}
2855
2856int audio_extn_utils_set_downmix_params(
2857 struct stream_out *out,
2858 struct mix_matrix_params *mm_params)
2859{
2860 int ret = -EINVAL, i = 0, j = 0;
2861 struct audio_usecase *usecase = NULL;
2862
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002863 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302864 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302865 goto exit;
2866 }
2867
2868 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2869 mm_params->num_output_channels <= 0 ||
2870 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2871 mm_params->num_input_channels <= 0)
2872 goto exit;
2873
2874 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302875 if (!usecase) {
2876 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002877 goto exit;
2878 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302879 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2880 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2881
2882 out->downmix_params.has_output_channel_map =
2883 mm_params->has_output_channel_map;
2884 for (i = 0; i < mm_params->num_output_channels; i++) {
2885 out->downmix_params.output_channel_map[i] =
2886 mm_params->output_channel_map[i];
2887 }
2888
2889 out->downmix_params.has_input_channel_map =
2890 mm_params->has_input_channel_map;
2891 for (i = 0; i < mm_params->num_input_channels; i++)
2892 out->downmix_params.input_channel_map[i] =
2893 mm_params->input_channel_map[i];
2894
2895 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2896 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302897 for (j = 0; j < mm_params->num_input_channels; j++) {
2898 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302899 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302900 mm_params->mixer_coeffs[i][j] * (2 << 13);
2901 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302902
2903 ret = platform_set_stream_downmix_params(out->dev->platform,
2904 out->pcm_device_id,
2905 usecase->out_snd_device,
2906 out->downmix_params);
2907
2908exit:
2909 return ret;
2910}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302911
2912bool audio_extn_utils_is_dolby_format(audio_format_t format)
2913{
2914 if (format == AUDIO_FORMAT_AC3 ||
2915 format == AUDIO_FORMAT_E_AC3 ||
2916 format == AUDIO_FORMAT_E_AC3_JOC)
2917 return true;
2918 else
2919 return false;
2920}
2921
`Deeraj Soman676c2702017-09-18 19:25:53 +05302922int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2923{
2924 int i;
2925 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2926 {"S32_LE", 32},
2927 {"S24_LE", 24},
2928 {"S16_LE", 16}};
2929 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302930
`Deeraj Soman676c2702017-09-18 19:25:53 +05302931 for (i = 0; i < num_configs; i++) {
2932 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2933 return mixer_bitwidth_config[i].value;
2934 }
2935
2936 return -EINVAL;
2937}
2938
2939int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2940{
2941 int i;
2942 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2943 {"KHZ_48", 48000},
2944 {"KHZ_96", 96000},
2945 {"KHZ_144", 144000},
2946 {"KHZ_192", 192000},
2947 {"KHZ_384", 384000},
2948 {"KHZ_44P1", 44100},
2949 {"KHZ_88P2", 88200},
2950 {"KHZ_176P4", 176400},
2951 {"KHZ_352P8", 352800}};
2952 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2953
2954 for (i = 0; i < num_configs; i++) {
2955 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2956 return mixer_samplerate_config[i].value;
2957 }
2958
2959 return -EINVAL;
2960}
2961
2962int audio_extn_utils_get_channels_from_string(const char *id_string)
2963{
2964 int i;
2965 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2966 {"Two", 2},
2967 {"Three",3},
2968 {"Four", 4},
2969 {"Five", 5},
2970 {"Six", 6},
2971 {"Seven", 7},
2972 {"Eight", 8}};
2973 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2974
2975 for (i = 0; i < num_configs; i++) {
2976 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2977 return mixer_channels_config[i].value;
2978 }
2979
2980 return -EINVAL;
2981}
Surendar karka30569792018-05-08 12:02:21 +05302982
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002983void audio_extn_utils_release_snd_device(snd_device_t snd_device)
2984{
2985 audio_extn_dev_arbi_release(snd_device);
2986 audio_extn_sound_trigger_update_device_status(snd_device,
2987 ST_EVENT_SND_DEVICE_FREE);
2988 audio_extn_listen_update_device_status(snd_device,
2989 LISTEN_EVENT_SND_DEVICE_FREE);
2990}
2991
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08002992int audio_extn_utils_get_license_params(
2993 const struct audio_device *adev,
2994 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05302995{
2996 if(!license_params)
2997 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08002998
2999 return platform_get_license_by_product(adev->platform,
3000 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303001}
3002
Aalique Grahame22e49102018-12-18 14:23:57 -08003003int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3004 int app_type,
3005 int *gain)
3006{
3007 int gain_cfg[4];
3008 const char *mixer_ctl_name = "App Type Gain";
3009 struct mixer_ctl *ctl;
3010 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3011 if (!ctl) {
3012 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3013 mixer_ctl_name);
3014 return -EINVAL;
3015 }
3016 gain_cfg[0] = 0;
3017 gain_cfg[1] = app_type;
3018 gain_cfg[2] = gain[0];
3019 gain_cfg[3] = gain[1];
3020 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3021 return mixer_ctl_set_array(ctl, gain_cfg,
3022 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3023}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003024
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003025static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003026{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003027 if (mVndkFwk.lib_handle != NULL)
3028 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003029
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003030 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3031 if (mVndkFwk.lib_handle == NULL) {
3032 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3033 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003034 }
3035
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003036 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3037 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3038 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3039 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3040 if (mVndkFwk.lib_handle) {
3041 dlclose(mVndkFwk.lib_handle);
3042 mVndkFwk.lib_handle = NULL;
3043 }
3044 return;
3045 }
3046
3047
3048 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3049 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3050 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3051 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3052 if (mVndkFwk.lib_handle) {
3053 dlclose(mVndkFwk.lib_handle);
3054 mVndkFwk.lib_handle = NULL;
3055 }
3056 }
3057
3058 return;
3059}
3060
3061bool audio_extn_utils_is_vendor_enhanced_fwk()
3062{
3063 static int is_vendor_enhanced_fwk = -EINVAL;
3064 if (is_vendor_enhanced_fwk != -EINVAL)
3065 return (bool)is_vendor_enhanced_fwk;
3066
3067 vndk_fwk_init();
3068
3069 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3070 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3071 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3072 } else {
3073 is_vendor_enhanced_fwk = 0;
3074 ALOGW("%s: default to non enhanced_fwk config", __func__);
3075 }
3076
3077 return (bool)is_vendor_enhanced_fwk;
3078}
3079
3080int audio_extn_utils_get_vendor_enhanced_info()
3081{
3082 static int vendor_enhanced_info = -EINVAL;
3083 if (vendor_enhanced_info != -EINVAL)
3084 return vendor_enhanced_info;
3085
3086 vndk_fwk_init();
3087
3088 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3089 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3090 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3091 } else {
3092 vendor_enhanced_info = 0x0;
3093 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3094 }
3095
3096 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003097}
Deeraj Soman14230922019-01-30 16:39:30 +05303098
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303099int audio_extn_utils_get_perf_mode_flag(void)
3100{
3101#ifdef COMPRESSED_PERF_MODE_FLAG
3102 return COMPRESSED_PERF_MODE_FLAG;
3103#else
3104 return 0;
3105#endif
3106}
3107
Deeraj Soman14230922019-01-30 16:39:30 +05303108size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3109 audio_format_t format,
3110 int channel_count,
3111 int64_t duration_ms,
3112 bool is_low_latency)
3113{
3114 size_t size = 0;
3115 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3116 uint32_t bytes_per_period_sample = 0;
3117
3118
3119 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3120 return 0;
3121
3122 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3123 capture_duration = duration_ms;
3124
3125 size = (sample_rate * capture_duration) / 1000;
3126 if (is_low_latency)
3127 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3128
3129
3130 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3131 size *= bytes_per_period_sample;
3132
3133 /* make sure the size is multiple of 32 bytes and additionally multiple of
3134 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3135 * At 48 kHz mono 16-bit PCM:
3136 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3137 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3138 *
3139 * The loop reaches result within 32 iterations, as initial size is
3140 * already a multiple of frame_size
3141 */
3142 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3143
3144 return size;
3145}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003146
3147int audio_extn_utils_hash_fn(void *key)
3148{
3149 return (int)key;
3150}
3151
3152bool audio_extn_utils_hash_eq(void *key1, void *key2)
3153{
3154 return (key1 == key2);
3155}