blob: 0a48953587bfe04e14b5f3c352be74dcc5f86e87 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08002 * Copyright (c) 2014-2018, 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>
30#include <cutils/log.h>
31#include <cutils/misc.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053032#include <unistd.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070033
Manish Dewangan07de2142017-02-27 19:27:20 +053034
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070035#include "audio_hw.h"
36#include "platform.h"
37#include "platform_api.h"
38#include "audio_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080039#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053040#include <sound/compress_params.h>
41#include <sound/compress_offload.h>
42#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053043
44#ifdef DYNAMIC_LOG_ENABLED
45#include <log_xml_parser.h>
46#define LOG_MASK HAL_MOD_FILE_UTILS
47#include <log_utils.h>
48#endif
49
Ashish Jain81eb2a82015-05-13 10:52:34 +053050#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
51#ifdef HDMI_PASSTHROUGH_ENABLED
52#include "audio_parsers.h"
53#endif
54#endif
55
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053056#ifdef LINUX_ENABLED
57#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053058#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053059#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070060#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053061#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053062#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070063
64#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053065#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070066
67#define DYNAMIC_VALUE_TAG "dynamic"
68#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053069#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070070#define FORMATS_TAG "formats"
71#define SAMPLING_RATES_TAG "sampling_rates"
72#define BIT_WIDTH_TAG "bit_width"
73#define APP_TYPE_TAG "app_type"
74
75#define STRING_TO_ENUM(string) { #string, string }
76#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
77
Ben Rombergera04fabc2014-11-14 12:16:03 -080078#define BASE_TABLE_SIZE 64
79#define MAX_BASEINDEX_LEN 256
80
Ben Romberger1aaaf862017-04-06 17:49:46 -070081#ifndef SND_AUDIOCODEC_TRUEHD
82#define SND_AUDIOCODEC_TRUEHD 0x00000023
83#endif
84
Vikram Panduranga93f080e2017-06-07 18:16:14 -070085#define APP_TYPE_VOIP_AUDIO 0x1113A
86
Ashish Jain81eb2a82015-05-13 10:52:34 +053087#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
88#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
89#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
90#define SR_44100 (0<<0) /* 44.1kHz */
91#define SR_NOTID (1<<0) /* non indicated */
92#define SR_48000 (2<<0) /* 48kHz */
93#define SR_32000 (3<<0) /* 32kHz */
94#define SR_22050 (4<<0) /* 22.05kHz */
95#define SR_24000 (6<<0) /* 24kHz */
96#define SR_88200 (8<<0) /* 88.2kHz */
97#define SR_96000 (10<<0) /* 96kHz */
98#define SR_176400 (12<<0) /* 176.4kHz */
99#define SR_192000 (14<<0) /* 192kHz */
100
101#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530102
103/* ToDo: Check and update a proper value in msec */
Weiyin Jiangc49a3b52018-08-17 16:16:08 +0800104#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
105#define PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY 62
106#define PCM_OFFLOAD_PLAYBACK_LATENCY \
107 (PCM_OFFLOAD_BUFFER_DURATION + PCM_OFFLOAD_PLAYBACK_DSP_PATHDELAY)
Manish Dewangan07de2142017-02-27 19:27:20 +0530108
Varun Balaraje49253e2017-07-06 19:48:56 +0530109#ifndef MAX_CHANNELS_SUPPORTED
110#define MAX_CHANNELS_SUPPORTED 8
111#endif
112
`Deeraj Soman676c2702017-09-18 19:25:53 +0530113typedef struct {
114 const char *id_string;
115 const int value;
116} mixer_config_lookup;
117
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700118struct string_to_enum {
119 const char *name;
120 uint32_t value;
121};
122
123const struct string_to_enum s_flag_name_to_enum_table[] = {
124 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
125 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
126 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800127 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
129 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
130 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700131 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700132#ifdef INCALL_MUSIC_ENABLED
133 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
134#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530136 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700137 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700138 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530139 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530140 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
141 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
142 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
143 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
144 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530145 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530146 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700147};
148
149const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530150 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700151 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530152 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
153 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530154 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700155 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
156 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
157 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700158 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
159 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700160 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700161 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700162 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530163 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700164 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700165 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530166#ifdef AUDIO_EXTN_FORMATS_ENABLED
167 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700168 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
169 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
170 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700171 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
172 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
173 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
174 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
175 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
176 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
177 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530179 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
180 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700181 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800182 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
183 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
184 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530185 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530186 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
187 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
188 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530189 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530190 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
191 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
192 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
193 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530194 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700195#endif
196};
197
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530198/* payload structure avt_device drift query */
199struct audio_avt_device_drift_stats {
200 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530201
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530202 /* Indicates the device interface direction as either
203 * source (Tx) or sink (Rx).
204 */
205 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530206
207 /* Reference timer for drift accumulation and time stamp information.
208 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
209 */
210 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530211 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530212} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530213
Ben Rombergera04fabc2014-11-14 12:16:03 -0800214static char bTable[BASE_TABLE_SIZE] = {
215 'A','B','C','D','E','F','G','H','I','J','K','L',
216 'M','N','O','P','Q','R','S','T','U','V','W','X',
217 'Y','Z','a','b','c','d','e','f','g','h','i','j',
218 'k','l','m','n','o','p','q','r','s','t','u','v',
219 'w','x','y','z','0','1','2','3','4','5','6','7',
220 '8','9','+','/'
221};
222
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700223static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
224 const char *name)
225{
226 size_t i;
227 for (i = 0; i < size; i++) {
228 if (strcmp(table[i].name, name) == 0) {
229 ALOGV("%s found %s", __func__, table[i].name);
230 return table[i].value;
231 }
232 }
233 return 0;
234}
235
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530236static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700237{
238 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530239 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800240 char *last_r;
241 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700242 while (flag_name != NULL) {
243 if (strlen(flag_name) != 0) {
244 flag |= string_to_enum(s_flag_name_to_enum_table,
245 ARRAY_SIZE(s_flag_name_to_enum_table),
246 flag_name);
247 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800248 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700249 }
250
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800251 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530252 io_flags.in_flags = (audio_input_flags_t)flag;
253 io_flags.out_flags = (audio_output_flags_t)flag;
254 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255}
256
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530257static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700258{
259 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800260 char *last_r;
261 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700262
263 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
264 return;
265
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530266 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700267 while (str != NULL) {
268 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
269 ARRAY_SIZE(s_format_name_to_enum_table), str);
270 ALOGV("%s: format - %d", __func__, format);
271 if (format != 0) {
272 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700273 if (sf_info == NULL)
274 break; /* return whatever was parsed */
275
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700276 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530277 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700278 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800279 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280 }
281}
282
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530283static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700284{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700285 struct stream_sample_rate *ss_info = NULL;
286 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800287 char *last_r;
288 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289
Amit Shekhar6f461b12014-08-01 14:52:58 -0700290 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
291 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700292
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530293 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700294 while (str != NULL) {
295 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
296 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
297 if (0 != sample_rate) {
298 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800299 if (!ss_info) {
300 ALOGE("%s: memory allocation failure", __func__);
301 return;
302 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700303 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530304 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700305 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800306 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700307 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700308}
309
310static int parse_bit_width_names(char *name)
311{
312 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800313 char *last_r;
314 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700315
316 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
317 bit_width = (int)strtol(str, (char **)NULL, 10);
318
319 ALOGV("%s: bit_width - %d", __func__, bit_width);
320 return bit_width;
321}
322
323static int parse_app_type_names(void *platform, char *name)
324{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700325 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800326 char *last_r;
327 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700328
329 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
330 app_type = (int)strtol(str, (char **)NULL, 10);
331
332 ALOGV("%s: app_type - %d", __func__, app_type);
333 return app_type;
334}
335
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530336static void update_streams_cfg_list(cnode *root, void *platform,
337 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700338{
339 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530340 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341
342 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530343 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700344
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530345 if (!s_info) {
346 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700347 return;
348 }
349
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700350 while (node) {
351 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530352 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530353 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
354 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700355 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530356 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700357 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
359 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700360 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530361 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700362 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530363 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700364 }
365 node = node->next;
366 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530367 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700368}
369
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530370static void load_cfg_list(cnode *root, void *platform,
371 struct listnode *streams_output_cfg_list,
372 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530374 cnode *node = NULL;
375
376 node = config_find(root, OUTPUTS_TAG);
377 if (node != NULL) {
378 node = node->first_child;
379 while (node) {
380 ALOGV("%s: loading output %s", __func__, node->name);
381 update_streams_cfg_list(node, platform, streams_output_cfg_list);
382 node = node->next;
383 }
384 } else {
385 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386 }
387
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530388 node = config_find(root, INPUTS_TAG);
389 if (node != NULL) {
390 node = node->first_child;
391 while (node) {
392 ALOGV("%s: loading input %s", __func__, node->name);
393 update_streams_cfg_list(node, platform, streams_input_cfg_list);
394 node = node->next;
395 }
396 } else {
397 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700398 }
399}
400
401static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530402 struct listnode *streams_output_cfg_list,
403 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700404{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530405 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700406 int length = 0, i, num_app_types = 0;
407 struct listnode *node;
408 bool update;
409 struct mixer_ctl *ctl = NULL;
410 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530411 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800412 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700413
414 if (!mixer) {
415 ALOGE("%s: mixer is null",__func__);
416 return;
417 }
418 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
419 if (!ctl) {
420 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
421 return;
422 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530423 app_type_cfg[length++] = num_app_types;
424
425 if (list_empty(streams_output_cfg_list)) {
426 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700427 app_type_cfg[length++] = 48000;
428 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530429 num_app_types += 1;
430 }
431 if (list_empty(streams_input_cfg_list)) {
432 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
433 app_type_cfg[length++] = 48000;
434 app_type_cfg[length++] = 16;
435 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700436 }
437
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800438 /* get target bit width for ADM enforce mode */
439 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
440
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700441 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700443 update = true;
444 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530445 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700446 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530447 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530448 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530449 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530450 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530451 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800452 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
453 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
454 (target_bit_width > app_type_cfg[i+3]))
455 app_type_cfg[i+3] = target_bit_width;
456
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700457 update = false;
458 break;
459 }
460 }
461 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462 num_app_types += 1;
463 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
464 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800465 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
466 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
467 (target_bit_width > app_type_cfg[length]))
468 app_type_cfg[length] = target_bit_width;
469
470 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530471 }
472 }
473 list_for_each(node, streams_input_cfg_list) {
474 s_info = node_to_item(node, struct streams_io_cfg, list);
475 update = true;
476 for (i=0; i<length; i=i+3) {
477 if (app_type_cfg[i+1] == 0)
478 break;
479 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530480 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530481 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530482 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530483 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
484 update = false;
485 break;
486 }
487 }
488 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
489 num_app_types += 1;
490 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
491 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
492 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700493 }
494 }
495 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
496 if (num_app_types) {
497 app_type_cfg[0] = num_app_types;
498 mixer_ctl_set_array(ctl, app_type_cfg, length);
499 }
500}
501
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530502void audio_extn_utils_update_streams_cfg_lists(void *platform,
503 struct mixer *mixer,
504 struct listnode *streams_output_cfg_list,
505 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700506{
507 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530508 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700509
510 ALOGV("%s", __func__);
511 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530512 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700513
514 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700515 if (root == NULL) {
516 ALOGE("cfg_list, NULL config root");
517 return;
518 }
519
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530520 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
521 if (data == NULL) {
522 ALOGD("%s: failed to open io config file(%s), trying older config file",
523 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
524 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
525 if (data == NULL) {
526 send_app_type_cfg(platform, mixer,
527 streams_output_cfg_list,
528 streams_input_cfg_list);
529 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800530 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530531 return;
532 }
533 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700534
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530535 config_load(root, data);
536 load_cfg_list(root, platform, streams_output_cfg_list,
537 streams_input_cfg_list);
538
539 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
540 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800541
542 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800543 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800544 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700545}
546
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530547static void audio_extn_utils_dump_streams_cfg_list(
548 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700549{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700550 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530551 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700552 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700553 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530554
555 list_for_each(node_i, streams_cfg_list) {
556 s_info = node_to_item(node_i, struct streams_io_cfg, list);
557 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
558 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
559 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
560 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700561 sf_info = node_to_item(node_j, struct stream_format, list);
562 ALOGV("format-%x", sf_info->format);
563 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530564 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700565 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
566 ALOGV("sample rate-%d", ss_info->sample_rate);
567 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700568 }
569}
570
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530571void audio_extn_utils_dump_streams_cfg_lists(
572 struct listnode *streams_output_cfg_list,
573 struct listnode *streams_input_cfg_list)
574{
575 ALOGV("%s", __func__);
576 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
577 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
578}
579
580static void audio_extn_utils_release_streams_cfg_list(
581 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700582{
583 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530584 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700585
586 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530587
588 while (!list_empty(streams_cfg_list)) {
589 node_i = list_head(streams_cfg_list);
590 s_info = node_to_item(node_i, struct streams_io_cfg, list);
591 while (!list_empty(&s_info->format_list)) {
592 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700593 list_remove(node_j);
594 free(node_to_item(node_j, struct stream_format, list));
595 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530596 while (!list_empty(&s_info->sample_rate_list)) {
597 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700598 list_remove(node_j);
599 free(node_to_item(node_j, struct stream_sample_rate, list));
600 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700601 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530602 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700603 }
604}
605
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530606void audio_extn_utils_release_streams_cfg_lists(
607 struct listnode *streams_output_cfg_list,
608 struct listnode *streams_input_cfg_list)
609{
610 ALOGV("%s", __func__);
611 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
612 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
613}
614
615static bool set_app_type_cfg(struct streams_io_cfg *s_info,
616 struct stream_app_type_cfg *app_type_cfg,
617 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700618 {
619 struct listnode *node_i;
620 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530621 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700622 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
623 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530624 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700625
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530626 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700627 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530628 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700629 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
630 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
631 return true;
632 }
633 }
634 /*
635 * Reiterate through the list assuming dafault sample rate.
636 * Handles scenario where input sample rate is higher
637 * than all sample rates in list for the input bit width.
638 */
639 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800640
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530641 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700642 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
643 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530644 (bit_width == s_info->app_type_cfg.bit_width)) {
645 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700646 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530647 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800648 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 -0700649 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
650 return true;
651 }
652 }
653 return false;
654}
655
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530656void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
657 struct listnode *streams_input_cfg_list,
658 audio_devices_t devices __unused,
659 audio_input_flags_t flags,
660 audio_format_t format,
661 uint32_t sample_rate,
662 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530663 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530664 struct stream_app_type_cfg *app_type_cfg)
665{
666 struct listnode *node_i, *node_j;
667 struct streams_io_cfg *s_info;
668 struct stream_format *sf_info;
669
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530670 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
671 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530672
673 list_for_each(node_i, streams_input_cfg_list) {
674 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530675 /* Along with flags do profile matching if set at either end.*/
676 if (s_info->flags.in_flags == flags &&
677 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
678 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530679 list_for_each(node_j, &s_info->format_list) {
680 sf_info = node_to_item(node_j, struct stream_format, list);
681 if (sf_info->format == format) {
682 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
683 return;
684 }
685 }
686 }
687 }
688 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
689 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
690 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
691 app_type_cfg->bit_width = 16;
692}
693
694void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700695 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700696 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700697 audio_output_flags_t flags,
698 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700699 uint32_t sample_rate,
700 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530701 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530702 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700703 struct stream_app_type_cfg *app_type_cfg)
704{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530705 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530706 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700707 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530708 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700709
Ashish Jain058165c2016-09-28 23:18:48 +0530710 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700711 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700712 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
713 if (-ENOSYS != bw)
714 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700715 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
716 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
717 }
718
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700719 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530720 if (!strncmp("true", value, sizeof("true"))) {
721 if ((popcount(channel_mask) > 2) &&
722 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700723 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530724 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
725 ALOGD("%s: MCH session defaulting sample rate to %d",
726 __func__, sample_rate);
727 }
728 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530729
730 /* Set sampling rate to 176.4 for DSD64
731 * and 352.8Khz for DSD128.
732 * Set Bit Width to 16. output will be 16 bit
733 * post DoP in ASM.
734 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700735 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530736 (format == AUDIO_FORMAT_DSD)) {
737 bit_width = 16;
738 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
739 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
740 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
741 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
742 }
743
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530744
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800745 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
746 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700747 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530748 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530749 /* Along with flags do profile matching if set at either end.*/
750 if (s_info->flags.out_flags == flags &&
751 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
752 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530753 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700754 sf_info = node_to_item(node_j, struct stream_format, list);
755 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530756 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700757 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700758 }
759 }
760 }
761 }
762 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530763 s_info = node_to_item(node_i, struct streams_io_cfg, list);
764 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700765 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530766 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
767 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
768 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700769 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530770 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700771 return;
772 }
773 }
774 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700775 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700776 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700777 app_type_cfg->bit_width = 16;
778}
779
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530780static bool audio_is_this_native_usecase(struct audio_usecase *uc)
781{
782 bool native_usecase = false;
783 struct stream_out *out = (struct stream_out*) uc->stream.out;
784
785 if (PCM_PLAYBACK == uc->type && out != NULL &&
786 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
787 is_offload_usecase(uc->id) &&
788 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
789 native_usecase = true;
790
791 return native_usecase;
792}
793
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800794bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
795{
796 /* DSP bitwidth enforce mode for ADM and AFE:
797 * includes:
798 * deep buffer, low latency, direct pcm and offload.
799 * excludes:
800 * ull(raw+fast), VOIP.
801 */
802 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
803 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
804 (flags & AUDIO_OUTPUT_FLAG_FAST)))
805 return false;
806
807
808 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
809 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
810 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
811 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
812 return true;
813 else
814 return false;
815}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800816
817static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
818{
819 return adev->vr_audio_mode_enabled;
820}
821
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530822void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
823 struct audio_device *adev,
824 struct audio_usecase *usecase)
825{
826 ALOGV("%s", __func__);
827
828 switch(usecase->type) {
829 case PCM_PLAYBACK:
830 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
831 &adev->streams_output_cfg_list,
832 usecase->stream.out->devices,
833 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700834 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530835 usecase->stream.out->sample_rate,
836 usecase->stream.out->bit_width,
837 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530838 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530839 &usecase->stream.out->app_type_cfg);
840 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
841 break;
842 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700843 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
844 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
845 else
846 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530847 &adev->streams_input_cfg_list,
848 usecase->stream.in->device,
849 usecase->stream.in->flags,
850 usecase->stream.in->format,
851 usecase->stream.in->sample_rate,
852 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530853 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530854 &usecase->stream.in->app_type_cfg);
855 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
856 break;
Siddartha Shaik343abc62017-08-08 11:15:25 +0530857 case TRANSCODE_LOOPBACK :
858 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
859 &adev->streams_output_cfg_list,
860 usecase->stream.inout->out_config.devices,
861 0,
862 usecase->stream.inout->out_config.format,
863 usecase->stream.inout->out_config.sample_rate,
864 usecase->stream.inout->out_config.bit_width,
865 usecase->stream.inout->out_config.channel_mask,
866 usecase->stream.inout->profile,
867 &usecase->stream.inout->out_app_type_cfg);
868 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
869 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530870 default:
871 ALOGE("%s: app type cfg not supported for usecase type (%d)",
872 __func__, usecase->type);
873 }
874}
875
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530876void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
877{
878 switch (snd_device) {
879 case SND_DEVICE_OUT_BT_SCO:
880 case SND_DEVICE_IN_BT_SCO_MIC:
881 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
882 *sample_rate = 8000;
883 break;
884 case SND_DEVICE_OUT_BT_SCO_WB:
885 case SND_DEVICE_IN_BT_SCO_MIC_WB:
886 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
887 *sample_rate = 16000;
888 break;
889 default:
890 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
891 break;
892 }
893}
894
Siena Richard7c2db772016-12-21 11:32:34 -0800895static int send_app_type_cfg_for_device(struct audio_device *adev,
896 struct audio_usecase *usecase,
897 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700898{
899 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530900 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
901 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700902 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800903 int pcm_device_id = 0, acdb_dev_id, app_type;
904 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530905 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530906 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +0530907 struct streams_io_cfg *s_info = NULL;
908 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530909 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700910
Siena Richard7c2db772016-12-21 11:32:34 -0800911 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
912 __func__, platform_get_snd_device_name(usecase->out_snd_device),
913 platform_get_snd_device_name(usecase->in_snd_device),
914 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700915
Siddartha Shaik343abc62017-08-08 11:15:25 +0530916 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
917 usecase->type != TRANSCODE_LOOPBACK) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530918 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700919 rc = 0;
920 goto exit_send_app_type_cfg;
921 }
922 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
923 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
924 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800925 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700926 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Siddartha Shaik343abc62017-08-08 11:15:25 +0530927 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK) &&
Varun Balaraje49253e2017-07-06 19:48:56 +0530928 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530929 (!is_offload_usecase(usecase->id)) &&
930 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530931 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 -0700932 rc = 0;
933 goto exit_send_app_type_cfg;
934 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800935
936 //if VR is active then only send the mixer control
937 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
938 ALOGI("ULL doesnt need sending app type cfg, returning");
939 rc = 0;
940 goto exit_send_app_type_cfg;
941 }
942
Siddartha Shaik343abc62017-08-08 11:15:25 +0530943 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700944 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530945 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
946 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700947 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700948 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530949 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
950 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530951 }
Siena Richard7c2db772016-12-21 11:32:34 -0800952
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700953 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
954 if (!ctl) {
955 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
956 mixer_ctl_name);
957 rc = -EINVAL;
958 goto exit_send_app_type_cfg;
959 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530960 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530961 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
962 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
963 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
964 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
965 } else {
966 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
967 }
Rohit Kumar1181b292017-01-31 18:07:17 +0530968 if (acdb_dev_id <= 0) {
969 ALOGE("%s: Couldn't get the acdb dev id", __func__);
970 rc = -EINVAL;
971 goto exit_send_app_type_cfg;
972 }
973
Siena Richard7c2db772016-12-21 11:32:34 -0800974 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
975 if (snd_device_be_idx < 0) {
976 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
977 __func__, platform_get_snd_device_name(snd_device),
978 snd_device_be_idx);
979 }
980
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530981 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530982
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700983 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530984 if (!strncmp("true", value, sizeof("true"))) {
985 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
986 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700987 !(usecase->stream.out->flags &
988 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530989 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
990 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530991
kunleizcdf25942017-09-20 14:01:21 +0800992 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
993 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
994 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700995 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530996 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
997 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
998 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
999 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1000 /*
1001 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1002 * configured device sample rate, if not update the COPP rate to be equal to the
1003 * device sample rate, else open COPP at stream sample rate
1004 */
1005 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1006 usecase->stream.out->sample_rate,
1007 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05301008 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1009 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -07001010 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1011 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +05301012 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -07001013 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301014 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1015 /*
1016 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1017 * for bit width use the stream param only.
1018 */
1019 audio_extn_a2dp_get_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1020 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1021 __func__, usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001022 }
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301023 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001024 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1025
Varun Balaraje49253e2017-07-06 19:48:56 +05301026 /* Interactive streams are supported with only direct app type id.
1027 * Get Direct profile app type and use it for interactive streams
1028 */
1029 list_for_each(node, &adev->streams_output_cfg_list) {
1030 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001031 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301032 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1033 AUDIO_OUTPUT_FLAG_DIRECT))
1034 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301035 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001036 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301037 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301038 else
1039 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001040 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001041 app_type_cfg[len++] = acdb_dev_id;
1042 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001043 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1044 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301045 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1046 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001047
1048 sample_rate = sample_rate * 4;
1049 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1050 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001051 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001052 app_type_cfg[len++] = sample_rate;
1053
Siena Richard7c2db772016-12-21 11:32:34 -08001054 if (snd_device_be_idx > 0)
1055 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301056
Siena Richard7c2db772016-12-21 11:32:34 -08001057 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1058 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301059
1060 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001061 app_type = usecase->stream.in->app_type_cfg.app_type;
1062 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301063 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001064 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1065 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301066 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1067 audio_extn_btsco_get_sample_rate(usecase->in_snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1068 } else {
1069 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1070 }
Siena Richard7c2db772016-12-21 11:32:34 -08001071 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1072 app_type_cfg[len++] = sample_rate;
1073 if (snd_device_be_idx > 0)
1074 app_type_cfg[len++] = snd_device_be_idx;
1075 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1076 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301077 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001078 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301079 if(usecase->type == TRANSCODE_LOOPBACK) {
1080 sample_rate = usecase->stream.inout->out_config.sample_rate;
1081 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1082 }
Siena Richard7c2db772016-12-21 11:32:34 -08001083 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301084 app_type_cfg[len++] = acdb_dev_id;
1085 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001086 if (snd_device_be_idx > 0)
1087 app_type_cfg[len++] = snd_device_be_idx;
1088 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1089 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301090 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301091
Siddartha Shaik343abc62017-08-08 11:15:25 +05301092 if(ctl)
1093 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001094 rc = 0;
1095exit_send_app_type_cfg:
1096 return rc;
1097}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001098
Siena Richard7c2db772016-12-21 11:32:34 -08001099int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1100 struct audio_usecase *usecase)
1101{
1102 int i, num_devices = 0;
1103 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301104 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001105 int rc = 0;
1106
1107 switch (usecase->type) {
1108 case PCM_PLAYBACK:
Siddartha Shaik343abc62017-08-08 11:15:25 +05301109 case TRANSCODE_LOOPBACK:
Siena Richard7c2db772016-12-21 11:32:34 -08001110 ALOGD("%s: usecase->out_snd_device %s",
1111 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1112 /* check for out combo device */
1113 if (platform_split_snd_device(adev->platform,
1114 usecase->out_snd_device,
1115 &num_devices, new_snd_devices)) {
1116 new_snd_devices[0] = usecase->out_snd_device;
1117 num_devices = 1;
1118 }
1119 break;
1120 case PCM_CAPTURE:
1121 ALOGD("%s: usecase->in_snd_device %s",
1122 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301123 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1124 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1125 }
Siena Richard7c2db772016-12-21 11:32:34 -08001126 /* check for in combo device */
1127 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301128 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001129 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +05301130 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001131 num_devices = 1;
1132 }
1133 break;
1134 default:
1135 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1136 rc = 0;
1137 break;
1138 }
1139
1140 for (i = 0; i < num_devices; i++) {
1141 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1142 if (rc)
1143 break;
1144 }
1145
1146 return rc;
1147}
1148
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301149int read_line_from_file(const char *path, char *buf, size_t count)
1150{
1151 char * fgets_ret;
1152 FILE * fd;
1153 int rv;
1154
1155 fd = fopen(path, "r");
1156 if (fd == NULL)
1157 return -1;
1158
1159 fgets_ret = fgets(buf, (int)count, fd);
1160 if (NULL != fgets_ret) {
1161 rv = (int)strlen(buf);
1162 } else {
1163 rv = ferror(fd);
1164 }
1165 fclose(fd);
1166
1167 return rv;
1168}
1169
Ashish Jainf1eaa582016-05-23 20:54:24 +05301170/*Translates ALSA formats to AOSP PCM formats*/
1171audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1172{
1173 audio_format_t format;
1174
1175 switch(alsa_format) {
1176 case SNDRV_PCM_FORMAT_S16_LE:
1177 format = AUDIO_FORMAT_PCM_16_BIT;
1178 break;
1179 case SNDRV_PCM_FORMAT_S24_3LE:
1180 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1181 break;
1182 case SNDRV_PCM_FORMAT_S24_LE:
1183 format = AUDIO_FORMAT_PCM_8_24_BIT;
1184 break;
1185 case SNDRV_PCM_FORMAT_S32_LE:
1186 format = AUDIO_FORMAT_PCM_32_BIT;
1187 break;
1188 default:
1189 ALOGW("Incorrect ALSA format");
1190 format = AUDIO_FORMAT_INVALID;
1191 }
1192 return format;
1193}
1194
1195/*Translates hal format (AOSP) to alsa formats*/
1196uint32_t hal_format_to_alsa(audio_format_t hal_format)
1197{
1198 uint32_t alsa_format;
1199
1200 switch (hal_format) {
1201 case AUDIO_FORMAT_PCM_32_BIT: {
1202 if (platform_supports_true_32bit())
1203 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1204 else
1205 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1206 }
1207 break;
1208 case AUDIO_FORMAT_PCM_8_BIT:
1209 alsa_format = SNDRV_PCM_FORMAT_S8;
1210 break;
1211 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1212 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1213 break;
1214 case AUDIO_FORMAT_PCM_8_24_BIT: {
1215 if (platform_supports_true_32bit())
1216 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1217 else
1218 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1219 }
1220 break;
1221 case AUDIO_FORMAT_PCM_FLOAT:
1222 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1223 break;
1224 default:
1225 case AUDIO_FORMAT_PCM_16_BIT:
1226 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1227 break;
1228 }
1229 return alsa_format;
1230}
1231
Ashish Jain83a6cc22016-06-28 14:34:17 +05301232/*Translates PCM formats to AOSP formats*/
1233audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1234{
1235 audio_format_t format = AUDIO_FORMAT_INVALID;
1236
1237 switch(pcm_format) {
1238 case PCM_FORMAT_S16_LE:
1239 format = AUDIO_FORMAT_PCM_16_BIT;
1240 break;
1241 case PCM_FORMAT_S24_3LE:
1242 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1243 break;
1244 case PCM_FORMAT_S24_LE:
1245 format = AUDIO_FORMAT_PCM_8_24_BIT;
1246 break;
1247 case PCM_FORMAT_S32_LE:
1248 format = AUDIO_FORMAT_PCM_32_BIT;
1249 break;
1250 default:
1251 ALOGW("Incorrect PCM format");
1252 format = AUDIO_FORMAT_INVALID;
1253 }
1254 return format;
1255}
1256
1257/*Translates hal format (AOSP) to alsa formats*/
1258uint32_t hal_format_to_pcm(audio_format_t hal_format)
1259{
1260 uint32_t pcm_format;
1261
1262 switch (hal_format) {
1263 case AUDIO_FORMAT_PCM_32_BIT:
1264 case AUDIO_FORMAT_PCM_8_24_BIT:
1265 case AUDIO_FORMAT_PCM_FLOAT: {
1266 if (platform_supports_true_32bit())
1267 pcm_format = PCM_FORMAT_S32_LE;
1268 else
1269 pcm_format = PCM_FORMAT_S24_3LE;
1270 }
1271 break;
1272 case AUDIO_FORMAT_PCM_8_BIT:
1273 pcm_format = PCM_FORMAT_S8;
1274 break;
1275 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1276 pcm_format = PCM_FORMAT_S24_3LE;
1277 break;
1278 default:
1279 case AUDIO_FORMAT_PCM_16_BIT:
1280 pcm_format = PCM_FORMAT_S16_LE;
1281 break;
1282 }
1283 return pcm_format;
1284}
1285
Ashish Jainf1eaa582016-05-23 20:54:24 +05301286uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1287 uint32_t sample_rate,
1288 uint32_t noOfChannels)
1289{
1290 uint32_t fragment_size = 0;
1291 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1292
1293 fragment_size = (pcm_offload_time
1294 * sample_rate
1295 * bytes_per_sample
1296 * noOfChannels)/1000;
1297 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1298 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1299 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1300 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1301 /*To have same PCM samples for all channels, the buffer size requires to
1302 *be multiple of (number of channels * bytes per sample)
1303 *For writes to succeed, the buffer must be written at address which is multiple of 32
1304 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001305 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301306
1307 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1308 return fragment_size;
1309}
1310
1311/* Calculates the fragment size required to configure compress session.
1312 * Based on the alsa format selected, decide if conversion is needed in
1313
1314 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1315 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1316 */
1317void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1318{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301319 audio_format_t dst_format = out->hal_op_format;
1320 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301321 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1322 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1323
1324 out->compr_config.fragment_size =
1325 get_alsa_fragment_size(hal_op_bytes_per_sample,
1326 out->sample_rate,
1327 popcount(out->channel_mask));
1328
1329 if ((src_format != dst_format) &&
1330 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1331
Ashish Jain83a6cc22016-06-28 14:34:17 +05301332 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301333 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1334 hal_op_bytes_per_sample);
1335 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301336 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301337 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301338 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301339 }
1340}
1341
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301342/* converts pcm format 24_8 to 8_24 inplace */
1343size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1344{
1345 size_t i = 0;
1346 int *int_buf_stream = buf;
1347
1348 if ((bytes % 4) != 0) {
1349 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1350 return -EINVAL;
1351 }
1352
1353 for (; i < (bytes / 4); i++)
1354 int_buf_stream[i] >>= 8;
1355
1356 return bytes;
1357}
1358
1359int get_snd_codec_id(audio_format_t format)
1360{
1361 int id = 0;
1362
1363 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1364 case AUDIO_FORMAT_MP3:
1365 id = SND_AUDIOCODEC_MP3;
1366 break;
1367 case AUDIO_FORMAT_AAC:
1368 id = SND_AUDIOCODEC_AAC;
1369 break;
1370 case AUDIO_FORMAT_AAC_ADTS:
1371 id = SND_AUDIOCODEC_AAC;
1372 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301373 case AUDIO_FORMAT_AAC_LATM:
1374 id = SND_AUDIOCODEC_AAC;
1375 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301376 case AUDIO_FORMAT_PCM:
1377 id = SND_AUDIOCODEC_PCM;
1378 break;
1379 case AUDIO_FORMAT_FLAC:
1380 id = SND_AUDIOCODEC_FLAC;
1381 break;
1382 case AUDIO_FORMAT_ALAC:
1383 id = SND_AUDIOCODEC_ALAC;
1384 break;
1385 case AUDIO_FORMAT_APE:
1386 id = SND_AUDIOCODEC_APE;
1387 break;
1388 case AUDIO_FORMAT_VORBIS:
1389 id = SND_AUDIOCODEC_VORBIS;
1390 break;
1391 case AUDIO_FORMAT_WMA:
1392 id = SND_AUDIOCODEC_WMA;
1393 break;
1394 case AUDIO_FORMAT_WMA_PRO:
1395 id = SND_AUDIOCODEC_WMA_PRO;
1396 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301397 case AUDIO_FORMAT_MP2:
1398 id = SND_AUDIOCODEC_MP2;
1399 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301400 case AUDIO_FORMAT_AC3:
1401 id = SND_AUDIOCODEC_AC3;
1402 break;
1403 case AUDIO_FORMAT_E_AC3:
1404 case AUDIO_FORMAT_E_AC3_JOC:
1405 id = SND_AUDIOCODEC_EAC3;
1406 break;
1407 case AUDIO_FORMAT_DTS:
1408 case AUDIO_FORMAT_DTS_HD:
1409 id = SND_AUDIOCODEC_DTS;
1410 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001411 case AUDIO_FORMAT_DOLBY_TRUEHD:
1412 id = SND_AUDIOCODEC_TRUEHD;
1413 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001414 case AUDIO_FORMAT_IEC61937:
1415 id = SND_AUDIOCODEC_IEC61937;
1416 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301417 case AUDIO_FORMAT_DSD:
1418 id = SND_AUDIOCODEC_DSD;
1419 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301420 case AUDIO_FORMAT_APTX:
1421 id = SND_AUDIOCODEC_APTX;
1422 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301423 default:
1424 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1425 }
1426
1427 return id;
1428}
1429
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001430void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1431 struct audio_usecase *usecase)
1432{
1433 int type = usecase->type;
1434
Dhananjay Kumar14245982017-01-16 20:21:00 +05301435 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001436 struct stream_out *out = usecase->stream.out;
1437 int snd_device = usecase->out_snd_device;
1438 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001439 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301440 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001441 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301442 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301443 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301444 platform_send_audio_calibration(adev->platform, usecase,
1445 usecase->stream.in->app_type_cfg.app_type,
1446 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001447 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301448 /* when app type is default. the sample rate is not used to send cal */
1449 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301450 platform_get_default_app_type_v2(adev->platform, usecase->type),
1451 48000);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301452 } else if (type == TRANSCODE_LOOPBACK && usecase->stream.inout != NULL) {
1453 int snd_device = usecase->out_snd_device;
1454 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1455 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1456 platform_send_audio_calibration(adev->platform, usecase,
1457 platform_get_default_app_type_v2(adev->platform, usecase->type),
1458 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001459 } else {
1460 /* No need to send audio calibration for voice and voip call usecases */
1461 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1462 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001463 }
1464}
1465
Ben Rombergera04fabc2014-11-14 12:16:03 -08001466// Base64 Encode and Decode
1467// Not all features supported. This must be used only with following conditions.
1468// Decode Modes: Support with and without padding
1469// CRLF not handling. So no CRLF in string to decode.
1470// Encode Modes: Supports only padding
1471int b64decode(char *inp, int ilen, uint8_t* outp)
1472{
1473 int i, j, k, ii, num;
1474 int rem, pcnt;
1475 uint32_t res=0;
1476 uint8_t getIndex[MAX_BASEINDEX_LEN];
1477 uint8_t tmp, cflag;
1478
1479 if(inp == NULL || outp == NULL || ilen <= 0) {
1480 ALOGE("[%s] received NULL pointer or zero length",__func__);
1481 return -1;
1482 }
1483
1484 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1485 for(i=0;i<BASE_TABLE_SIZE;i++) {
1486 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1487 }
1488 getIndex[(uint8_t)'=']=0;
1489
1490 j=0;k=0;
1491 num = ilen/4;
1492 rem = ilen%4;
1493 if(rem==0)
1494 num = num-1;
1495 cflag=0;
1496 for(i=0; i<num; i++) {
1497 res=0;
1498 for(ii=0;ii<4;ii++) {
1499 res = res << 6;
1500 tmp = getIndex[(uint8_t)inp[j++]];
1501 res = res | tmp;
1502 cflag = cflag | tmp;
1503 }
1504 outp[k++] = (res >> 16)&0xFF;
1505 outp[k++] = (res >> 8)&0xFF;
1506 outp[k++] = res & 0xFF;
1507 }
1508
1509 // Handle last bytes special
1510 pcnt=0;
1511 if(rem == 0) {
1512 //With padding or full data
1513 res = 0;
1514 for(ii=0;ii<4;ii++) {
1515 if(inp[j] == '=')
1516 pcnt++;
1517 res = res << 6;
1518 tmp = getIndex[(uint8_t)inp[j++]];
1519 res = res | tmp;
1520 cflag = cflag | tmp;
1521 }
1522 outp[k++] = res >> 16;
1523 if(pcnt == 2)
1524 goto done;
1525 outp[k++] = (res>>8)&0xFF;
1526 if(pcnt == 1)
1527 goto done;
1528 outp[k++] = res&0xFF;
1529 } else {
1530 //without padding
1531 res = 0;
1532 for(i=0;i<rem;i++) {
1533 res = res << 6;
1534 tmp = getIndex[(uint8_t)inp[j++]];
1535 res = res | tmp;
1536 cflag = cflag | tmp;
1537 }
1538 for(i=rem;i<4;i++) {
1539 res = res << 6;
1540 pcnt++;
1541 }
1542 outp[k++] = res >> 16;
1543 if(pcnt == 2)
1544 goto done;
1545 outp[k++] = (res>>8)&0xFF;
1546 if(pcnt == 1)
1547 goto done;
1548 outp[k++] = res&0xFF;
1549 }
1550done:
1551 if(cflag == 0xFF) {
1552 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1553 __func__, inp);
1554 return 0;
1555 }
1556 return k;
1557}
1558
1559int b64encode(uint8_t *inp, int ilen, char* outp)
1560{
1561 int i,j,k, num;
1562 int rem=0;
1563 uint32_t res=0;
1564
1565 if(inp == NULL || outp == NULL || ilen<=0) {
1566 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1567 return -1;
1568 }
1569
1570 num = ilen/3;
1571 rem = ilen%3;
1572 j=0;k=0;
1573 for(i=0; i<num; i++) {
1574 //prepare index
1575 res = inp[j++]<<16;
1576 res = res | inp[j++]<<8;
1577 res = res | inp[j++];
1578 //get output map from index
1579 outp[k++] = (char) bTable[(res>>18)&0x3F];
1580 outp[k++] = (char) bTable[(res>>12)&0x3F];
1581 outp[k++] = (char) bTable[(res>>6)&0x3F];
1582 outp[k++] = (char) bTable[res&0x3F];
1583 }
1584
1585 switch(rem) {
1586 case 1:
1587 res = inp[j++]<<16;
1588 outp[k++] = (char) bTable[res>>18];
1589 outp[k++] = (char) bTable[(res>>12)&0x3F];
1590 //outp[k++] = '=';
1591 //outp[k++] = '=';
1592 break;
1593 case 2:
1594 res = inp[j++]<<16;
1595 res = res | inp[j++]<<8;
1596 outp[k++] = (char) bTable[res>>18];
1597 outp[k++] = (char) bTable[(res>>12)&0x3F];
1598 outp[k++] = (char) bTable[(res>>6)&0x3F];
1599 //outp[k++] = '=';
1600 break;
1601 default:
1602 break;
1603 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001604 outp[k] = '\0';
1605 return k;
1606}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301607
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301608
1609int audio_extn_utils_get_codec_version(const char *snd_card_name,
1610 int card_num,
1611 char *codec_version)
1612{
1613 char procfs_path[50];
1614 FILE *fp;
1615
1616 if (strstr(snd_card_name, "tasha")) {
1617 snprintf(procfs_path, sizeof(procfs_path),
1618 "/proc/asound/card%d/codecs/tasha/version", card_num);
1619 if ((fp = fopen(procfs_path, "r")) != NULL) {
1620 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1621 fclose(fp);
1622 } else {
1623 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1624 return -ENOENT;
1625 }
1626 ALOGD("%s: codec version %s", __func__, codec_version);
1627 }
1628
1629 return 0;
1630}
1631
1632
Ashish Jain81eb2a82015-05-13 10:52:34 +05301633#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1634
1635void get_default_compressed_channel_status(
1636 unsigned char *channel_status)
1637{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301638 memset(channel_status,0,24);
1639
1640 /* block start bit in preamble bit 3 */
1641 channel_status[0] |= PROFESSIONAL;
1642 //compre out
1643 channel_status[0] |= NON_LPCM;
1644 // sample rate; fixed 48K for default/transcode
1645 channel_status[3] |= SR_48000;
1646}
1647
1648#ifdef HDMI_PASSTHROUGH_ENABLED
1649int32_t get_compressed_channel_status(void *audio_stream_data,
1650 uint32_t audio_frame_size,
1651 unsigned char *channel_status,
1652 enum audio_parser_code_type codec_type)
1653 // codec_type - AUDIO_PARSER_CODEC_AC3
1654 // - AUDIO_PARSER_CODEC_DTS
1655{
1656 unsigned char *stream;
1657 int ret = 0;
1658 stream = (unsigned char *)audio_stream_data;
1659
1660 if (audio_stream_data == NULL || audio_frame_size == 0) {
1661 ALOGW("no buffer to get channel status, return default for compress");
1662 get_default_compressed_channel_status(channel_status);
1663 return ret;
1664 }
1665
1666 memset(channel_status,0,24);
1667 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1668 {
1669 ALOGE("init audio parser failed");
1670 return -1;
1671 }
1672 ret = get_channel_status(channel_status, codec_type);
1673 return ret;
1674
1675}
1676
1677#endif
1678
1679void get_lpcm_channel_status(uint32_t sampleRate,
1680 unsigned char *channel_status)
1681{
1682 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301683 memset(channel_status,0,24);
1684 /* block start bit in preamble bit 3 */
1685 channel_status[0] |= PROFESSIONAL;
1686 //LPCM OUT
1687 channel_status[0] &= ~NON_LPCM;
1688
1689 switch (sampleRate) {
1690 case 8000:
1691 case 11025:
1692 case 12000:
1693 case 16000:
1694 case 22050:
1695 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301696 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301697 case 24000:
1698 channel_status[3] |= SR_24000;
1699 break;
1700 case 32000:
1701 channel_status[3] |= SR_32000;
1702 break;
1703 case 44100:
1704 channel_status[3] |= SR_44100;
1705 break;
1706 case 48000:
1707 channel_status[3] |= SR_48000;
1708 break;
1709 case 88200:
1710 channel_status[3] |= SR_88200;
1711 break;
1712 case 96000:
1713 channel_status[3] |= SR_96000;
1714 break;
1715 case 176400:
1716 channel_status[3] |= SR_176400;
1717 break;
1718 case 192000:
1719 channel_status[3] |= SR_192000;
1720 break;
1721 default:
1722 ALOGV("Invalid sample_rate %u\n", sampleRate);
1723 status = -1;
1724 break;
1725 }
1726}
1727
1728void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1729{
1730 unsigned char channel_status[24]={0};
1731 struct snd_aes_iec958 iec958;
1732 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1733 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301734 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301735#ifdef HDMI_PASSTHROUGH_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301736 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301737 /*TODO:Extend code to support DTS passthrough*/
1738 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301739 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301740 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1741 } else
1742#endif
1743 {
1744 /*set channel status bit for LPCM*/
1745 get_lpcm_channel_status(out->sample_rate, channel_status);
1746 }
1747
1748 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1749 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1750 if (!ctl) {
1751 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1752 __func__, mixer_ctl_name);
1753 return;
1754 }
1755 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1756 ALOGE("%s: Could not set channel status for ext HDMI ",
1757 __func__);
1758 return;
1759 }
1760
1761}
1762#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301763
1764int audio_extn_utils_get_avt_device_drift(
1765 struct audio_usecase *usecase,
1766 struct audio_avt_device_drift_param *drift_param)
1767{
1768 int ret = 0, count = 0;
1769 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301770 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301771 struct mixer_ctl *ctl = NULL;
1772 struct audio_avt_device_drift_stats drift_stats;
1773 struct audio_device *adev = NULL;
1774
1775 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301776 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1777 if (!backend) {
1778 ALOGE("%s: Unsupported device %d", __func__,
1779 usecase->stream.out->devices);
1780 ret = -EINVAL;
1781 goto done;
1782 }
1783 strlcpy(avt_device_drift_mixer_ctl_name,
1784 backend,
1785 MIXER_PATH_MAX_LENGTH);
1786
1787 count = strlen(backend);
1788 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1789 strlcat(&avt_device_drift_mixer_ctl_name[count],
1790 " DRIFT",
1791 MIXER_PATH_MAX_LENGTH - count);
1792 } else {
1793 ret = -EINVAL;
1794 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301795 }
1796 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301797 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301798 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301799 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301800 }
1801
Naresh Tanniru6160c712017-04-17 15:43:48 +05301802 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301803 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1804 if (!ctl) {
1805 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1806 __func__, avt_device_drift_mixer_ctl_name);
1807
1808 ret = -EINVAL;
1809 goto done;
1810 }
1811
1812 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1813 avt_device_drift_mixer_ctl_name);
1814
1815 mixer_ctl_update(ctl);
1816 count = mixer_ctl_get_num_values(ctl);
1817 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1818 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1819 __func__);
1820
1821 ret = -EINVAL;
1822 goto done;
1823 }
1824
1825 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1826 if (ret != 0) {
1827 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1828 __func__);
1829
1830 ret = -EINVAL;
1831 goto done;
1832 }
1833 memcpy(drift_param, &drift_stats.drift_param,
1834 sizeof(struct audio_avt_device_drift_param));
1835done:
1836 return ret;
1837}
Manish Dewangan07de2142017-02-27 19:27:20 +05301838
1839#ifdef SNDRV_COMPRESS_PATH_DELAY
1840int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1841{
1842 int ret = -EINVAL;
1843 struct snd_compr_metadata metadata;
1844 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1845
Weiyin Jiangc49a3b52018-08-17 16:16:08 +08001846 /* override the latency for pcm offload use case */
1847 if ((out->flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
1848 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
1849 delay_ms = PCM_OFFLOAD_PLAYBACK_LATENCY;
1850 }
1851
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001852 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301853 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1854 if (!(is_offload_usecase(out->usecase))) {
1855 ALOGE("%s:: not supported for non offload session", __func__);
1856 goto exit;
1857 }
1858
1859 if (!out->compr) {
1860 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1861 __func__);
1862 goto exit;
1863 }
1864
1865 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1866 ret = compress_get_metadata(out->compr, &metadata);
1867 if(ret) {
1868 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1869 goto exit;
1870 }
1871 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1872 } else {
1873 ALOGD("%s:: Using Fix DSP delay",__func__);
1874 }
1875
1876exit:
1877 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1878 return delay_ms;
1879}
1880#else
1881int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1882{
1883 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1884}
1885#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301886
1887#ifdef SNDRV_COMPRESS_RENDER_MODE
1888int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1889{
1890 struct snd_compr_metadata metadata;
1891 int ret = -EINVAL;
1892
1893 if (!(is_offload_usecase(out->usecase))) {
1894 ALOGE("%s:: not supported for non offload session", __func__);
1895 goto exit;
1896 }
1897
1898 if (!out->compr) {
1899 ALOGD("%s:: Invalid compress handle",
1900 __func__);
1901 goto exit;
1902 }
1903
1904 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1905
1906 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1907 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1908 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1909 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1910 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1911 } else {
1912 ret = 0;
1913 goto exit;
1914 }
1915 ret = compress_set_metadata(out->compr, &metadata);
1916 if(ret) {
1917 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1918 }
1919exit:
1920 return ret;
1921}
1922#else
1923int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1924{
1925 ALOGD("%s:: configuring render mode not supported", __func__);
1926 return 0;
1927}
1928#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301929
1930#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1931int audio_extn_utils_compress_set_clk_rec_mode(
1932 struct audio_usecase *usecase)
1933{
1934 struct snd_compr_metadata metadata;
1935 struct stream_out *out = NULL;
1936 int ret = -EINVAL;
1937
Naresh Tanniru7586e292017-04-13 10:38:13 +05301938 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301939 ALOGE("%s:: Invalid use case", __func__);
1940 goto exit;
1941 }
1942
1943 out = usecase->stream.out;
1944 if (!out) {
1945 ALOGE("%s:: invalid stream", __func__);
1946 goto exit;
1947 }
1948
1949 if (!is_offload_usecase(out->usecase)) {
1950 ALOGE("%s:: not supported for non offload session", __func__);
1951 goto exit;
1952 }
1953
1954 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1955 ALOGD("%s:: clk recovery is only supported in STC render mode",
1956 __func__);
1957 ret = 0;
1958 goto exit;
1959 }
1960
1961 if (!out->compr) {
1962 ALOGD("%s:: Invalid compress handle",
1963 __func__);
1964 goto exit;
1965 }
1966 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1967 switch(usecase->out_snd_device) {
1968 case SND_DEVICE_OUT_HDMI:
1969 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1970 case SND_DEVICE_OUT_DISPLAY_PORT:
1971 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1972 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1973 break;
1974 default:
1975 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1976 break;
1977 }
1978
1979 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1980
1981 ret = compress_set_metadata(out->compr, &metadata);
1982 if(ret) {
1983 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1984 }
1985
1986exit:
1987 return ret;
1988}
1989#else
1990int audio_extn_utils_compress_set_clk_rec_mode(
1991 struct audio_usecase *usecase __unused)
1992{
1993 ALOGD("%s:: configuring render mode not supported", __func__);
1994 return 0;
1995}
1996#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301997
1998#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1999int audio_extn_utils_compress_set_render_window(
2000 struct stream_out *out,
2001 struct audio_out_render_window_param *render_window)
2002{
2003 struct snd_compr_metadata metadata;
2004 int ret = -EINVAL;
2005
Manish Dewangan27346042017-03-01 12:56:12 +05302006 if(render_window == NULL) {
2007 ALOGE("%s:: Invalid render_window", __func__);
2008 goto exit;
2009 }
2010
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002011 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2012 __func__,render_window->render_ws, render_window->render_we);
2013
Manish Dewangan27346042017-03-01 12:56:12 +05302014 if (!is_offload_usecase(out->usecase)) {
2015 ALOGE("%s:: not supported for non offload session", __func__);
2016 goto exit;
2017 }
2018
Naresh Tanniru6160c712017-04-17 15:43:48 +05302019 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2020 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302021 ALOGD("%s:: only supported in timestamp mode, current "
2022 "render mode mode %d", __func__, out->render_mode);
2023 goto exit;
2024 }
2025
2026 if (!out->compr) {
2027 ALOGW("%s:: offload session not yet opened,"
2028 "render window will be configure later", __func__);
2029 /* store render window to reconfigure in start_output_stream() */
2030 goto exit;
2031 }
2032
2033 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2034 /*render window start value */
2035 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2036 metadata.value[1] = \
2037 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2038 /*render window end value */
2039 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2040 metadata.value[3] = \
2041 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2042
2043 ret = compress_set_metadata(out->compr, &metadata);
2044 if(ret) {
2045 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2046 }
2047
2048exit:
2049 return ret;
2050}
2051#else
2052int audio_extn_utils_compress_set_render_window(
2053 struct stream_out *out __unused,
2054 struct audio_out_render_window_param *render_window __unused)
2055{
2056 ALOGD("%s:: configuring render window not supported", __func__);
2057 return 0;
2058}
2059#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302060
2061#ifdef SNDRV_COMPRESS_START_DELAY
2062int audio_extn_utils_compress_set_start_delay(
2063 struct stream_out *out,
2064 struct audio_out_start_delay_param *delay_param)
2065{
2066 struct snd_compr_metadata metadata;
2067 int ret = -EINVAL;
2068
2069 if(delay_param == NULL) {
2070 ALOGE("%s:: Invalid delay_param", __func__);
2071 goto exit;
2072 }
2073
2074 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2075 delay_param->start_delay);
2076
2077 if (!is_offload_usecase(out->usecase)) {
2078 ALOGE("%s:: not supported for non offload session", __func__);
2079 goto exit;
2080 }
2081
Naresh Tanniru6160c712017-04-17 15:43:48 +05302082 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2083 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302084 ALOGD("%s:: only supported in timestamp mode, current "
2085 "render mode mode %d", __func__, out->render_mode);
2086 goto exit;
2087 }
2088
2089 if (!out->compr) {
2090 ALOGW("%s:: offload session not yet opened,"
2091 "start delay will be configure later", __func__);
2092 goto exit;
2093 }
2094
2095 metadata.key = SNDRV_COMPRESS_START_DELAY;
2096 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2097 metadata.value[1] = \
2098 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2099
2100 ret = compress_set_metadata(out->compr, &metadata);
2101 if(ret) {
2102 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2103 }
2104
2105exit:
2106 return ret;
2107}
2108#else
2109int audio_extn_utils_compress_set_start_delay(
2110 struct stream_out *out __unused,
2111 struct audio_out_start_delay_param *delay_param __unused)
2112{
2113 ALOGD("%s:: configuring render window not supported", __func__);
2114 return 0;
2115}
2116#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002117
2118#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302119#define RETRY_US 1000000
2120#define RETRY_NUMBER 40
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002121
2122int audio_extn_utils_get_snd_card_num()
2123{
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302124 int snd_card_num = 0;
2125 struct mixer *mixer = NULL;
2126
2127 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2128 if (mixer)
2129 mixer_close(mixer);
2130 return snd_card_num;
2131}
2132
2133int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2134{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002135
2136 void *hw_info = NULL;
2137 struct mixer *mixer = NULL;
2138 int retry_num = 0;
Ashish Jain30ae8942017-11-05 17:21:23 +05302139 int snd_card_num = 0, min_snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002140 char* snd_card_name = NULL;
2141
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302142 if (!mixer_handle) {
2143 ALOGE("invalid mixer handle");
2144 return -1;
2145 }
2146 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302147 /*
2148 * Try with all the sound cards ( 0 to 8 ) and if none of them were detected
2149 * sleep for 1 sec and try detections with sound card 0 again.
2150 * If sound card gets detected, check if it is relevant, if not check with the
2151 * other sound cards. To ensure that the irrelevant sound card is not check again,
2152 * we maintain it in min_snd_card_num.
2153 */
2154 while (retry_num < RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002155
Ashish Jain30ae8942017-11-05 17:21:23 +05302156 while (snd_card_num <= MAX_SND_CARD) {
2157 mixer = mixer_open(snd_card_num);
2158 if (!mixer)
2159 snd_card_num++;
2160 else
2161 break;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002162 }
2163
2164 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302165 usleep(RETRY_US);
2166 snd_card_num = min_snd_card_num;
2167 retry_num++;
2168 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002169 }
2170
2171 snd_card_name = strdup(mixer_get_name(mixer));
2172 if (!snd_card_name) {
2173 ALOGE("failed to allocate memory for snd_card_name\n");
2174 mixer_close(mixer);
2175 return -1;
2176 }
2177 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2178
2179 hw_info = hw_info_init(snd_card_name);
2180 if (hw_info) {
2181 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2182 break;
2183 }
2184 ALOGE("%s: Failed to init hardware info", __func__);
Ashish Jain30ae8942017-11-05 17:21:23 +05302185 min_snd_card_num++;
2186 snd_card_num = min_snd_card_num;
2187
2188 if (snd_card_num >= MAX_SND_CARD)
2189 break;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002190
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302191 free(snd_card_name);
2192 snd_card_name = NULL;
2193
2194 mixer_close(mixer);
2195 mixer = NULL;
2196 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002197 if (snd_card_name)
2198 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302199
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302200 if (hw_info)
2201 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002202
Ashish Jain30ae8942017-11-05 17:21:23 +05302203 if ((snd_card_num >= MAX_SND_CARD) || (retry_num >= RETRY_NUMBER)) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002204 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2205 return -1;
2206 }
2207
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302208 if (mixer)
2209 *mixer_handle = mixer;
2210
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002211 return snd_card_num;
2212}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302213
Soumya Managoli9fee7c62018-04-06 16:21:50 +05302214void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2215{
2216 if (mixer)
2217 mixer_close(mixer);
2218}
2219
Naresh Tanniru6160c712017-04-17 15:43:48 +05302220#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2221int audio_extn_utils_compress_enable_drift_correction(
2222 struct stream_out *out,
2223 struct audio_out_enable_drift_correction *drift)
2224{
2225 struct snd_compr_metadata metadata;
2226 int ret = -EINVAL;
2227
2228 if(drift == NULL) {
2229 ALOGE("%s:: Invalid param", __func__);
2230 goto exit;
2231 }
2232
2233 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2234
2235 if (!is_offload_usecase(out->usecase)) {
2236 ALOGE("%s:: not supported for non offload session", __func__);
2237 goto exit;
2238 }
2239
2240 if (!out->compr) {
2241 ALOGW("%s:: offload session not yet opened,"
2242 "start delay will be configure later", __func__);
2243 goto exit;
2244 }
2245
2246 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2247 metadata.value[0] = drift->enable;
2248 out->drift_correction_enabled = drift->enable;
2249
2250 ret = compress_set_metadata(out->compr, &metadata);
2251 if(ret) {
2252 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2253 out->drift_correction_enabled = false;
2254 }
2255
2256exit:
2257 return ret;
2258}
2259#else
2260int audio_extn_utils_compress_enable_drift_correction(
2261 struct stream_out *out __unused,
2262 struct audio_out_enable_drift_correction *drift __unused)
2263{
2264 ALOGD("%s:: configuring drift enablement not supported", __func__);
2265 return 0;
2266}
2267#endif
2268
2269#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2270int audio_extn_utils_compress_correct_drift(
2271 struct stream_out *out,
2272 struct audio_out_correct_drift *drift_param)
2273{
2274 struct snd_compr_metadata metadata;
2275 int ret = -EINVAL;
2276
2277 if (drift_param == NULL) {
2278 ALOGE("%s:: Invalid drift_param", __func__);
2279 goto exit;
2280 }
2281
2282 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2283 drift_param->adjust_time);
2284
2285 if (!is_offload_usecase(out->usecase)) {
2286 ALOGE("%s:: not supported for non offload session", __func__);
2287 goto exit;
2288 }
2289
2290 if (!out->compr) {
2291 ALOGW("%s:: offload session not yet opened", __func__);
2292 goto exit;
2293 }
2294
2295 if (!out->drift_correction_enabled) {
2296 ALOGE("%s:: drift correction not enabled", __func__);
2297 goto exit;
2298 }
2299
2300 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2301 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2302 metadata.value[1] = \
2303 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2304
2305 ret = compress_set_metadata(out->compr, &metadata);
2306 if(ret)
2307 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2308exit:
2309 return ret;
2310}
2311#else
2312int audio_extn_utils_compress_correct_drift(
2313 struct stream_out *out __unused,
2314 struct audio_out_correct_drift *drift_param __unused)
2315{
2316 ALOGD("%s:: setting adjust clock not supported", __func__);
2317 return 0;
2318}
2319#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302320
2321int audio_extn_utils_set_channel_map(
2322 struct stream_out *out,
2323 struct audio_out_channel_map_param *channel_map_param)
2324{
2325 int ret = -EINVAL, i = 0;
2326 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2327
2328 if (channel_map_param == NULL) {
2329 ALOGE("%s:: Invalid channel_map", __func__);
2330 goto exit;
2331 }
2332
2333 if (channel_map_param->channels != channels) {
2334 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2335 __func__, channel_map_param->channels, channels);
2336 goto exit;
2337 }
2338
2339 for ( i = 0; i < channels; i++) {
2340 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2341 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2342 }
2343 ret = 0;
2344exit:
2345 return ret;
2346}
Varun Balaraje49253e2017-07-06 19:48:56 +05302347
2348int audio_extn_utils_set_pan_scale_params(
2349 struct stream_out *out,
2350 struct mix_matrix_params *mm_params)
2351{
2352 int ret = -EINVAL, i = 0, j = 0;
2353
Varun Balaraj003ee752017-08-07 17:54:55 +05302354 if (mm_params == NULL || out == NULL) {
2355 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302356 goto exit;
2357 }
2358
2359 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2360 mm_params->num_output_channels <= 0 ||
2361 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2362 mm_params->num_input_channels <= 0)
2363 goto exit;
2364
2365 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2366 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2367 out->pan_scale_params.has_output_channel_map =
2368 mm_params->has_output_channel_map;
2369 for (i = 0; i < mm_params->num_output_channels; i++)
2370 out->pan_scale_params.output_channel_map[i] =
2371 mm_params->output_channel_map[i];
2372
2373 out->pan_scale_params.has_input_channel_map =
2374 mm_params->has_input_channel_map;
2375 for (i = 0; i < mm_params->num_input_channels; i++)
2376 out->pan_scale_params.input_channel_map[i] =
2377 mm_params->input_channel_map[i];
2378
2379 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2380 for (i = 0; i < mm_params->num_output_channels; i++)
2381 for (j = 0; j < mm_params->num_input_channels; j++) {
2382 //Convert the channel coefficient gains in Q14 format
2383 out->pan_scale_params.mixer_coeffs[i][j] =
2384 mm_params->mixer_coeffs[i][j] * (2 << 13);
2385 }
2386
2387 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2388 out->pcm_device_id,
2389 out->pan_scale_params);
2390
2391exit:
2392 return ret;
2393}
2394
2395int audio_extn_utils_set_downmix_params(
2396 struct stream_out *out,
2397 struct mix_matrix_params *mm_params)
2398{
2399 int ret = -EINVAL, i = 0, j = 0;
2400 struct audio_usecase *usecase = NULL;
2401
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002402 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302403 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302404 goto exit;
2405 }
2406
2407 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2408 mm_params->num_output_channels <= 0 ||
2409 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2410 mm_params->num_input_channels <= 0)
2411 goto exit;
2412
2413 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302414 if (!usecase) {
2415 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002416 goto exit;
2417 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302418 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2419 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2420
2421 out->downmix_params.has_output_channel_map =
2422 mm_params->has_output_channel_map;
2423 for (i = 0; i < mm_params->num_output_channels; i++) {
2424 out->downmix_params.output_channel_map[i] =
2425 mm_params->output_channel_map[i];
2426 }
2427
2428 out->downmix_params.has_input_channel_map =
2429 mm_params->has_input_channel_map;
2430 for (i = 0; i < mm_params->num_input_channels; i++)
2431 out->downmix_params.input_channel_map[i] =
2432 mm_params->input_channel_map[i];
2433
2434 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2435 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302436 for (j = 0; j < mm_params->num_input_channels; j++) {
2437 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302438 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302439 mm_params->mixer_coeffs[i][j] * (2 << 13);
2440 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302441
2442 ret = platform_set_stream_downmix_params(out->dev->platform,
2443 out->pcm_device_id,
2444 usecase->out_snd_device,
2445 out->downmix_params);
2446
2447exit:
2448 return ret;
2449}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302450
2451bool audio_extn_utils_is_dolby_format(audio_format_t format)
2452{
2453 if (format == AUDIO_FORMAT_AC3 ||
2454 format == AUDIO_FORMAT_E_AC3 ||
2455 format == AUDIO_FORMAT_E_AC3_JOC)
2456 return true;
2457 else
2458 return false;
2459}
2460
`Deeraj Soman676c2702017-09-18 19:25:53 +05302461int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2462{
2463 int i;
2464 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2465 {"S32_LE", 32},
2466 {"S24_LE", 24},
2467 {"S16_LE", 16}};
2468 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302469
`Deeraj Soman676c2702017-09-18 19:25:53 +05302470 for (i = 0; i < num_configs; i++) {
2471 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2472 return mixer_bitwidth_config[i].value;
2473 }
2474
2475 return -EINVAL;
2476}
2477
2478int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2479{
2480 int i;
2481 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2482 {"KHZ_48", 48000},
2483 {"KHZ_96", 96000},
2484 {"KHZ_144", 144000},
2485 {"KHZ_192", 192000},
2486 {"KHZ_384", 384000},
2487 {"KHZ_44P1", 44100},
2488 {"KHZ_88P2", 88200},
2489 {"KHZ_176P4", 176400},
2490 {"KHZ_352P8", 352800}};
2491 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2492
2493 for (i = 0; i < num_configs; i++) {
2494 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2495 return mixer_samplerate_config[i].value;
2496 }
2497
2498 return -EINVAL;
2499}
2500
2501int audio_extn_utils_get_channels_from_string(const char *id_string)
2502{
2503 int i;
2504 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2505 {"Two", 2},
2506 {"Three",3},
2507 {"Four", 4},
2508 {"Five", 5},
2509 {"Six", 6},
2510 {"Seven", 7},
2511 {"Eight", 8}};
2512 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2513
2514 for (i = 0; i < num_configs; i++) {
2515 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2516 return mixer_channels_config[i].value;
2517 }
2518
2519 return -EINVAL;
2520}