blob: 66d7441533b22e37e4513f12611f9e84cb25c972 [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
Roopesh Nataraja0f68be72020-04-28 12:32:02 -070057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME "audio_io_policy.conf"
58#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME "audio_output_policy.conf"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059
60#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053061#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define DYNAMIC_VALUE_TAG "dynamic"
64#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053065#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066#define FORMATS_TAG "formats"
67#define SAMPLING_RATES_TAG "sampling_rates"
68#define BIT_WIDTH_TAG "bit_width"
69#define APP_TYPE_TAG "app_type"
70
71#define STRING_TO_ENUM(string) { #string, string }
72#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73
Ben Rombergera04fabc2014-11-14 12:16:03 -080074#define BASE_TABLE_SIZE 64
75#define MAX_BASEINDEX_LEN 256
76
Ben Romberger1aaaf862017-04-06 17:49:46 -070077#ifndef SND_AUDIOCODEC_TRUEHD
78#define SND_AUDIOCODEC_TRUEHD 0x00000023
79#endif
80
Vikram Panduranga93f080e2017-06-07 18:16:14 -070081#define APP_TYPE_VOIP_AUDIO 0x1113A
82
Ashish Jain81eb2a82015-05-13 10:52:34 +053083#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
84#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
85#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
86#define SR_44100 (0<<0) /* 44.1kHz */
87#define SR_NOTID (1<<0) /* non indicated */
88#define SR_48000 (2<<0) /* 48kHz */
89#define SR_32000 (3<<0) /* 32kHz */
90#define SR_22050 (4<<0) /* 22.05kHz */
91#define SR_24000 (6<<0) /* 24kHz */
92#define SR_88200 (8<<0) /* 88.2kHz */
93#define SR_96000 (10<<0) /* 96kHz */
94#define SR_176400 (12<<0) /* 176.4kHz */
95#define SR_192000 (14<<0) /* 192kHz */
96
97#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053098
99/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800100#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
101#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
Samyak Jain2546f8a2019-08-29 19:24:21 +0530102#define PCM_OFFLOAD_PLAYBACK_LATENCY PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY
Manish Dewangan07de2142017-02-27 19:27:20 +0530103
Varun Balaraje49253e2017-07-06 19:48:56 +0530104#ifndef MAX_CHANNELS_SUPPORTED
105#define MAX_CHANNELS_SUPPORTED 8
106#endif
107
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700108#ifdef __LP64__
109#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
110#else
111#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
112#endif
113
Weiyin Jiangd5718bd2019-09-04 16:52:08 +0800114typedef struct vndkfwk_s {
115 void *lib_handle;
116 int (*isVendorEnhancedFwk)(void);
117 int (*getVendorEnhancedInfo)(void);
118 const char *lib_name;
119} vndkfwk_t;
120
121static vndkfwk_t mVndkFwk = {
122 NULL, NULL, NULL, VNDK_FWK_LIB_PATH};
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -0700123
`Deeraj Soman676c2702017-09-18 19:25:53 +0530124typedef struct {
125 const char *id_string;
126 const int value;
127} mixer_config_lookup;
128
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700129struct string_to_enum {
130 const char *name;
131 uint32_t value;
132};
133
134const struct string_to_enum s_flag_name_to_enum_table[] = {
135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
140 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
141 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700142 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700144 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530145 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700146 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700147 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530148 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Derek Chen090dfc52018-03-22 22:15:29 -0400149 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_MEDIA),
150 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION),
151 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NAV_GUIDANCE),
152 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PHONE),
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800153 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FRONT_PASSENGER),
Derek Chendf05eea2019-08-01 13:57:49 -0700154 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_REAR_SEAT),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530155 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
156 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
157 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
158 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
159 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530160 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530161 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200162 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
George Gao3d477082020-09-17 22:29:07 -0700163 STRING_TO_ENUM(AUDIO_INPUT_FLAG_MMAP_NOIRQ),
164 STRING_TO_ENUM(AUDIO_INPUT_FLAG_VOIP_TX),
165 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_AV_SYNC),
166 STRING_TO_ENUM(AUDIO_INPUT_FLAG_DIRECT),
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 }
George Gao3d477082020-09-17 22:29:07 -0700250 ALOGE("%s cound not find %s", __func__, name);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251 return 0;
252}
253
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530254static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255{
256 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530257 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800258 char *last_r;
259 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700260 while (flag_name != NULL) {
261 if (strlen(flag_name) != 0) {
262 flag |= string_to_enum(s_flag_name_to_enum_table,
263 ARRAY_SIZE(s_flag_name_to_enum_table),
264 flag_name);
265 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800266 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700267 }
268
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800269 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530270 io_flags.in_flags = (audio_input_flags_t)flag;
271 io_flags.out_flags = (audio_output_flags_t)flag;
272 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700273}
274
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530275static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700276{
277 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800278 char *last_r;
279 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280
281 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
282 return;
283
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700285 while (str != NULL) {
286 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
287 ARRAY_SIZE(s_format_name_to_enum_table), str);
288 ALOGV("%s: format - %d", __func__, format);
289 if (format != 0) {
290 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700291 if (sf_info == NULL)
292 break; /* return whatever was parsed */
293
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700294 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530295 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800297 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700298 }
299}
300
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530301static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700303 struct stream_sample_rate *ss_info = NULL;
304 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800305 char *last_r;
306 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700307
Amit Shekhar6f461b12014-08-01 14:52:58 -0700308 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
309 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700310
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530311 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700312 while (str != NULL) {
313 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
314 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
315 if (0 != sample_rate) {
316 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800317 if (!ss_info) {
318 ALOGE("%s: memory allocation failure", __func__);
319 return;
320 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700321 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530322 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700323 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800324 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700325 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700326}
327
328static int parse_bit_width_names(char *name)
329{
330 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800331 char *last_r;
332 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700333
334 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
335 bit_width = (int)strtol(str, (char **)NULL, 10);
336
337 ALOGV("%s: bit_width - %d", __func__, bit_width);
338 return bit_width;
339}
340
341static int parse_app_type_names(void *platform, char *name)
342{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700343 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800344 char *last_r;
345 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700346
347 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
348 app_type = (int)strtol(str, (char **)NULL, 10);
349
350 ALOGV("%s: app_type - %d", __func__, app_type);
351 return app_type;
352}
353
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530354static void update_streams_cfg_list(cnode *root, void *platform,
355 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700356{
357 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700359
360 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530361 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700362
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530363 if (!s_info) {
364 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700365 return;
366 }
367
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368 while (node) {
369 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530370 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530371 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
372 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700375 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530376 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
377 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700378 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530379 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700380 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530381 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 }
383 node = node->next;
384 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386}
387
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530388static void load_cfg_list(cnode *root, void *platform,
389 struct listnode *streams_output_cfg_list,
390 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700391{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530392 cnode *node = NULL;
393
394 node = config_find(root, OUTPUTS_TAG);
395 if (node != NULL) {
396 node = node->first_child;
397 while (node) {
398 ALOGV("%s: loading output %s", __func__, node->name);
399 update_streams_cfg_list(node, platform, streams_output_cfg_list);
400 node = node->next;
401 }
402 } else {
403 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700404 }
405
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530406 node = config_find(root, INPUTS_TAG);
407 if (node != NULL) {
408 node = node->first_child;
409 while (node) {
410 ALOGV("%s: loading input %s", __func__, node->name);
411 update_streams_cfg_list(node, platform, streams_input_cfg_list);
412 node = node->next;
413 }
414 } else {
415 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700416 }
417}
418
419static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530420 struct listnode *streams_output_cfg_list,
421 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700422{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530423 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700424 int length = 0, i, num_app_types = 0;
425 struct listnode *node;
426 bool update;
427 struct mixer_ctl *ctl = NULL;
428 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530429 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800430 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700431
432 if (!mixer) {
433 ALOGE("%s: mixer is null",__func__);
434 return;
435 }
436 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
437 if (!ctl) {
438 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
439 return;
440 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530441 app_type_cfg[length++] = num_app_types;
442
443 if (list_empty(streams_output_cfg_list)) {
444 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700445 app_type_cfg[length++] = 48000;
446 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530447 num_app_types += 1;
448 }
449 if (list_empty(streams_input_cfg_list)) {
450 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
451 app_type_cfg[length++] = 48000;
452 app_type_cfg[length++] = 16;
453 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 }
455
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800456 /* get target bit width for ADM enforce mode */
457 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
458
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700459 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700461 update = true;
462 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530463 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700464 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530465 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530466 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530467 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530468 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530469 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800470 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
471 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
472 (target_bit_width > app_type_cfg[i+3]))
473 app_type_cfg[i+3] = target_bit_width;
474
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700475 update = false;
476 break;
477 }
478 }
479 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530480 num_app_types += 1;
481 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
482 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800483 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
484 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
485 (target_bit_width > app_type_cfg[length]))
486 app_type_cfg[length] = target_bit_width;
487
488 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530489 }
490 }
491 list_for_each(node, streams_input_cfg_list) {
492 s_info = node_to_item(node, struct streams_io_cfg, list);
493 update = true;
494 for (i=0; i<length; i=i+3) {
495 if (app_type_cfg[i+1] == 0)
496 break;
497 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530498 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530499 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530500 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530501 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
502 update = false;
503 break;
504 }
505 }
506 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
507 num_app_types += 1;
508 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
509 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
510 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700511 }
512 }
513 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
514 if (num_app_types) {
515 app_type_cfg[0] = num_app_types;
516 mixer_ctl_set_array(ctl, app_type_cfg, length);
517 }
518}
519
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700520/* Function to retrieve audio vendor configs path */
521void audio_get_vendor_config_path (char* config_file_path, int path_size)
522{
523 char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
524 if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
525#ifdef LINUX_ENABLED
526 /* Audio configs are stored in /etc */
527 snprintf(config_file_path, path_size, "%s", "/etc");
528#else
529 /* Audio configs are stored in /vendor/etc */
530 snprintf(config_file_path, path_size, "%s", "/vendor/etc");
531#endif
532 } else {
533 /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
534 snprintf(config_file_path, path_size,
535 "%s%s", "/vendor/etc/audio/sku_", vendor_sku);
536 }
537}
538
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530539void audio_extn_utils_update_streams_cfg_lists(void *platform,
540 struct mixer *mixer,
541 struct listnode *streams_output_cfg_list,
542 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700543{
544 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530545 char *data = NULL;
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700546 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
547 char audio_io_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
548 char audio_output_policy_file[VENDOR_CONFIG_FILE_MAX_LENGTH];
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700549
550 ALOGV("%s", __func__);
551 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530552 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700553
554 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700555 if (root == NULL) {
556 ALOGE("cfg_list, NULL config root");
557 return;
558 }
559
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700560 /* Get path for audio configuration files in vendor */
561 audio_get_vendor_config_path(vendor_config_path,
562 sizeof(vendor_config_path));
563
564 /* Get path for audio_io_policy_file in vendor */
565 snprintf(audio_io_policy_file, sizeof(audio_io_policy_file),
566 "%s/%s", vendor_config_path, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE_NAME);
567
568 /* Load audio_io_policy_file from vendor */
569 data = (char *)load_file(audio_io_policy_file, NULL);
570
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530571 if (data == NULL) {
572 ALOGD("%s: failed to open io config file(%s), trying older config file",
Roopesh Nataraja0f68be72020-04-28 12:32:02 -0700573 __func__, audio_io_policy_file);
574
575 /* Get path for audio_output_policy_file in vendor */
576 snprintf(audio_output_policy_file, sizeof(audio_output_policy_file),
577 "%s/%s", vendor_config_path,
578 AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE_NAME);
579
580 /* Load audio_output_policy_file from vendor */
581 data = (char *)load_file(audio_output_policy_file, NULL);
582
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530583 if (data == NULL) {
584 send_app_type_cfg(platform, mixer,
585 streams_output_cfg_list,
586 streams_input_cfg_list);
587 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800588 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530589 return;
590 }
591 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700592
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530593 config_load(root, data);
594 load_cfg_list(root, platform, streams_output_cfg_list,
595 streams_input_cfg_list);
596
597 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
598 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800599
600 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800601 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800602 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700603}
604
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530605static void audio_extn_utils_dump_streams_cfg_list(
606 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700607{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700608 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530609 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700610 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700611 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530612
613 list_for_each(node_i, streams_cfg_list) {
614 s_info = node_to_item(node_i, struct streams_io_cfg, list);
615 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
616 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
617 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
618 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700619 sf_info = node_to_item(node_j, struct stream_format, list);
620 ALOGV("format-%x", sf_info->format);
621 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530622 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700623 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
624 ALOGV("sample rate-%d", ss_info->sample_rate);
625 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700626 }
627}
628
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530629void audio_extn_utils_dump_streams_cfg_lists(
630 struct listnode *streams_output_cfg_list,
631 struct listnode *streams_input_cfg_list)
632{
633 ALOGV("%s", __func__);
634 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
635 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
636}
637
638static void audio_extn_utils_release_streams_cfg_list(
639 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700640{
641 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530642 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700643
644 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530645
646 while (!list_empty(streams_cfg_list)) {
647 node_i = list_head(streams_cfg_list);
648 s_info = node_to_item(node_i, struct streams_io_cfg, list);
649 while (!list_empty(&s_info->format_list)) {
650 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700651 list_remove(node_j);
652 free(node_to_item(node_j, struct stream_format, list));
653 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530654 while (!list_empty(&s_info->sample_rate_list)) {
655 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700656 list_remove(node_j);
657 free(node_to_item(node_j, struct stream_sample_rate, list));
658 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700659 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530660 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700661 }
662}
663
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530664void audio_extn_utils_release_streams_cfg_lists(
665 struct listnode *streams_output_cfg_list,
666 struct listnode *streams_input_cfg_list)
667{
668 ALOGV("%s", __func__);
669 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
670 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
671}
672
673static bool set_app_type_cfg(struct streams_io_cfg *s_info,
674 struct stream_app_type_cfg *app_type_cfg,
675 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700676 {
677 struct listnode *node_i;
678 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530679 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700680 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
681 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530682 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700683
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530684 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700685 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530686 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700687 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
688 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
689 return true;
690 }
691 }
692 /*
693 * Reiterate through the list assuming dafault sample rate.
694 * Handles scenario where input sample rate is higher
695 * than all sample rates in list for the input bit width.
696 */
697 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800698
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530699 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700700 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
701 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530702 (bit_width == s_info->app_type_cfg.bit_width)) {
703 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700704 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530705 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800706 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 -0700707 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
708 return true;
709 }
710 }
711 return false;
712}
713
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530714void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
715 struct listnode *streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800716 struct listnode *devices __unused,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530717 audio_input_flags_t flags,
718 audio_format_t format,
719 uint32_t sample_rate,
720 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530721 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530722 struct stream_app_type_cfg *app_type_cfg)
723{
724 struct listnode *node_i, *node_j;
725 struct streams_io_cfg *s_info;
726 struct stream_format *sf_info;
727
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530728 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
729 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530730
731 list_for_each(node_i, streams_input_cfg_list) {
732 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530733 /* Along with flags do profile matching if set at either end.*/
734 if (s_info->flags.in_flags == flags &&
735 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
736 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530737 list_for_each(node_j, &s_info->format_list) {
738 sf_info = node_to_item(node_j, struct stream_format, list);
739 if (sf_info->format == format) {
740 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
741 return;
742 }
743 }
744 }
745 }
746 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
747 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
748 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
749 app_type_cfg->bit_width = 16;
750}
751
752void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700753 struct listnode *streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800754 struct listnode *devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700755 audio_output_flags_t flags,
756 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700757 uint32_t sample_rate,
758 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530759 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530760 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700761 struct stream_app_type_cfg *app_type_cfg)
762{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530763 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530764 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700765 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530766 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800768 if (compare_device_type(devices, AUDIO_DEVICE_OUT_SPEAKER)) {
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530769 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
770 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700771 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530772 else if (-ENOSYS == bw)
773 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700774 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700775 ALOGV("%s Allowing 24 and above bits playback on speaker \
776 ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700777 }
778
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700779 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530780 if (!strncmp("true", value, sizeof("true"))) {
781 if ((popcount(channel_mask) > 2) &&
782 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700783 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530784 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
785 ALOGD("%s: MCH session defaulting sample rate to %d",
786 __func__, sample_rate);
787 }
788 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530789
790 /* Set sampling rate to 176.4 for DSD64
791 * and 352.8Khz for DSD128.
792 * Set Bit Width to 16. output will be 16 bit
793 * post DoP in ASM.
794 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700795 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530796 (format == AUDIO_FORMAT_DSD)) {
797 bit_width = 16;
798 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
799 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
800 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
801 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
802 }
803
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530804
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800805 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
806 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700807 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530808 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530809 /* Along with flags do profile matching if set at either end.*/
810 if (s_info->flags.out_flags == flags &&
811 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
812 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530813 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700814 sf_info = node_to_item(node_j, struct stream_format, list);
815 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530816 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700817 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700818 }
819 }
820 }
821 }
822 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530823 s_info = node_to_item(node_i, struct streams_io_cfg, list);
824 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700825 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530826 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
827 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
828 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700829 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530830 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700831 return;
832 }
833 }
834 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700835 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700836 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700837 app_type_cfg->bit_width = 16;
838}
839
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530840static bool audio_is_this_native_usecase(struct audio_usecase *uc)
841{
842 bool native_usecase = false;
843 struct stream_out *out = (struct stream_out*) uc->stream.out;
844
845 if (PCM_PLAYBACK == uc->type && out != NULL &&
846 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
847 is_offload_usecase(uc->id) &&
848 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
849 native_usecase = true;
850
851 return native_usecase;
852}
853
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800854bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
855{
856 /* DSP bitwidth enforce mode for ADM and AFE:
857 * includes:
858 * deep buffer, low latency, direct pcm and offload.
859 * excludes:
860 * ull(raw+fast), VOIP.
861 */
862 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
863 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
864 (flags & AUDIO_OUTPUT_FLAG_FAST)))
865 return false;
866
867
868 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
869 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
870 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
871 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
872 return true;
873 else
874 return false;
875}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800876
877static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
878{
879 return adev->vr_audio_mode_enabled;
880}
881
Derek Chenf7092792017-05-23 12:23:53 -0400882static void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
883{
884 switch (snd_device) {
885 case SND_DEVICE_OUT_BT_SCO:
886 case SND_DEVICE_IN_BT_SCO_MIC:
887 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
888 *sample_rate = 8000;
889 break;
890 case SND_DEVICE_OUT_BT_SCO_WB:
891 case SND_DEVICE_IN_BT_SCO_MIC_WB:
892 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
893 *sample_rate = 16000;
894 break;
895 default:
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700896 ALOGV("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
Derek Chenf7092792017-05-23 12:23:53 -0400897 break;
898 }
899}
900
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530901void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
902 struct audio_device *adev,
903 struct audio_usecase *usecase)
904{
905 ALOGV("%s", __func__);
906
907 switch(usecase->type) {
908 case PCM_PLAYBACK:
909 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
910 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800911 &usecase->stream.out->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530912 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700913 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530914 usecase->stream.out->sample_rate,
915 usecase->stream.out->bit_width,
916 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530917 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530918 &usecase->stream.out->app_type_cfg);
919 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
920 break;
921 case PCM_CAPTURE:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530922 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
923 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700924 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
925 else
926 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530927 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800928 &usecase->stream.in->device_list,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530929 usecase->stream.in->flags,
930 usecase->stream.in->format,
931 usecase->stream.in->sample_rate,
932 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530933 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530934 &usecase->stream.in->app_type_cfg);
935 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
936 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530937 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530938 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
939 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800940 &usecase->stream.inout->out_config.device_list,
Siddartha Shaik343abc62017-08-08 11:15:25 +0530941 0,
942 usecase->stream.inout->out_config.format,
943 usecase->stream.inout->out_config.sample_rate,
944 usecase->stream.inout->out_config.bit_width,
945 usecase->stream.inout->out_config.channel_mask,
946 usecase->stream.inout->profile,
947 &usecase->stream.inout->out_app_type_cfg);
948 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
949 break;
Derek Chenf7092792017-05-23 12:23:53 -0400950 case PCM_HFP_CALL:
951 switch (usecase->id) {
952 case USECASE_AUDIO_HFP_SCO:
953 case USECASE_AUDIO_HFP_SCO_WB:
954 audio_extn_btsco_get_sample_rate(usecase->out_snd_device,
955 &usecase->out_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500956 usecase->in_app_type_cfg.sample_rate = usecase->out_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400957 break;
958 case USECASE_AUDIO_HFP_SCO_DOWNLINK:
959 case USECASE_AUDIO_HFP_SCO_WB_DOWNLINK:
960 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
961 &usecase->in_app_type_cfg.sample_rate);
Derek Chend7307012019-11-12 12:10:41 -0500962 usecase->out_app_type_cfg.sample_rate = usecase->in_app_type_cfg.sample_rate;
Derek Chenf7092792017-05-23 12:23:53 -0400963 break;
964 default:
965 ALOGE("%s: usecase id (%d) not supported, use default sample rate",
966 __func__, usecase->id);
967 usecase->in_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
968 usecase->out_app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
969 break;
970 }
971 /* update out_app_type_cfg */
972 usecase->out_app_type_cfg.bit_width =
973 platform_get_snd_device_bit_width(usecase->out_snd_device);
974 usecase->out_app_type_cfg.app_type =
975 platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
976 /* update in_app_type_cfg */
977 usecase->in_app_type_cfg.bit_width =
978 platform_get_snd_device_bit_width(usecase->in_snd_device);
979 usecase->in_app_type_cfg.app_type =
980 platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
981 ALOGV("%s Selected apptype: playback %d capture %d",
982 __func__, usecase->out_app_type_cfg.app_type, usecase->in_app_type_cfg.app_type);
983 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530984 default:
985 ALOGE("%s: app type cfg not supported for usecase type (%d)",
986 __func__, usecase->type);
987 }
988}
989
Aalique Grahame22e49102018-12-18 14:23:57 -0800990static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
991 int pcm_device_id, int app_type,
992 int acdb_dev_id, int sample_rate,
993 int stream_type,
994 snd_device_t snd_device)
995{
996
997 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
998 struct mixer_ctl *ctl;
999 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
1000 int snd_device_be_idx = -1;
1001
1002 if (stream_type == PCM_PLAYBACK) {
1003 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1004 "Audio Stream %d App Type Cfg", pcm_device_id);
1005 } else if (stream_type == PCM_CAPTURE) {
1006 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1007 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
1008 }
1009
1010 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1011 if (!ctl) {
1012 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1013 __func__, mixer_ctl_name);
1014 rc = -EINVAL;
1015 goto exit;
1016 }
1017 app_type_cfg[len++] = app_type;
1018 app_type_cfg[len++] = acdb_dev_id;
1019 app_type_cfg[len++] = sample_rate;
1020
1021 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1022 if (snd_device_be_idx > 0)
1023 app_type_cfg[len++] = snd_device_be_idx;
1024 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
1025 "sample rate %d, snd_device_be_idx %d",
1026 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
1027 snd_device_be_idx);
1028 mixer_ctl_set_array(ctl, app_type_cfg, len);
1029
1030exit:
1031 return rc;
1032}
1033
1034static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
1035 struct audio_usecase *usecase)
1036{
1037 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
1038 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1039 int app_type = 0, rc = 0;
Derek Chenf7092792017-05-23 12:23:53 -04001040 bool is_bus_dev_usecase = false;
Aalique Grahame22e49102018-12-18 14:23:57 -08001041
1042 ALOGV("%s", __func__);
1043
1044 if (usecase->type != PCM_HFP_CALL) {
Derek Chenf7092792017-05-23 12:23:53 -04001045 ALOGV("%s: not a HFP path, no need to cfg app type", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001046 rc = 0;
1047 goto exit_send_app_type_cfg;
1048 }
1049 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
Derek Chenf7092792017-05-23 12:23:53 -04001050 (usecase->id != USECASE_AUDIO_HFP_SCO_WB) &&
1051 (usecase->id != USECASE_AUDIO_HFP_SCO_DOWNLINK) &&
1052 (usecase->id != USECASE_AUDIO_HFP_SCO_WB_DOWNLINK)) {
1053 ALOGV("%s: a usecase where app type cfg is not required", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08001054 rc = 0;
1055 goto exit_send_app_type_cfg;
1056 }
1057
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001058 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Derek Chenf7092792017-05-23 12:23:53 -04001059 is_bus_dev_usecase = true;
1060
Aalique Grahame22e49102018-12-18 14:23:57 -08001061 snd_device = usecase->out_snd_device;
1062 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
1063
1064 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1065 if (acdb_dev_id < 0) {
1066 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1067 rc = -EINVAL;
1068 goto exit_send_app_type_cfg;
1069 }
1070
1071 if (usecase->type == PCM_HFP_CALL) {
1072
1073 /* config HFP session:1 playback path */
Derek Chenf7092792017-05-23 12:23:53 -04001074 if (is_bus_dev_usecase) {
1075 app_type = usecase->out_app_type_cfg.app_type;
1076 sample_rate= usecase->out_app_type_cfg.sample_rate;
1077 } else {
1078 snd_device = SND_DEVICE_NONE; // use legacy behavior
1079 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1080 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1081 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001082 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1083 acdb_dev_id, sample_rate,
1084 PCM_PLAYBACK,
Derek Chenf7092792017-05-23 12:23:53 -04001085 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001086 if (rc < 0)
1087 goto exit_send_app_type_cfg;
1088
1089 /* config HFP session:1 capture path */
Derek Chenf7092792017-05-23 12:23:53 -04001090 if (is_bus_dev_usecase) {
1091 snd_device = usecase->in_snd_device;
1092 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
1093 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1094 if (acdb_dev_id < 0) {
1095 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1096 rc = -EINVAL;
1097 goto exit_send_app_type_cfg;
1098 }
1099 app_type = usecase->in_app_type_cfg.app_type;
1100 sample_rate= usecase->in_app_type_cfg.sample_rate;
1101 } else {
1102 snd_device = SND_DEVICE_NONE; // use legacy behavior
1103 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1104 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001105 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1106 acdb_dev_id, sample_rate,
1107 PCM_CAPTURE,
Derek Chenf7092792017-05-23 12:23:53 -04001108 snd_device);
Aalique Grahame22e49102018-12-18 14:23:57 -08001109 if (rc < 0)
1110 goto exit_send_app_type_cfg;
1111
Derek Chenf7092792017-05-23 12:23:53 -04001112 if (is_bus_dev_usecase)
1113 goto exit_send_app_type_cfg;
1114
Aalique Grahame22e49102018-12-18 14:23:57 -08001115 /* config HFP session:2 capture path */
1116 pcm_device_id = HFP_ASM_RX_TX;
1117 snd_device = usecase->in_snd_device;
1118 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1119 if (acdb_dev_id <= 0) {
1120 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1121 rc = -EINVAL;
1122 goto exit_send_app_type_cfg;
1123 }
1124 app_type = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE);
1125 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1126 acdb_dev_id, sample_rate, PCM_CAPTURE,
1127 SND_DEVICE_NONE);
1128 if (rc < 0)
1129 goto exit_send_app_type_cfg;
1130
1131 /* config HFP session:2 playback path */
1132 app_type = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK);
1133 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
1134 acdb_dev_id, sample_rate,
1135 PCM_PLAYBACK, SND_DEVICE_NONE);
1136 if (rc < 0)
1137 goto exit_send_app_type_cfg;
1138 }
1139
1140 rc = 0;
1141exit_send_app_type_cfg:
1142 return rc;
1143}
1144
Zhou Song06761dd2019-04-28 18:08:17 +08001145int audio_extn_utils_get_app_sample_rate_for_device(
1146 struct audio_device *adev,
1147 struct audio_usecase *usecase, int snd_device)
1148{
1149 char value[PROPERTY_VALUE_MAX] = {0};
1150 int sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1151
1152 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
1153 property_get("vendor.audio.playback.mch.downsample",value,"");
1154 if (!strncmp("true", value, sizeof("true"))) {
1155 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
1156 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
1157 !(usecase->stream.out->flags &
1158 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
1159 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1160 }
1161
1162 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1163 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001164 } else if (compare_device_type(&usecase->stream.out->device_list,
1165 AUDIO_DEVICE_OUT_SPEAKER)) {
Zhou Song06761dd2019-04-28 18:08:17 +08001166 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1167 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1168 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1169 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1170 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1171 /*
1172 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1173 * configured device sample rate, if not update the COPP rate to be equal to the
1174 * device sample rate, else open COPP at stream sample rate
1175 */
1176 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1177 usecase->stream.out->sample_rate,
1178 &usecase->stream.out->app_type_cfg.sample_rate);
1179 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1180 !audio_is_this_native_usecase(usecase)) &&
1181 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1182 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
1183 /* Reset to default if no native stream is active*/
1184 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Zhou Song9e9b3812019-12-30 17:52:10 +08001185 } else if (usecase->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
1186 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
1187 usecase->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) {
Zhou Song06761dd2019-04-28 18:08:17 +08001188 /*
1189 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1190 * for bit width use the stream param only.
1191 */
1192 audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1193 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1194 __func__, usecase->stream.out->app_type_cfg.sample_rate);
1195 }
1196 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
1197 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1198
1199 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
1200 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1201 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
1202 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1203 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
1204 sample_rate = sample_rate * 4;
1205 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1206 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
1207 }
1208 } else if (usecase->type == PCM_CAPTURE) {
1209 if (usecase->stream.in != NULL) {
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05301210 if (usecase->id == USECASE_AUDIO_RECORD_VOIP
1211 || usecase->id == USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY)
Zhou Song06761dd2019-04-28 18:08:17 +08001212 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
1213 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1214 audio_extn_btsco_get_sample_rate(usecase->in_snd_device,
1215 &usecase->stream.in->app_type_cfg.sample_rate);
1216 } else {
1217 audio_extn_btsco_get_sample_rate(snd_device,
1218 &usecase->stream.in->app_type_cfg.sample_rate);
1219 }
1220 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1221 } else if (usecase->id == USECASE_AUDIO_SPKR_CALIB_TX) {
Sujin Panickerb6d76262019-09-09 10:55:38 +05301222 if ((property_get("vendor.audio.spkr_prot.tx.sampling_rate", value, NULL) > 0))
1223 sample_rate = atoi(value);
1224 else
1225 sample_rate = SAMPLE_RATE_8000;
Zhou Song06761dd2019-04-28 18:08:17 +08001226 }
1227 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
1228 sample_rate = usecase->stream.inout->out_config.sample_rate;
1229 }
1230 return sample_rate;
1231}
1232
Siena Richard7c2db772016-12-21 11:32:34 -08001233static int send_app_type_cfg_for_device(struct audio_device *adev,
1234 struct audio_usecase *usecase,
1235 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001236{
1237 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301238 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
1239 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001240 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -08001241 int pcm_device_id = 0, acdb_dev_id, app_type;
1242 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05301243 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Varun Balaraje49253e2017-07-06 19:48:56 +05301244 struct streams_io_cfg *s_info = NULL;
1245 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301246 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001247
Siena Richard7c2db772016-12-21 11:32:34 -08001248 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
1249 __func__, platform_get_snd_device_name(usecase->out_snd_device),
1250 platform_get_snd_device_name(usecase->in_snd_device),
1251 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001252
Siddartha Shaik343abc62017-08-08 11:15:25 +05301253 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301254 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301255 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001256 rc = 0;
1257 goto exit_send_app_type_cfg;
1258 }
1259 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
Vignesh Kulothungan0a2eff02019-07-11 16:30:13 -07001260 (usecase->id != USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) &&
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001261 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
1262 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001263 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -07001264 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +05301265 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +05301266 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301267 (!is_offload_usecase(usecase->id)) &&
Derek Chenf6318be2017-06-12 17:16:24 -04001268 (usecase->type != PCM_CAPTURE) &&
1269 (!audio_extn_auto_hal_is_bus_device_usecase(usecase->id))) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301270 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 -07001271 rc = 0;
1272 goto exit_send_app_type_cfg;
1273 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08001274
1275 //if VR is active then only send the mixer control
1276 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
1277 ALOGI("ULL doesnt need sending app type cfg, returning");
1278 rc = 0;
1279 goto exit_send_app_type_cfg;
1280 }
1281
Surendar Karka93cd25a2018-08-28 14:21:37 +05301282 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001283 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301284 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1285 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001286 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -07001287 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301288 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1289 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +05301290 }
Siena Richard7c2db772016-12-21 11:32:34 -08001291
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001292 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1293 if (!ctl) {
1294 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1295 mixer_ctl_name);
1296 rc = -EINVAL;
1297 goto exit_send_app_type_cfg;
1298 }
Aditya Bavanari701a6992017-03-30 19:17:16 +05301299 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301300 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
1301 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
1302 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
1303 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
1304 } else {
1305 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
1306 }
Rohit Kumar1181b292017-01-31 18:07:17 +05301307 if (acdb_dev_id <= 0) {
1308 ALOGE("%s: Couldn't get the acdb dev id", __func__);
1309 rc = -EINVAL;
1310 goto exit_send_app_type_cfg;
1311 }
1312
Siena Richard7c2db772016-12-21 11:32:34 -08001313 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
1314 if (snd_device_be_idx < 0) {
1315 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
1316 __func__, platform_get_snd_device_name(snd_device),
1317 snd_device_be_idx);
1318 }
1319
Zhou Song06761dd2019-04-28 18:08:17 +08001320 sample_rate = audio_extn_utils_get_app_sample_rate_for_device(adev, usecase, snd_device);
1321
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301322 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Varun Balaraje49253e2017-07-06 19:48:56 +05301323 /* Interactive streams are supported with only direct app type id.
1324 * Get Direct profile app type and use it for interactive streams
1325 */
1326 list_for_each(node, &adev->streams_output_cfg_list) {
1327 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001328 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301329 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1330 AUDIO_OUTPUT_FLAG_DIRECT))
1331 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301332 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001333 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301334 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301335 else
1336 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001337 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001338 app_type_cfg[len++] = acdb_dev_id;
Naresh Tanniru3a406772017-05-10 13:09:05 -07001339 app_type_cfg[len++] = sample_rate;
1340
Siena Richard7c2db772016-12-21 11:32:34 -08001341 if (snd_device_be_idx > 0)
1342 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301343
Siena Richard7c2db772016-12-21 11:32:34 -08001344 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1345 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301346
1347 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001348 app_type = usecase->stream.in->app_type_cfg.app_type;
1349 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301350 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001351 app_type_cfg[len++] = sample_rate;
1352 if (snd_device_be_idx > 0)
1353 app_type_cfg[len++] = snd_device_be_idx;
1354 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1355 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301356 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001357 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301358 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301359 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1360 }
Siena Richard7c2db772016-12-21 11:32:34 -08001361 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301362 app_type_cfg[len++] = acdb_dev_id;
1363 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001364 if (snd_device_be_idx > 0)
1365 app_type_cfg[len++] = snd_device_be_idx;
1366 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1367 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301368 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301369
Siddartha Shaik343abc62017-08-08 11:15:25 +05301370 if(ctl)
1371 mixer_ctl_set_array(ctl, app_type_cfg, len);
Zhou Songd9bd9302020-08-04 16:34:45 +08001372
1373 /* send app type cfg for haptics */
1374 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
1375 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1376 "Audio Stream %d App Type Cfg", adev->haptic_pcm_device_id );
1377 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1378 if (!ctl) {
1379 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
1380 mixer_ctl_name);
1381 rc = -EINVAL;
1382 goto exit_send_app_type_cfg;
1383 }
1384 acdb_dev_id = platform_get_snd_device_acdb_id(SND_DEVICE_OUT_HAPTICS);
1385 snd_device_be_idx = platform_get_snd_device_backend_index(SND_DEVICE_OUT_HAPTICS);
1386 /* haptics acdb id */
1387 app_type_cfg[1] = acdb_dev_id;
1388 /* haptics be index */
1389 app_type_cfg[3] = snd_device_be_idx;
1390 mixer_ctl_set_array(ctl, app_type_cfg, len);
1391 }
1392
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001393 rc = 0;
1394exit_send_app_type_cfg:
1395 return rc;
1396}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001397
Deeraj Soman14230922019-01-30 16:39:30 +05301398static int audio_extn_utils_check_input_parameters(uint32_t sample_rate,
1399 audio_format_t format,
1400 int channel_count)
1401{
1402 int ret = 0;
1403
1404 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
1405 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
1406 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
1407 !voice_extn_compress_voip_is_format_supported(format) &&
1408 !audio_extn_compr_cap_format_supported(format) &&
1409 !audio_extn_cin_format_supported(format))
1410 ret = -EINVAL;
1411
1412 switch (channel_count) {
1413 case 1:
1414 case 2:
1415 case 3:
1416 case 4:
1417 case 6:
1418 case 8:
1419 break;
1420 default:
1421 ret = -EINVAL;
1422 }
1423
1424 switch (sample_rate) {
1425 case 8000:
1426 case 11025:
1427 case 12000:
1428 case 16000:
1429 case 22050:
1430 case 24000:
1431 case 32000:
1432 case 44100:
1433 case 48000:
1434 case 88200:
1435 case 96000:
1436 case 176400:
1437 case 192000:
1438 break;
1439 default:
1440 ret = -EINVAL;
1441 }
1442
1443 return ret;
1444}
1445
1446static inline uint32_t audio_extn_utils_nearest_multiple(uint32_t num, uint32_t multiplier)
1447{
1448 uint32_t remainder = 0;
1449
1450 if (!multiplier)
1451 return num;
1452
1453 remainder = num % multiplier;
1454 if (remainder)
1455 num += (multiplier - remainder);
1456
1457 return num;
1458}
1459
1460static inline uint32_t audio_extn_utils_lcm(uint32_t num1, uint32_t num2)
1461{
1462 uint32_t high = num1, low = num2, temp = 0;
1463
1464 if (!num1 || !num2)
1465 return 0;
1466
1467 if (num1 < num2) {
1468 high = num2;
1469 low = num1;
1470 }
1471
1472 while (low != 0) {
1473 temp = low;
1474 low = high % low;
1475 high = temp;
1476 }
1477 return (num1 * num2)/high;
1478}
1479
Siena Richard7c2db772016-12-21 11:32:34 -08001480int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1481 struct audio_usecase *usecase)
1482{
1483 int i, num_devices = 0;
1484 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301485 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001486 int rc = 0;
1487
Aalique Grahame22e49102018-12-18 14:23:57 -08001488 if (usecase->type == PCM_HFP_CALL) {
1489 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
1490 }
1491
Siena Richard7c2db772016-12-21 11:32:34 -08001492 switch (usecase->type) {
1493 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301494 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001495 ALOGD("%s: usecase->out_snd_device %s",
1496 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1497 /* check for out combo device */
1498 if (platform_split_snd_device(adev->platform,
1499 usecase->out_snd_device,
1500 &num_devices, new_snd_devices)) {
1501 new_snd_devices[0] = usecase->out_snd_device;
1502 num_devices = 1;
1503 }
1504 break;
1505 case PCM_CAPTURE:
1506 ALOGD("%s: usecase->in_snd_device %s",
1507 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301508 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1509 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1510 }
Siena Richard7c2db772016-12-21 11:32:34 -08001511 /* check for in combo device */
1512 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301513 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001514 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301515 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001516 num_devices = 1;
1517 }
1518 break;
1519 default:
1520 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1521 rc = 0;
1522 break;
1523 }
1524
1525 for (i = 0; i < num_devices; i++) {
1526 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1527 if (rc)
1528 break;
1529 }
1530
1531 return rc;
1532}
1533
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301534int read_line_from_file(const char *path, char *buf, size_t count)
1535{
1536 char * fgets_ret;
1537 FILE * fd;
1538 int rv;
1539
1540 fd = fopen(path, "r");
1541 if (fd == NULL)
1542 return -1;
1543
1544 fgets_ret = fgets(buf, (int)count, fd);
1545 if (NULL != fgets_ret) {
1546 rv = (int)strlen(buf);
1547 } else {
1548 rv = ferror(fd);
1549 }
1550 fclose(fd);
1551
1552 return rv;
1553}
1554
Ashish Jainf1eaa582016-05-23 20:54:24 +05301555/*Translates ALSA formats to AOSP PCM formats*/
1556audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1557{
1558 audio_format_t format;
1559
1560 switch(alsa_format) {
1561 case SNDRV_PCM_FORMAT_S16_LE:
1562 format = AUDIO_FORMAT_PCM_16_BIT;
1563 break;
1564 case SNDRV_PCM_FORMAT_S24_3LE:
1565 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1566 break;
1567 case SNDRV_PCM_FORMAT_S24_LE:
1568 format = AUDIO_FORMAT_PCM_8_24_BIT;
1569 break;
1570 case SNDRV_PCM_FORMAT_S32_LE:
1571 format = AUDIO_FORMAT_PCM_32_BIT;
1572 break;
1573 default:
1574 ALOGW("Incorrect ALSA format");
1575 format = AUDIO_FORMAT_INVALID;
1576 }
1577 return format;
1578}
1579
1580/*Translates hal format (AOSP) to alsa formats*/
1581uint32_t hal_format_to_alsa(audio_format_t hal_format)
1582{
1583 uint32_t alsa_format;
1584
1585 switch (hal_format) {
1586 case AUDIO_FORMAT_PCM_32_BIT: {
1587 if (platform_supports_true_32bit())
1588 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1589 else
1590 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1591 }
1592 break;
1593 case AUDIO_FORMAT_PCM_8_BIT:
1594 alsa_format = SNDRV_PCM_FORMAT_S8;
1595 break;
1596 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1597 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1598 break;
1599 case AUDIO_FORMAT_PCM_8_24_BIT: {
1600 if (platform_supports_true_32bit())
1601 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1602 else
1603 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1604 }
1605 break;
1606 case AUDIO_FORMAT_PCM_FLOAT:
1607 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1608 break;
1609 default:
1610 case AUDIO_FORMAT_PCM_16_BIT:
1611 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1612 break;
1613 }
1614 return alsa_format;
1615}
1616
Ashish Jain83a6cc22016-06-28 14:34:17 +05301617/*Translates PCM formats to AOSP formats*/
1618audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1619{
1620 audio_format_t format = AUDIO_FORMAT_INVALID;
1621
1622 switch(pcm_format) {
1623 case PCM_FORMAT_S16_LE:
1624 format = AUDIO_FORMAT_PCM_16_BIT;
1625 break;
1626 case PCM_FORMAT_S24_3LE:
1627 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1628 break;
1629 case PCM_FORMAT_S24_LE:
1630 format = AUDIO_FORMAT_PCM_8_24_BIT;
1631 break;
1632 case PCM_FORMAT_S32_LE:
1633 format = AUDIO_FORMAT_PCM_32_BIT;
1634 break;
1635 default:
1636 ALOGW("Incorrect PCM format");
1637 format = AUDIO_FORMAT_INVALID;
1638 }
1639 return format;
1640}
1641
1642/*Translates hal format (AOSP) to alsa formats*/
1643uint32_t hal_format_to_pcm(audio_format_t hal_format)
1644{
1645 uint32_t pcm_format;
1646
1647 switch (hal_format) {
1648 case AUDIO_FORMAT_PCM_32_BIT:
1649 case AUDIO_FORMAT_PCM_8_24_BIT:
1650 case AUDIO_FORMAT_PCM_FLOAT: {
1651 if (platform_supports_true_32bit())
1652 pcm_format = PCM_FORMAT_S32_LE;
1653 else
1654 pcm_format = PCM_FORMAT_S24_3LE;
1655 }
1656 break;
1657 case AUDIO_FORMAT_PCM_8_BIT:
1658 pcm_format = PCM_FORMAT_S8;
1659 break;
1660 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1661 pcm_format = PCM_FORMAT_S24_3LE;
1662 break;
1663 default:
1664 case AUDIO_FORMAT_PCM_16_BIT:
1665 pcm_format = PCM_FORMAT_S16_LE;
1666 break;
1667 }
1668 return pcm_format;
1669}
1670
Ashish Jainf1eaa582016-05-23 20:54:24 +05301671uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1672 uint32_t sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301673 uint32_t noOfChannels,
1674 int64_t duration_ms)
Ashish Jainf1eaa582016-05-23 20:54:24 +05301675{
1676 uint32_t fragment_size = 0;
1677 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1678
Deeraj Soman65358ab2019-02-07 15:40:49 +05301679 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
1680 pcm_offload_time = duration_ms;
1681
Ashish Jainf1eaa582016-05-23 20:54:24 +05301682 fragment_size = (pcm_offload_time
1683 * sample_rate
1684 * bytes_per_sample
1685 * noOfChannels)/1000;
1686 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1687 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1688 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1689 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1690 /*To have same PCM samples for all channels, the buffer size requires to
1691 *be multiple of (number of channels * bytes per sample)
1692 *For writes to succeed, the buffer must be written at address which is multiple of 32
1693 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001694 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301695
1696 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1697 return fragment_size;
1698}
1699
1700/* Calculates the fragment size required to configure compress session.
1701 * Based on the alsa format selected, decide if conversion is needed in
1702
1703 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1704 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1705 */
1706void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1707{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301708 audio_format_t dst_format = out->hal_op_format;
1709 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301710 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1711 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1712
1713 out->compr_config.fragment_size =
1714 get_alsa_fragment_size(hal_op_bytes_per_sample,
1715 out->sample_rate,
Deeraj Soman65358ab2019-02-07 15:40:49 +05301716 popcount(out->channel_mask),
1717 out->info.duration_us / 1000);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301718
1719 if ((src_format != dst_format) &&
1720 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1721
Ashish Jain83a6cc22016-06-28 14:34:17 +05301722 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301723 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1724 hal_op_bytes_per_sample);
1725 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301726 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301727 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301728 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301729 }
1730}
1731
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301732/* converts pcm format 24_8 to 8_24 inplace */
1733size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1734{
1735 size_t i = 0;
1736 int *int_buf_stream = buf;
1737
1738 if ((bytes % 4) != 0) {
1739 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1740 return -EINVAL;
1741 }
1742
1743 for (; i < (bytes / 4); i++)
1744 int_buf_stream[i] >>= 8;
1745
1746 return bytes;
1747}
1748
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001749#ifdef AUDIO_GKI_ENABLED
1750int get_snd_codec_id(audio_format_t format)
1751{
1752 int id = 0;
1753
1754 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1755 case AUDIO_FORMAT_MP3:
1756 id = SND_AUDIOCODEC_MP3;
1757 break;
1758 case AUDIO_FORMAT_AAC:
1759 id = SND_AUDIOCODEC_AAC;
1760 break;
1761 case AUDIO_FORMAT_AAC_ADTS:
1762 id = SND_AUDIOCODEC_AAC;
1763 break;
1764 case AUDIO_FORMAT_AAC_LATM:
1765 id = SND_AUDIOCODEC_AAC;
1766 break;
1767 case AUDIO_FORMAT_PCM:
1768 id = SND_AUDIOCODEC_PCM;
1769 break;
1770 case AUDIO_FORMAT_FLAC:
1771 case AUDIO_FORMAT_ALAC:
1772 case AUDIO_FORMAT_APE:
1773 case AUDIO_FORMAT_VORBIS:
1774 case AUDIO_FORMAT_WMA:
1775 case AUDIO_FORMAT_WMA_PRO:
1776 case AUDIO_FORMAT_DSD:
1777 case AUDIO_FORMAT_APTX:
1778 id = SND_AUDIOCODEC_BESPOKE;
1779 break;
1780 case AUDIO_FORMAT_MP2:
1781 id = SND_AUDIOCODEC_MP2;
1782 break;
1783 case AUDIO_FORMAT_AC3:
1784 id = SND_AUDIOCODEC_AC3;
1785 break;
1786 case AUDIO_FORMAT_E_AC3:
1787 case AUDIO_FORMAT_E_AC3_JOC:
1788 id = SND_AUDIOCODEC_EAC3;
1789 break;
1790 case AUDIO_FORMAT_DTS:
1791 case AUDIO_FORMAT_DTS_HD:
1792 id = SND_AUDIOCODEC_DTS;
1793 break;
1794 case AUDIO_FORMAT_DOLBY_TRUEHD:
1795 id = SND_AUDIOCODEC_TRUEHD;
1796 break;
1797 case AUDIO_FORMAT_IEC61937:
1798 id = SND_AUDIOCODEC_IEC61937;
1799 break;
1800 default:
1801 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1802 }
1803
1804 return id;
1805}
1806#else
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301807int get_snd_codec_id(audio_format_t format)
1808{
1809 int id = 0;
1810
1811 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1812 case AUDIO_FORMAT_MP3:
1813 id = SND_AUDIOCODEC_MP3;
1814 break;
1815 case AUDIO_FORMAT_AAC:
1816 id = SND_AUDIOCODEC_AAC;
1817 break;
1818 case AUDIO_FORMAT_AAC_ADTS:
1819 id = SND_AUDIOCODEC_AAC;
1820 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301821 case AUDIO_FORMAT_AAC_LATM:
1822 id = SND_AUDIOCODEC_AAC;
1823 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301824 case AUDIO_FORMAT_PCM:
1825 id = SND_AUDIOCODEC_PCM;
1826 break;
1827 case AUDIO_FORMAT_FLAC:
1828 id = SND_AUDIOCODEC_FLAC;
1829 break;
1830 case AUDIO_FORMAT_ALAC:
1831 id = SND_AUDIOCODEC_ALAC;
1832 break;
1833 case AUDIO_FORMAT_APE:
1834 id = SND_AUDIOCODEC_APE;
1835 break;
1836 case AUDIO_FORMAT_VORBIS:
1837 id = SND_AUDIOCODEC_VORBIS;
1838 break;
1839 case AUDIO_FORMAT_WMA:
1840 id = SND_AUDIOCODEC_WMA;
1841 break;
1842 case AUDIO_FORMAT_WMA_PRO:
1843 id = SND_AUDIOCODEC_WMA_PRO;
1844 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301845 case AUDIO_FORMAT_MP2:
1846 id = SND_AUDIOCODEC_MP2;
1847 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301848 case AUDIO_FORMAT_AC3:
1849 id = SND_AUDIOCODEC_AC3;
1850 break;
1851 case AUDIO_FORMAT_E_AC3:
1852 case AUDIO_FORMAT_E_AC3_JOC:
1853 id = SND_AUDIOCODEC_EAC3;
1854 break;
1855 case AUDIO_FORMAT_DTS:
1856 case AUDIO_FORMAT_DTS_HD:
1857 id = SND_AUDIOCODEC_DTS;
1858 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001859 case AUDIO_FORMAT_DOLBY_TRUEHD:
1860 id = SND_AUDIOCODEC_TRUEHD;
1861 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001862 case AUDIO_FORMAT_IEC61937:
1863 id = SND_AUDIOCODEC_IEC61937;
1864 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301865 case AUDIO_FORMAT_DSD:
1866 id = SND_AUDIOCODEC_DSD;
1867 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301868 case AUDIO_FORMAT_APTX:
1869 id = SND_AUDIOCODEC_APTX;
1870 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301871 default:
1872 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1873 }
1874
1875 return id;
1876}
Xiaojun Sang782e5b12020-06-29 21:13:06 +08001877#endif
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301878
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001879void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1880 struct audio_usecase *usecase)
1881{
1882 int type = usecase->type;
1883
Dhananjay Kumar14245982017-01-16 20:21:00 +05301884 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301885 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001886 usecase->stream.out->app_type_cfg.app_type);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301887 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301888 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001889 usecase->stream.in->app_type_cfg.app_type);
1890 } else if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE) ||
1891 (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301892 platform_send_audio_calibration(adev->platform, usecase,
Zhou Song06761dd2019-04-28 18:08:17 +08001893 platform_get_default_app_type_v2(adev->platform, usecase->type));
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001894 } else {
1895 /* No need to send audio calibration for voice and voip call usecases */
1896 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1897 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001898 }
1899}
1900
Ben Rombergera04fabc2014-11-14 12:16:03 -08001901// Base64 Encode and Decode
1902// Not all features supported. This must be used only with following conditions.
1903// Decode Modes: Support with and without padding
1904// CRLF not handling. So no CRLF in string to decode.
1905// Encode Modes: Supports only padding
1906int b64decode(char *inp, int ilen, uint8_t* outp)
1907{
1908 int i, j, k, ii, num;
1909 int rem, pcnt;
1910 uint32_t res=0;
1911 uint8_t getIndex[MAX_BASEINDEX_LEN];
1912 uint8_t tmp, cflag;
1913
1914 if(inp == NULL || outp == NULL || ilen <= 0) {
1915 ALOGE("[%s] received NULL pointer or zero length",__func__);
1916 return -1;
1917 }
1918
1919 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1920 for(i=0;i<BASE_TABLE_SIZE;i++) {
1921 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1922 }
1923 getIndex[(uint8_t)'=']=0;
1924
1925 j=0;k=0;
1926 num = ilen/4;
1927 rem = ilen%4;
1928 if(rem==0)
1929 num = num-1;
1930 cflag=0;
1931 for(i=0; i<num; i++) {
1932 res=0;
1933 for(ii=0;ii<4;ii++) {
1934 res = res << 6;
1935 tmp = getIndex[(uint8_t)inp[j++]];
1936 res = res | tmp;
1937 cflag = cflag | tmp;
1938 }
1939 outp[k++] = (res >> 16)&0xFF;
1940 outp[k++] = (res >> 8)&0xFF;
1941 outp[k++] = res & 0xFF;
1942 }
1943
1944 // Handle last bytes special
1945 pcnt=0;
1946 if(rem == 0) {
1947 //With padding or full data
1948 res = 0;
1949 for(ii=0;ii<4;ii++) {
1950 if(inp[j] == '=')
1951 pcnt++;
1952 res = res << 6;
1953 tmp = getIndex[(uint8_t)inp[j++]];
1954 res = res | tmp;
1955 cflag = cflag | tmp;
1956 }
1957 outp[k++] = res >> 16;
1958 if(pcnt == 2)
1959 goto done;
1960 outp[k++] = (res>>8)&0xFF;
1961 if(pcnt == 1)
1962 goto done;
1963 outp[k++] = res&0xFF;
1964 } else {
1965 //without padding
1966 res = 0;
1967 for(i=0;i<rem;i++) {
1968 res = res << 6;
1969 tmp = getIndex[(uint8_t)inp[j++]];
1970 res = res | tmp;
1971 cflag = cflag | tmp;
1972 }
1973 for(i=rem;i<4;i++) {
1974 res = res << 6;
1975 pcnt++;
1976 }
1977 outp[k++] = res >> 16;
1978 if(pcnt == 2)
1979 goto done;
1980 outp[k++] = (res>>8)&0xFF;
1981 if(pcnt == 1)
1982 goto done;
1983 outp[k++] = res&0xFF;
1984 }
1985done:
1986 if(cflag == 0xFF) {
1987 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1988 __func__, inp);
1989 return 0;
1990 }
1991 return k;
1992}
1993
1994int b64encode(uint8_t *inp, int ilen, char* outp)
1995{
1996 int i,j,k, num;
1997 int rem=0;
1998 uint32_t res=0;
1999
2000 if(inp == NULL || outp == NULL || ilen<=0) {
2001 ALOGE("[%s] received NULL pointer or zero input length",__func__);
2002 return -1;
2003 }
2004
2005 num = ilen/3;
2006 rem = ilen%3;
2007 j=0;k=0;
2008 for(i=0; i<num; i++) {
2009 //prepare index
2010 res = inp[j++]<<16;
2011 res = res | inp[j++]<<8;
2012 res = res | inp[j++];
2013 //get output map from index
2014 outp[k++] = (char) bTable[(res>>18)&0x3F];
2015 outp[k++] = (char) bTable[(res>>12)&0x3F];
2016 outp[k++] = (char) bTable[(res>>6)&0x3F];
2017 outp[k++] = (char) bTable[res&0x3F];
2018 }
2019
2020 switch(rem) {
2021 case 1:
2022 res = inp[j++]<<16;
2023 outp[k++] = (char) bTable[res>>18];
2024 outp[k++] = (char) bTable[(res>>12)&0x3F];
2025 //outp[k++] = '=';
2026 //outp[k++] = '=';
2027 break;
2028 case 2:
2029 res = inp[j++]<<16;
2030 res = res | inp[j++]<<8;
2031 outp[k++] = (char) bTable[res>>18];
2032 outp[k++] = (char) bTable[(res>>12)&0x3F];
2033 outp[k++] = (char) bTable[(res>>6)&0x3F];
2034 //outp[k++] = '=';
2035 break;
2036 default:
2037 break;
2038 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08002039 outp[k] = '\0';
2040 return k;
2041}
Ashish Jain81eb2a82015-05-13 10:52:34 +05302042
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302043
2044int audio_extn_utils_get_codec_version(const char *snd_card_name,
2045 int card_num,
2046 char *codec_version)
2047{
2048 char procfs_path[50];
2049 FILE *fp;
2050
2051 if (strstr(snd_card_name, "tasha")) {
2052 snprintf(procfs_path, sizeof(procfs_path),
2053 "/proc/asound/card%d/codecs/tasha/version", card_num);
2054 if ((fp = fopen(procfs_path, "r")) != NULL) {
2055 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
2056 fclose(fp);
2057 } else {
2058 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2059 return -ENOENT;
2060 }
2061 ALOGD("%s: codec version %s", __func__, codec_version);
2062 }
2063
2064 return 0;
2065}
2066
Preetam Singh Ranawat9d0d8e42019-07-15 12:27:25 +05302067int audio_extn_utils_get_codec_variant(int card_num,
2068 char *codec_variant)
2069{
2070 char procfs_path[50];
2071 FILE *fp;
2072 snprintf(procfs_path, sizeof(procfs_path),
2073 "/proc/asound/card%d/codecs/wcd938x/variant", card_num);
2074 if ((fp = fopen(procfs_path, "r")) == NULL) {
2075 snprintf(procfs_path, sizeof(procfs_path),
2076 "/proc/asound/card%d/codecs/wcd937x/variant", card_num);
2077 if ((fp = fopen(procfs_path, "r")) == NULL) {
2078 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
2079 return -ENOENT;
2080 }
2081 }
2082 fgets(codec_variant, CODEC_VARIANT_MAX_LENGTH, fp);
2083 fclose(fp);
2084 ALOGD("%s: codec variant is %s", __func__, codec_variant);
2085 return 0;
2086}
2087
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302088
Ashish Jain81eb2a82015-05-13 10:52:34 +05302089#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
2090
2091void get_default_compressed_channel_status(
2092 unsigned char *channel_status)
2093{
Ashish Jain81eb2a82015-05-13 10:52:34 +05302094 memset(channel_status,0,24);
2095
2096 /* block start bit in preamble bit 3 */
2097 channel_status[0] |= PROFESSIONAL;
2098 //compre out
2099 channel_status[0] |= NON_LPCM;
2100 // sample rate; fixed 48K for default/transcode
2101 channel_status[3] |= SR_48000;
2102}
2103
Ashish Jain81eb2a82015-05-13 10:52:34 +05302104int32_t get_compressed_channel_status(void *audio_stream_data,
2105 uint32_t audio_frame_size,
2106 unsigned char *channel_status,
2107 enum audio_parser_code_type codec_type)
2108 // codec_type - AUDIO_PARSER_CODEC_AC3
2109 // - AUDIO_PARSER_CODEC_DTS
2110{
2111 unsigned char *stream;
2112 int ret = 0;
2113 stream = (unsigned char *)audio_stream_data;
2114
2115 if (audio_stream_data == NULL || audio_frame_size == 0) {
2116 ALOGW("no buffer to get channel status, return default for compress");
2117 get_default_compressed_channel_status(channel_status);
2118 return ret;
2119 }
2120
2121 memset(channel_status,0,24);
2122 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
2123 {
2124 ALOGE("init audio parser failed");
2125 return -1;
2126 }
2127 ret = get_channel_status(channel_status, codec_type);
2128 return ret;
2129
2130}
2131
Ashish Jain81eb2a82015-05-13 10:52:34 +05302132void get_lpcm_channel_status(uint32_t sampleRate,
2133 unsigned char *channel_status)
2134{
2135 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302136 memset(channel_status,0,24);
2137 /* block start bit in preamble bit 3 */
2138 channel_status[0] |= PROFESSIONAL;
2139 //LPCM OUT
2140 channel_status[0] &= ~NON_LPCM;
2141
2142 switch (sampleRate) {
2143 case 8000:
2144 case 11025:
2145 case 12000:
2146 case 16000:
2147 case 22050:
2148 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05302149 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05302150 case 24000:
2151 channel_status[3] |= SR_24000;
2152 break;
2153 case 32000:
2154 channel_status[3] |= SR_32000;
2155 break;
2156 case 44100:
2157 channel_status[3] |= SR_44100;
2158 break;
2159 case 48000:
2160 channel_status[3] |= SR_48000;
2161 break;
2162 case 88200:
2163 channel_status[3] |= SR_88200;
2164 break;
2165 case 96000:
2166 channel_status[3] |= SR_96000;
2167 break;
2168 case 176400:
2169 channel_status[3] |= SR_176400;
2170 break;
2171 case 192000:
2172 channel_status[3] |= SR_192000;
2173 break;
2174 default:
2175 ALOGV("Invalid sample_rate %u\n", sampleRate);
2176 status = -1;
2177 break;
2178 }
2179}
2180
2181void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
2182{
2183 unsigned char channel_status[24]={0};
2184 struct snd_aes_iec958 iec958;
2185 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
2186 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302187 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002188
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302189 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05302190 /*TODO:Extend code to support DTS passthrough*/
2191 /*set compressed channel status bits*/
Arun Mirpurie008ed22019-03-21 11:21:04 -07002192 audio_extn_passthru_is_passthrough_stream(out) &&
2193 audio_extn_is_hdmi_passthru_enabled()) {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302194 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
Arun Mirpurie008ed22019-03-21 11:21:04 -07002195 } else {
Ashish Jain81eb2a82015-05-13 10:52:34 +05302196 /*set channel status bit for LPCM*/
2197 get_lpcm_channel_status(out->sample_rate, channel_status);
2198 }
2199
2200 memcpy(iec958.status, channel_status,sizeof(iec958.status));
2201 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
2202 if (!ctl) {
2203 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2204 __func__, mixer_ctl_name);
2205 return;
2206 }
2207 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
2208 ALOGE("%s: Could not set channel status for ext HDMI ",
2209 __func__);
2210 return;
2211 }
2212
2213}
2214#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302215
2216int audio_extn_utils_get_avt_device_drift(
2217 struct audio_usecase *usecase,
2218 struct audio_avt_device_drift_param *drift_param)
2219{
2220 int ret = 0, count = 0;
2221 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05302222 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302223 struct mixer_ctl *ctl = NULL;
2224 struct audio_avt_device_drift_stats drift_stats;
2225 struct audio_device *adev = NULL;
2226
2227 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05302228 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
2229 if (!backend) {
2230 ALOGE("%s: Unsupported device %d", __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002231 get_device_types(&usecase->stream.out->device_list));
Naresh Tanniru6160c712017-04-17 15:43:48 +05302232 ret = -EINVAL;
2233 goto done;
2234 }
2235 strlcpy(avt_device_drift_mixer_ctl_name,
2236 backend,
2237 MIXER_PATH_MAX_LENGTH);
2238
2239 count = strlen(backend);
2240 if (MIXER_PATH_MAX_LENGTH - count > 0) {
2241 strlcat(&avt_device_drift_mixer_ctl_name[count],
2242 " DRIFT",
2243 MIXER_PATH_MAX_LENGTH - count);
2244 } else {
2245 ret = -EINVAL;
2246 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302247 }
2248 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05302249 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302250 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05302251 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302252 }
2253
Naresh Tanniru6160c712017-04-17 15:43:48 +05302254 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05302255 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
2256 if (!ctl) {
2257 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2258 __func__, avt_device_drift_mixer_ctl_name);
2259
2260 ret = -EINVAL;
2261 goto done;
2262 }
2263
2264 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
2265 avt_device_drift_mixer_ctl_name);
2266
2267 mixer_ctl_update(ctl);
2268 count = mixer_ctl_get_num_values(ctl);
2269 if (count != sizeof(struct audio_avt_device_drift_stats)) {
2270 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
2271 __func__);
2272
2273 ret = -EINVAL;
2274 goto done;
2275 }
2276
2277 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
2278 if (ret != 0) {
2279 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
2280 __func__);
2281
2282 ret = -EINVAL;
2283 goto done;
2284 }
2285 memcpy(drift_param, &drift_stats.drift_param,
2286 sizeof(struct audio_avt_device_drift_param));
2287done:
2288 return ret;
2289}
Manish Dewangan07de2142017-02-27 19:27:20 +05302290
2291#ifdef SNDRV_COMPRESS_PATH_DELAY
2292int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
2293{
2294 int ret = -EINVAL;
2295 struct snd_compr_metadata metadata;
2296 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2297
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002298 /* override the latency for pcm offload use case */
2299 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
2300 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2301 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
2302 }
2303
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002304 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05302305 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
2306 if (!(is_offload_usecase(out->usecase))) {
2307 ALOGE("%s:: not supported for non offload session", __func__);
2308 goto exit;
2309 }
2310
2311 if (!out->compr) {
2312 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
2313 __func__);
2314 goto exit;
2315 }
2316
2317 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
2318 ret = compress_get_metadata(out->compr, &metadata);
2319 if(ret) {
2320 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2321 goto exit;
2322 }
2323 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
2324 } else {
2325 ALOGD("%s:: Using Fix DSP delay",__func__);
2326 }
2327
2328exit:
2329 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
2330 return delay_ms;
2331}
2332#else
2333int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
2334{
2335 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2336}
2337#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05302338
2339#ifdef SNDRV_COMPRESS_RENDER_MODE
2340int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
2341{
2342 struct snd_compr_metadata metadata;
2343 int ret = -EINVAL;
2344
2345 if (!(is_offload_usecase(out->usecase))) {
2346 ALOGE("%s:: not supported for non offload session", __func__);
2347 goto exit;
2348 }
2349
2350 if (!out->compr) {
2351 ALOGD("%s:: Invalid compress handle",
2352 __func__);
2353 goto exit;
2354 }
2355
2356 ALOGD("%s:: render mode %d", __func__, out->render_mode);
2357
2358 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
2359 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
2360 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
2361 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
2362 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
2363 } else {
2364 ret = 0;
2365 goto exit;
2366 }
2367 ret = compress_set_metadata(out->compr, &metadata);
2368 if(ret) {
2369 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2370 }
2371exit:
2372 return ret;
2373}
2374#else
2375int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
2376{
2377 ALOGD("%s:: configuring render mode not supported", __func__);
2378 return 0;
2379}
2380#endif
Manish Dewangan58229382017-02-02 15:48:41 +05302381
2382#ifdef SNDRV_COMPRESS_CLK_REC_MODE
2383int audio_extn_utils_compress_set_clk_rec_mode(
2384 struct audio_usecase *usecase)
2385{
2386 struct snd_compr_metadata metadata;
2387 struct stream_out *out = NULL;
2388 int ret = -EINVAL;
2389
Naresh Tanniru7586e292017-04-13 10:38:13 +05302390 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05302391 ALOGE("%s:: Invalid use case", __func__);
2392 goto exit;
2393 }
2394
2395 out = usecase->stream.out;
2396 if (!out) {
2397 ALOGE("%s:: invalid stream", __func__);
2398 goto exit;
2399 }
2400
2401 if (!is_offload_usecase(out->usecase)) {
2402 ALOGE("%s:: not supported for non offload session", __func__);
2403 goto exit;
2404 }
2405
2406 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
2407 ALOGD("%s:: clk recovery is only supported in STC render mode",
2408 __func__);
2409 ret = 0;
2410 goto exit;
2411 }
2412
2413 if (!out->compr) {
2414 ALOGD("%s:: Invalid compress handle",
2415 __func__);
2416 goto exit;
2417 }
2418 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
2419 switch(usecase->out_snd_device) {
2420 case SND_DEVICE_OUT_HDMI:
2421 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
2422 case SND_DEVICE_OUT_DISPLAY_PORT:
2423 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
2424 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
2425 break;
2426 default:
2427 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
2428 break;
2429 }
2430
2431 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
2432
2433 ret = compress_set_metadata(out->compr, &metadata);
2434 if(ret) {
2435 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2436 }
2437
2438exit:
2439 return ret;
2440}
2441#else
2442int audio_extn_utils_compress_set_clk_rec_mode(
2443 struct audio_usecase *usecase __unused)
2444{
2445 ALOGD("%s:: configuring render mode not supported", __func__);
2446 return 0;
2447}
2448#endif
Manish Dewangan27346042017-03-01 12:56:12 +05302449
2450#ifdef SNDRV_COMPRESS_RENDER_WINDOW
2451int audio_extn_utils_compress_set_render_window(
2452 struct stream_out *out,
2453 struct audio_out_render_window_param *render_window)
2454{
2455 struct snd_compr_metadata metadata;
2456 int ret = -EINVAL;
2457
Manish Dewangan27346042017-03-01 12:56:12 +05302458 if(render_window == NULL) {
2459 ALOGE("%s:: Invalid render_window", __func__);
2460 goto exit;
2461 }
2462
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002463 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2464 __func__,render_window->render_ws, render_window->render_we);
2465
Manish Dewangan27346042017-03-01 12:56:12 +05302466 if (!is_offload_usecase(out->usecase)) {
2467 ALOGE("%s:: not supported for non offload session", __func__);
2468 goto exit;
2469 }
2470
Naresh Tanniru6160c712017-04-17 15:43:48 +05302471 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2472 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302473 ALOGD("%s:: only supported in timestamp mode, current "
2474 "render mode mode %d", __func__, out->render_mode);
2475 goto exit;
2476 }
2477
2478 if (!out->compr) {
2479 ALOGW("%s:: offload session not yet opened,"
2480 "render window will be configure later", __func__);
2481 /* store render window to reconfigure in start_output_stream() */
2482 goto exit;
2483 }
2484
2485 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2486 /*render window start value */
2487 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2488 metadata.value[1] = \
2489 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2490 /*render window end value */
2491 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2492 metadata.value[3] = \
2493 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2494
2495 ret = compress_set_metadata(out->compr, &metadata);
2496 if(ret) {
2497 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2498 }
2499
2500exit:
2501 return ret;
2502}
2503#else
2504int audio_extn_utils_compress_set_render_window(
2505 struct stream_out *out __unused,
2506 struct audio_out_render_window_param *render_window __unused)
2507{
2508 ALOGD("%s:: configuring render window not supported", __func__);
2509 return 0;
2510}
2511#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302512
2513#ifdef SNDRV_COMPRESS_START_DELAY
2514int audio_extn_utils_compress_set_start_delay(
2515 struct stream_out *out,
2516 struct audio_out_start_delay_param *delay_param)
2517{
2518 struct snd_compr_metadata metadata;
2519 int ret = -EINVAL;
2520
2521 if(delay_param == NULL) {
2522 ALOGE("%s:: Invalid delay_param", __func__);
2523 goto exit;
2524 }
2525
2526 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2527 delay_param->start_delay);
2528
2529 if (!is_offload_usecase(out->usecase)) {
2530 ALOGE("%s:: not supported for non offload session", __func__);
2531 goto exit;
2532 }
2533
Naresh Tanniru6160c712017-04-17 15:43:48 +05302534 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2535 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302536 ALOGD("%s:: only supported in timestamp mode, current "
2537 "render mode mode %d", __func__, out->render_mode);
2538 goto exit;
2539 }
2540
2541 if (!out->compr) {
2542 ALOGW("%s:: offload session not yet opened,"
2543 "start delay will be configure later", __func__);
2544 goto exit;
2545 }
2546
2547 metadata.key = SNDRV_COMPRESS_START_DELAY;
2548 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2549 metadata.value[1] = \
2550 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2551
2552 ret = compress_set_metadata(out->compr, &metadata);
2553 if(ret) {
2554 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2555 }
2556
2557exit:
2558 return ret;
2559}
2560#else
2561int audio_extn_utils_compress_set_start_delay(
2562 struct stream_out *out __unused,
2563 struct audio_out_start_delay_param *delay_param __unused)
2564{
2565 ALOGD("%s:: configuring render window not supported", __func__);
2566 return 0;
2567}
2568#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002569
Surendar Karka287348c2019-04-10 18:31:46 +05302570#ifdef SNDRV_COMPRESS_DSP_POSITION
2571int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out,
2572 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2573{
2574 int ret = -EINVAL;
2575 uint64_t *val = NULL;
2576 uint64_t time = 0;
2577 struct snd_compr_metadata metadata;
2578
2579 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2580 metadata.key = SNDRV_COMPRESS_DSP_POSITION;
2581 metadata.value[0] = clock_id;
2582 ret = compress_get_metadata(out->compr, &metadata);
2583 if (ret) {
2584 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2585 ret = -errno;
2586 goto exit;
2587 }
2588 val = (uint64_t *)&metadata.value[1];
2589 *frames = *val;
2590 time = *(val + 1);
2591 timestamp->tv_sec = time / 1000000;
2592 timestamp->tv_nsec = (time % 1000000)*1000;
2593
2594exit:
2595 return ret;
2596}
2597#else
2598int audio_extn_utils_compress_get_dsp_presentation_pos(struct stream_out *out __unused,
2599 uint64_t *frames __unused, struct timespec *timestamp __unused,
2600 int32_t clock_id __unused)
2601{
2602 ALOGD("%s:: dsp presentation position not supported", __func__);
2603 return 0;
2604
2605}
2606#endif
2607
2608#ifdef SNDRV_PCM_IOCTL_DSP_POSITION
2609int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out,
2610 uint64_t *frames, struct timespec *timestamp, int32_t clock_id)
2611{
2612 int ret = -EINVAL;
2613 uint64_t time = 0;
2614 struct snd_pcm_prsnt_position prsnt_position;
2615
2616 ALOGV("%s:: Quering DSP position with clock id %d",__func__, clock_id);
2617 prsnt_position.clock_id = clock_id;
2618 ret = pcm_ioctl(out->pcm, SNDRV_PCM_IOCTL_DSP_POSITION, &prsnt_position);
2619 if (ret) {
2620 ALOGE("%s::error %d", __func__, ret);
2621 ret = -EIO;
2622 goto exit;
2623 }
2624
2625 *frames = prsnt_position.frames;
2626 time = prsnt_position.timestamp;
2627 timestamp->tv_sec = time / 1000000;
2628 timestamp->tv_nsec = (time % 1000000)*1000;
2629
2630exit:
2631 return ret;
2632}
2633#else
2634int audio_extn_utils_pcm_get_dsp_presentation_pos(struct stream_out *out __unused,
2635 uint64_t *frames __unused, struct timespec *timestamp __unused,
2636 int32_t clock_id __unused)
2637{
2638 ALOGD("%s:: dsp presentation position not supported", __func__);
2639 return 0;
2640
2641}
2642#endif
2643
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002644#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302645#define RETRY_US 1000000
2646#define RETRY_NUMBER 40
Aalique Grahame22e49102018-12-18 14:23:57 -08002647#define PLATFORM_INFO_XML_PATH "audio_platform_info.xml"
2648#define PLATFORM_INFO_XML_BASE_STRING "audio_platform_info"
2649
Aalique Grahame22e49102018-12-18 14:23:57 -08002650bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
2651{
2652 char full_config_path[MIXER_PATH_MAX_LENGTH];
Saurav Kumarf5d2c342020-07-29 19:31:49 +05302653 char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH];
2654
2655 /* Get path for audio configuration files in vendor */
2656 audio_get_vendor_config_path(vendor_config_path,
2657 sizeof(vendor_config_path));
2658 snprintf(full_config_path, sizeof(full_config_path),
2659 "%s/%s", vendor_config_path, file_name);
2660 if (F_OK == access(full_config_path, 0)) {
2661 strlcpy(file_name, full_config_path, MIXER_PATH_MAX_LENGTH);
2662 return true;
Aalique Grahame22e49102018-12-18 14:23:57 -08002663 }
2664 return false;
2665}
2666
2667/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
2668int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
2669{
2670 if (NULL == snd_card_name) {
2671 return -1;
2672 }
2673
2674 struct snd_card_split *snd_split_handle = NULL;
2675 int ret = 0;
2676 audio_extn_set_snd_card_split(snd_card_name);
2677 snd_split_handle = audio_extn_get_snd_card_split();
2678
2679 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
2680 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
2681 snd_split_handle->form_factor);
2682
2683 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2684 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2685 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
2686 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
2687
2688 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
2689 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
2690 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
2691 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
2692 }
2693 }
2694
2695 return ret;
2696}
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002697
2698int audio_extn_utils_get_snd_card_num()
2699{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302700 int snd_card_num = 0;
2701 struct mixer *mixer = NULL;
2702
2703 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2704 if (mixer)
2705 mixer_close(mixer);
2706 return snd_card_num;
2707}
2708
2709int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2710{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002711
2712 void *hw_info = NULL;
2713 struct mixer *mixer = NULL;
2714 int retry_num = 0;
mpangfaa7bae2019-01-15 16:38:13 +08002715 int snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002716 char* snd_card_name = NULL;
mpangfaa7bae2019-01-15 16:38:13 +08002717 int snd_card_detection_info[MAX_SND_CARD] = {0};
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002718
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302719 if (!mixer_handle) {
2720 ALOGE("invalid mixer handle");
2721 return -1;
2722 }
2723 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302724 /*
mpangfaa7bae2019-01-15 16:38:13 +08002725 * Try with all the sound cards ( 0 to 7 ) and if none of them were detected
Ashish Jain30ae8942017-11-05 17:21:23 +05302726 * sleep for 1 sec and try detections with sound card 0 again.
2727 * If sound card gets detected, check if it is relevant, if not check with the
2728 * other sound cards. To ensure that the irrelevant sound card is not check again,
2729 * we maintain it in min_snd_card_num.
2730 */
2731 while (retry_num < RETRY_NUMBER) {
mpangfaa7bae2019-01-15 16:38:13 +08002732 snd_card_num = 0;
2733 while (snd_card_num < MAX_SND_CARD) {
2734 if (snd_card_detection_info[snd_card_num] == 0) {
2735 mixer = mixer_open(snd_card_num);
2736 if (!mixer)
2737 snd_card_num++;
2738 else
2739 break;
2740 } else
2741 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002742 }
2743
2744 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302745 usleep(RETRY_US);
Ashish Jain30ae8942017-11-05 17:21:23 +05302746 retry_num++;
mpangfaa7bae2019-01-15 16:38:13 +08002747 ALOGD("%s: retry, retry_num %d", __func__, retry_num);
Ashish Jain30ae8942017-11-05 17:21:23 +05302748 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002749 }
2750
2751 snd_card_name = strdup(mixer_get_name(mixer));
2752 if (!snd_card_name) {
2753 ALOGE("failed to allocate memory for snd_card_name\n");
2754 mixer_close(mixer);
2755 return -1;
2756 }
2757 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
mpangfaa7bae2019-01-15 16:38:13 +08002758 snd_card_detection_info[snd_card_num] = 1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002759 hw_info = hw_info_init(snd_card_name);
2760 if (hw_info) {
2761 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2762 break;
2763 }
mpangfaa7bae2019-01-15 16:38:13 +08002764 ALOGE("%s: Failed to init hardware info, snd_card_num:%d", __func__, snd_card_num);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002765
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302766 free(snd_card_name);
2767 snd_card_name = NULL;
2768
2769 mixer_close(mixer);
2770 mixer = NULL;
2771 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002772 if (snd_card_name)
2773 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302774
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302775 if (hw_info)
2776 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002777
mpangfaa7bae2019-01-15 16:38:13 +08002778 if (retry_num >= RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002779 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2780 return -1;
2781 }
2782
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302783 if (mixer)
2784 *mixer_handle = mixer;
2785
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002786 return snd_card_num;
2787}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302788
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302789void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2790{
2791 if (mixer)
2792 mixer_close(mixer);
2793}
2794
Naresh Tanniru6160c712017-04-17 15:43:48 +05302795#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2796int audio_extn_utils_compress_enable_drift_correction(
2797 struct stream_out *out,
2798 struct audio_out_enable_drift_correction *drift)
2799{
2800 struct snd_compr_metadata metadata;
2801 int ret = -EINVAL;
2802
2803 if(drift == NULL) {
2804 ALOGE("%s:: Invalid param", __func__);
2805 goto exit;
2806 }
2807
2808 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2809
2810 if (!is_offload_usecase(out->usecase)) {
2811 ALOGE("%s:: not supported for non offload session", __func__);
2812 goto exit;
2813 }
2814
2815 if (!out->compr) {
2816 ALOGW("%s:: offload session not yet opened,"
2817 "start delay will be configure later", __func__);
2818 goto exit;
2819 }
2820
2821 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2822 metadata.value[0] = drift->enable;
2823 out->drift_correction_enabled = drift->enable;
2824
2825 ret = compress_set_metadata(out->compr, &metadata);
2826 if(ret) {
2827 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2828 out->drift_correction_enabled = false;
2829 }
2830
2831exit:
2832 return ret;
2833}
2834#else
2835int audio_extn_utils_compress_enable_drift_correction(
2836 struct stream_out *out __unused,
2837 struct audio_out_enable_drift_correction *drift __unused)
2838{
2839 ALOGD("%s:: configuring drift enablement not supported", __func__);
2840 return 0;
2841}
2842#endif
2843
2844#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2845int audio_extn_utils_compress_correct_drift(
2846 struct stream_out *out,
2847 struct audio_out_correct_drift *drift_param)
2848{
2849 struct snd_compr_metadata metadata;
2850 int ret = -EINVAL;
2851
2852 if (drift_param == NULL) {
2853 ALOGE("%s:: Invalid drift_param", __func__);
2854 goto exit;
2855 }
2856
2857 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2858 drift_param->adjust_time);
2859
2860 if (!is_offload_usecase(out->usecase)) {
2861 ALOGE("%s:: not supported for non offload session", __func__);
2862 goto exit;
2863 }
2864
2865 if (!out->compr) {
2866 ALOGW("%s:: offload session not yet opened", __func__);
2867 goto exit;
2868 }
2869
2870 if (!out->drift_correction_enabled) {
2871 ALOGE("%s:: drift correction not enabled", __func__);
2872 goto exit;
2873 }
2874
2875 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2876 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2877 metadata.value[1] = \
2878 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2879
2880 ret = compress_set_metadata(out->compr, &metadata);
2881 if(ret)
2882 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2883exit:
2884 return ret;
2885}
2886#else
2887int audio_extn_utils_compress_correct_drift(
2888 struct stream_out *out __unused,
2889 struct audio_out_correct_drift *drift_param __unused)
2890{
2891 ALOGD("%s:: setting adjust clock not supported", __func__);
2892 return 0;
2893}
2894#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302895
2896int audio_extn_utils_set_channel_map(
2897 struct stream_out *out,
2898 struct audio_out_channel_map_param *channel_map_param)
2899{
2900 int ret = -EINVAL, i = 0;
2901 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2902
2903 if (channel_map_param == NULL) {
2904 ALOGE("%s:: Invalid channel_map", __func__);
2905 goto exit;
2906 }
2907
2908 if (channel_map_param->channels != channels) {
2909 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2910 __func__, channel_map_param->channels, channels);
2911 goto exit;
2912 }
2913
2914 for ( i = 0; i < channels; i++) {
2915 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2916 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2917 }
2918 ret = 0;
2919exit:
2920 return ret;
2921}
Varun Balaraje49253e2017-07-06 19:48:56 +05302922
2923int audio_extn_utils_set_pan_scale_params(
2924 struct stream_out *out,
2925 struct mix_matrix_params *mm_params)
2926{
2927 int ret = -EINVAL, i = 0, j = 0;
2928
Varun Balaraj003ee752017-08-07 17:54:55 +05302929 if (mm_params == NULL || out == NULL) {
2930 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302931 goto exit;
2932 }
2933
2934 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2935 mm_params->num_output_channels <= 0 ||
2936 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2937 mm_params->num_input_channels <= 0)
2938 goto exit;
2939
2940 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2941 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2942 out->pan_scale_params.has_output_channel_map =
2943 mm_params->has_output_channel_map;
2944 for (i = 0; i < mm_params->num_output_channels; i++)
2945 out->pan_scale_params.output_channel_map[i] =
2946 mm_params->output_channel_map[i];
2947
2948 out->pan_scale_params.has_input_channel_map =
2949 mm_params->has_input_channel_map;
2950 for (i = 0; i < mm_params->num_input_channels; i++)
2951 out->pan_scale_params.input_channel_map[i] =
2952 mm_params->input_channel_map[i];
2953
2954 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2955 for (i = 0; i < mm_params->num_output_channels; i++)
2956 for (j = 0; j < mm_params->num_input_channels; j++) {
2957 //Convert the channel coefficient gains in Q14 format
2958 out->pan_scale_params.mixer_coeffs[i][j] =
2959 mm_params->mixer_coeffs[i][j] * (2 << 13);
2960 }
2961
2962 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2963 out->pcm_device_id,
2964 out->pan_scale_params);
2965
2966exit:
2967 return ret;
2968}
2969
2970int audio_extn_utils_set_downmix_params(
2971 struct stream_out *out,
2972 struct mix_matrix_params *mm_params)
2973{
2974 int ret = -EINVAL, i = 0, j = 0;
2975 struct audio_usecase *usecase = NULL;
2976
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002977 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302978 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302979 goto exit;
2980 }
2981
2982 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2983 mm_params->num_output_channels <= 0 ||
2984 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2985 mm_params->num_input_channels <= 0)
2986 goto exit;
2987
2988 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302989 if (!usecase) {
2990 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002991 goto exit;
2992 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302993 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2994 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2995
2996 out->downmix_params.has_output_channel_map =
2997 mm_params->has_output_channel_map;
2998 for (i = 0; i < mm_params->num_output_channels; i++) {
2999 out->downmix_params.output_channel_map[i] =
3000 mm_params->output_channel_map[i];
3001 }
3002
3003 out->downmix_params.has_input_channel_map =
3004 mm_params->has_input_channel_map;
3005 for (i = 0; i < mm_params->num_input_channels; i++)
3006 out->downmix_params.input_channel_map[i] =
3007 mm_params->input_channel_map[i];
3008
3009 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
3010 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303011 for (j = 0; j < mm_params->num_input_channels; j++) {
3012 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05303013 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05303014 mm_params->mixer_coeffs[i][j] * (2 << 13);
3015 }
Varun Balaraje49253e2017-07-06 19:48:56 +05303016
3017 ret = platform_set_stream_downmix_params(out->dev->platform,
3018 out->pcm_device_id,
3019 usecase->out_snd_device,
3020 out->downmix_params);
3021
3022exit:
3023 return ret;
3024}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303025
3026bool audio_extn_utils_is_dolby_format(audio_format_t format)
3027{
3028 if (format == AUDIO_FORMAT_AC3 ||
3029 format == AUDIO_FORMAT_E_AC3 ||
3030 format == AUDIO_FORMAT_E_AC3_JOC)
3031 return true;
3032 else
3033 return false;
3034}
3035
`Deeraj Soman676c2702017-09-18 19:25:53 +05303036int audio_extn_utils_get_bit_width_from_string(const char *id_string)
3037{
3038 int i;
3039 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
3040 {"S32_LE", 32},
3041 {"S24_LE", 24},
3042 {"S16_LE", 16}};
3043 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303044
`Deeraj Soman676c2702017-09-18 19:25:53 +05303045 for (i = 0; i < num_configs; i++) {
3046 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
3047 return mixer_bitwidth_config[i].value;
3048 }
3049
3050 return -EINVAL;
3051}
3052
3053int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
3054{
3055 int i;
3056 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
3057 {"KHZ_48", 48000},
3058 {"KHZ_96", 96000},
3059 {"KHZ_144", 144000},
3060 {"KHZ_192", 192000},
3061 {"KHZ_384", 384000},
3062 {"KHZ_44P1", 44100},
3063 {"KHZ_88P2", 88200},
3064 {"KHZ_176P4", 176400},
3065 {"KHZ_352P8", 352800}};
3066 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
3067
3068 for (i = 0; i < num_configs; i++) {
3069 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
3070 return mixer_samplerate_config[i].value;
3071 }
3072
3073 return -EINVAL;
3074}
3075
3076int audio_extn_utils_get_channels_from_string(const char *id_string)
3077{
3078 int i;
3079 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
3080 {"Two", 2},
3081 {"Three",3},
3082 {"Four", 4},
3083 {"Five", 5},
3084 {"Six", 6},
3085 {"Seven", 7},
3086 {"Eight", 8}};
3087 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
3088
3089 for (i = 0; i < num_configs; i++) {
3090 if (!strcmp(id_string, mixer_channels_config[i].id_string))
3091 return mixer_channels_config[i].value;
3092 }
3093
3094 return -EINVAL;
3095}
Surendar karka30569792018-05-08 12:02:21 +05303096
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08003097void audio_extn_utils_release_snd_device(snd_device_t snd_device)
3098{
3099 audio_extn_dev_arbi_release(snd_device);
3100 audio_extn_sound_trigger_update_device_status(snd_device,
3101 ST_EVENT_SND_DEVICE_FREE);
3102 audio_extn_listen_update_device_status(snd_device,
3103 LISTEN_EVENT_SND_DEVICE_FREE);
3104}
3105
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003106int audio_extn_utils_get_license_params(
3107 const struct audio_device *adev,
3108 struct audio_license_params *license_params)
Surendar karka30569792018-05-08 12:02:21 +05303109{
3110 if(!license_params)
3111 return -EINVAL;
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003112
3113 return platform_get_license_by_product(adev->platform,
3114 (const char*)license_params->product, &license_params->key, license_params->license);
Surendar karka30569792018-05-08 12:02:21 +05303115}
3116
Aalique Grahame22e49102018-12-18 14:23:57 -08003117int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
3118 int app_type,
3119 int *gain)
3120{
3121 int gain_cfg[4];
3122 const char *mixer_ctl_name = "App Type Gain";
3123 struct mixer_ctl *ctl;
3124 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3125 if (!ctl) {
3126 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
3127 mixer_ctl_name);
3128 return -EINVAL;
3129 }
3130 gain_cfg[0] = 0;
3131 gain_cfg[1] = app_type;
3132 gain_cfg[2] = gain[0];
3133 gain_cfg[3] = gain[1];
3134 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
3135 return mixer_ctl_set_array(ctl, gain_cfg,
3136 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
3137}
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003138
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003139static void vndk_fwk_init()
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003140{
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003141 if (mVndkFwk.lib_handle != NULL)
3142 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003143
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003144 mVndkFwk.lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
3145 if (mVndkFwk.lib_handle == NULL) {
3146 ALOGW("%s: failed to dlopen VNDK_FWK_LIB %s", __func__, strerror(errno));
3147 return;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003148 }
3149
Weiyin Jiangd5718bd2019-09-04 16:52:08 +08003150 *(void **)(&mVndkFwk.isVendorEnhancedFwk) =
3151 dlsym(mVndkFwk.lib_handle, "isRunningWithVendorEnhancedFramework");
3152 if (mVndkFwk.isVendorEnhancedFwk == NULL) {
3153 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3154 if (mVndkFwk.lib_handle) {
3155 dlclose(mVndkFwk.lib_handle);
3156 mVndkFwk.lib_handle = NULL;
3157 }
3158 return;
3159 }
3160
3161
3162 *(void **)(&mVndkFwk.getVendorEnhancedInfo) =
3163 dlsym(mVndkFwk.lib_handle, "getVendorEnhancedInfo");
3164 if (mVndkFwk.getVendorEnhancedInfo == NULL) {
3165 ALOGW("%s: dlsym failed %s", __func__, strerror(errno));
3166 if (mVndkFwk.lib_handle) {
3167 dlclose(mVndkFwk.lib_handle);
3168 mVndkFwk.lib_handle = NULL;
3169 }
3170 }
3171
3172 return;
3173}
3174
3175bool audio_extn_utils_is_vendor_enhanced_fwk()
3176{
3177 static int is_vendor_enhanced_fwk = -EINVAL;
3178 if (is_vendor_enhanced_fwk != -EINVAL)
3179 return (bool)is_vendor_enhanced_fwk;
3180
3181 vndk_fwk_init();
3182
3183 if (mVndkFwk.isVendorEnhancedFwk != NULL) {
3184 is_vendor_enhanced_fwk = mVndkFwk.isVendorEnhancedFwk();
3185 ALOGW("%s: is_vendor_enhanced_fwk %d", __func__, is_vendor_enhanced_fwk);
3186 } else {
3187 is_vendor_enhanced_fwk = 0;
3188 ALOGW("%s: default to non enhanced_fwk config", __func__);
3189 }
3190
3191 return (bool)is_vendor_enhanced_fwk;
3192}
3193
3194int audio_extn_utils_get_vendor_enhanced_info()
3195{
3196 static int vendor_enhanced_info = -EINVAL;
3197 if (vendor_enhanced_info != -EINVAL)
3198 return vendor_enhanced_info;
3199
3200 vndk_fwk_init();
3201
3202 if (mVndkFwk.getVendorEnhancedInfo != NULL) {
3203 vendor_enhanced_info = mVndkFwk.getVendorEnhancedInfo();
3204 ALOGW("%s: vendor_enhanced_info 0x%x", __func__, vendor_enhanced_info);
3205 } else {
3206 vendor_enhanced_info = 0x0;
3207 ALOGW("%s: default to vendor_enhanced_info 0x0", __func__);
3208 }
3209
3210 return vendor_enhanced_info;
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07003211}
Deeraj Soman14230922019-01-30 16:39:30 +05303212
Deeraj Somanfa377bf2019-02-06 12:57:59 +05303213int audio_extn_utils_get_perf_mode_flag(void)
3214{
3215#ifdef COMPRESSED_PERF_MODE_FLAG
3216 return COMPRESSED_PERF_MODE_FLAG;
3217#else
3218 return 0;
3219#endif
3220}
3221
Deeraj Soman14230922019-01-30 16:39:30 +05303222size_t audio_extn_utils_get_input_buffer_size(uint32_t sample_rate,
3223 audio_format_t format,
3224 int channel_count,
3225 int64_t duration_ms,
3226 bool is_low_latency)
3227{
3228 size_t size = 0;
3229 size_t capture_duration = AUDIO_CAPTURE_PERIOD_DURATION_MSEC;
3230 uint32_t bytes_per_period_sample = 0;
3231
3232
3233 if (audio_extn_utils_check_input_parameters(sample_rate, format, channel_count) != 0)
3234 return 0;
3235
3236 if (duration_ms >= MIN_OFFLOAD_BUFFER_DURATION_MS && duration_ms <= MAX_OFFLOAD_BUFFER_DURATION_MS)
3237 capture_duration = duration_ms;
3238
3239 size = (sample_rate * capture_duration) / 1000;
3240 if (is_low_latency)
3241 size = LOW_LATENCY_CAPTURE_PERIOD_SIZE;
3242
3243
3244 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3245 size *= bytes_per_period_sample;
3246
3247 /* make sure the size is multiple of 32 bytes and additionally multiple of
3248 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
3249 * At 48 kHz mono 16-bit PCM:
3250 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3251 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3252 *
3253 * The loop reaches result within 32 iterations, as initial size is
3254 * already a multiple of frame_size
3255 */
3256 size = audio_extn_utils_nearest_multiple(size, audio_extn_utils_lcm(32, bytes_per_period_sample));
3257
3258 return size;
3259}
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07003260
3261int audio_extn_utils_hash_fn(void *key)
3262{
3263 return (int)key;
3264}
3265
3266bool audio_extn_utils_hash_eq(void *key1, void *key2)
3267{
3268 return (key1 == key2);
3269}