blob: fb1362cc8dbc1fe8fb08c71cd71e87e717635170 [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;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700396
397 if (!mixer) {
398 ALOGE("%s: mixer is null",__func__);
399 return;
400 }
401 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
402 if (!ctl) {
403 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
404 return;
405 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530406 app_type_cfg[length++] = num_app_types;
407
408 if (list_empty(streams_output_cfg_list)) {
409 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700410 app_type_cfg[length++] = 48000;
411 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530412 num_app_types += 1;
413 }
414 if (list_empty(streams_input_cfg_list)) {
415 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
416 app_type_cfg[length++] = 48000;
417 app_type_cfg[length++] = 16;
418 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700419 }
420
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700421 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530422 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700423 update = true;
424 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530425 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700426 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530427 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530428 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530429 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530430 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530431 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700432 update = false;
433 break;
434 }
435 }
436 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530437 num_app_types += 1;
438 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
439 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
440 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
441 }
442 }
443 list_for_each(node, streams_input_cfg_list) {
444 s_info = node_to_item(node, struct streams_io_cfg, list);
445 update = true;
446 for (i=0; i<length; i=i+3) {
447 if (app_type_cfg[i+1] == 0)
448 break;
449 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530450 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530451 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530452 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530453 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
454 update = false;
455 break;
456 }
457 }
458 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
459 num_app_types += 1;
460 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
461 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
462 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700463 }
464 }
465 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
466 if (num_app_types) {
467 app_type_cfg[0] = num_app_types;
468 mixer_ctl_set_array(ctl, app_type_cfg, length);
469 }
470}
471
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530472void audio_extn_utils_update_streams_cfg_lists(void *platform,
473 struct mixer *mixer,
474 struct listnode *streams_output_cfg_list,
475 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700476{
477 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530478 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700479
480 ALOGV("%s", __func__);
481 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530482 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700483
484 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700485 if (root == NULL) {
486 ALOGE("cfg_list, NULL config root");
487 return;
488 }
489
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530490 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
491 if (data == NULL) {
492 ALOGD("%s: failed to open io config file(%s), trying older config file",
493 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
494 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
495 if (data == NULL) {
496 send_app_type_cfg(platform, mixer,
497 streams_output_cfg_list,
498 streams_input_cfg_list);
499 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800500 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530501 return;
502 }
503 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700504
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530505 config_load(root, data);
506 load_cfg_list(root, platform, streams_output_cfg_list,
507 streams_input_cfg_list);
508
509 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
510 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800511
512 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800513 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800514 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700515}
516
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530517static void audio_extn_utils_dump_streams_cfg_list(
518 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700519{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700520 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530521 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700522 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700523 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530524
525 list_for_each(node_i, streams_cfg_list) {
526 s_info = node_to_item(node_i, struct streams_io_cfg, list);
527 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
528 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
529 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
530 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700531 sf_info = node_to_item(node_j, struct stream_format, list);
532 ALOGV("format-%x", sf_info->format);
533 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530534 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700535 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
536 ALOGV("sample rate-%d", ss_info->sample_rate);
537 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700538 }
539}
540
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530541void audio_extn_utils_dump_streams_cfg_lists(
542 struct listnode *streams_output_cfg_list,
543 struct listnode *streams_input_cfg_list)
544{
545 ALOGV("%s", __func__);
546 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
547 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
548}
549
550static void audio_extn_utils_release_streams_cfg_list(
551 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700552{
553 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530554 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700555
556 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530557
558 while (!list_empty(streams_cfg_list)) {
559 node_i = list_head(streams_cfg_list);
560 s_info = node_to_item(node_i, struct streams_io_cfg, list);
561 while (!list_empty(&s_info->format_list)) {
562 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700563 list_remove(node_j);
564 free(node_to_item(node_j, struct stream_format, list));
565 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530566 while (!list_empty(&s_info->sample_rate_list)) {
567 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700568 list_remove(node_j);
569 free(node_to_item(node_j, struct stream_sample_rate, list));
570 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700571 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530572 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700573 }
574}
575
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530576void audio_extn_utils_release_streams_cfg_lists(
577 struct listnode *streams_output_cfg_list,
578 struct listnode *streams_input_cfg_list)
579{
580 ALOGV("%s", __func__);
581 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
582 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
583}
584
585static bool set_app_type_cfg(struct streams_io_cfg *s_info,
586 struct stream_app_type_cfg *app_type_cfg,
587 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700588 {
589 struct listnode *node_i;
590 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530591 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700592 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
593 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530594 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700595
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530596 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700597 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530598 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700599 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
600 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
601 return true;
602 }
603 }
604 /*
605 * Reiterate through the list assuming dafault sample rate.
606 * Handles scenario where input sample rate is higher
607 * than all sample rates in list for the input bit width.
608 */
609 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800610
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530611 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700612 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
613 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530614 (bit_width == s_info->app_type_cfg.bit_width)) {
615 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700616 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530617 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800618 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 -0700619 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
620 return true;
621 }
622 }
623 return false;
624}
625
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530626void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
627 struct listnode *streams_input_cfg_list,
628 audio_devices_t devices __unused,
629 audio_input_flags_t flags,
630 audio_format_t format,
631 uint32_t sample_rate,
632 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530633 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530634 struct stream_app_type_cfg *app_type_cfg)
635{
636 struct listnode *node_i, *node_j;
637 struct streams_io_cfg *s_info;
638 struct stream_format *sf_info;
639
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530640 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
641 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530642
643 list_for_each(node_i, streams_input_cfg_list) {
644 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530645 /* Along with flags do profile matching if set at either end.*/
646 if (s_info->flags.in_flags == flags &&
647 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
648 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530649 list_for_each(node_j, &s_info->format_list) {
650 sf_info = node_to_item(node_j, struct stream_format, list);
651 if (sf_info->format == format) {
652 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
653 return;
654 }
655 }
656 }
657 }
658 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
659 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
660 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
661 app_type_cfg->bit_width = 16;
662}
663
664void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700665 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700666 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700667 audio_output_flags_t flags,
668 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700669 uint32_t sample_rate,
670 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530671 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530672 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700673 struct stream_app_type_cfg *app_type_cfg)
674{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530675 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530676 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700677 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530678 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700679
Ashish Jain058165c2016-09-28 23:18:48 +0530680 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700681 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700682 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
683 if (-ENOSYS != bw)
684 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700685 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
686 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
687 }
688
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700689 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530690 if (!strncmp("true", value, sizeof("true"))) {
691 if ((popcount(channel_mask) > 2) &&
692 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
693 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
694 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
695 ALOGD("%s: MCH session defaulting sample rate to %d",
696 __func__, sample_rate);
697 }
698 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530699
700 /* Set sampling rate to 176.4 for DSD64
701 * and 352.8Khz for DSD128.
702 * Set Bit Width to 16. output will be 16 bit
703 * post DoP in ASM.
704 */
705 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
706 (format == AUDIO_FORMAT_DSD)) {
707 bit_width = 16;
708 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
709 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
710 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
711 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
712 }
713
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530714 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
715 //TODO: Handle fractional sampling rate configuration for LL
716 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
717 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
718 __func__, sample_rate, bit_width);
719 }
720
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800721 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
722 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700723 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530724 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530725 /* Along with flags do profile matching if set at either end.*/
726 if (s_info->flags.out_flags == flags &&
727 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
728 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530729 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700730 sf_info = node_to_item(node_j, struct stream_format, list);
731 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530732 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700733 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700734 }
735 }
736 }
737 }
738 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530739 s_info = node_to_item(node_i, struct streams_io_cfg, list);
740 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700741 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530742 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
743 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
744 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700745 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530746 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700747 return;
748 }
749 }
750 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700751 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700752 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700753 app_type_cfg->bit_width = 16;
754}
755
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530756static bool audio_is_this_native_usecase(struct audio_usecase *uc)
757{
758 bool native_usecase = false;
759 struct stream_out *out = (struct stream_out*) uc->stream.out;
760
761 if (PCM_PLAYBACK == uc->type && out != NULL &&
762 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
763 is_offload_usecase(uc->id) &&
764 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
765 native_usecase = true;
766
767 return native_usecase;
768}
769
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800770
771static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
772{
773 return adev->vr_audio_mode_enabled;
774}
775
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530776void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
777 struct audio_device *adev,
778 struct audio_usecase *usecase)
779{
780 ALOGV("%s", __func__);
781
782 switch(usecase->type) {
783 case PCM_PLAYBACK:
784 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
785 &adev->streams_output_cfg_list,
786 usecase->stream.out->devices,
787 usecase->stream.out->flags,
788 usecase->stream.out->format,
789 usecase->stream.out->sample_rate,
790 usecase->stream.out->bit_width,
791 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530792 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530793 &usecase->stream.out->app_type_cfg);
794 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
795 break;
796 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700797 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
798 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
799 else
800 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530801 &adev->streams_input_cfg_list,
802 usecase->stream.in->device,
803 usecase->stream.in->flags,
804 usecase->stream.in->format,
805 usecase->stream.in->sample_rate,
806 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530807 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530808 &usecase->stream.in->app_type_cfg);
809 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
810 break;
811 default:
812 ALOGE("%s: app type cfg not supported for usecase type (%d)",
813 __func__, usecase->type);
814 }
815}
816
Siena Richard7c2db772016-12-21 11:32:34 -0800817static int send_app_type_cfg_for_device(struct audio_device *adev,
818 struct audio_usecase *usecase,
819 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700820{
821 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530822 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
823 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700824 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800825 int pcm_device_id = 0, acdb_dev_id, app_type;
826 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530827 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530828 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +0530829 struct streams_io_cfg *s_info = NULL;
830 struct listnode *node = NULL;
831 int direct_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700832
Siena Richard7c2db772016-12-21 11:32:34 -0800833 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
834 __func__, platform_get_snd_device_name(usecase->out_snd_device),
835 platform_get_snd_device_name(usecase->in_snd_device),
836 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700837
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530838 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530839 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700840 rc = 0;
841 goto exit_send_app_type_cfg;
842 }
843 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
844 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
845 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800846 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700847 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Varun Balaraje49253e2017-07-06 19:48:56 +0530848 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530849 (!is_offload_usecase(usecase->id)) &&
850 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530851 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 -0700852 rc = 0;
853 goto exit_send_app_type_cfg;
854 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800855
856 //if VR is active then only send the mixer control
857 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
858 ALOGI("ULL doesnt need sending app type cfg, returning");
859 rc = 0;
860 goto exit_send_app_type_cfg;
861 }
862
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530863 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700864 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530865 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
866 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700867 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700868 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530869 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
870 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530871 }
Siena Richard7c2db772016-12-21 11:32:34 -0800872
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700873 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
874 if (!ctl) {
875 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
876 mixer_ctl_name);
877 rc = -EINVAL;
878 goto exit_send_app_type_cfg;
879 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530880 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530881
Rohit Kumar1181b292017-01-31 18:07:17 +0530882 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
883 if (acdb_dev_id <= 0) {
884 ALOGE("%s: Couldn't get the acdb dev id", __func__);
885 rc = -EINVAL;
886 goto exit_send_app_type_cfg;
887 }
888
Siena Richard7c2db772016-12-21 11:32:34 -0800889 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
890 if (snd_device_be_idx < 0) {
891 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
892 __func__, platform_get_snd_device_name(snd_device),
893 snd_device_be_idx);
894 }
895
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530896 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530897
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700898 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530899 if (!strncmp("true", value, sizeof("true"))) {
900 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
901 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
902 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
903 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
904 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530905
Ashish Jain4826f6c2017-02-06 13:33:20 +0530906 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700907 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530908 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
909 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
910 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
911 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
912 /*
913 * To best utlize DSP, check if the stream sample rate is supported/multiple of
914 * configured device sample rate, if not update the COPP rate to be equal to the
915 * device sample rate, else open COPP at stream sample rate
916 */
917 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
918 usecase->stream.out->sample_rate,
919 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530920 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
921 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700922 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
923 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530924 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700925 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
926 }
927 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
928
Varun Balaraje49253e2017-07-06 19:48:56 +0530929 /* Interactive streams are supported with only direct app type id.
930 * Get Direct profile app type and use it for interactive streams
931 */
932 list_for_each(node, &adev->streams_output_cfg_list) {
933 s_info = node_to_item(node, struct streams_io_cfg, list);
934 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_DIRECT)
935 direct_app_type = s_info->app_type_cfg.app_type;
936 }
937 if (usecase->stream.out->flags == AUDIO_OUTPUT_FLAG_INTERACTIVE)
938 app_type = direct_app_type;
939 else
940 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -0800941 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700942 app_type_cfg[len++] = acdb_dev_id;
943 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700944 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
945 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530946 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -0700947
948 sample_rate = sample_rate * 4;
949 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
950 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -0700951 }
Naresh Tanniru3a406772017-05-10 13:09:05 -0700952 app_type_cfg[len++] = sample_rate;
953
Siena Richard7c2db772016-12-21 11:32:34 -0800954 if (snd_device_be_idx > 0)
955 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530956
Siena Richard7c2db772016-12-21 11:32:34 -0800957 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
958 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530959
960 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800961 app_type = usecase->stream.in->app_type_cfg.app_type;
962 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530963 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800964 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
965 app_type_cfg[len++] = sample_rate;
966 if (snd_device_be_idx > 0)
967 app_type_cfg[len++] = snd_device_be_idx;
968 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
969 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530970 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800971 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
972 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530973 app_type_cfg[len++] = acdb_dev_id;
974 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800975 if (snd_device_be_idx > 0)
976 app_type_cfg[len++] = snd_device_be_idx;
977 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
978 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530979 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530980
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700981 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700982 rc = 0;
983exit_send_app_type_cfg:
984 return rc;
985}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700986
Siena Richard7c2db772016-12-21 11:32:34 -0800987int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
988 struct audio_usecase *usecase)
989{
990 int i, num_devices = 0;
991 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
992 int rc = 0;
993
994 switch (usecase->type) {
995 case PCM_PLAYBACK:
996 ALOGD("%s: usecase->out_snd_device %s",
997 __func__, platform_get_snd_device_name(usecase->out_snd_device));
998 /* check for out combo device */
999 if (platform_split_snd_device(adev->platform,
1000 usecase->out_snd_device,
1001 &num_devices, new_snd_devices)) {
1002 new_snd_devices[0] = usecase->out_snd_device;
1003 num_devices = 1;
1004 }
1005 break;
1006 case PCM_CAPTURE:
1007 ALOGD("%s: usecase->in_snd_device %s",
1008 __func__, platform_get_snd_device_name(usecase->in_snd_device));
1009 /* check for in combo device */
1010 if (platform_split_snd_device(adev->platform,
1011 usecase->in_snd_device,
1012 &num_devices, new_snd_devices)) {
1013 new_snd_devices[0] = usecase->in_snd_device;
1014 num_devices = 1;
1015 }
1016 break;
1017 default:
1018 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1019 rc = 0;
1020 break;
1021 }
1022
1023 for (i = 0; i < num_devices; i++) {
1024 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1025 if (rc)
1026 break;
1027 }
1028
1029 return rc;
1030}
1031
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301032int read_line_from_file(const char *path, char *buf, size_t count)
1033{
1034 char * fgets_ret;
1035 FILE * fd;
1036 int rv;
1037
1038 fd = fopen(path, "r");
1039 if (fd == NULL)
1040 return -1;
1041
1042 fgets_ret = fgets(buf, (int)count, fd);
1043 if (NULL != fgets_ret) {
1044 rv = (int)strlen(buf);
1045 } else {
1046 rv = ferror(fd);
1047 }
1048 fclose(fd);
1049
1050 return rv;
1051}
1052
Ashish Jainf1eaa582016-05-23 20:54:24 +05301053/*Translates ALSA formats to AOSP PCM formats*/
1054audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1055{
1056 audio_format_t format;
1057
1058 switch(alsa_format) {
1059 case SNDRV_PCM_FORMAT_S16_LE:
1060 format = AUDIO_FORMAT_PCM_16_BIT;
1061 break;
1062 case SNDRV_PCM_FORMAT_S24_3LE:
1063 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1064 break;
1065 case SNDRV_PCM_FORMAT_S24_LE:
1066 format = AUDIO_FORMAT_PCM_8_24_BIT;
1067 break;
1068 case SNDRV_PCM_FORMAT_S32_LE:
1069 format = AUDIO_FORMAT_PCM_32_BIT;
1070 break;
1071 default:
1072 ALOGW("Incorrect ALSA format");
1073 format = AUDIO_FORMAT_INVALID;
1074 }
1075 return format;
1076}
1077
1078/*Translates hal format (AOSP) to alsa formats*/
1079uint32_t hal_format_to_alsa(audio_format_t hal_format)
1080{
1081 uint32_t alsa_format;
1082
1083 switch (hal_format) {
1084 case AUDIO_FORMAT_PCM_32_BIT: {
1085 if (platform_supports_true_32bit())
1086 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1087 else
1088 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1089 }
1090 break;
1091 case AUDIO_FORMAT_PCM_8_BIT:
1092 alsa_format = SNDRV_PCM_FORMAT_S8;
1093 break;
1094 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1095 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1096 break;
1097 case AUDIO_FORMAT_PCM_8_24_BIT: {
1098 if (platform_supports_true_32bit())
1099 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1100 else
1101 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1102 }
1103 break;
1104 case AUDIO_FORMAT_PCM_FLOAT:
1105 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1106 break;
1107 default:
1108 case AUDIO_FORMAT_PCM_16_BIT:
1109 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1110 break;
1111 }
1112 return alsa_format;
1113}
1114
Ashish Jain83a6cc22016-06-28 14:34:17 +05301115/*Translates PCM formats to AOSP formats*/
1116audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1117{
1118 audio_format_t format = AUDIO_FORMAT_INVALID;
1119
1120 switch(pcm_format) {
1121 case PCM_FORMAT_S16_LE:
1122 format = AUDIO_FORMAT_PCM_16_BIT;
1123 break;
1124 case PCM_FORMAT_S24_3LE:
1125 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1126 break;
1127 case PCM_FORMAT_S24_LE:
1128 format = AUDIO_FORMAT_PCM_8_24_BIT;
1129 break;
1130 case PCM_FORMAT_S32_LE:
1131 format = AUDIO_FORMAT_PCM_32_BIT;
1132 break;
1133 default:
1134 ALOGW("Incorrect PCM format");
1135 format = AUDIO_FORMAT_INVALID;
1136 }
1137 return format;
1138}
1139
1140/*Translates hal format (AOSP) to alsa formats*/
1141uint32_t hal_format_to_pcm(audio_format_t hal_format)
1142{
1143 uint32_t pcm_format;
1144
1145 switch (hal_format) {
1146 case AUDIO_FORMAT_PCM_32_BIT:
1147 case AUDIO_FORMAT_PCM_8_24_BIT:
1148 case AUDIO_FORMAT_PCM_FLOAT: {
1149 if (platform_supports_true_32bit())
1150 pcm_format = PCM_FORMAT_S32_LE;
1151 else
1152 pcm_format = PCM_FORMAT_S24_3LE;
1153 }
1154 break;
1155 case AUDIO_FORMAT_PCM_8_BIT:
1156 pcm_format = PCM_FORMAT_S8;
1157 break;
1158 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1159 pcm_format = PCM_FORMAT_S24_3LE;
1160 break;
1161 default:
1162 case AUDIO_FORMAT_PCM_16_BIT:
1163 pcm_format = PCM_FORMAT_S16_LE;
1164 break;
1165 }
1166 return pcm_format;
1167}
1168
Ashish Jainf1eaa582016-05-23 20:54:24 +05301169uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1170 uint32_t sample_rate,
1171 uint32_t noOfChannels)
1172{
1173 uint32_t fragment_size = 0;
1174 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1175
1176 fragment_size = (pcm_offload_time
1177 * sample_rate
1178 * bytes_per_sample
1179 * noOfChannels)/1000;
1180 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1181 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1182 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1183 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1184 /*To have same PCM samples for all channels, the buffer size requires to
1185 *be multiple of (number of channels * bytes per sample)
1186 *For writes to succeed, the buffer must be written at address which is multiple of 32
1187 */
1188 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1189
1190 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1191 return fragment_size;
1192}
1193
1194/* Calculates the fragment size required to configure compress session.
1195 * Based on the alsa format selected, decide if conversion is needed in
1196
1197 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1198 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1199 */
1200void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1201{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301202 audio_format_t dst_format = out->hal_op_format;
1203 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301204 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1205 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1206
1207 out->compr_config.fragment_size =
1208 get_alsa_fragment_size(hal_op_bytes_per_sample,
1209 out->sample_rate,
1210 popcount(out->channel_mask));
1211
1212 if ((src_format != dst_format) &&
1213 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1214
Ashish Jain83a6cc22016-06-28 14:34:17 +05301215 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301216 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1217 hal_op_bytes_per_sample);
1218 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301219 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301220 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301221 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301222 }
1223}
1224
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301225/* converts pcm format 24_8 to 8_24 inplace */
1226size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1227{
1228 size_t i = 0;
1229 int *int_buf_stream = buf;
1230
1231 if ((bytes % 4) != 0) {
1232 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1233 return -EINVAL;
1234 }
1235
1236 for (; i < (bytes / 4); i++)
1237 int_buf_stream[i] >>= 8;
1238
1239 return bytes;
1240}
1241
1242int get_snd_codec_id(audio_format_t format)
1243{
1244 int id = 0;
1245
1246 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1247 case AUDIO_FORMAT_MP3:
1248 id = SND_AUDIOCODEC_MP3;
1249 break;
1250 case AUDIO_FORMAT_AAC:
1251 id = SND_AUDIOCODEC_AAC;
1252 break;
1253 case AUDIO_FORMAT_AAC_ADTS:
1254 id = SND_AUDIOCODEC_AAC;
1255 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301256 case AUDIO_FORMAT_AAC_LATM:
1257 id = SND_AUDIOCODEC_AAC;
1258 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301259 case AUDIO_FORMAT_PCM:
1260 id = SND_AUDIOCODEC_PCM;
1261 break;
1262 case AUDIO_FORMAT_FLAC:
1263 id = SND_AUDIOCODEC_FLAC;
1264 break;
1265 case AUDIO_FORMAT_ALAC:
1266 id = SND_AUDIOCODEC_ALAC;
1267 break;
1268 case AUDIO_FORMAT_APE:
1269 id = SND_AUDIOCODEC_APE;
1270 break;
1271 case AUDIO_FORMAT_VORBIS:
1272 id = SND_AUDIOCODEC_VORBIS;
1273 break;
1274 case AUDIO_FORMAT_WMA:
1275 id = SND_AUDIOCODEC_WMA;
1276 break;
1277 case AUDIO_FORMAT_WMA_PRO:
1278 id = SND_AUDIOCODEC_WMA_PRO;
1279 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301280 case AUDIO_FORMAT_MP2:
1281 id = SND_AUDIOCODEC_MP2;
1282 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301283 case AUDIO_FORMAT_AC3:
1284 id = SND_AUDIOCODEC_AC3;
1285 break;
1286 case AUDIO_FORMAT_E_AC3:
1287 case AUDIO_FORMAT_E_AC3_JOC:
1288 id = SND_AUDIOCODEC_EAC3;
1289 break;
1290 case AUDIO_FORMAT_DTS:
1291 case AUDIO_FORMAT_DTS_HD:
1292 id = SND_AUDIOCODEC_DTS;
1293 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001294 case AUDIO_FORMAT_DOLBY_TRUEHD:
1295 id = SND_AUDIOCODEC_TRUEHD;
1296 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001297 case AUDIO_FORMAT_IEC61937:
1298 id = SND_AUDIOCODEC_IEC61937;
1299 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301300 case AUDIO_FORMAT_DSD:
1301 id = SND_AUDIOCODEC_DSD;
1302 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301303 case AUDIO_FORMAT_APTX:
1304 id = SND_AUDIOCODEC_APTX;
1305 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301306 default:
1307 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1308 }
1309
1310 return id;
1311}
1312
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001313void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1314 struct audio_usecase *usecase)
1315{
1316 int type = usecase->type;
1317
Dhananjay Kumar14245982017-01-16 20:21:00 +05301318 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001319 struct stream_out *out = usecase->stream.out;
1320 int snd_device = usecase->out_snd_device;
1321 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001322 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301323 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001324 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301325 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301326 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301327 platform_send_audio_calibration(adev->platform, usecase,
1328 usecase->stream.in->app_type_cfg.app_type,
1329 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001330 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301331 /* when app type is default. the sample rate is not used to send cal */
1332 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301333 platform_get_default_app_type_v2(adev->platform, usecase->type),
1334 48000);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001335 } else {
1336 /* No need to send audio calibration for voice and voip call usecases */
1337 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1338 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001339 }
1340}
1341
Ben Rombergera04fabc2014-11-14 12:16:03 -08001342// Base64 Encode and Decode
1343// Not all features supported. This must be used only with following conditions.
1344// Decode Modes: Support with and without padding
1345// CRLF not handling. So no CRLF in string to decode.
1346// Encode Modes: Supports only padding
1347int b64decode(char *inp, int ilen, uint8_t* outp)
1348{
1349 int i, j, k, ii, num;
1350 int rem, pcnt;
1351 uint32_t res=0;
1352 uint8_t getIndex[MAX_BASEINDEX_LEN];
1353 uint8_t tmp, cflag;
1354
1355 if(inp == NULL || outp == NULL || ilen <= 0) {
1356 ALOGE("[%s] received NULL pointer or zero length",__func__);
1357 return -1;
1358 }
1359
1360 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1361 for(i=0;i<BASE_TABLE_SIZE;i++) {
1362 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1363 }
1364 getIndex[(uint8_t)'=']=0;
1365
1366 j=0;k=0;
1367 num = ilen/4;
1368 rem = ilen%4;
1369 if(rem==0)
1370 num = num-1;
1371 cflag=0;
1372 for(i=0; i<num; i++) {
1373 res=0;
1374 for(ii=0;ii<4;ii++) {
1375 res = res << 6;
1376 tmp = getIndex[(uint8_t)inp[j++]];
1377 res = res | tmp;
1378 cflag = cflag | tmp;
1379 }
1380 outp[k++] = (res >> 16)&0xFF;
1381 outp[k++] = (res >> 8)&0xFF;
1382 outp[k++] = res & 0xFF;
1383 }
1384
1385 // Handle last bytes special
1386 pcnt=0;
1387 if(rem == 0) {
1388 //With padding or full data
1389 res = 0;
1390 for(ii=0;ii<4;ii++) {
1391 if(inp[j] == '=')
1392 pcnt++;
1393 res = res << 6;
1394 tmp = getIndex[(uint8_t)inp[j++]];
1395 res = res | tmp;
1396 cflag = cflag | tmp;
1397 }
1398 outp[k++] = res >> 16;
1399 if(pcnt == 2)
1400 goto done;
1401 outp[k++] = (res>>8)&0xFF;
1402 if(pcnt == 1)
1403 goto done;
1404 outp[k++] = res&0xFF;
1405 } else {
1406 //without padding
1407 res = 0;
1408 for(i=0;i<rem;i++) {
1409 res = res << 6;
1410 tmp = getIndex[(uint8_t)inp[j++]];
1411 res = res | tmp;
1412 cflag = cflag | tmp;
1413 }
1414 for(i=rem;i<4;i++) {
1415 res = res << 6;
1416 pcnt++;
1417 }
1418 outp[k++] = res >> 16;
1419 if(pcnt == 2)
1420 goto done;
1421 outp[k++] = (res>>8)&0xFF;
1422 if(pcnt == 1)
1423 goto done;
1424 outp[k++] = res&0xFF;
1425 }
1426done:
1427 if(cflag == 0xFF) {
1428 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1429 __func__, inp);
1430 return 0;
1431 }
1432 return k;
1433}
1434
1435int b64encode(uint8_t *inp, int ilen, char* outp)
1436{
1437 int i,j,k, num;
1438 int rem=0;
1439 uint32_t res=0;
1440
1441 if(inp == NULL || outp == NULL || ilen<=0) {
1442 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1443 return -1;
1444 }
1445
1446 num = ilen/3;
1447 rem = ilen%3;
1448 j=0;k=0;
1449 for(i=0; i<num; i++) {
1450 //prepare index
1451 res = inp[j++]<<16;
1452 res = res | inp[j++]<<8;
1453 res = res | inp[j++];
1454 //get output map from index
1455 outp[k++] = (char) bTable[(res>>18)&0x3F];
1456 outp[k++] = (char) bTable[(res>>12)&0x3F];
1457 outp[k++] = (char) bTable[(res>>6)&0x3F];
1458 outp[k++] = (char) bTable[res&0x3F];
1459 }
1460
1461 switch(rem) {
1462 case 1:
1463 res = inp[j++]<<16;
1464 outp[k++] = (char) bTable[res>>18];
1465 outp[k++] = (char) bTable[(res>>12)&0x3F];
1466 //outp[k++] = '=';
1467 //outp[k++] = '=';
1468 break;
1469 case 2:
1470 res = inp[j++]<<16;
1471 res = res | inp[j++]<<8;
1472 outp[k++] = (char) bTable[res>>18];
1473 outp[k++] = (char) bTable[(res>>12)&0x3F];
1474 outp[k++] = (char) bTable[(res>>6)&0x3F];
1475 //outp[k++] = '=';
1476 break;
1477 default:
1478 break;
1479 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001480 outp[k] = '\0';
1481 return k;
1482}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301483
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301484
1485int audio_extn_utils_get_codec_version(const char *snd_card_name,
1486 int card_num,
1487 char *codec_version)
1488{
1489 char procfs_path[50];
1490 FILE *fp;
1491
1492 if (strstr(snd_card_name, "tasha")) {
1493 snprintf(procfs_path, sizeof(procfs_path),
1494 "/proc/asound/card%d/codecs/tasha/version", card_num);
1495 if ((fp = fopen(procfs_path, "r")) != NULL) {
1496 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1497 fclose(fp);
1498 } else {
1499 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1500 return -ENOENT;
1501 }
1502 ALOGD("%s: codec version %s", __func__, codec_version);
1503 }
1504
1505 return 0;
1506}
1507
1508
Ashish Jain81eb2a82015-05-13 10:52:34 +05301509#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1510
1511void get_default_compressed_channel_status(
1512 unsigned char *channel_status)
1513{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301514 memset(channel_status,0,24);
1515
1516 /* block start bit in preamble bit 3 */
1517 channel_status[0] |= PROFESSIONAL;
1518 //compre out
1519 channel_status[0] |= NON_LPCM;
1520 // sample rate; fixed 48K for default/transcode
1521 channel_status[3] |= SR_48000;
1522}
1523
1524#ifdef HDMI_PASSTHROUGH_ENABLED
1525int32_t get_compressed_channel_status(void *audio_stream_data,
1526 uint32_t audio_frame_size,
1527 unsigned char *channel_status,
1528 enum audio_parser_code_type codec_type)
1529 // codec_type - AUDIO_PARSER_CODEC_AC3
1530 // - AUDIO_PARSER_CODEC_DTS
1531{
1532 unsigned char *stream;
1533 int ret = 0;
1534 stream = (unsigned char *)audio_stream_data;
1535
1536 if (audio_stream_data == NULL || audio_frame_size == 0) {
1537 ALOGW("no buffer to get channel status, return default for compress");
1538 get_default_compressed_channel_status(channel_status);
1539 return ret;
1540 }
1541
1542 memset(channel_status,0,24);
1543 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1544 {
1545 ALOGE("init audio parser failed");
1546 return -1;
1547 }
1548 ret = get_channel_status(channel_status, codec_type);
1549 return ret;
1550
1551}
1552
1553#endif
1554
1555void get_lpcm_channel_status(uint32_t sampleRate,
1556 unsigned char *channel_status)
1557{
1558 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301559 memset(channel_status,0,24);
1560 /* block start bit in preamble bit 3 */
1561 channel_status[0] |= PROFESSIONAL;
1562 //LPCM OUT
1563 channel_status[0] &= ~NON_LPCM;
1564
1565 switch (sampleRate) {
1566 case 8000:
1567 case 11025:
1568 case 12000:
1569 case 16000:
1570 case 22050:
1571 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301572 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301573 case 24000:
1574 channel_status[3] |= SR_24000;
1575 break;
1576 case 32000:
1577 channel_status[3] |= SR_32000;
1578 break;
1579 case 44100:
1580 channel_status[3] |= SR_44100;
1581 break;
1582 case 48000:
1583 channel_status[3] |= SR_48000;
1584 break;
1585 case 88200:
1586 channel_status[3] |= SR_88200;
1587 break;
1588 case 96000:
1589 channel_status[3] |= SR_96000;
1590 break;
1591 case 176400:
1592 channel_status[3] |= SR_176400;
1593 break;
1594 case 192000:
1595 channel_status[3] |= SR_192000;
1596 break;
1597 default:
1598 ALOGV("Invalid sample_rate %u\n", sampleRate);
1599 status = -1;
1600 break;
1601 }
1602}
1603
1604void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1605{
1606 unsigned char channel_status[24]={0};
1607 struct snd_aes_iec958 iec958;
1608 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1609 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301610 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301611#ifdef HDMI_PASSTHROUGH_ENABLED
1612 if (audio_extn_is_dolby_format(out->format) &&
1613 /*TODO:Extend code to support DTS passthrough*/
1614 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301615 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301616 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1617 } else
1618#endif
1619 {
1620 /*set channel status bit for LPCM*/
1621 get_lpcm_channel_status(out->sample_rate, channel_status);
1622 }
1623
1624 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1625 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1626 if (!ctl) {
1627 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1628 __func__, mixer_ctl_name);
1629 return;
1630 }
1631 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1632 ALOGE("%s: Could not set channel status for ext HDMI ",
1633 __func__);
1634 return;
1635 }
1636
1637}
1638#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301639
1640int audio_extn_utils_get_avt_device_drift(
1641 struct audio_usecase *usecase,
1642 struct audio_avt_device_drift_param *drift_param)
1643{
1644 int ret = 0, count = 0;
1645 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301646 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301647 struct mixer_ctl *ctl = NULL;
1648 struct audio_avt_device_drift_stats drift_stats;
1649 struct audio_device *adev = NULL;
1650
1651 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301652 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1653 if (!backend) {
1654 ALOGE("%s: Unsupported device %d", __func__,
1655 usecase->stream.out->devices);
1656 ret = -EINVAL;
1657 goto done;
1658 }
1659 strlcpy(avt_device_drift_mixer_ctl_name,
1660 backend,
1661 MIXER_PATH_MAX_LENGTH);
1662
1663 count = strlen(backend);
1664 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1665 strlcat(&avt_device_drift_mixer_ctl_name[count],
1666 " DRIFT",
1667 MIXER_PATH_MAX_LENGTH - count);
1668 } else {
1669 ret = -EINVAL;
1670 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301671 }
1672 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301673 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301674 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301675 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301676 }
1677
Naresh Tanniru6160c712017-04-17 15:43:48 +05301678 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301679 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1680 if (!ctl) {
1681 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1682 __func__, avt_device_drift_mixer_ctl_name);
1683
1684 ret = -EINVAL;
1685 goto done;
1686 }
1687
1688 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1689 avt_device_drift_mixer_ctl_name);
1690
1691 mixer_ctl_update(ctl);
1692 count = mixer_ctl_get_num_values(ctl);
1693 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1694 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1695 __func__);
1696
1697 ret = -EINVAL;
1698 goto done;
1699 }
1700
1701 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1702 if (ret != 0) {
1703 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1704 __func__);
1705
1706 ret = -EINVAL;
1707 goto done;
1708 }
1709 memcpy(drift_param, &drift_stats.drift_param,
1710 sizeof(struct audio_avt_device_drift_param));
1711done:
1712 return ret;
1713}
Manish Dewangan07de2142017-02-27 19:27:20 +05301714
1715#ifdef SNDRV_COMPRESS_PATH_DELAY
1716int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1717{
1718 int ret = -EINVAL;
1719 struct snd_compr_metadata metadata;
1720 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1721
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001722 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301723 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1724 if (!(is_offload_usecase(out->usecase))) {
1725 ALOGE("%s:: not supported for non offload session", __func__);
1726 goto exit;
1727 }
1728
1729 if (!out->compr) {
1730 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1731 __func__);
1732 goto exit;
1733 }
1734
1735 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1736 ret = compress_get_metadata(out->compr, &metadata);
1737 if(ret) {
1738 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1739 goto exit;
1740 }
1741 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1742 } else {
1743 ALOGD("%s:: Using Fix DSP delay",__func__);
1744 }
1745
1746exit:
1747 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1748 return delay_ms;
1749}
1750#else
1751int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1752{
1753 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1754}
1755#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301756
1757#ifdef SNDRV_COMPRESS_RENDER_MODE
1758int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1759{
1760 struct snd_compr_metadata metadata;
1761 int ret = -EINVAL;
1762
1763 if (!(is_offload_usecase(out->usecase))) {
1764 ALOGE("%s:: not supported for non offload session", __func__);
1765 goto exit;
1766 }
1767
1768 if (!out->compr) {
1769 ALOGD("%s:: Invalid compress handle",
1770 __func__);
1771 goto exit;
1772 }
1773
1774 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1775
1776 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1777 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1778 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1779 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1780 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1781 } else {
1782 ret = 0;
1783 goto exit;
1784 }
1785 ret = compress_set_metadata(out->compr, &metadata);
1786 if(ret) {
1787 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1788 }
1789exit:
1790 return ret;
1791}
1792#else
1793int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1794{
1795 ALOGD("%s:: configuring render mode not supported", __func__);
1796 return 0;
1797}
1798#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301799
1800#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1801int audio_extn_utils_compress_set_clk_rec_mode(
1802 struct audio_usecase *usecase)
1803{
1804 struct snd_compr_metadata metadata;
1805 struct stream_out *out = NULL;
1806 int ret = -EINVAL;
1807
Naresh Tanniru7586e292017-04-13 10:38:13 +05301808 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301809 ALOGE("%s:: Invalid use case", __func__);
1810 goto exit;
1811 }
1812
1813 out = usecase->stream.out;
1814 if (!out) {
1815 ALOGE("%s:: invalid stream", __func__);
1816 goto exit;
1817 }
1818
1819 if (!is_offload_usecase(out->usecase)) {
1820 ALOGE("%s:: not supported for non offload session", __func__);
1821 goto exit;
1822 }
1823
1824 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1825 ALOGD("%s:: clk recovery is only supported in STC render mode",
1826 __func__);
1827 ret = 0;
1828 goto exit;
1829 }
1830
1831 if (!out->compr) {
1832 ALOGD("%s:: Invalid compress handle",
1833 __func__);
1834 goto exit;
1835 }
1836 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1837 switch(usecase->out_snd_device) {
1838 case SND_DEVICE_OUT_HDMI:
1839 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1840 case SND_DEVICE_OUT_DISPLAY_PORT:
1841 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1842 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1843 break;
1844 default:
1845 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1846 break;
1847 }
1848
1849 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1850
1851 ret = compress_set_metadata(out->compr, &metadata);
1852 if(ret) {
1853 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1854 }
1855
1856exit:
1857 return ret;
1858}
1859#else
1860int audio_extn_utils_compress_set_clk_rec_mode(
1861 struct audio_usecase *usecase __unused)
1862{
1863 ALOGD("%s:: configuring render mode not supported", __func__);
1864 return 0;
1865}
1866#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301867
1868#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1869int audio_extn_utils_compress_set_render_window(
1870 struct stream_out *out,
1871 struct audio_out_render_window_param *render_window)
1872{
1873 struct snd_compr_metadata metadata;
1874 int ret = -EINVAL;
1875
Manish Dewangan27346042017-03-01 12:56:12 +05301876 if(render_window == NULL) {
1877 ALOGE("%s:: Invalid render_window", __func__);
1878 goto exit;
1879 }
1880
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001881 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1882 __func__,render_window->render_ws, render_window->render_we);
1883
Manish Dewangan27346042017-03-01 12:56:12 +05301884 if (!is_offload_usecase(out->usecase)) {
1885 ALOGE("%s:: not supported for non offload session", __func__);
1886 goto exit;
1887 }
1888
Naresh Tanniru6160c712017-04-17 15:43:48 +05301889 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1890 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301891 ALOGD("%s:: only supported in timestamp mode, current "
1892 "render mode mode %d", __func__, out->render_mode);
1893 goto exit;
1894 }
1895
1896 if (!out->compr) {
1897 ALOGW("%s:: offload session not yet opened,"
1898 "render window will be configure later", __func__);
1899 /* store render window to reconfigure in start_output_stream() */
1900 goto exit;
1901 }
1902
1903 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1904 /*render window start value */
1905 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1906 metadata.value[1] = \
1907 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1908 /*render window end value */
1909 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1910 metadata.value[3] = \
1911 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1912
1913 ret = compress_set_metadata(out->compr, &metadata);
1914 if(ret) {
1915 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1916 }
1917
1918exit:
1919 return ret;
1920}
1921#else
1922int audio_extn_utils_compress_set_render_window(
1923 struct stream_out *out __unused,
1924 struct audio_out_render_window_param *render_window __unused)
1925{
1926 ALOGD("%s:: configuring render window not supported", __func__);
1927 return 0;
1928}
1929#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05301930
1931#ifdef SNDRV_COMPRESS_START_DELAY
1932int audio_extn_utils_compress_set_start_delay(
1933 struct stream_out *out,
1934 struct audio_out_start_delay_param *delay_param)
1935{
1936 struct snd_compr_metadata metadata;
1937 int ret = -EINVAL;
1938
1939 if(delay_param == NULL) {
1940 ALOGE("%s:: Invalid delay_param", __func__);
1941 goto exit;
1942 }
1943
1944 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
1945 delay_param->start_delay);
1946
1947 if (!is_offload_usecase(out->usecase)) {
1948 ALOGE("%s:: not supported for non offload session", __func__);
1949 goto exit;
1950 }
1951
Naresh Tanniru6160c712017-04-17 15:43:48 +05301952 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1953 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05301954 ALOGD("%s:: only supported in timestamp mode, current "
1955 "render mode mode %d", __func__, out->render_mode);
1956 goto exit;
1957 }
1958
1959 if (!out->compr) {
1960 ALOGW("%s:: offload session not yet opened,"
1961 "start delay will be configure later", __func__);
1962 goto exit;
1963 }
1964
1965 metadata.key = SNDRV_COMPRESS_START_DELAY;
1966 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
1967 metadata.value[1] = \
1968 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
1969
1970 ret = compress_set_metadata(out->compr, &metadata);
1971 if(ret) {
1972 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1973 }
1974
1975exit:
1976 return ret;
1977}
1978#else
1979int audio_extn_utils_compress_set_start_delay(
1980 struct stream_out *out __unused,
1981 struct audio_out_start_delay_param *delay_param __unused)
1982{
1983 ALOGD("%s:: configuring render window not supported", __func__);
1984 return 0;
1985}
1986#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07001987
1988#define MAX_SND_CARD 8
1989#define RETRY_US 500000
1990#define RETRY_NUMBER 10
1991
1992int audio_extn_utils_get_snd_card_num()
1993{
1994
1995 void *hw_info = NULL;
1996 struct mixer *mixer = NULL;
1997 int retry_num = 0;
1998 int snd_card_num = 0;
1999 char* snd_card_name = NULL;
2000
2001 while (snd_card_num < MAX_SND_CARD) {
2002 mixer = mixer_open(snd_card_num);
2003
2004 while (!mixer && retry_num < RETRY_NUMBER) {
2005 usleep(RETRY_US);
2006 mixer = mixer_open(snd_card_num);
2007 retry_num++;
2008 }
2009
2010 if (!mixer) {
2011 ALOGE("%s: Unable to open the mixer card: %d", __func__,
2012 snd_card_num);
2013 retry_num = 0;
2014 snd_card_num++;
2015 continue;
2016 }
2017
2018 snd_card_name = strdup(mixer_get_name(mixer));
2019 if (!snd_card_name) {
2020 ALOGE("failed to allocate memory for snd_card_name\n");
2021 mixer_close(mixer);
2022 return -1;
2023 }
2024 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2025
2026 hw_info = hw_info_init(snd_card_name);
2027 if (hw_info) {
2028 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2029 break;
2030 }
2031 ALOGE("%s: Failed to init hardware info", __func__);
2032 retry_num = 0;
2033 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002034
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302035 free(snd_card_name);
2036 snd_card_name = NULL;
2037
2038 mixer_close(mixer);
2039 mixer = NULL;
2040 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002041 if (snd_card_name)
2042 free(snd_card_name);
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302043 if (mixer)
2044 mixer_close(mixer);
2045 if (hw_info)
2046 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002047
2048 if (snd_card_num >= MAX_SND_CARD) {
2049 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2050 return -1;
2051 }
2052
2053 return snd_card_num;
2054}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302055
2056#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2057int audio_extn_utils_compress_enable_drift_correction(
2058 struct stream_out *out,
2059 struct audio_out_enable_drift_correction *drift)
2060{
2061 struct snd_compr_metadata metadata;
2062 int ret = -EINVAL;
2063
2064 if(drift == NULL) {
2065 ALOGE("%s:: Invalid param", __func__);
2066 goto exit;
2067 }
2068
2069 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2070
2071 if (!is_offload_usecase(out->usecase)) {
2072 ALOGE("%s:: not supported for non offload session", __func__);
2073 goto exit;
2074 }
2075
2076 if (!out->compr) {
2077 ALOGW("%s:: offload session not yet opened,"
2078 "start delay will be configure later", __func__);
2079 goto exit;
2080 }
2081
2082 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2083 metadata.value[0] = drift->enable;
2084 out->drift_correction_enabled = drift->enable;
2085
2086 ret = compress_set_metadata(out->compr, &metadata);
2087 if(ret) {
2088 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2089 out->drift_correction_enabled = false;
2090 }
2091
2092exit:
2093 return ret;
2094}
2095#else
2096int audio_extn_utils_compress_enable_drift_correction(
2097 struct stream_out *out __unused,
2098 struct audio_out_enable_drift_correction *drift __unused)
2099{
2100 ALOGD("%s:: configuring drift enablement not supported", __func__);
2101 return 0;
2102}
2103#endif
2104
2105#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2106int audio_extn_utils_compress_correct_drift(
2107 struct stream_out *out,
2108 struct audio_out_correct_drift *drift_param)
2109{
2110 struct snd_compr_metadata metadata;
2111 int ret = -EINVAL;
2112
2113 if (drift_param == NULL) {
2114 ALOGE("%s:: Invalid drift_param", __func__);
2115 goto exit;
2116 }
2117
2118 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2119 drift_param->adjust_time);
2120
2121 if (!is_offload_usecase(out->usecase)) {
2122 ALOGE("%s:: not supported for non offload session", __func__);
2123 goto exit;
2124 }
2125
2126 if (!out->compr) {
2127 ALOGW("%s:: offload session not yet opened", __func__);
2128 goto exit;
2129 }
2130
2131 if (!out->drift_correction_enabled) {
2132 ALOGE("%s:: drift correction not enabled", __func__);
2133 goto exit;
2134 }
2135
2136 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2137 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2138 metadata.value[1] = \
2139 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2140
2141 ret = compress_set_metadata(out->compr, &metadata);
2142 if(ret)
2143 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2144exit:
2145 return ret;
2146}
2147#else
2148int audio_extn_utils_compress_correct_drift(
2149 struct stream_out *out __unused,
2150 struct audio_out_correct_drift *drift_param __unused)
2151{
2152 ALOGD("%s:: setting adjust clock not supported", __func__);
2153 return 0;
2154}
2155#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302156
2157int audio_extn_utils_set_channel_map(
2158 struct stream_out *out,
2159 struct audio_out_channel_map_param *channel_map_param)
2160{
2161 int ret = -EINVAL, i = 0;
2162 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2163
2164 if (channel_map_param == NULL) {
2165 ALOGE("%s:: Invalid channel_map", __func__);
2166 goto exit;
2167 }
2168
2169 if (channel_map_param->channels != channels) {
2170 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2171 __func__, channel_map_param->channels, channels);
2172 goto exit;
2173 }
2174
2175 for ( i = 0; i < channels; i++) {
2176 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2177 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2178 }
2179 ret = 0;
2180exit:
2181 return ret;
2182}
Varun Balaraje49253e2017-07-06 19:48:56 +05302183
2184int audio_extn_utils_set_pan_scale_params(
2185 struct stream_out *out,
2186 struct mix_matrix_params *mm_params)
2187{
2188 int ret = -EINVAL, i = 0, j = 0;
2189
2190 if (mm_params == NULL && out != NULL) {
2191 ALOGE("%s:: Invalid mix matrix params", __func__);
2192 goto exit;
2193 }
2194
2195 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2196 mm_params->num_output_channels <= 0 ||
2197 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2198 mm_params->num_input_channels <= 0)
2199 goto exit;
2200
2201 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2202 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2203 out->pan_scale_params.has_output_channel_map =
2204 mm_params->has_output_channel_map;
2205 for (i = 0; i < mm_params->num_output_channels; i++)
2206 out->pan_scale_params.output_channel_map[i] =
2207 mm_params->output_channel_map[i];
2208
2209 out->pan_scale_params.has_input_channel_map =
2210 mm_params->has_input_channel_map;
2211 for (i = 0; i < mm_params->num_input_channels; i++)
2212 out->pan_scale_params.input_channel_map[i] =
2213 mm_params->input_channel_map[i];
2214
2215 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2216 for (i = 0; i < mm_params->num_output_channels; i++)
2217 for (j = 0; j < mm_params->num_input_channels; j++) {
2218 //Convert the channel coefficient gains in Q14 format
2219 out->pan_scale_params.mixer_coeffs[i][j] =
2220 mm_params->mixer_coeffs[i][j] * (2 << 13);
2221 }
2222
2223 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2224 out->pcm_device_id,
2225 out->pan_scale_params);
2226
2227exit:
2228 return ret;
2229}
2230
2231int audio_extn_utils_set_downmix_params(
2232 struct stream_out *out,
2233 struct mix_matrix_params *mm_params)
2234{
2235 int ret = -EINVAL, i = 0, j = 0;
2236 struct audio_usecase *usecase = NULL;
2237
2238 if (mm_params == NULL && out != NULL) {
2239 ALOGE("%s:: Invalid mix matrix params", __func__);
2240 goto exit;
2241 }
2242
2243 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2244 mm_params->num_output_channels <= 0 ||
2245 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2246 mm_params->num_input_channels <= 0)
2247 goto exit;
2248
2249 usecase = get_usecase_from_list(out->dev, out->usecase);
2250 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2251 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2252
2253 out->downmix_params.has_output_channel_map =
2254 mm_params->has_output_channel_map;
2255 for (i = 0; i < mm_params->num_output_channels; i++) {
2256 out->downmix_params.output_channel_map[i] =
2257 mm_params->output_channel_map[i];
2258 }
2259
2260 out->downmix_params.has_input_channel_map =
2261 mm_params->has_input_channel_map;
2262 for (i = 0; i < mm_params->num_input_channels; i++)
2263 out->downmix_params.input_channel_map[i] =
2264 mm_params->input_channel_map[i];
2265
2266 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2267 for (i = 0; i < mm_params->num_output_channels; i++)
2268 for (j = 0; j < mm_params->num_input_channels; j++)
2269 out->downmix_params.mixer_coeffs[i][j] =
2270 mm_params->mixer_coeffs[i][j];
2271
2272 ret = platform_set_stream_downmix_params(out->dev->platform,
2273 out->pcm_device_id,
2274 usecase->out_snd_device,
2275 out->downmix_params);
2276
2277exit:
2278 return ret;
2279}