blob: 48d20eeff0b63daf9cc987e4b7efb898baba5393 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Dhananjay Kumar14245982017-01-16 20:21:00 +05302 * Copyright (c) 2014-2017, 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>
32
Manish Dewangan07de2142017-02-27 19:27:20 +053033
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034#include "audio_hw.h"
35#include "platform.h"
36#include "platform_api.h"
37#include "audio_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080038#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053039#include <sound/compress_params.h>
40#include <sound/compress_offload.h>
41#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053042
43#ifdef DYNAMIC_LOG_ENABLED
44#include <log_xml_parser.h>
45#define LOG_MASK HAL_MOD_FILE_UTILS
46#include <log_utils.h>
47#endif
48
Ashish Jain81eb2a82015-05-13 10:52:34 +053049#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
50#ifdef HDMI_PASSTHROUGH_ENABLED
51#include "audio_parsers.h"
52#endif
53#endif
54
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053055#ifdef LINUX_ENABLED
56#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053058#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053060#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053061#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053064#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070065
66#define DYNAMIC_VALUE_TAG "dynamic"
67#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053068#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070069#define FORMATS_TAG "formats"
70#define SAMPLING_RATES_TAG "sampling_rates"
71#define BIT_WIDTH_TAG "bit_width"
72#define APP_TYPE_TAG "app_type"
73
74#define STRING_TO_ENUM(string) { #string, string }
75#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
76
Ben Rombergera04fabc2014-11-14 12:16:03 -080077#define BASE_TABLE_SIZE 64
78#define MAX_BASEINDEX_LEN 256
79
Ben Romberger1aaaf862017-04-06 17:49:46 -070080#ifndef SND_AUDIOCODEC_TRUEHD
81#define SND_AUDIOCODEC_TRUEHD 0x00000023
82#endif
83
Vikram Panduranga93f080e2017-06-07 18:16:14 -070084#define APP_TYPE_VOIP_AUDIO 0x1113A
85
Ashish Jain81eb2a82015-05-13 10:52:34 +053086#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
87#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
88#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
89#define SR_44100 (0<<0) /* 44.1kHz */
90#define SR_NOTID (1<<0) /* non indicated */
91#define SR_48000 (2<<0) /* 48kHz */
92#define SR_32000 (3<<0) /* 32kHz */
93#define SR_22050 (4<<0) /* 22.05kHz */
94#define SR_24000 (6<<0) /* 24kHz */
95#define SR_88200 (8<<0) /* 88.2kHz */
96#define SR_96000 (10<<0) /* 96kHz */
97#define SR_176400 (12<<0) /* 176.4kHz */
98#define SR_192000 (14<<0) /* 192kHz */
99
100#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530101
102/* ToDo: Check and update a proper value in msec */
103#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
104
Varun Balaraje49253e2017-07-06 19:48:56 +0530105#ifndef MAX_CHANNELS_SUPPORTED
106#define MAX_CHANNELS_SUPPORTED 8
107#endif
108
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700109struct string_to_enum {
110 const char *name;
111 uint32_t value;
112};
113
114const struct string_to_enum s_flag_name_to_enum_table[] = {
115 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
116 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
117 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800118 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700119 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
120 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
121 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700122 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700123#ifdef INCALL_MUSIC_ENABLED
124 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
125#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700126 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530127 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700128 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Varun Balaraje49253e2017-07-06 19:48:56 +0530129 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530130 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
131 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
132 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
133 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
134 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530135 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700136};
137
138const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530139 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700140 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530141 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
142 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530143 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700144 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
145 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
146 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700147 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
148 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700149 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700150 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700151 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530152 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700153 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700154 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530155#ifdef AUDIO_EXTN_FORMATS_ENABLED
156 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700157 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
158 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
159 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700160 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
161 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
162 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
163 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
164 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
165 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
166 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700167 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530168 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
169 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700170 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800171 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
172 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
173 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530174 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530175 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
176 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
177 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530178 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530179 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
180 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
181 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
182 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530183 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700184#endif
185};
186
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530187/* payload structure avt_device drift query */
188struct audio_avt_device_drift_stats {
189 uint32_t minor_version;
190 /* Indicates the device interface direction as either
191 * source (Tx) or sink (Rx).
192 */
193 uint16_t device_direction;
194 /*params exposed to client */
195 struct audio_avt_device_drift_param drift_param;
196};
197
Ben Rombergera04fabc2014-11-14 12:16:03 -0800198static char bTable[BASE_TABLE_SIZE] = {
199 'A','B','C','D','E','F','G','H','I','J','K','L',
200 'M','N','O','P','Q','R','S','T','U','V','W','X',
201 'Y','Z','a','b','c','d','e','f','g','h','i','j',
202 'k','l','m','n','o','p','q','r','s','t','u','v',
203 'w','x','y','z','0','1','2','3','4','5','6','7',
204 '8','9','+','/'
205};
206
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700207static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
208 const char *name)
209{
210 size_t i;
211 for (i = 0; i < size; i++) {
212 if (strcmp(table[i].name, name) == 0) {
213 ALOGV("%s found %s", __func__, table[i].name);
214 return table[i].value;
215 }
216 }
217 return 0;
218}
219
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530220static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700221{
222 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530223 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800224 char *last_r;
225 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700226 while (flag_name != NULL) {
227 if (strlen(flag_name) != 0) {
228 flag |= string_to_enum(s_flag_name_to_enum_table,
229 ARRAY_SIZE(s_flag_name_to_enum_table),
230 flag_name);
231 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800232 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700233 }
234
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800235 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530236 io_flags.in_flags = (audio_input_flags_t)flag;
237 io_flags.out_flags = (audio_output_flags_t)flag;
238 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700239}
240
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530241static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700242{
243 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800244 char *last_r;
245 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700246
247 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
248 return;
249
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530250 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251 while (str != NULL) {
252 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
253 ARRAY_SIZE(s_format_name_to_enum_table), str);
254 ALOGV("%s: format - %d", __func__, format);
255 if (format != 0) {
256 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700257 if (sf_info == NULL)
258 break; /* return whatever was parsed */
259
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700260 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530261 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700262 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800263 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700264 }
265}
266
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530267static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700269 struct stream_sample_rate *ss_info = NULL;
270 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800271 char *last_r;
272 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700273
Amit Shekhar6f461b12014-08-01 14:52:58 -0700274 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
275 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700276
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530277 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700278 while (str != NULL) {
279 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
280 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
281 if (0 != sample_rate) {
282 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800283 if (!ss_info) {
284 ALOGE("%s: memory allocation failure", __func__);
285 return;
286 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700287 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530288 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700289 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800290 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700291 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700292}
293
294static int parse_bit_width_names(char *name)
295{
296 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800297 char *last_r;
298 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299
300 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
301 bit_width = (int)strtol(str, (char **)NULL, 10);
302
303 ALOGV("%s: bit_width - %d", __func__, bit_width);
304 return bit_width;
305}
306
307static int parse_app_type_names(void *platform, char *name)
308{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700309 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800310 char *last_r;
311 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700312
313 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
314 app_type = (int)strtol(str, (char **)NULL, 10);
315
316 ALOGV("%s: app_type - %d", __func__, app_type);
317 return app_type;
318}
319
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530320static void update_streams_cfg_list(cnode *root, void *platform,
321 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700322{
323 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530324 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700325
326 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530327 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700328
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530329 if (!s_info) {
330 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700331 return;
332 }
333
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700334 while (node) {
335 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530336 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530337 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
338 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700339 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530340 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530342 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
343 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700344 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530345 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700346 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530347 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700348 }
349 node = node->next;
350 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530351 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700352}
353
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530354static void load_cfg_list(cnode *root, void *platform,
355 struct listnode *streams_output_cfg_list,
356 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700357{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 cnode *node = NULL;
359
360 node = config_find(root, OUTPUTS_TAG);
361 if (node != NULL) {
362 node = node->first_child;
363 while (node) {
364 ALOGV("%s: loading output %s", __func__, node->name);
365 update_streams_cfg_list(node, platform, streams_output_cfg_list);
366 node = node->next;
367 }
368 } else {
369 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370 }
371
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530372 node = config_find(root, INPUTS_TAG);
373 if (node != NULL) {
374 node = node->first_child;
375 while (node) {
376 ALOGV("%s: loading input %s", __func__, node->name);
377 update_streams_cfg_list(node, platform, streams_input_cfg_list);
378 node = node->next;
379 }
380 } else {
381 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 }
383}
384
385static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530386 struct listnode *streams_output_cfg_list,
387 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700388{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530389 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700390 int length = 0, i, num_app_types = 0;
391 struct listnode *node;
392 bool update;
393 struct mixer_ctl *ctl = NULL;
394 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530395 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800396 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700397
398 if (!mixer) {
399 ALOGE("%s: mixer is null",__func__);
400 return;
401 }
402 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
403 if (!ctl) {
404 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
405 return;
406 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530407 app_type_cfg[length++] = num_app_types;
408
409 if (list_empty(streams_output_cfg_list)) {
410 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700411 app_type_cfg[length++] = 48000;
412 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530413 num_app_types += 1;
414 }
415 if (list_empty(streams_input_cfg_list)) {
416 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
417 app_type_cfg[length++] = 48000;
418 app_type_cfg[length++] = 16;
419 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700420 }
421
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800422 /* get target bit width for ADM enforce mode */
423 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
424
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700425 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530426 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700427 update = true;
428 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530429 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700430 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530431 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530432 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530433 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530434 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530435 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800436 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
437 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
438 (target_bit_width > app_type_cfg[i+3]))
439 app_type_cfg[i+3] = target_bit_width;
440
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700441 update = false;
442 break;
443 }
444 }
445 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530446 num_app_types += 1;
447 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
448 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800449 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
450 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
451 (target_bit_width > app_type_cfg[length]))
452 app_type_cfg[length] = target_bit_width;
453
454 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530455 }
456 }
457 list_for_each(node, streams_input_cfg_list) {
458 s_info = node_to_item(node, struct streams_io_cfg, list);
459 update = true;
460 for (i=0; i<length; i=i+3) {
461 if (app_type_cfg[i+1] == 0)
462 break;
463 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530464 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530465 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530466 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530467 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
468 update = false;
469 break;
470 }
471 }
472 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
473 num_app_types += 1;
474 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
475 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
476 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700477 }
478 }
479 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
480 if (num_app_types) {
481 app_type_cfg[0] = num_app_types;
482 mixer_ctl_set_array(ctl, app_type_cfg, length);
483 }
484}
485
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530486void audio_extn_utils_update_streams_cfg_lists(void *platform,
487 struct mixer *mixer,
488 struct listnode *streams_output_cfg_list,
489 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700490{
491 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530492 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700493
494 ALOGV("%s", __func__);
495 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530496 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700497
498 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700499 if (root == NULL) {
500 ALOGE("cfg_list, NULL config root");
501 return;
502 }
503
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530504 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
505 if (data == NULL) {
506 ALOGD("%s: failed to open io config file(%s), trying older config file",
507 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
508 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
509 if (data == NULL) {
510 send_app_type_cfg(platform, mixer,
511 streams_output_cfg_list,
512 streams_input_cfg_list);
513 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800514 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530515 return;
516 }
517 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700518
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530519 config_load(root, data);
520 load_cfg_list(root, platform, streams_output_cfg_list,
521 streams_input_cfg_list);
522
523 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
524 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800525
526 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800527 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800528 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700529}
530
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530531static void audio_extn_utils_dump_streams_cfg_list(
532 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700533{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700534 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530535 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700536 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700537 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530538
539 list_for_each(node_i, streams_cfg_list) {
540 s_info = node_to_item(node_i, struct streams_io_cfg, list);
541 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
542 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
543 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
544 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700545 sf_info = node_to_item(node_j, struct stream_format, list);
546 ALOGV("format-%x", sf_info->format);
547 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530548 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700549 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
550 ALOGV("sample rate-%d", ss_info->sample_rate);
551 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700552 }
553}
554
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530555void audio_extn_utils_dump_streams_cfg_lists(
556 struct listnode *streams_output_cfg_list,
557 struct listnode *streams_input_cfg_list)
558{
559 ALOGV("%s", __func__);
560 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
561 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
562}
563
564static void audio_extn_utils_release_streams_cfg_list(
565 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700566{
567 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530568 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700569
570 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530571
572 while (!list_empty(streams_cfg_list)) {
573 node_i = list_head(streams_cfg_list);
574 s_info = node_to_item(node_i, struct streams_io_cfg, list);
575 while (!list_empty(&s_info->format_list)) {
576 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700577 list_remove(node_j);
578 free(node_to_item(node_j, struct stream_format, list));
579 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530580 while (!list_empty(&s_info->sample_rate_list)) {
581 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700582 list_remove(node_j);
583 free(node_to_item(node_j, struct stream_sample_rate, list));
584 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700585 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530586 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700587 }
588}
589
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530590void audio_extn_utils_release_streams_cfg_lists(
591 struct listnode *streams_output_cfg_list,
592 struct listnode *streams_input_cfg_list)
593{
594 ALOGV("%s", __func__);
595 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
596 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
597}
598
599static bool set_app_type_cfg(struct streams_io_cfg *s_info,
600 struct stream_app_type_cfg *app_type_cfg,
601 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700602 {
603 struct listnode *node_i;
604 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530605 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700606 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
607 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530608 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700609
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530610 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700611 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530612 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700613 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
614 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
615 return true;
616 }
617 }
618 /*
619 * Reiterate through the list assuming dafault sample rate.
620 * Handles scenario where input sample rate is higher
621 * than all sample rates in list for the input bit width.
622 */
623 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800624
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530625 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700626 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
627 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530628 (bit_width == s_info->app_type_cfg.bit_width)) {
629 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700630 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530631 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800632 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 -0700633 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
634 return true;
635 }
636 }
637 return false;
638}
639
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530640void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
641 struct listnode *streams_input_cfg_list,
642 audio_devices_t devices __unused,
643 audio_input_flags_t flags,
644 audio_format_t format,
645 uint32_t sample_rate,
646 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530647 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530648 struct stream_app_type_cfg *app_type_cfg)
649{
650 struct listnode *node_i, *node_j;
651 struct streams_io_cfg *s_info;
652 struct stream_format *sf_info;
653
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530654 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
655 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530656
657 list_for_each(node_i, streams_input_cfg_list) {
658 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530659 /* Along with flags do profile matching if set at either end.*/
660 if (s_info->flags.in_flags == flags &&
661 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
662 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530663 list_for_each(node_j, &s_info->format_list) {
664 sf_info = node_to_item(node_j, struct stream_format, list);
665 if (sf_info->format == format) {
666 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
667 return;
668 }
669 }
670 }
671 }
672 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
673 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
674 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
675 app_type_cfg->bit_width = 16;
676}
677
678void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700679 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700680 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700681 audio_output_flags_t flags,
682 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700683 uint32_t sample_rate,
684 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530685 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530686 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700687 struct stream_app_type_cfg *app_type_cfg)
688{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530689 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530690 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700691 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530692 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700693
Ashish Jain058165c2016-09-28 23:18:48 +0530694 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700695 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700696 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
697 if (-ENOSYS != bw)
698 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700699 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
700 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
701 }
702
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700703 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530704 if (!strncmp("true", value, sizeof("true"))) {
705 if ((popcount(channel_mask) > 2) &&
706 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
707 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
708 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
709 ALOGD("%s: MCH session defaulting sample rate to %d",
710 __func__, sample_rate);
711 }
712 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530713
714 /* Set sampling rate to 176.4 for DSD64
715 * and 352.8Khz for DSD128.
716 * Set Bit Width to 16. output will be 16 bit
717 * post DoP in ASM.
718 */
719 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
720 (format == AUDIO_FORMAT_DSD)) {
721 bit_width = 16;
722 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
723 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
724 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
725 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
726 }
727
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530728 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
729 //TODO: Handle fractional sampling rate configuration for LL
730 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
731 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
732 __func__, sample_rate, bit_width);
733 }
734
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800735 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
736 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700737 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530738 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530739 /* Along with flags do profile matching if set at either end.*/
740 if (s_info->flags.out_flags == flags &&
741 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
742 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530743 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700744 sf_info = node_to_item(node_j, struct stream_format, list);
745 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530746 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700747 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700748 }
749 }
750 }
751 }
752 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530753 s_info = node_to_item(node_i, struct streams_io_cfg, list);
754 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700755 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530756 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
757 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
758 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700759 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530760 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700761 return;
762 }
763 }
764 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700765 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700766 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767 app_type_cfg->bit_width = 16;
768}
769
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530770static bool audio_is_this_native_usecase(struct audio_usecase *uc)
771{
772 bool native_usecase = false;
773 struct stream_out *out = (struct stream_out*) uc->stream.out;
774
775 if (PCM_PLAYBACK == uc->type && out != NULL &&
776 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
777 is_offload_usecase(uc->id) &&
778 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
779 native_usecase = true;
780
781 return native_usecase;
782}
783
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800784bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
785{
786 /* DSP bitwidth enforce mode for ADM and AFE:
787 * includes:
788 * deep buffer, low latency, direct pcm and offload.
789 * excludes:
790 * ull(raw+fast), VOIP.
791 */
792 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
793 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
794 (flags & AUDIO_OUTPUT_FLAG_FAST)))
795 return false;
796
797
798 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
799 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
800 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
801 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
802 return true;
803 else
804 return false;
805}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800806
807static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
808{
809 return adev->vr_audio_mode_enabled;
810}
811
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530812void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
813 struct audio_device *adev,
814 struct audio_usecase *usecase)
815{
816 ALOGV("%s", __func__);
817
818 switch(usecase->type) {
819 case PCM_PLAYBACK:
820 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
821 &adev->streams_output_cfg_list,
822 usecase->stream.out->devices,
823 usecase->stream.out->flags,
824 usecase->stream.out->format,
825 usecase->stream.out->sample_rate,
826 usecase->stream.out->bit_width,
827 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530828 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530829 &usecase->stream.out->app_type_cfg);
830 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
831 break;
832 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700833 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
834 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
835 else
836 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530837 &adev->streams_input_cfg_list,
838 usecase->stream.in->device,
839 usecase->stream.in->flags,
840 usecase->stream.in->format,
841 usecase->stream.in->sample_rate,
842 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530843 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530844 &usecase->stream.in->app_type_cfg);
845 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
846 break;
847 default:
848 ALOGE("%s: app type cfg not supported for usecase type (%d)",
849 __func__, usecase->type);
850 }
851}
852
Siena Richard7c2db772016-12-21 11:32:34 -0800853static int send_app_type_cfg_for_device(struct audio_device *adev,
854 struct audio_usecase *usecase,
855 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700856{
857 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530858 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
859 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700860 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800861 int pcm_device_id = 0, acdb_dev_id, app_type;
862 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530863 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530864 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +0530865 struct streams_io_cfg *s_info = NULL;
866 struct listnode *node = NULL;
867 int direct_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700868
Siena Richard7c2db772016-12-21 11:32:34 -0800869 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
870 __func__, platform_get_snd_device_name(usecase->out_snd_device),
871 platform_get_snd_device_name(usecase->in_snd_device),
872 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700873
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530874 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530875 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700876 rc = 0;
877 goto exit_send_app_type_cfg;
878 }
879 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
880 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
881 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800882 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700883 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Varun Balaraje49253e2017-07-06 19:48:56 +0530884 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530885 (!is_offload_usecase(usecase->id)) &&
886 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530887 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 -0700888 rc = 0;
889 goto exit_send_app_type_cfg;
890 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800891
892 //if VR is active then only send the mixer control
893 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
894 ALOGI("ULL doesnt need sending app type cfg, returning");
895 rc = 0;
896 goto exit_send_app_type_cfg;
897 }
898
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530899 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700900 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530901 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
902 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700903 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700904 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530905 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
906 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530907 }
Siena Richard7c2db772016-12-21 11:32:34 -0800908
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700909 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
910 if (!ctl) {
911 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
912 mixer_ctl_name);
913 rc = -EINVAL;
914 goto exit_send_app_type_cfg;
915 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530916 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530917
Rohit Kumar1181b292017-01-31 18:07:17 +0530918 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
919 if (acdb_dev_id <= 0) {
920 ALOGE("%s: Couldn't get the acdb dev id", __func__);
921 rc = -EINVAL;
922 goto exit_send_app_type_cfg;
923 }
924
Siena Richard7c2db772016-12-21 11:32:34 -0800925 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
926 if (snd_device_be_idx < 0) {
927 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
928 __func__, platform_get_snd_device_name(snd_device),
929 snd_device_be_idx);
930 }
931
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530932 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530933
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700934 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530935 if (!strncmp("true", value, sizeof("true"))) {
936 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
937 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
938 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
939 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
940 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530941
Ashish Jain4826f6c2017-02-06 13:33:20 +0530942 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700943 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530944 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
945 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
946 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
947 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
948 /*
949 * To best utlize DSP, check if the stream sample rate is supported/multiple of
950 * configured device sample rate, if not update the COPP rate to be equal to the
951 * device sample rate, else open COPP at stream sample rate
952 */
953 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
954 usecase->stream.out->sample_rate,
955 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530956 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
957 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700958 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
959 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530960 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700961 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
962 }
963 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
964
Varun Balaraje49253e2017-07-06 19:48:56 +0530965 /* Interactive streams are supported with only direct app type id.
966 * Get Direct profile app type and use it for interactive streams
967 */
968 list_for_each(node, &adev->streams_output_cfg_list) {
969 s_info = node_to_item(node, struct streams_io_cfg, list);
970 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_DIRECT)
971 direct_app_type = s_info->app_type_cfg.app_type;
972 }
973 if (usecase->stream.out->flags == AUDIO_OUTPUT_FLAG_INTERACTIVE)
974 app_type = direct_app_type;
975 else
976 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -0800977 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700978 app_type_cfg[len++] = acdb_dev_id;
979 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700980 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
981 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530982 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -0700983
984 sample_rate = sample_rate * 4;
985 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
986 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -0700987 }
Naresh Tanniru3a406772017-05-10 13:09:05 -0700988 app_type_cfg[len++] = sample_rate;
989
Siena Richard7c2db772016-12-21 11:32:34 -0800990 if (snd_device_be_idx > 0)
991 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530992
Siena Richard7c2db772016-12-21 11:32:34 -0800993 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
994 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530995
996 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800997 app_type = usecase->stream.in->app_type_cfg.app_type;
998 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530999 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001000 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1001 app_type_cfg[len++] = sample_rate;
1002 if (snd_device_be_idx > 0)
1003 app_type_cfg[len++] = snd_device_be_idx;
1004 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1005 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301006 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001007 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
1008 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301009 app_type_cfg[len++] = acdb_dev_id;
1010 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001011 if (snd_device_be_idx > 0)
1012 app_type_cfg[len++] = snd_device_be_idx;
1013 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1014 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301015 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301016
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001017 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001018 rc = 0;
1019exit_send_app_type_cfg:
1020 return rc;
1021}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001022
Siena Richard7c2db772016-12-21 11:32:34 -08001023int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1024 struct audio_usecase *usecase)
1025{
1026 int i, num_devices = 0;
1027 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
1028 int rc = 0;
1029
1030 switch (usecase->type) {
1031 case PCM_PLAYBACK:
1032 ALOGD("%s: usecase->out_snd_device %s",
1033 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1034 /* check for out combo device */
1035 if (platform_split_snd_device(adev->platform,
1036 usecase->out_snd_device,
1037 &num_devices, new_snd_devices)) {
1038 new_snd_devices[0] = usecase->out_snd_device;
1039 num_devices = 1;
1040 }
1041 break;
1042 case PCM_CAPTURE:
1043 ALOGD("%s: usecase->in_snd_device %s",
1044 __func__, platform_get_snd_device_name(usecase->in_snd_device));
1045 /* check for in combo device */
1046 if (platform_split_snd_device(adev->platform,
1047 usecase->in_snd_device,
1048 &num_devices, new_snd_devices)) {
1049 new_snd_devices[0] = usecase->in_snd_device;
1050 num_devices = 1;
1051 }
1052 break;
1053 default:
1054 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1055 rc = 0;
1056 break;
1057 }
1058
1059 for (i = 0; i < num_devices; i++) {
1060 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1061 if (rc)
1062 break;
1063 }
1064
1065 return rc;
1066}
1067
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301068int read_line_from_file(const char *path, char *buf, size_t count)
1069{
1070 char * fgets_ret;
1071 FILE * fd;
1072 int rv;
1073
1074 fd = fopen(path, "r");
1075 if (fd == NULL)
1076 return -1;
1077
1078 fgets_ret = fgets(buf, (int)count, fd);
1079 if (NULL != fgets_ret) {
1080 rv = (int)strlen(buf);
1081 } else {
1082 rv = ferror(fd);
1083 }
1084 fclose(fd);
1085
1086 return rv;
1087}
1088
Ashish Jainf1eaa582016-05-23 20:54:24 +05301089/*Translates ALSA formats to AOSP PCM formats*/
1090audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1091{
1092 audio_format_t format;
1093
1094 switch(alsa_format) {
1095 case SNDRV_PCM_FORMAT_S16_LE:
1096 format = AUDIO_FORMAT_PCM_16_BIT;
1097 break;
1098 case SNDRV_PCM_FORMAT_S24_3LE:
1099 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1100 break;
1101 case SNDRV_PCM_FORMAT_S24_LE:
1102 format = AUDIO_FORMAT_PCM_8_24_BIT;
1103 break;
1104 case SNDRV_PCM_FORMAT_S32_LE:
1105 format = AUDIO_FORMAT_PCM_32_BIT;
1106 break;
1107 default:
1108 ALOGW("Incorrect ALSA format");
1109 format = AUDIO_FORMAT_INVALID;
1110 }
1111 return format;
1112}
1113
1114/*Translates hal format (AOSP) to alsa formats*/
1115uint32_t hal_format_to_alsa(audio_format_t hal_format)
1116{
1117 uint32_t alsa_format;
1118
1119 switch (hal_format) {
1120 case AUDIO_FORMAT_PCM_32_BIT: {
1121 if (platform_supports_true_32bit())
1122 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1123 else
1124 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1125 }
1126 break;
1127 case AUDIO_FORMAT_PCM_8_BIT:
1128 alsa_format = SNDRV_PCM_FORMAT_S8;
1129 break;
1130 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1131 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1132 break;
1133 case AUDIO_FORMAT_PCM_8_24_BIT: {
1134 if (platform_supports_true_32bit())
1135 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1136 else
1137 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1138 }
1139 break;
1140 case AUDIO_FORMAT_PCM_FLOAT:
1141 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1142 break;
1143 default:
1144 case AUDIO_FORMAT_PCM_16_BIT:
1145 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1146 break;
1147 }
1148 return alsa_format;
1149}
1150
Ashish Jain83a6cc22016-06-28 14:34:17 +05301151/*Translates PCM formats to AOSP formats*/
1152audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1153{
1154 audio_format_t format = AUDIO_FORMAT_INVALID;
1155
1156 switch(pcm_format) {
1157 case PCM_FORMAT_S16_LE:
1158 format = AUDIO_FORMAT_PCM_16_BIT;
1159 break;
1160 case PCM_FORMAT_S24_3LE:
1161 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1162 break;
1163 case PCM_FORMAT_S24_LE:
1164 format = AUDIO_FORMAT_PCM_8_24_BIT;
1165 break;
1166 case PCM_FORMAT_S32_LE:
1167 format = AUDIO_FORMAT_PCM_32_BIT;
1168 break;
1169 default:
1170 ALOGW("Incorrect PCM format");
1171 format = AUDIO_FORMAT_INVALID;
1172 }
1173 return format;
1174}
1175
1176/*Translates hal format (AOSP) to alsa formats*/
1177uint32_t hal_format_to_pcm(audio_format_t hal_format)
1178{
1179 uint32_t pcm_format;
1180
1181 switch (hal_format) {
1182 case AUDIO_FORMAT_PCM_32_BIT:
1183 case AUDIO_FORMAT_PCM_8_24_BIT:
1184 case AUDIO_FORMAT_PCM_FLOAT: {
1185 if (platform_supports_true_32bit())
1186 pcm_format = PCM_FORMAT_S32_LE;
1187 else
1188 pcm_format = PCM_FORMAT_S24_3LE;
1189 }
1190 break;
1191 case AUDIO_FORMAT_PCM_8_BIT:
1192 pcm_format = PCM_FORMAT_S8;
1193 break;
1194 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1195 pcm_format = PCM_FORMAT_S24_3LE;
1196 break;
1197 default:
1198 case AUDIO_FORMAT_PCM_16_BIT:
1199 pcm_format = PCM_FORMAT_S16_LE;
1200 break;
1201 }
1202 return pcm_format;
1203}
1204
Ashish Jainf1eaa582016-05-23 20:54:24 +05301205uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1206 uint32_t sample_rate,
1207 uint32_t noOfChannels)
1208{
1209 uint32_t fragment_size = 0;
1210 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1211
1212 fragment_size = (pcm_offload_time
1213 * sample_rate
1214 * bytes_per_sample
1215 * noOfChannels)/1000;
1216 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1217 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1218 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1219 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1220 /*To have same PCM samples for all channels, the buffer size requires to
1221 *be multiple of (number of channels * bytes per sample)
1222 *For writes to succeed, the buffer must be written at address which is multiple of 32
1223 */
1224 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1225
1226 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1227 return fragment_size;
1228}
1229
1230/* Calculates the fragment size required to configure compress session.
1231 * Based on the alsa format selected, decide if conversion is needed in
1232
1233 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1234 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1235 */
1236void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1237{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301238 audio_format_t dst_format = out->hal_op_format;
1239 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301240 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1241 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1242
1243 out->compr_config.fragment_size =
1244 get_alsa_fragment_size(hal_op_bytes_per_sample,
1245 out->sample_rate,
1246 popcount(out->channel_mask));
1247
1248 if ((src_format != dst_format) &&
1249 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1250
Ashish Jain83a6cc22016-06-28 14:34:17 +05301251 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301252 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1253 hal_op_bytes_per_sample);
1254 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301255 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301256 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301257 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301258 }
1259}
1260
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301261/* converts pcm format 24_8 to 8_24 inplace */
1262size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1263{
1264 size_t i = 0;
1265 int *int_buf_stream = buf;
1266
1267 if ((bytes % 4) != 0) {
1268 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1269 return -EINVAL;
1270 }
1271
1272 for (; i < (bytes / 4); i++)
1273 int_buf_stream[i] >>= 8;
1274
1275 return bytes;
1276}
1277
1278int get_snd_codec_id(audio_format_t format)
1279{
1280 int id = 0;
1281
1282 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1283 case AUDIO_FORMAT_MP3:
1284 id = SND_AUDIOCODEC_MP3;
1285 break;
1286 case AUDIO_FORMAT_AAC:
1287 id = SND_AUDIOCODEC_AAC;
1288 break;
1289 case AUDIO_FORMAT_AAC_ADTS:
1290 id = SND_AUDIOCODEC_AAC;
1291 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301292 case AUDIO_FORMAT_AAC_LATM:
1293 id = SND_AUDIOCODEC_AAC;
1294 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301295 case AUDIO_FORMAT_PCM:
1296 id = SND_AUDIOCODEC_PCM;
1297 break;
1298 case AUDIO_FORMAT_FLAC:
1299 id = SND_AUDIOCODEC_FLAC;
1300 break;
1301 case AUDIO_FORMAT_ALAC:
1302 id = SND_AUDIOCODEC_ALAC;
1303 break;
1304 case AUDIO_FORMAT_APE:
1305 id = SND_AUDIOCODEC_APE;
1306 break;
1307 case AUDIO_FORMAT_VORBIS:
1308 id = SND_AUDIOCODEC_VORBIS;
1309 break;
1310 case AUDIO_FORMAT_WMA:
1311 id = SND_AUDIOCODEC_WMA;
1312 break;
1313 case AUDIO_FORMAT_WMA_PRO:
1314 id = SND_AUDIOCODEC_WMA_PRO;
1315 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301316 case AUDIO_FORMAT_MP2:
1317 id = SND_AUDIOCODEC_MP2;
1318 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301319 case AUDIO_FORMAT_AC3:
1320 id = SND_AUDIOCODEC_AC3;
1321 break;
1322 case AUDIO_FORMAT_E_AC3:
1323 case AUDIO_FORMAT_E_AC3_JOC:
1324 id = SND_AUDIOCODEC_EAC3;
1325 break;
1326 case AUDIO_FORMAT_DTS:
1327 case AUDIO_FORMAT_DTS_HD:
1328 id = SND_AUDIOCODEC_DTS;
1329 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001330 case AUDIO_FORMAT_DOLBY_TRUEHD:
1331 id = SND_AUDIOCODEC_TRUEHD;
1332 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001333 case AUDIO_FORMAT_IEC61937:
1334 id = SND_AUDIOCODEC_IEC61937;
1335 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301336 case AUDIO_FORMAT_DSD:
1337 id = SND_AUDIOCODEC_DSD;
1338 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301339 case AUDIO_FORMAT_APTX:
1340 id = SND_AUDIOCODEC_APTX;
1341 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301342 default:
1343 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1344 }
1345
1346 return id;
1347}
1348
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001349void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1350 struct audio_usecase *usecase)
1351{
1352 int type = usecase->type;
1353
Dhananjay Kumar14245982017-01-16 20:21:00 +05301354 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001355 struct stream_out *out = usecase->stream.out;
1356 int snd_device = usecase->out_snd_device;
1357 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001358 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301359 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001360 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301361 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301362 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301363 platform_send_audio_calibration(adev->platform, usecase,
1364 usecase->stream.in->app_type_cfg.app_type,
1365 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001366 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301367 /* when app type is default. the sample rate is not used to send cal */
1368 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301369 platform_get_default_app_type_v2(adev->platform, usecase->type),
1370 48000);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001371 } else {
1372 /* No need to send audio calibration for voice and voip call usecases */
1373 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1374 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001375 }
1376}
1377
Ben Rombergera04fabc2014-11-14 12:16:03 -08001378// Base64 Encode and Decode
1379// Not all features supported. This must be used only with following conditions.
1380// Decode Modes: Support with and without padding
1381// CRLF not handling. So no CRLF in string to decode.
1382// Encode Modes: Supports only padding
1383int b64decode(char *inp, int ilen, uint8_t* outp)
1384{
1385 int i, j, k, ii, num;
1386 int rem, pcnt;
1387 uint32_t res=0;
1388 uint8_t getIndex[MAX_BASEINDEX_LEN];
1389 uint8_t tmp, cflag;
1390
1391 if(inp == NULL || outp == NULL || ilen <= 0) {
1392 ALOGE("[%s] received NULL pointer or zero length",__func__);
1393 return -1;
1394 }
1395
1396 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1397 for(i=0;i<BASE_TABLE_SIZE;i++) {
1398 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1399 }
1400 getIndex[(uint8_t)'=']=0;
1401
1402 j=0;k=0;
1403 num = ilen/4;
1404 rem = ilen%4;
1405 if(rem==0)
1406 num = num-1;
1407 cflag=0;
1408 for(i=0; i<num; i++) {
1409 res=0;
1410 for(ii=0;ii<4;ii++) {
1411 res = res << 6;
1412 tmp = getIndex[(uint8_t)inp[j++]];
1413 res = res | tmp;
1414 cflag = cflag | tmp;
1415 }
1416 outp[k++] = (res >> 16)&0xFF;
1417 outp[k++] = (res >> 8)&0xFF;
1418 outp[k++] = res & 0xFF;
1419 }
1420
1421 // Handle last bytes special
1422 pcnt=0;
1423 if(rem == 0) {
1424 //With padding or full data
1425 res = 0;
1426 for(ii=0;ii<4;ii++) {
1427 if(inp[j] == '=')
1428 pcnt++;
1429 res = res << 6;
1430 tmp = getIndex[(uint8_t)inp[j++]];
1431 res = res | tmp;
1432 cflag = cflag | tmp;
1433 }
1434 outp[k++] = res >> 16;
1435 if(pcnt == 2)
1436 goto done;
1437 outp[k++] = (res>>8)&0xFF;
1438 if(pcnt == 1)
1439 goto done;
1440 outp[k++] = res&0xFF;
1441 } else {
1442 //without padding
1443 res = 0;
1444 for(i=0;i<rem;i++) {
1445 res = res << 6;
1446 tmp = getIndex[(uint8_t)inp[j++]];
1447 res = res | tmp;
1448 cflag = cflag | tmp;
1449 }
1450 for(i=rem;i<4;i++) {
1451 res = res << 6;
1452 pcnt++;
1453 }
1454 outp[k++] = res >> 16;
1455 if(pcnt == 2)
1456 goto done;
1457 outp[k++] = (res>>8)&0xFF;
1458 if(pcnt == 1)
1459 goto done;
1460 outp[k++] = res&0xFF;
1461 }
1462done:
1463 if(cflag == 0xFF) {
1464 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1465 __func__, inp);
1466 return 0;
1467 }
1468 return k;
1469}
1470
1471int b64encode(uint8_t *inp, int ilen, char* outp)
1472{
1473 int i,j,k, num;
1474 int rem=0;
1475 uint32_t res=0;
1476
1477 if(inp == NULL || outp == NULL || ilen<=0) {
1478 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1479 return -1;
1480 }
1481
1482 num = ilen/3;
1483 rem = ilen%3;
1484 j=0;k=0;
1485 for(i=0; i<num; i++) {
1486 //prepare index
1487 res = inp[j++]<<16;
1488 res = res | inp[j++]<<8;
1489 res = res | inp[j++];
1490 //get output map from index
1491 outp[k++] = (char) bTable[(res>>18)&0x3F];
1492 outp[k++] = (char) bTable[(res>>12)&0x3F];
1493 outp[k++] = (char) bTable[(res>>6)&0x3F];
1494 outp[k++] = (char) bTable[res&0x3F];
1495 }
1496
1497 switch(rem) {
1498 case 1:
1499 res = inp[j++]<<16;
1500 outp[k++] = (char) bTable[res>>18];
1501 outp[k++] = (char) bTable[(res>>12)&0x3F];
1502 //outp[k++] = '=';
1503 //outp[k++] = '=';
1504 break;
1505 case 2:
1506 res = inp[j++]<<16;
1507 res = res | inp[j++]<<8;
1508 outp[k++] = (char) bTable[res>>18];
1509 outp[k++] = (char) bTable[(res>>12)&0x3F];
1510 outp[k++] = (char) bTable[(res>>6)&0x3F];
1511 //outp[k++] = '=';
1512 break;
1513 default:
1514 break;
1515 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001516 outp[k] = '\0';
1517 return k;
1518}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301519
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301520
1521int audio_extn_utils_get_codec_version(const char *snd_card_name,
1522 int card_num,
1523 char *codec_version)
1524{
1525 char procfs_path[50];
1526 FILE *fp;
1527
1528 if (strstr(snd_card_name, "tasha")) {
1529 snprintf(procfs_path, sizeof(procfs_path),
1530 "/proc/asound/card%d/codecs/tasha/version", card_num);
1531 if ((fp = fopen(procfs_path, "r")) != NULL) {
1532 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1533 fclose(fp);
1534 } else {
1535 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1536 return -ENOENT;
1537 }
1538 ALOGD("%s: codec version %s", __func__, codec_version);
1539 }
1540
1541 return 0;
1542}
1543
1544
Ashish Jain81eb2a82015-05-13 10:52:34 +05301545#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1546
1547void get_default_compressed_channel_status(
1548 unsigned char *channel_status)
1549{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301550 memset(channel_status,0,24);
1551
1552 /* block start bit in preamble bit 3 */
1553 channel_status[0] |= PROFESSIONAL;
1554 //compre out
1555 channel_status[0] |= NON_LPCM;
1556 // sample rate; fixed 48K for default/transcode
1557 channel_status[3] |= SR_48000;
1558}
1559
1560#ifdef HDMI_PASSTHROUGH_ENABLED
1561int32_t get_compressed_channel_status(void *audio_stream_data,
1562 uint32_t audio_frame_size,
1563 unsigned char *channel_status,
1564 enum audio_parser_code_type codec_type)
1565 // codec_type - AUDIO_PARSER_CODEC_AC3
1566 // - AUDIO_PARSER_CODEC_DTS
1567{
1568 unsigned char *stream;
1569 int ret = 0;
1570 stream = (unsigned char *)audio_stream_data;
1571
1572 if (audio_stream_data == NULL || audio_frame_size == 0) {
1573 ALOGW("no buffer to get channel status, return default for compress");
1574 get_default_compressed_channel_status(channel_status);
1575 return ret;
1576 }
1577
1578 memset(channel_status,0,24);
1579 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1580 {
1581 ALOGE("init audio parser failed");
1582 return -1;
1583 }
1584 ret = get_channel_status(channel_status, codec_type);
1585 return ret;
1586
1587}
1588
1589#endif
1590
1591void get_lpcm_channel_status(uint32_t sampleRate,
1592 unsigned char *channel_status)
1593{
1594 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301595 memset(channel_status,0,24);
1596 /* block start bit in preamble bit 3 */
1597 channel_status[0] |= PROFESSIONAL;
1598 //LPCM OUT
1599 channel_status[0] &= ~NON_LPCM;
1600
1601 switch (sampleRate) {
1602 case 8000:
1603 case 11025:
1604 case 12000:
1605 case 16000:
1606 case 22050:
1607 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301608 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301609 case 24000:
1610 channel_status[3] |= SR_24000;
1611 break;
1612 case 32000:
1613 channel_status[3] |= SR_32000;
1614 break;
1615 case 44100:
1616 channel_status[3] |= SR_44100;
1617 break;
1618 case 48000:
1619 channel_status[3] |= SR_48000;
1620 break;
1621 case 88200:
1622 channel_status[3] |= SR_88200;
1623 break;
1624 case 96000:
1625 channel_status[3] |= SR_96000;
1626 break;
1627 case 176400:
1628 channel_status[3] |= SR_176400;
1629 break;
1630 case 192000:
1631 channel_status[3] |= SR_192000;
1632 break;
1633 default:
1634 ALOGV("Invalid sample_rate %u\n", sampleRate);
1635 status = -1;
1636 break;
1637 }
1638}
1639
1640void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1641{
1642 unsigned char channel_status[24]={0};
1643 struct snd_aes_iec958 iec958;
1644 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1645 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301646 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301647#ifdef HDMI_PASSTHROUGH_ENABLED
1648 if (audio_extn_is_dolby_format(out->format) &&
1649 /*TODO:Extend code to support DTS passthrough*/
1650 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301651 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301652 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1653 } else
1654#endif
1655 {
1656 /*set channel status bit for LPCM*/
1657 get_lpcm_channel_status(out->sample_rate, channel_status);
1658 }
1659
1660 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1661 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1662 if (!ctl) {
1663 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1664 __func__, mixer_ctl_name);
1665 return;
1666 }
1667 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1668 ALOGE("%s: Could not set channel status for ext HDMI ",
1669 __func__);
1670 return;
1671 }
1672
1673}
1674#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301675
1676int audio_extn_utils_get_avt_device_drift(
1677 struct audio_usecase *usecase,
1678 struct audio_avt_device_drift_param *drift_param)
1679{
1680 int ret = 0, count = 0;
1681 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301682 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301683 struct mixer_ctl *ctl = NULL;
1684 struct audio_avt_device_drift_stats drift_stats;
1685 struct audio_device *adev = NULL;
1686
1687 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301688 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1689 if (!backend) {
1690 ALOGE("%s: Unsupported device %d", __func__,
1691 usecase->stream.out->devices);
1692 ret = -EINVAL;
1693 goto done;
1694 }
1695 strlcpy(avt_device_drift_mixer_ctl_name,
1696 backend,
1697 MIXER_PATH_MAX_LENGTH);
1698
1699 count = strlen(backend);
1700 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1701 strlcat(&avt_device_drift_mixer_ctl_name[count],
1702 " DRIFT",
1703 MIXER_PATH_MAX_LENGTH - count);
1704 } else {
1705 ret = -EINVAL;
1706 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301707 }
1708 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301709 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301710 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301711 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301712 }
1713
Naresh Tanniru6160c712017-04-17 15:43:48 +05301714 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301715 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1716 if (!ctl) {
1717 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1718 __func__, avt_device_drift_mixer_ctl_name);
1719
1720 ret = -EINVAL;
1721 goto done;
1722 }
1723
1724 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1725 avt_device_drift_mixer_ctl_name);
1726
1727 mixer_ctl_update(ctl);
1728 count = mixer_ctl_get_num_values(ctl);
1729 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1730 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1731 __func__);
1732
1733 ret = -EINVAL;
1734 goto done;
1735 }
1736
1737 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1738 if (ret != 0) {
1739 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1740 __func__);
1741
1742 ret = -EINVAL;
1743 goto done;
1744 }
1745 memcpy(drift_param, &drift_stats.drift_param,
1746 sizeof(struct audio_avt_device_drift_param));
1747done:
1748 return ret;
1749}
Manish Dewangan07de2142017-02-27 19:27:20 +05301750
1751#ifdef SNDRV_COMPRESS_PATH_DELAY
1752int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1753{
1754 int ret = -EINVAL;
1755 struct snd_compr_metadata metadata;
1756 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1757
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001758 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301759 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1760 if (!(is_offload_usecase(out->usecase))) {
1761 ALOGE("%s:: not supported for non offload session", __func__);
1762 goto exit;
1763 }
1764
1765 if (!out->compr) {
1766 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1767 __func__);
1768 goto exit;
1769 }
1770
1771 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1772 ret = compress_get_metadata(out->compr, &metadata);
1773 if(ret) {
1774 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1775 goto exit;
1776 }
1777 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1778 } else {
1779 ALOGD("%s:: Using Fix DSP delay",__func__);
1780 }
1781
1782exit:
1783 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1784 return delay_ms;
1785}
1786#else
1787int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1788{
1789 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1790}
1791#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301792
1793#ifdef SNDRV_COMPRESS_RENDER_MODE
1794int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1795{
1796 struct snd_compr_metadata metadata;
1797 int ret = -EINVAL;
1798
1799 if (!(is_offload_usecase(out->usecase))) {
1800 ALOGE("%s:: not supported for non offload session", __func__);
1801 goto exit;
1802 }
1803
1804 if (!out->compr) {
1805 ALOGD("%s:: Invalid compress handle",
1806 __func__);
1807 goto exit;
1808 }
1809
1810 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1811
1812 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1813 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1814 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1815 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1816 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1817 } else {
1818 ret = 0;
1819 goto exit;
1820 }
1821 ret = compress_set_metadata(out->compr, &metadata);
1822 if(ret) {
1823 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1824 }
1825exit:
1826 return ret;
1827}
1828#else
1829int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1830{
1831 ALOGD("%s:: configuring render mode not supported", __func__);
1832 return 0;
1833}
1834#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301835
1836#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1837int audio_extn_utils_compress_set_clk_rec_mode(
1838 struct audio_usecase *usecase)
1839{
1840 struct snd_compr_metadata metadata;
1841 struct stream_out *out = NULL;
1842 int ret = -EINVAL;
1843
Naresh Tanniru7586e292017-04-13 10:38:13 +05301844 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301845 ALOGE("%s:: Invalid use case", __func__);
1846 goto exit;
1847 }
1848
1849 out = usecase->stream.out;
1850 if (!out) {
1851 ALOGE("%s:: invalid stream", __func__);
1852 goto exit;
1853 }
1854
1855 if (!is_offload_usecase(out->usecase)) {
1856 ALOGE("%s:: not supported for non offload session", __func__);
1857 goto exit;
1858 }
1859
1860 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1861 ALOGD("%s:: clk recovery is only supported in STC render mode",
1862 __func__);
1863 ret = 0;
1864 goto exit;
1865 }
1866
1867 if (!out->compr) {
1868 ALOGD("%s:: Invalid compress handle",
1869 __func__);
1870 goto exit;
1871 }
1872 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1873 switch(usecase->out_snd_device) {
1874 case SND_DEVICE_OUT_HDMI:
1875 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1876 case SND_DEVICE_OUT_DISPLAY_PORT:
1877 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1878 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1879 break;
1880 default:
1881 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1882 break;
1883 }
1884
1885 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1886
1887 ret = compress_set_metadata(out->compr, &metadata);
1888 if(ret) {
1889 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1890 }
1891
1892exit:
1893 return ret;
1894}
1895#else
1896int audio_extn_utils_compress_set_clk_rec_mode(
1897 struct audio_usecase *usecase __unused)
1898{
1899 ALOGD("%s:: configuring render mode not supported", __func__);
1900 return 0;
1901}
1902#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301903
1904#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1905int audio_extn_utils_compress_set_render_window(
1906 struct stream_out *out,
1907 struct audio_out_render_window_param *render_window)
1908{
1909 struct snd_compr_metadata metadata;
1910 int ret = -EINVAL;
1911
Manish Dewangan27346042017-03-01 12:56:12 +05301912 if(render_window == NULL) {
1913 ALOGE("%s:: Invalid render_window", __func__);
1914 goto exit;
1915 }
1916
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001917 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1918 __func__,render_window->render_ws, render_window->render_we);
1919
Manish Dewangan27346042017-03-01 12:56:12 +05301920 if (!is_offload_usecase(out->usecase)) {
1921 ALOGE("%s:: not supported for non offload session", __func__);
1922 goto exit;
1923 }
1924
Naresh Tanniru6160c712017-04-17 15:43:48 +05301925 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1926 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301927 ALOGD("%s:: only supported in timestamp mode, current "
1928 "render mode mode %d", __func__, out->render_mode);
1929 goto exit;
1930 }
1931
1932 if (!out->compr) {
1933 ALOGW("%s:: offload session not yet opened,"
1934 "render window will be configure later", __func__);
1935 /* store render window to reconfigure in start_output_stream() */
1936 goto exit;
1937 }
1938
1939 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1940 /*render window start value */
1941 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1942 metadata.value[1] = \
1943 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1944 /*render window end value */
1945 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1946 metadata.value[3] = \
1947 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1948
1949 ret = compress_set_metadata(out->compr, &metadata);
1950 if(ret) {
1951 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1952 }
1953
1954exit:
1955 return ret;
1956}
1957#else
1958int audio_extn_utils_compress_set_render_window(
1959 struct stream_out *out __unused,
1960 struct audio_out_render_window_param *render_window __unused)
1961{
1962 ALOGD("%s:: configuring render window not supported", __func__);
1963 return 0;
1964}
1965#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05301966
1967#ifdef SNDRV_COMPRESS_START_DELAY
1968int audio_extn_utils_compress_set_start_delay(
1969 struct stream_out *out,
1970 struct audio_out_start_delay_param *delay_param)
1971{
1972 struct snd_compr_metadata metadata;
1973 int ret = -EINVAL;
1974
1975 if(delay_param == NULL) {
1976 ALOGE("%s:: Invalid delay_param", __func__);
1977 goto exit;
1978 }
1979
1980 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
1981 delay_param->start_delay);
1982
1983 if (!is_offload_usecase(out->usecase)) {
1984 ALOGE("%s:: not supported for non offload session", __func__);
1985 goto exit;
1986 }
1987
Naresh Tanniru6160c712017-04-17 15:43:48 +05301988 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1989 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05301990 ALOGD("%s:: only supported in timestamp mode, current "
1991 "render mode mode %d", __func__, out->render_mode);
1992 goto exit;
1993 }
1994
1995 if (!out->compr) {
1996 ALOGW("%s:: offload session not yet opened,"
1997 "start delay will be configure later", __func__);
1998 goto exit;
1999 }
2000
2001 metadata.key = SNDRV_COMPRESS_START_DELAY;
2002 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2003 metadata.value[1] = \
2004 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2005
2006 ret = compress_set_metadata(out->compr, &metadata);
2007 if(ret) {
2008 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2009 }
2010
2011exit:
2012 return ret;
2013}
2014#else
2015int audio_extn_utils_compress_set_start_delay(
2016 struct stream_out *out __unused,
2017 struct audio_out_start_delay_param *delay_param __unused)
2018{
2019 ALOGD("%s:: configuring render window not supported", __func__);
2020 return 0;
2021}
2022#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002023
2024#define MAX_SND_CARD 8
2025#define RETRY_US 500000
2026#define RETRY_NUMBER 10
2027
2028int audio_extn_utils_get_snd_card_num()
2029{
2030
2031 void *hw_info = NULL;
2032 struct mixer *mixer = NULL;
2033 int retry_num = 0;
2034 int snd_card_num = 0;
2035 char* snd_card_name = NULL;
2036
2037 while (snd_card_num < MAX_SND_CARD) {
2038 mixer = mixer_open(snd_card_num);
2039
2040 while (!mixer && retry_num < RETRY_NUMBER) {
2041 usleep(RETRY_US);
2042 mixer = mixer_open(snd_card_num);
2043 retry_num++;
2044 }
2045
2046 if (!mixer) {
2047 ALOGE("%s: Unable to open the mixer card: %d", __func__,
2048 snd_card_num);
2049 retry_num = 0;
2050 snd_card_num++;
2051 continue;
2052 }
2053
2054 snd_card_name = strdup(mixer_get_name(mixer));
2055 if (!snd_card_name) {
2056 ALOGE("failed to allocate memory for snd_card_name\n");
2057 mixer_close(mixer);
2058 return -1;
2059 }
2060 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2061
2062 hw_info = hw_info_init(snd_card_name);
2063 if (hw_info) {
2064 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2065 break;
2066 }
2067 ALOGE("%s: Failed to init hardware info", __func__);
2068 retry_num = 0;
2069 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002070
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302071 free(snd_card_name);
2072 snd_card_name = NULL;
2073
2074 mixer_close(mixer);
2075 mixer = NULL;
2076 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002077 if (snd_card_name)
2078 free(snd_card_name);
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302079 if (mixer)
2080 mixer_close(mixer);
2081 if (hw_info)
2082 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002083
2084 if (snd_card_num >= MAX_SND_CARD) {
2085 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2086 return -1;
2087 }
2088
2089 return snd_card_num;
2090}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302091
2092#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2093int audio_extn_utils_compress_enable_drift_correction(
2094 struct stream_out *out,
2095 struct audio_out_enable_drift_correction *drift)
2096{
2097 struct snd_compr_metadata metadata;
2098 int ret = -EINVAL;
2099
2100 if(drift == NULL) {
2101 ALOGE("%s:: Invalid param", __func__);
2102 goto exit;
2103 }
2104
2105 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2106
2107 if (!is_offload_usecase(out->usecase)) {
2108 ALOGE("%s:: not supported for non offload session", __func__);
2109 goto exit;
2110 }
2111
2112 if (!out->compr) {
2113 ALOGW("%s:: offload session not yet opened,"
2114 "start delay will be configure later", __func__);
2115 goto exit;
2116 }
2117
2118 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2119 metadata.value[0] = drift->enable;
2120 out->drift_correction_enabled = drift->enable;
2121
2122 ret = compress_set_metadata(out->compr, &metadata);
2123 if(ret) {
2124 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2125 out->drift_correction_enabled = false;
2126 }
2127
2128exit:
2129 return ret;
2130}
2131#else
2132int audio_extn_utils_compress_enable_drift_correction(
2133 struct stream_out *out __unused,
2134 struct audio_out_enable_drift_correction *drift __unused)
2135{
2136 ALOGD("%s:: configuring drift enablement not supported", __func__);
2137 return 0;
2138}
2139#endif
2140
2141#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2142int audio_extn_utils_compress_correct_drift(
2143 struct stream_out *out,
2144 struct audio_out_correct_drift *drift_param)
2145{
2146 struct snd_compr_metadata metadata;
2147 int ret = -EINVAL;
2148
2149 if (drift_param == NULL) {
2150 ALOGE("%s:: Invalid drift_param", __func__);
2151 goto exit;
2152 }
2153
2154 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2155 drift_param->adjust_time);
2156
2157 if (!is_offload_usecase(out->usecase)) {
2158 ALOGE("%s:: not supported for non offload session", __func__);
2159 goto exit;
2160 }
2161
2162 if (!out->compr) {
2163 ALOGW("%s:: offload session not yet opened", __func__);
2164 goto exit;
2165 }
2166
2167 if (!out->drift_correction_enabled) {
2168 ALOGE("%s:: drift correction not enabled", __func__);
2169 goto exit;
2170 }
2171
2172 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2173 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2174 metadata.value[1] = \
2175 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2176
2177 ret = compress_set_metadata(out->compr, &metadata);
2178 if(ret)
2179 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2180exit:
2181 return ret;
2182}
2183#else
2184int audio_extn_utils_compress_correct_drift(
2185 struct stream_out *out __unused,
2186 struct audio_out_correct_drift *drift_param __unused)
2187{
2188 ALOGD("%s:: setting adjust clock not supported", __func__);
2189 return 0;
2190}
2191#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302192
2193int audio_extn_utils_set_channel_map(
2194 struct stream_out *out,
2195 struct audio_out_channel_map_param *channel_map_param)
2196{
2197 int ret = -EINVAL, i = 0;
2198 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2199
2200 if (channel_map_param == NULL) {
2201 ALOGE("%s:: Invalid channel_map", __func__);
2202 goto exit;
2203 }
2204
2205 if (channel_map_param->channels != channels) {
2206 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2207 __func__, channel_map_param->channels, channels);
2208 goto exit;
2209 }
2210
2211 for ( i = 0; i < channels; i++) {
2212 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2213 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2214 }
2215 ret = 0;
2216exit:
2217 return ret;
2218}
Varun Balaraje49253e2017-07-06 19:48:56 +05302219
2220int audio_extn_utils_set_pan_scale_params(
2221 struct stream_out *out,
2222 struct mix_matrix_params *mm_params)
2223{
2224 int ret = -EINVAL, i = 0, j = 0;
2225
2226 if (mm_params == NULL && out != NULL) {
2227 ALOGE("%s:: Invalid mix matrix params", __func__);
2228 goto exit;
2229 }
2230
2231 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2232 mm_params->num_output_channels <= 0 ||
2233 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2234 mm_params->num_input_channels <= 0)
2235 goto exit;
2236
2237 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2238 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2239 out->pan_scale_params.has_output_channel_map =
2240 mm_params->has_output_channel_map;
2241 for (i = 0; i < mm_params->num_output_channels; i++)
2242 out->pan_scale_params.output_channel_map[i] =
2243 mm_params->output_channel_map[i];
2244
2245 out->pan_scale_params.has_input_channel_map =
2246 mm_params->has_input_channel_map;
2247 for (i = 0; i < mm_params->num_input_channels; i++)
2248 out->pan_scale_params.input_channel_map[i] =
2249 mm_params->input_channel_map[i];
2250
2251 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2252 for (i = 0; i < mm_params->num_output_channels; i++)
2253 for (j = 0; j < mm_params->num_input_channels; j++) {
2254 //Convert the channel coefficient gains in Q14 format
2255 out->pan_scale_params.mixer_coeffs[i][j] =
2256 mm_params->mixer_coeffs[i][j] * (2 << 13);
2257 }
2258
2259 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2260 out->pcm_device_id,
2261 out->pan_scale_params);
2262
2263exit:
2264 return ret;
2265}
2266
2267int audio_extn_utils_set_downmix_params(
2268 struct stream_out *out,
2269 struct mix_matrix_params *mm_params)
2270{
2271 int ret = -EINVAL, i = 0, j = 0;
2272 struct audio_usecase *usecase = NULL;
2273
2274 if (mm_params == NULL && out != NULL) {
2275 ALOGE("%s:: Invalid mix matrix params", __func__);
2276 goto exit;
2277 }
2278
2279 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2280 mm_params->num_output_channels <= 0 ||
2281 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2282 mm_params->num_input_channels <= 0)
2283 goto exit;
2284
2285 usecase = get_usecase_from_list(out->dev, out->usecase);
2286 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2287 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2288
2289 out->downmix_params.has_output_channel_map =
2290 mm_params->has_output_channel_map;
2291 for (i = 0; i < mm_params->num_output_channels; i++) {
2292 out->downmix_params.output_channel_map[i] =
2293 mm_params->output_channel_map[i];
2294 }
2295
2296 out->downmix_params.has_input_channel_map =
2297 mm_params->has_input_channel_map;
2298 for (i = 0; i < mm_params->num_input_channels; i++)
2299 out->downmix_params.input_channel_map[i] =
2300 mm_params->input_channel_map[i];
2301
2302 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2303 for (i = 0; i < mm_params->num_output_channels; i++)
2304 for (j = 0; j < mm_params->num_input_channels; j++)
2305 out->downmix_params.mixer_coeffs[i][j] =
2306 mm_params->mixer_coeffs[i][j];
2307
2308 ret = platform_set_stream_downmix_params(out->dev->platform,
2309 out->pcm_device_id,
2310 usecase->out_snd_device,
2311 out->downmix_params);
2312
2313exit:
2314 return ret;
2315}