blob: de233ea7b6436f2fb763a8937d0081638c053690 [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
`Deeraj Soman676c2702017-09-18 19:25:53 +0530109typedef struct {
110 const char *id_string;
111 const int value;
112} mixer_config_lookup;
113
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700114struct string_to_enum {
115 const char *name;
116 uint32_t value;
117};
118
119const struct string_to_enum s_flag_name_to_enum_table[] = {
120 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
121 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
122 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800123 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700124 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
125 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
126 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700127 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128#ifdef INCALL_MUSIC_ENABLED
129 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
130#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700131 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530132 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700133 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700134 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530136 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
137 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
138 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
139 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
140 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530141 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700142};
143
144const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530145 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700146 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530147 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
148 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530149 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700150 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
151 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
152 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700153 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
154 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700155 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700156 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700157 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530158 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700159 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700160 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530161#ifdef AUDIO_EXTN_FORMATS_ENABLED
162 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700163 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
164 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
165 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700166 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
167 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
168 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
169 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
170 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
171 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
172 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700173 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530174 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
175 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700176 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800177 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
178 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
179 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530180 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530181 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
182 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
183 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530184 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530185 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
186 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
187 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
188 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530189 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700190#endif
191};
192
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530193/* payload structure avt_device drift query */
194struct audio_avt_device_drift_stats {
195 uint32_t minor_version;
196 /* Indicates the device interface direction as either
197 * source (Tx) or sink (Rx).
198 */
199 uint16_t device_direction;
200 /*params exposed to client */
201 struct audio_avt_device_drift_param drift_param;
202};
203
Ben Rombergera04fabc2014-11-14 12:16:03 -0800204static char bTable[BASE_TABLE_SIZE] = {
205 'A','B','C','D','E','F','G','H','I','J','K','L',
206 'M','N','O','P','Q','R','S','T','U','V','W','X',
207 'Y','Z','a','b','c','d','e','f','g','h','i','j',
208 'k','l','m','n','o','p','q','r','s','t','u','v',
209 'w','x','y','z','0','1','2','3','4','5','6','7',
210 '8','9','+','/'
211};
212
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700213static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
214 const char *name)
215{
216 size_t i;
217 for (i = 0; i < size; i++) {
218 if (strcmp(table[i].name, name) == 0) {
219 ALOGV("%s found %s", __func__, table[i].name);
220 return table[i].value;
221 }
222 }
223 return 0;
224}
225
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530226static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700227{
228 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530229 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800230 char *last_r;
231 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700232 while (flag_name != NULL) {
233 if (strlen(flag_name) != 0) {
234 flag |= string_to_enum(s_flag_name_to_enum_table,
235 ARRAY_SIZE(s_flag_name_to_enum_table),
236 flag_name);
237 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800238 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700239 }
240
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800241 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530242 io_flags.in_flags = (audio_input_flags_t)flag;
243 io_flags.out_flags = (audio_output_flags_t)flag;
244 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700245}
246
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530247static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700248{
249 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800250 char *last_r;
251 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700252
253 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
254 return;
255
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530256 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700257 while (str != NULL) {
258 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
259 ARRAY_SIZE(s_format_name_to_enum_table), str);
260 ALOGV("%s: format - %d", __func__, format);
261 if (format != 0) {
262 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700263 if (sf_info == NULL)
264 break; /* return whatever was parsed */
265
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700266 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530267 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800269 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700270 }
271}
272
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530273static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700274{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700275 struct stream_sample_rate *ss_info = NULL;
276 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800277 char *last_r;
278 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700279
Amit Shekhar6f461b12014-08-01 14:52:58 -0700280 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
281 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700282
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530283 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700284 while (str != NULL) {
285 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
286 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
287 if (0 != sample_rate) {
288 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800289 if (!ss_info) {
290 ALOGE("%s: memory allocation failure", __func__);
291 return;
292 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700293 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530294 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700295 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800296 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700297 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700298}
299
300static int parse_bit_width_names(char *name)
301{
302 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800303 char *last_r;
304 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305
306 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
307 bit_width = (int)strtol(str, (char **)NULL, 10);
308
309 ALOGV("%s: bit_width - %d", __func__, bit_width);
310 return bit_width;
311}
312
313static int parse_app_type_names(void *platform, char *name)
314{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700315 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800316 char *last_r;
317 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700318
319 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
320 app_type = (int)strtol(str, (char **)NULL, 10);
321
322 ALOGV("%s: app_type - %d", __func__, app_type);
323 return app_type;
324}
325
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530326static void update_streams_cfg_list(cnode *root, void *platform,
327 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700328{
329 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530330 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700331
332 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530333 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700334
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530335 if (!s_info) {
336 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700337 return;
338 }
339
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700340 while (node) {
341 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530342 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530343 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
344 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700345 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530346 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700347 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530348 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
349 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700350 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530351 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700352 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530353 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354 }
355 node = node->next;
356 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530357 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700358}
359
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530360static void load_cfg_list(cnode *root, void *platform,
361 struct listnode *streams_output_cfg_list,
362 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700363{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530364 cnode *node = NULL;
365
366 node = config_find(root, OUTPUTS_TAG);
367 if (node != NULL) {
368 node = node->first_child;
369 while (node) {
370 ALOGV("%s: loading output %s", __func__, node->name);
371 update_streams_cfg_list(node, platform, streams_output_cfg_list);
372 node = node->next;
373 }
374 } else {
375 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700376 }
377
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530378 node = config_find(root, INPUTS_TAG);
379 if (node != NULL) {
380 node = node->first_child;
381 while (node) {
382 ALOGV("%s: loading input %s", __func__, node->name);
383 update_streams_cfg_list(node, platform, streams_input_cfg_list);
384 node = node->next;
385 }
386 } else {
387 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700388 }
389}
390
391static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530392 struct listnode *streams_output_cfg_list,
393 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700394{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530395 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700396 int length = 0, i, num_app_types = 0;
397 struct listnode *node;
398 bool update;
399 struct mixer_ctl *ctl = NULL;
400 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530401 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800402 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700403
404 if (!mixer) {
405 ALOGE("%s: mixer is null",__func__);
406 return;
407 }
408 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
409 if (!ctl) {
410 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
411 return;
412 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530413 app_type_cfg[length++] = num_app_types;
414
415 if (list_empty(streams_output_cfg_list)) {
416 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700417 app_type_cfg[length++] = 48000;
418 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530419 num_app_types += 1;
420 }
421 if (list_empty(streams_input_cfg_list)) {
422 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
423 app_type_cfg[length++] = 48000;
424 app_type_cfg[length++] = 16;
425 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700426 }
427
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800428 /* get target bit width for ADM enforce mode */
429 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
430
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700431 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530432 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700433 update = true;
434 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530435 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700436 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530437 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530438 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530439 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530440 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530441 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800442 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
443 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
444 (target_bit_width > app_type_cfg[i+3]))
445 app_type_cfg[i+3] = target_bit_width;
446
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700447 update = false;
448 break;
449 }
450 }
451 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530452 num_app_types += 1;
453 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
454 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800455 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
456 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
457 (target_bit_width > app_type_cfg[length]))
458 app_type_cfg[length] = target_bit_width;
459
460 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530461 }
462 }
463 list_for_each(node, streams_input_cfg_list) {
464 s_info = node_to_item(node, struct streams_io_cfg, list);
465 update = true;
466 for (i=0; i<length; i=i+3) {
467 if (app_type_cfg[i+1] == 0)
468 break;
469 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530470 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530471 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530472 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530473 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
474 update = false;
475 break;
476 }
477 }
478 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
479 num_app_types += 1;
480 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
481 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
482 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700483 }
484 }
485 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
486 if (num_app_types) {
487 app_type_cfg[0] = num_app_types;
488 mixer_ctl_set_array(ctl, app_type_cfg, length);
489 }
490}
491
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530492void audio_extn_utils_update_streams_cfg_lists(void *platform,
493 struct mixer *mixer,
494 struct listnode *streams_output_cfg_list,
495 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700496{
497 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530498 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700499
500 ALOGV("%s", __func__);
501 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530502 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700503
504 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700505 if (root == NULL) {
506 ALOGE("cfg_list, NULL config root");
507 return;
508 }
509
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530510 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
511 if (data == NULL) {
512 ALOGD("%s: failed to open io config file(%s), trying older config file",
513 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
514 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
515 if (data == NULL) {
516 send_app_type_cfg(platform, mixer,
517 streams_output_cfg_list,
518 streams_input_cfg_list);
519 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800520 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530521 return;
522 }
523 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700524
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530525 config_load(root, data);
526 load_cfg_list(root, platform, streams_output_cfg_list,
527 streams_input_cfg_list);
528
529 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
530 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800531
532 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800533 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800534 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700535}
536
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530537static void audio_extn_utils_dump_streams_cfg_list(
538 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700539{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700540 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530541 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700542 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700543 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530544
545 list_for_each(node_i, streams_cfg_list) {
546 s_info = node_to_item(node_i, struct streams_io_cfg, list);
547 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
548 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
549 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
550 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700551 sf_info = node_to_item(node_j, struct stream_format, list);
552 ALOGV("format-%x", sf_info->format);
553 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530554 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700555 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
556 ALOGV("sample rate-%d", ss_info->sample_rate);
557 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700558 }
559}
560
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530561void audio_extn_utils_dump_streams_cfg_lists(
562 struct listnode *streams_output_cfg_list,
563 struct listnode *streams_input_cfg_list)
564{
565 ALOGV("%s", __func__);
566 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
567 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
568}
569
570static void audio_extn_utils_release_streams_cfg_list(
571 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700572{
573 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530574 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700575
576 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530577
578 while (!list_empty(streams_cfg_list)) {
579 node_i = list_head(streams_cfg_list);
580 s_info = node_to_item(node_i, struct streams_io_cfg, list);
581 while (!list_empty(&s_info->format_list)) {
582 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700583 list_remove(node_j);
584 free(node_to_item(node_j, struct stream_format, list));
585 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530586 while (!list_empty(&s_info->sample_rate_list)) {
587 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700588 list_remove(node_j);
589 free(node_to_item(node_j, struct stream_sample_rate, list));
590 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700591 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530592 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700593 }
594}
595
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530596void audio_extn_utils_release_streams_cfg_lists(
597 struct listnode *streams_output_cfg_list,
598 struct listnode *streams_input_cfg_list)
599{
600 ALOGV("%s", __func__);
601 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
602 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
603}
604
605static bool set_app_type_cfg(struct streams_io_cfg *s_info,
606 struct stream_app_type_cfg *app_type_cfg,
607 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700608 {
609 struct listnode *node_i;
610 struct stream_sample_rate *ss_info;
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)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700615
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530616 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700617 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530618 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700619 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
620 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
621 return true;
622 }
623 }
624 /*
625 * Reiterate through the list assuming dafault sample rate.
626 * Handles scenario where input sample rate is higher
627 * than all sample rates in list for the input bit width.
628 */
629 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800630
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530631 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700632 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
633 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530634 (bit_width == s_info->app_type_cfg.bit_width)) {
635 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700636 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530637 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800638 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 -0700639 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
640 return true;
641 }
642 }
643 return false;
644}
645
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530646void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
647 struct listnode *streams_input_cfg_list,
648 audio_devices_t devices __unused,
649 audio_input_flags_t flags,
650 audio_format_t format,
651 uint32_t sample_rate,
652 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530653 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530654 struct stream_app_type_cfg *app_type_cfg)
655{
656 struct listnode *node_i, *node_j;
657 struct streams_io_cfg *s_info;
658 struct stream_format *sf_info;
659
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530660 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
661 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530662
663 list_for_each(node_i, streams_input_cfg_list) {
664 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530665 /* Along with flags do profile matching if set at either end.*/
666 if (s_info->flags.in_flags == flags &&
667 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
668 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530669 list_for_each(node_j, &s_info->format_list) {
670 sf_info = node_to_item(node_j, struct stream_format, list);
671 if (sf_info->format == format) {
672 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
673 return;
674 }
675 }
676 }
677 }
678 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
679 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
680 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
681 app_type_cfg->bit_width = 16;
682}
683
684void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700685 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700686 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700687 audio_output_flags_t flags,
688 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700689 uint32_t sample_rate,
690 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530691 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530692 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700693 struct stream_app_type_cfg *app_type_cfg)
694{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530695 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530696 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700697 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530698 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700699
Ashish Jain058165c2016-09-28 23:18:48 +0530700 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700701 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700702 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
703 if (-ENOSYS != bw)
704 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700705 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
706 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
707 }
708
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700709 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530710 if (!strncmp("true", value, sizeof("true"))) {
711 if ((popcount(channel_mask) > 2) &&
712 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
713 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
714 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
715 ALOGD("%s: MCH session defaulting sample rate to %d",
716 __func__, sample_rate);
717 }
718 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530719
720 /* Set sampling rate to 176.4 for DSD64
721 * and 352.8Khz for DSD128.
722 * Set Bit Width to 16. output will be 16 bit
723 * post DoP in ASM.
724 */
725 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
726 (format == AUDIO_FORMAT_DSD)) {
727 bit_width = 16;
728 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
729 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
730 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
731 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
732 }
733
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530734 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
735 //TODO: Handle fractional sampling rate configuration for LL
736 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
737 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
738 __func__, sample_rate, bit_width);
739 }
740
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800741 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
742 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700743 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530744 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530745 /* Along with flags do profile matching if set at either end.*/
746 if (s_info->flags.out_flags == flags &&
747 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
748 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530749 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700750 sf_info = node_to_item(node_j, struct stream_format, list);
751 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530752 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700753 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700754 }
755 }
756 }
757 }
758 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530759 s_info = node_to_item(node_i, struct streams_io_cfg, list);
760 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700761 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530762 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
763 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
764 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700765 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530766 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767 return;
768 }
769 }
770 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700771 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700772 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700773 app_type_cfg->bit_width = 16;
774}
775
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530776static bool audio_is_this_native_usecase(struct audio_usecase *uc)
777{
778 bool native_usecase = false;
779 struct stream_out *out = (struct stream_out*) uc->stream.out;
780
781 if (PCM_PLAYBACK == uc->type && out != NULL &&
782 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
783 is_offload_usecase(uc->id) &&
784 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
785 native_usecase = true;
786
787 return native_usecase;
788}
789
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800790bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
791{
792 /* DSP bitwidth enforce mode for ADM and AFE:
793 * includes:
794 * deep buffer, low latency, direct pcm and offload.
795 * excludes:
796 * ull(raw+fast), VOIP.
797 */
798 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
799 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
800 (flags & AUDIO_OUTPUT_FLAG_FAST)))
801 return false;
802
803
804 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
805 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
806 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
807 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
808 return true;
809 else
810 return false;
811}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800812
813static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
814{
815 return adev->vr_audio_mode_enabled;
816}
817
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530818void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
819 struct audio_device *adev,
820 struct audio_usecase *usecase)
821{
822 ALOGV("%s", __func__);
823
824 switch(usecase->type) {
825 case PCM_PLAYBACK:
826 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
827 &adev->streams_output_cfg_list,
828 usecase->stream.out->devices,
829 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700830 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530831 usecase->stream.out->sample_rate,
832 usecase->stream.out->bit_width,
833 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530834 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530835 &usecase->stream.out->app_type_cfg);
836 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
837 break;
838 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700839 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
840 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
841 else
842 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530843 &adev->streams_input_cfg_list,
844 usecase->stream.in->device,
845 usecase->stream.in->flags,
846 usecase->stream.in->format,
847 usecase->stream.in->sample_rate,
848 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530849 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530850 &usecase->stream.in->app_type_cfg);
851 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
852 break;
Siddartha Shaik343abc62017-08-08 11:15:25 +0530853 case TRANSCODE_LOOPBACK :
854 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
855 &adev->streams_output_cfg_list,
856 usecase->stream.inout->out_config.devices,
857 0,
858 usecase->stream.inout->out_config.format,
859 usecase->stream.inout->out_config.sample_rate,
860 usecase->stream.inout->out_config.bit_width,
861 usecase->stream.inout->out_config.channel_mask,
862 usecase->stream.inout->profile,
863 &usecase->stream.inout->out_app_type_cfg);
864 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
865 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530866 default:
867 ALOGE("%s: app type cfg not supported for usecase type (%d)",
868 __func__, usecase->type);
869 }
870}
871
Siena Richard7c2db772016-12-21 11:32:34 -0800872static int send_app_type_cfg_for_device(struct audio_device *adev,
873 struct audio_usecase *usecase,
874 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700875{
876 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530877 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
878 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700879 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800880 int pcm_device_id = 0, acdb_dev_id, app_type;
881 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530882 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530883 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +0530884 struct streams_io_cfg *s_info = NULL;
885 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530886 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700887
Siena Richard7c2db772016-12-21 11:32:34 -0800888 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
889 __func__, platform_get_snd_device_name(usecase->out_snd_device),
890 platform_get_snd_device_name(usecase->in_snd_device),
891 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700892
Siddartha Shaik343abc62017-08-08 11:15:25 +0530893 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
894 usecase->type != TRANSCODE_LOOPBACK) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530895 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700896 rc = 0;
897 goto exit_send_app_type_cfg;
898 }
899 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
900 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
901 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800902 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700903 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Siddartha Shaik343abc62017-08-08 11:15:25 +0530904 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK) &&
Varun Balaraje49253e2017-07-06 19:48:56 +0530905 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530906 (!is_offload_usecase(usecase->id)) &&
907 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530908 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 -0700909 rc = 0;
910 goto exit_send_app_type_cfg;
911 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800912
913 //if VR is active then only send the mixer control
914 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
915 ALOGI("ULL doesnt need sending app type cfg, returning");
916 rc = 0;
917 goto exit_send_app_type_cfg;
918 }
919
Siddartha Shaik343abc62017-08-08 11:15:25 +0530920 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700921 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530922 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
923 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700924 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700925 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530926 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
927 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530928 }
Siena Richard7c2db772016-12-21 11:32:34 -0800929
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700930 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
931 if (!ctl) {
932 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
933 mixer_ctl_name);
934 rc = -EINVAL;
935 goto exit_send_app_type_cfg;
936 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530937 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530938
Rohit Kumar1181b292017-01-31 18:07:17 +0530939 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
940 if (acdb_dev_id <= 0) {
941 ALOGE("%s: Couldn't get the acdb dev id", __func__);
942 rc = -EINVAL;
943 goto exit_send_app_type_cfg;
944 }
945
Siena Richard7c2db772016-12-21 11:32:34 -0800946 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
947 if (snd_device_be_idx < 0) {
948 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
949 __func__, platform_get_snd_device_name(snd_device),
950 snd_device_be_idx);
951 }
952
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530953 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530954
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700955 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530956 if (!strncmp("true", value, sizeof("true"))) {
957 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
958 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
959 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
960 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
961 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530962
Ashish Jain4826f6c2017-02-06 13:33:20 +0530963 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700964 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530965 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
966 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
967 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
968 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
969 /*
970 * To best utlize DSP, check if the stream sample rate is supported/multiple of
971 * configured device sample rate, if not update the COPP rate to be equal to the
972 * device sample rate, else open COPP at stream sample rate
973 */
974 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
975 usecase->stream.out->sample_rate,
976 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530977 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
978 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700979 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
980 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530981 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700982 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
983 }
984 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
985
Varun Balaraje49253e2017-07-06 19:48:56 +0530986 /* Interactive streams are supported with only direct app type id.
987 * Get Direct profile app type and use it for interactive streams
988 */
989 list_for_each(node, &adev->streams_output_cfg_list) {
990 s_info = node_to_item(node, struct streams_io_cfg, list);
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530991 if (s_info->flags.out_flags == (AUDIO_OUTPUT_FLAG_BD |
992 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
993 AUDIO_OUTPUT_FLAG_DIRECT))
994 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +0530995 }
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530996 if (usecase->stream.out->flags == AUDIO_OUTPUT_FLAG_INTERACTIVE)
997 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +0530998 else
999 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001000 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001001 app_type_cfg[len++] = acdb_dev_id;
1002 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001003 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1004 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301005 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1006 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001007
1008 sample_rate = sample_rate * 4;
1009 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1010 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001011 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001012 app_type_cfg[len++] = sample_rate;
1013
Siena Richard7c2db772016-12-21 11:32:34 -08001014 if (snd_device_be_idx > 0)
1015 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301016
Siena Richard7c2db772016-12-21 11:32:34 -08001017 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1018 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301019
1020 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001021 app_type = usecase->stream.in->app_type_cfg.app_type;
1022 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301023 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -08001024 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1025 app_type_cfg[len++] = sample_rate;
1026 if (snd_device_be_idx > 0)
1027 app_type_cfg[len++] = snd_device_be_idx;
1028 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1029 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301030 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001031 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301032 if(usecase->type == TRANSCODE_LOOPBACK) {
1033 sample_rate = usecase->stream.inout->out_config.sample_rate;
1034 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1035 }
Siena Richard7c2db772016-12-21 11:32:34 -08001036 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301037 app_type_cfg[len++] = acdb_dev_id;
1038 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001039 if (snd_device_be_idx > 0)
1040 app_type_cfg[len++] = snd_device_be_idx;
1041 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1042 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301043 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301044
Siddartha Shaik343abc62017-08-08 11:15:25 +05301045 if(ctl)
1046 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001047 rc = 0;
1048exit_send_app_type_cfg:
1049 return rc;
1050}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001051
Siena Richard7c2db772016-12-21 11:32:34 -08001052int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1053 struct audio_usecase *usecase)
1054{
1055 int i, num_devices = 0;
1056 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
1057 int rc = 0;
1058
1059 switch (usecase->type) {
1060 case PCM_PLAYBACK:
Siddartha Shaik343abc62017-08-08 11:15:25 +05301061 case TRANSCODE_LOOPBACK:
Siena Richard7c2db772016-12-21 11:32:34 -08001062 ALOGD("%s: usecase->out_snd_device %s",
1063 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1064 /* check for out combo device */
1065 if (platform_split_snd_device(adev->platform,
1066 usecase->out_snd_device,
1067 &num_devices, new_snd_devices)) {
1068 new_snd_devices[0] = usecase->out_snd_device;
1069 num_devices = 1;
1070 }
1071 break;
1072 case PCM_CAPTURE:
1073 ALOGD("%s: usecase->in_snd_device %s",
1074 __func__, platform_get_snd_device_name(usecase->in_snd_device));
1075 /* check for in combo device */
1076 if (platform_split_snd_device(adev->platform,
1077 usecase->in_snd_device,
1078 &num_devices, new_snd_devices)) {
1079 new_snd_devices[0] = usecase->in_snd_device;
1080 num_devices = 1;
1081 }
1082 break;
1083 default:
1084 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1085 rc = 0;
1086 break;
1087 }
1088
1089 for (i = 0; i < num_devices; i++) {
1090 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1091 if (rc)
1092 break;
1093 }
1094
1095 return rc;
1096}
1097
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301098int read_line_from_file(const char *path, char *buf, size_t count)
1099{
1100 char * fgets_ret;
1101 FILE * fd;
1102 int rv;
1103
1104 fd = fopen(path, "r");
1105 if (fd == NULL)
1106 return -1;
1107
1108 fgets_ret = fgets(buf, (int)count, fd);
1109 if (NULL != fgets_ret) {
1110 rv = (int)strlen(buf);
1111 } else {
1112 rv = ferror(fd);
1113 }
1114 fclose(fd);
1115
1116 return rv;
1117}
1118
Ashish Jainf1eaa582016-05-23 20:54:24 +05301119/*Translates ALSA formats to AOSP PCM formats*/
1120audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1121{
1122 audio_format_t format;
1123
1124 switch(alsa_format) {
1125 case SNDRV_PCM_FORMAT_S16_LE:
1126 format = AUDIO_FORMAT_PCM_16_BIT;
1127 break;
1128 case SNDRV_PCM_FORMAT_S24_3LE:
1129 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1130 break;
1131 case SNDRV_PCM_FORMAT_S24_LE:
1132 format = AUDIO_FORMAT_PCM_8_24_BIT;
1133 break;
1134 case SNDRV_PCM_FORMAT_S32_LE:
1135 format = AUDIO_FORMAT_PCM_32_BIT;
1136 break;
1137 default:
1138 ALOGW("Incorrect ALSA format");
1139 format = AUDIO_FORMAT_INVALID;
1140 }
1141 return format;
1142}
1143
1144/*Translates hal format (AOSP) to alsa formats*/
1145uint32_t hal_format_to_alsa(audio_format_t hal_format)
1146{
1147 uint32_t alsa_format;
1148
1149 switch (hal_format) {
1150 case AUDIO_FORMAT_PCM_32_BIT: {
1151 if (platform_supports_true_32bit())
1152 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1153 else
1154 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1155 }
1156 break;
1157 case AUDIO_FORMAT_PCM_8_BIT:
1158 alsa_format = SNDRV_PCM_FORMAT_S8;
1159 break;
1160 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1161 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1162 break;
1163 case AUDIO_FORMAT_PCM_8_24_BIT: {
1164 if (platform_supports_true_32bit())
1165 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1166 else
1167 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1168 }
1169 break;
1170 case AUDIO_FORMAT_PCM_FLOAT:
1171 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1172 break;
1173 default:
1174 case AUDIO_FORMAT_PCM_16_BIT:
1175 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1176 break;
1177 }
1178 return alsa_format;
1179}
1180
Ashish Jain83a6cc22016-06-28 14:34:17 +05301181/*Translates PCM formats to AOSP formats*/
1182audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1183{
1184 audio_format_t format = AUDIO_FORMAT_INVALID;
1185
1186 switch(pcm_format) {
1187 case PCM_FORMAT_S16_LE:
1188 format = AUDIO_FORMAT_PCM_16_BIT;
1189 break;
1190 case PCM_FORMAT_S24_3LE:
1191 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1192 break;
1193 case PCM_FORMAT_S24_LE:
1194 format = AUDIO_FORMAT_PCM_8_24_BIT;
1195 break;
1196 case PCM_FORMAT_S32_LE:
1197 format = AUDIO_FORMAT_PCM_32_BIT;
1198 break;
1199 default:
1200 ALOGW("Incorrect PCM format");
1201 format = AUDIO_FORMAT_INVALID;
1202 }
1203 return format;
1204}
1205
1206/*Translates hal format (AOSP) to alsa formats*/
1207uint32_t hal_format_to_pcm(audio_format_t hal_format)
1208{
1209 uint32_t pcm_format;
1210
1211 switch (hal_format) {
1212 case AUDIO_FORMAT_PCM_32_BIT:
1213 case AUDIO_FORMAT_PCM_8_24_BIT:
1214 case AUDIO_FORMAT_PCM_FLOAT: {
1215 if (platform_supports_true_32bit())
1216 pcm_format = PCM_FORMAT_S32_LE;
1217 else
1218 pcm_format = PCM_FORMAT_S24_3LE;
1219 }
1220 break;
1221 case AUDIO_FORMAT_PCM_8_BIT:
1222 pcm_format = PCM_FORMAT_S8;
1223 break;
1224 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1225 pcm_format = PCM_FORMAT_S24_3LE;
1226 break;
1227 default:
1228 case AUDIO_FORMAT_PCM_16_BIT:
1229 pcm_format = PCM_FORMAT_S16_LE;
1230 break;
1231 }
1232 return pcm_format;
1233}
1234
Ashish Jainf1eaa582016-05-23 20:54:24 +05301235uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1236 uint32_t sample_rate,
1237 uint32_t noOfChannels)
1238{
1239 uint32_t fragment_size = 0;
1240 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1241
1242 fragment_size = (pcm_offload_time
1243 * sample_rate
1244 * bytes_per_sample
1245 * noOfChannels)/1000;
1246 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1247 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1248 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1249 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1250 /*To have same PCM samples for all channels, the buffer size requires to
1251 *be multiple of (number of channels * bytes per sample)
1252 *For writes to succeed, the buffer must be written at address which is multiple of 32
1253 */
Aniket Kumar Lataf0531e02017-09-07 15:14:46 -07001254 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels));
1255 fragment_size = ALIGN(fragment_size, 32);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301256
1257 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1258 return fragment_size;
1259}
1260
1261/* Calculates the fragment size required to configure compress session.
1262 * Based on the alsa format selected, decide if conversion is needed in
1263
1264 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1265 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1266 */
1267void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1268{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301269 audio_format_t dst_format = out->hal_op_format;
1270 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301271 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1272 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1273
1274 out->compr_config.fragment_size =
1275 get_alsa_fragment_size(hal_op_bytes_per_sample,
1276 out->sample_rate,
1277 popcount(out->channel_mask));
1278
1279 if ((src_format != dst_format) &&
1280 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1281
Ashish Jain83a6cc22016-06-28 14:34:17 +05301282 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301283 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1284 hal_op_bytes_per_sample);
1285 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301286 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301287 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301288 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301289 }
1290}
1291
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301292/* converts pcm format 24_8 to 8_24 inplace */
1293size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1294{
1295 size_t i = 0;
1296 int *int_buf_stream = buf;
1297
1298 if ((bytes % 4) != 0) {
1299 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1300 return -EINVAL;
1301 }
1302
1303 for (; i < (bytes / 4); i++)
1304 int_buf_stream[i] >>= 8;
1305
1306 return bytes;
1307}
1308
1309int get_snd_codec_id(audio_format_t format)
1310{
1311 int id = 0;
1312
1313 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1314 case AUDIO_FORMAT_MP3:
1315 id = SND_AUDIOCODEC_MP3;
1316 break;
1317 case AUDIO_FORMAT_AAC:
1318 id = SND_AUDIOCODEC_AAC;
1319 break;
1320 case AUDIO_FORMAT_AAC_ADTS:
1321 id = SND_AUDIOCODEC_AAC;
1322 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301323 case AUDIO_FORMAT_AAC_LATM:
1324 id = SND_AUDIOCODEC_AAC;
1325 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301326 case AUDIO_FORMAT_PCM:
1327 id = SND_AUDIOCODEC_PCM;
1328 break;
1329 case AUDIO_FORMAT_FLAC:
1330 id = SND_AUDIOCODEC_FLAC;
1331 break;
1332 case AUDIO_FORMAT_ALAC:
1333 id = SND_AUDIOCODEC_ALAC;
1334 break;
1335 case AUDIO_FORMAT_APE:
1336 id = SND_AUDIOCODEC_APE;
1337 break;
1338 case AUDIO_FORMAT_VORBIS:
1339 id = SND_AUDIOCODEC_VORBIS;
1340 break;
1341 case AUDIO_FORMAT_WMA:
1342 id = SND_AUDIOCODEC_WMA;
1343 break;
1344 case AUDIO_FORMAT_WMA_PRO:
1345 id = SND_AUDIOCODEC_WMA_PRO;
1346 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301347 case AUDIO_FORMAT_MP2:
1348 id = SND_AUDIOCODEC_MP2;
1349 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301350 case AUDIO_FORMAT_AC3:
1351 id = SND_AUDIOCODEC_AC3;
1352 break;
1353 case AUDIO_FORMAT_E_AC3:
1354 case AUDIO_FORMAT_E_AC3_JOC:
1355 id = SND_AUDIOCODEC_EAC3;
1356 break;
1357 case AUDIO_FORMAT_DTS:
1358 case AUDIO_FORMAT_DTS_HD:
1359 id = SND_AUDIOCODEC_DTS;
1360 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001361 case AUDIO_FORMAT_DOLBY_TRUEHD:
1362 id = SND_AUDIOCODEC_TRUEHD;
1363 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001364 case AUDIO_FORMAT_IEC61937:
1365 id = SND_AUDIOCODEC_IEC61937;
1366 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301367 case AUDIO_FORMAT_DSD:
1368 id = SND_AUDIOCODEC_DSD;
1369 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301370 case AUDIO_FORMAT_APTX:
1371 id = SND_AUDIOCODEC_APTX;
1372 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301373 default:
1374 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1375 }
1376
1377 return id;
1378}
1379
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001380void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1381 struct audio_usecase *usecase)
1382{
1383 int type = usecase->type;
1384
Dhananjay Kumar14245982017-01-16 20:21:00 +05301385 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001386 struct stream_out *out = usecase->stream.out;
1387 int snd_device = usecase->out_snd_device;
1388 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001389 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301390 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001391 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301392 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301393 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301394 platform_send_audio_calibration(adev->platform, usecase,
1395 usecase->stream.in->app_type_cfg.app_type,
1396 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001397 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301398 /* when app type is default. the sample rate is not used to send cal */
1399 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301400 platform_get_default_app_type_v2(adev->platform, usecase->type),
1401 48000);
Siddartha Shaik343abc62017-08-08 11:15:25 +05301402 } else if (type == TRANSCODE_LOOPBACK && usecase->stream.inout != NULL) {
1403 int snd_device = usecase->out_snd_device;
1404 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1405 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1406 platform_send_audio_calibration(adev->platform, usecase,
1407 platform_get_default_app_type_v2(adev->platform, usecase->type),
1408 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001409 } else {
1410 /* No need to send audio calibration for voice and voip call usecases */
1411 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1412 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001413 }
1414}
1415
Ben Rombergera04fabc2014-11-14 12:16:03 -08001416// Base64 Encode and Decode
1417// Not all features supported. This must be used only with following conditions.
1418// Decode Modes: Support with and without padding
1419// CRLF not handling. So no CRLF in string to decode.
1420// Encode Modes: Supports only padding
1421int b64decode(char *inp, int ilen, uint8_t* outp)
1422{
1423 int i, j, k, ii, num;
1424 int rem, pcnt;
1425 uint32_t res=0;
1426 uint8_t getIndex[MAX_BASEINDEX_LEN];
1427 uint8_t tmp, cflag;
1428
1429 if(inp == NULL || outp == NULL || ilen <= 0) {
1430 ALOGE("[%s] received NULL pointer or zero length",__func__);
1431 return -1;
1432 }
1433
1434 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1435 for(i=0;i<BASE_TABLE_SIZE;i++) {
1436 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1437 }
1438 getIndex[(uint8_t)'=']=0;
1439
1440 j=0;k=0;
1441 num = ilen/4;
1442 rem = ilen%4;
1443 if(rem==0)
1444 num = num-1;
1445 cflag=0;
1446 for(i=0; i<num; i++) {
1447 res=0;
1448 for(ii=0;ii<4;ii++) {
1449 res = res << 6;
1450 tmp = getIndex[(uint8_t)inp[j++]];
1451 res = res | tmp;
1452 cflag = cflag | tmp;
1453 }
1454 outp[k++] = (res >> 16)&0xFF;
1455 outp[k++] = (res >> 8)&0xFF;
1456 outp[k++] = res & 0xFF;
1457 }
1458
1459 // Handle last bytes special
1460 pcnt=0;
1461 if(rem == 0) {
1462 //With padding or full data
1463 res = 0;
1464 for(ii=0;ii<4;ii++) {
1465 if(inp[j] == '=')
1466 pcnt++;
1467 res = res << 6;
1468 tmp = getIndex[(uint8_t)inp[j++]];
1469 res = res | tmp;
1470 cflag = cflag | tmp;
1471 }
1472 outp[k++] = res >> 16;
1473 if(pcnt == 2)
1474 goto done;
1475 outp[k++] = (res>>8)&0xFF;
1476 if(pcnt == 1)
1477 goto done;
1478 outp[k++] = res&0xFF;
1479 } else {
1480 //without padding
1481 res = 0;
1482 for(i=0;i<rem;i++) {
1483 res = res << 6;
1484 tmp = getIndex[(uint8_t)inp[j++]];
1485 res = res | tmp;
1486 cflag = cflag | tmp;
1487 }
1488 for(i=rem;i<4;i++) {
1489 res = res << 6;
1490 pcnt++;
1491 }
1492 outp[k++] = res >> 16;
1493 if(pcnt == 2)
1494 goto done;
1495 outp[k++] = (res>>8)&0xFF;
1496 if(pcnt == 1)
1497 goto done;
1498 outp[k++] = res&0xFF;
1499 }
1500done:
1501 if(cflag == 0xFF) {
1502 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1503 __func__, inp);
1504 return 0;
1505 }
1506 return k;
1507}
1508
1509int b64encode(uint8_t *inp, int ilen, char* outp)
1510{
1511 int i,j,k, num;
1512 int rem=0;
1513 uint32_t res=0;
1514
1515 if(inp == NULL || outp == NULL || ilen<=0) {
1516 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1517 return -1;
1518 }
1519
1520 num = ilen/3;
1521 rem = ilen%3;
1522 j=0;k=0;
1523 for(i=0; i<num; i++) {
1524 //prepare index
1525 res = inp[j++]<<16;
1526 res = res | inp[j++]<<8;
1527 res = res | inp[j++];
1528 //get output map from index
1529 outp[k++] = (char) bTable[(res>>18)&0x3F];
1530 outp[k++] = (char) bTable[(res>>12)&0x3F];
1531 outp[k++] = (char) bTable[(res>>6)&0x3F];
1532 outp[k++] = (char) bTable[res&0x3F];
1533 }
1534
1535 switch(rem) {
1536 case 1:
1537 res = inp[j++]<<16;
1538 outp[k++] = (char) bTable[res>>18];
1539 outp[k++] = (char) bTable[(res>>12)&0x3F];
1540 //outp[k++] = '=';
1541 //outp[k++] = '=';
1542 break;
1543 case 2:
1544 res = inp[j++]<<16;
1545 res = res | inp[j++]<<8;
1546 outp[k++] = (char) bTable[res>>18];
1547 outp[k++] = (char) bTable[(res>>12)&0x3F];
1548 outp[k++] = (char) bTable[(res>>6)&0x3F];
1549 //outp[k++] = '=';
1550 break;
1551 default:
1552 break;
1553 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001554 outp[k] = '\0';
1555 return k;
1556}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301557
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301558
1559int audio_extn_utils_get_codec_version(const char *snd_card_name,
1560 int card_num,
1561 char *codec_version)
1562{
1563 char procfs_path[50];
1564 FILE *fp;
1565
1566 if (strstr(snd_card_name, "tasha")) {
1567 snprintf(procfs_path, sizeof(procfs_path),
1568 "/proc/asound/card%d/codecs/tasha/version", card_num);
1569 if ((fp = fopen(procfs_path, "r")) != NULL) {
1570 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1571 fclose(fp);
1572 } else {
1573 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1574 return -ENOENT;
1575 }
1576 ALOGD("%s: codec version %s", __func__, codec_version);
1577 }
1578
1579 return 0;
1580}
1581
1582
Ashish Jain81eb2a82015-05-13 10:52:34 +05301583#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1584
1585void get_default_compressed_channel_status(
1586 unsigned char *channel_status)
1587{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301588 memset(channel_status,0,24);
1589
1590 /* block start bit in preamble bit 3 */
1591 channel_status[0] |= PROFESSIONAL;
1592 //compre out
1593 channel_status[0] |= NON_LPCM;
1594 // sample rate; fixed 48K for default/transcode
1595 channel_status[3] |= SR_48000;
1596}
1597
1598#ifdef HDMI_PASSTHROUGH_ENABLED
1599int32_t get_compressed_channel_status(void *audio_stream_data,
1600 uint32_t audio_frame_size,
1601 unsigned char *channel_status,
1602 enum audio_parser_code_type codec_type)
1603 // codec_type - AUDIO_PARSER_CODEC_AC3
1604 // - AUDIO_PARSER_CODEC_DTS
1605{
1606 unsigned char *stream;
1607 int ret = 0;
1608 stream = (unsigned char *)audio_stream_data;
1609
1610 if (audio_stream_data == NULL || audio_frame_size == 0) {
1611 ALOGW("no buffer to get channel status, return default for compress");
1612 get_default_compressed_channel_status(channel_status);
1613 return ret;
1614 }
1615
1616 memset(channel_status,0,24);
1617 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1618 {
1619 ALOGE("init audio parser failed");
1620 return -1;
1621 }
1622 ret = get_channel_status(channel_status, codec_type);
1623 return ret;
1624
1625}
1626
1627#endif
1628
1629void get_lpcm_channel_status(uint32_t sampleRate,
1630 unsigned char *channel_status)
1631{
1632 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301633 memset(channel_status,0,24);
1634 /* block start bit in preamble bit 3 */
1635 channel_status[0] |= PROFESSIONAL;
1636 //LPCM OUT
1637 channel_status[0] &= ~NON_LPCM;
1638
1639 switch (sampleRate) {
1640 case 8000:
1641 case 11025:
1642 case 12000:
1643 case 16000:
1644 case 22050:
1645 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301646 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301647 case 24000:
1648 channel_status[3] |= SR_24000;
1649 break;
1650 case 32000:
1651 channel_status[3] |= SR_32000;
1652 break;
1653 case 44100:
1654 channel_status[3] |= SR_44100;
1655 break;
1656 case 48000:
1657 channel_status[3] |= SR_48000;
1658 break;
1659 case 88200:
1660 channel_status[3] |= SR_88200;
1661 break;
1662 case 96000:
1663 channel_status[3] |= SR_96000;
1664 break;
1665 case 176400:
1666 channel_status[3] |= SR_176400;
1667 break;
1668 case 192000:
1669 channel_status[3] |= SR_192000;
1670 break;
1671 default:
1672 ALOGV("Invalid sample_rate %u\n", sampleRate);
1673 status = -1;
1674 break;
1675 }
1676}
1677
1678void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1679{
1680 unsigned char channel_status[24]={0};
1681 struct snd_aes_iec958 iec958;
1682 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1683 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301684 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301685#ifdef HDMI_PASSTHROUGH_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301686 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301687 /*TODO:Extend code to support DTS passthrough*/
1688 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301689 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301690 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1691 } else
1692#endif
1693 {
1694 /*set channel status bit for LPCM*/
1695 get_lpcm_channel_status(out->sample_rate, channel_status);
1696 }
1697
1698 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1699 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1700 if (!ctl) {
1701 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1702 __func__, mixer_ctl_name);
1703 return;
1704 }
1705 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1706 ALOGE("%s: Could not set channel status for ext HDMI ",
1707 __func__);
1708 return;
1709 }
1710
1711}
1712#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301713
1714int audio_extn_utils_get_avt_device_drift(
1715 struct audio_usecase *usecase,
1716 struct audio_avt_device_drift_param *drift_param)
1717{
1718 int ret = 0, count = 0;
1719 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301720 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301721 struct mixer_ctl *ctl = NULL;
1722 struct audio_avt_device_drift_stats drift_stats;
1723 struct audio_device *adev = NULL;
1724
1725 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301726 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1727 if (!backend) {
1728 ALOGE("%s: Unsupported device %d", __func__,
1729 usecase->stream.out->devices);
1730 ret = -EINVAL;
1731 goto done;
1732 }
1733 strlcpy(avt_device_drift_mixer_ctl_name,
1734 backend,
1735 MIXER_PATH_MAX_LENGTH);
1736
1737 count = strlen(backend);
1738 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1739 strlcat(&avt_device_drift_mixer_ctl_name[count],
1740 " DRIFT",
1741 MIXER_PATH_MAX_LENGTH - count);
1742 } else {
1743 ret = -EINVAL;
1744 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301745 }
1746 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301747 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301748 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301749 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301750 }
1751
Naresh Tanniru6160c712017-04-17 15:43:48 +05301752 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301753 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1754 if (!ctl) {
1755 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1756 __func__, avt_device_drift_mixer_ctl_name);
1757
1758 ret = -EINVAL;
1759 goto done;
1760 }
1761
1762 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1763 avt_device_drift_mixer_ctl_name);
1764
1765 mixer_ctl_update(ctl);
1766 count = mixer_ctl_get_num_values(ctl);
1767 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1768 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1769 __func__);
1770
1771 ret = -EINVAL;
1772 goto done;
1773 }
1774
1775 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1776 if (ret != 0) {
1777 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1778 __func__);
1779
1780 ret = -EINVAL;
1781 goto done;
1782 }
1783 memcpy(drift_param, &drift_stats.drift_param,
1784 sizeof(struct audio_avt_device_drift_param));
1785done:
1786 return ret;
1787}
Manish Dewangan07de2142017-02-27 19:27:20 +05301788
1789#ifdef SNDRV_COMPRESS_PATH_DELAY
1790int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1791{
1792 int ret = -EINVAL;
1793 struct snd_compr_metadata metadata;
1794 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1795
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001796 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301797 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1798 if (!(is_offload_usecase(out->usecase))) {
1799 ALOGE("%s:: not supported for non offload session", __func__);
1800 goto exit;
1801 }
1802
1803 if (!out->compr) {
1804 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1805 __func__);
1806 goto exit;
1807 }
1808
1809 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1810 ret = compress_get_metadata(out->compr, &metadata);
1811 if(ret) {
1812 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1813 goto exit;
1814 }
1815 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1816 } else {
1817 ALOGD("%s:: Using Fix DSP delay",__func__);
1818 }
1819
1820exit:
1821 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1822 return delay_ms;
1823}
1824#else
1825int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1826{
1827 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1828}
1829#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301830
1831#ifdef SNDRV_COMPRESS_RENDER_MODE
1832int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1833{
1834 struct snd_compr_metadata metadata;
1835 int ret = -EINVAL;
1836
1837 if (!(is_offload_usecase(out->usecase))) {
1838 ALOGE("%s:: not supported for non offload session", __func__);
1839 goto exit;
1840 }
1841
1842 if (!out->compr) {
1843 ALOGD("%s:: Invalid compress handle",
1844 __func__);
1845 goto exit;
1846 }
1847
1848 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1849
1850 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1851 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1852 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1853 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1854 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1855 } else {
1856 ret = 0;
1857 goto exit;
1858 }
1859 ret = compress_set_metadata(out->compr, &metadata);
1860 if(ret) {
1861 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1862 }
1863exit:
1864 return ret;
1865}
1866#else
1867int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1868{
1869 ALOGD("%s:: configuring render mode not supported", __func__);
1870 return 0;
1871}
1872#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301873
1874#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1875int audio_extn_utils_compress_set_clk_rec_mode(
1876 struct audio_usecase *usecase)
1877{
1878 struct snd_compr_metadata metadata;
1879 struct stream_out *out = NULL;
1880 int ret = -EINVAL;
1881
Naresh Tanniru7586e292017-04-13 10:38:13 +05301882 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301883 ALOGE("%s:: Invalid use case", __func__);
1884 goto exit;
1885 }
1886
1887 out = usecase->stream.out;
1888 if (!out) {
1889 ALOGE("%s:: invalid stream", __func__);
1890 goto exit;
1891 }
1892
1893 if (!is_offload_usecase(out->usecase)) {
1894 ALOGE("%s:: not supported for non offload session", __func__);
1895 goto exit;
1896 }
1897
1898 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1899 ALOGD("%s:: clk recovery is only supported in STC render mode",
1900 __func__);
1901 ret = 0;
1902 goto exit;
1903 }
1904
1905 if (!out->compr) {
1906 ALOGD("%s:: Invalid compress handle",
1907 __func__);
1908 goto exit;
1909 }
1910 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1911 switch(usecase->out_snd_device) {
1912 case SND_DEVICE_OUT_HDMI:
1913 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1914 case SND_DEVICE_OUT_DISPLAY_PORT:
1915 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1916 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1917 break;
1918 default:
1919 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1920 break;
1921 }
1922
1923 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1924
1925 ret = compress_set_metadata(out->compr, &metadata);
1926 if(ret) {
1927 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1928 }
1929
1930exit:
1931 return ret;
1932}
1933#else
1934int audio_extn_utils_compress_set_clk_rec_mode(
1935 struct audio_usecase *usecase __unused)
1936{
1937 ALOGD("%s:: configuring render mode not supported", __func__);
1938 return 0;
1939}
1940#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301941
1942#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1943int audio_extn_utils_compress_set_render_window(
1944 struct stream_out *out,
1945 struct audio_out_render_window_param *render_window)
1946{
1947 struct snd_compr_metadata metadata;
1948 int ret = -EINVAL;
1949
Manish Dewangan27346042017-03-01 12:56:12 +05301950 if(render_window == NULL) {
1951 ALOGE("%s:: Invalid render_window", __func__);
1952 goto exit;
1953 }
1954
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001955 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1956 __func__,render_window->render_ws, render_window->render_we);
1957
Manish Dewangan27346042017-03-01 12:56:12 +05301958 if (!is_offload_usecase(out->usecase)) {
1959 ALOGE("%s:: not supported for non offload session", __func__);
1960 goto exit;
1961 }
1962
Naresh Tanniru6160c712017-04-17 15:43:48 +05301963 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1964 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301965 ALOGD("%s:: only supported in timestamp mode, current "
1966 "render mode mode %d", __func__, out->render_mode);
1967 goto exit;
1968 }
1969
1970 if (!out->compr) {
1971 ALOGW("%s:: offload session not yet opened,"
1972 "render window will be configure later", __func__);
1973 /* store render window to reconfigure in start_output_stream() */
1974 goto exit;
1975 }
1976
1977 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1978 /*render window start value */
1979 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1980 metadata.value[1] = \
1981 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1982 /*render window end value */
1983 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1984 metadata.value[3] = \
1985 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1986
1987 ret = compress_set_metadata(out->compr, &metadata);
1988 if(ret) {
1989 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1990 }
1991
1992exit:
1993 return ret;
1994}
1995#else
1996int audio_extn_utils_compress_set_render_window(
1997 struct stream_out *out __unused,
1998 struct audio_out_render_window_param *render_window __unused)
1999{
2000 ALOGD("%s:: configuring render window not supported", __func__);
2001 return 0;
2002}
2003#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302004
2005#ifdef SNDRV_COMPRESS_START_DELAY
2006int audio_extn_utils_compress_set_start_delay(
2007 struct stream_out *out,
2008 struct audio_out_start_delay_param *delay_param)
2009{
2010 struct snd_compr_metadata metadata;
2011 int ret = -EINVAL;
2012
2013 if(delay_param == NULL) {
2014 ALOGE("%s:: Invalid delay_param", __func__);
2015 goto exit;
2016 }
2017
2018 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2019 delay_param->start_delay);
2020
2021 if (!is_offload_usecase(out->usecase)) {
2022 ALOGE("%s:: not supported for non offload session", __func__);
2023 goto exit;
2024 }
2025
Naresh Tanniru6160c712017-04-17 15:43:48 +05302026 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2027 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302028 ALOGD("%s:: only supported in timestamp mode, current "
2029 "render mode mode %d", __func__, out->render_mode);
2030 goto exit;
2031 }
2032
2033 if (!out->compr) {
2034 ALOGW("%s:: offload session not yet opened,"
2035 "start delay will be configure later", __func__);
2036 goto exit;
2037 }
2038
2039 metadata.key = SNDRV_COMPRESS_START_DELAY;
2040 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2041 metadata.value[1] = \
2042 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2043
2044 ret = compress_set_metadata(out->compr, &metadata);
2045 if(ret) {
2046 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2047 }
2048
2049exit:
2050 return ret;
2051}
2052#else
2053int audio_extn_utils_compress_set_start_delay(
2054 struct stream_out *out __unused,
2055 struct audio_out_start_delay_param *delay_param __unused)
2056{
2057 ALOGD("%s:: configuring render window not supported", __func__);
2058 return 0;
2059}
2060#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002061
2062#define MAX_SND_CARD 8
2063#define RETRY_US 500000
2064#define RETRY_NUMBER 10
2065
2066int audio_extn_utils_get_snd_card_num()
2067{
2068
2069 void *hw_info = NULL;
2070 struct mixer *mixer = NULL;
2071 int retry_num = 0;
2072 int snd_card_num = 0;
2073 char* snd_card_name = NULL;
2074
2075 while (snd_card_num < MAX_SND_CARD) {
2076 mixer = mixer_open(snd_card_num);
2077
2078 while (!mixer && retry_num < RETRY_NUMBER) {
2079 usleep(RETRY_US);
2080 mixer = mixer_open(snd_card_num);
2081 retry_num++;
2082 }
2083
2084 if (!mixer) {
2085 ALOGE("%s: Unable to open the mixer card: %d", __func__,
2086 snd_card_num);
2087 retry_num = 0;
2088 snd_card_num++;
2089 continue;
2090 }
2091
2092 snd_card_name = strdup(mixer_get_name(mixer));
2093 if (!snd_card_name) {
2094 ALOGE("failed to allocate memory for snd_card_name\n");
2095 mixer_close(mixer);
2096 return -1;
2097 }
2098 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2099
2100 hw_info = hw_info_init(snd_card_name);
2101 if (hw_info) {
2102 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2103 break;
2104 }
2105 ALOGE("%s: Failed to init hardware info", __func__);
2106 retry_num = 0;
2107 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002108
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302109 free(snd_card_name);
2110 snd_card_name = NULL;
2111
2112 mixer_close(mixer);
2113 mixer = NULL;
2114 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002115 if (snd_card_name)
2116 free(snd_card_name);
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302117 if (mixer)
2118 mixer_close(mixer);
2119 if (hw_info)
2120 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002121
2122 if (snd_card_num >= MAX_SND_CARD) {
2123 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2124 return -1;
2125 }
2126
2127 return snd_card_num;
2128}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302129
2130#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2131int audio_extn_utils_compress_enable_drift_correction(
2132 struct stream_out *out,
2133 struct audio_out_enable_drift_correction *drift)
2134{
2135 struct snd_compr_metadata metadata;
2136 int ret = -EINVAL;
2137
2138 if(drift == NULL) {
2139 ALOGE("%s:: Invalid param", __func__);
2140 goto exit;
2141 }
2142
2143 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2144
2145 if (!is_offload_usecase(out->usecase)) {
2146 ALOGE("%s:: not supported for non offload session", __func__);
2147 goto exit;
2148 }
2149
2150 if (!out->compr) {
2151 ALOGW("%s:: offload session not yet opened,"
2152 "start delay will be configure later", __func__);
2153 goto exit;
2154 }
2155
2156 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2157 metadata.value[0] = drift->enable;
2158 out->drift_correction_enabled = drift->enable;
2159
2160 ret = compress_set_metadata(out->compr, &metadata);
2161 if(ret) {
2162 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2163 out->drift_correction_enabled = false;
2164 }
2165
2166exit:
2167 return ret;
2168}
2169#else
2170int audio_extn_utils_compress_enable_drift_correction(
2171 struct stream_out *out __unused,
2172 struct audio_out_enable_drift_correction *drift __unused)
2173{
2174 ALOGD("%s:: configuring drift enablement not supported", __func__);
2175 return 0;
2176}
2177#endif
2178
2179#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2180int audio_extn_utils_compress_correct_drift(
2181 struct stream_out *out,
2182 struct audio_out_correct_drift *drift_param)
2183{
2184 struct snd_compr_metadata metadata;
2185 int ret = -EINVAL;
2186
2187 if (drift_param == NULL) {
2188 ALOGE("%s:: Invalid drift_param", __func__);
2189 goto exit;
2190 }
2191
2192 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2193 drift_param->adjust_time);
2194
2195 if (!is_offload_usecase(out->usecase)) {
2196 ALOGE("%s:: not supported for non offload session", __func__);
2197 goto exit;
2198 }
2199
2200 if (!out->compr) {
2201 ALOGW("%s:: offload session not yet opened", __func__);
2202 goto exit;
2203 }
2204
2205 if (!out->drift_correction_enabled) {
2206 ALOGE("%s:: drift correction not enabled", __func__);
2207 goto exit;
2208 }
2209
2210 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2211 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2212 metadata.value[1] = \
2213 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2214
2215 ret = compress_set_metadata(out->compr, &metadata);
2216 if(ret)
2217 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2218exit:
2219 return ret;
2220}
2221#else
2222int audio_extn_utils_compress_correct_drift(
2223 struct stream_out *out __unused,
2224 struct audio_out_correct_drift *drift_param __unused)
2225{
2226 ALOGD("%s:: setting adjust clock not supported", __func__);
2227 return 0;
2228}
2229#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302230
2231int audio_extn_utils_set_channel_map(
2232 struct stream_out *out,
2233 struct audio_out_channel_map_param *channel_map_param)
2234{
2235 int ret = -EINVAL, i = 0;
2236 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2237
2238 if (channel_map_param == NULL) {
2239 ALOGE("%s:: Invalid channel_map", __func__);
2240 goto exit;
2241 }
2242
2243 if (channel_map_param->channels != channels) {
2244 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2245 __func__, channel_map_param->channels, channels);
2246 goto exit;
2247 }
2248
2249 for ( i = 0; i < channels; i++) {
2250 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2251 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2252 }
2253 ret = 0;
2254exit:
2255 return ret;
2256}
Varun Balaraje49253e2017-07-06 19:48:56 +05302257
2258int audio_extn_utils_set_pan_scale_params(
2259 struct stream_out *out,
2260 struct mix_matrix_params *mm_params)
2261{
2262 int ret = -EINVAL, i = 0, j = 0;
2263
Varun Balaraj003ee752017-08-07 17:54:55 +05302264 if (mm_params == NULL || out == NULL) {
2265 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302266 goto exit;
2267 }
2268
2269 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2270 mm_params->num_output_channels <= 0 ||
2271 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2272 mm_params->num_input_channels <= 0)
2273 goto exit;
2274
2275 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2276 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2277 out->pan_scale_params.has_output_channel_map =
2278 mm_params->has_output_channel_map;
2279 for (i = 0; i < mm_params->num_output_channels; i++)
2280 out->pan_scale_params.output_channel_map[i] =
2281 mm_params->output_channel_map[i];
2282
2283 out->pan_scale_params.has_input_channel_map =
2284 mm_params->has_input_channel_map;
2285 for (i = 0; i < mm_params->num_input_channels; i++)
2286 out->pan_scale_params.input_channel_map[i] =
2287 mm_params->input_channel_map[i];
2288
2289 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2290 for (i = 0; i < mm_params->num_output_channels; i++)
2291 for (j = 0; j < mm_params->num_input_channels; j++) {
2292 //Convert the channel coefficient gains in Q14 format
2293 out->pan_scale_params.mixer_coeffs[i][j] =
2294 mm_params->mixer_coeffs[i][j] * (2 << 13);
2295 }
2296
2297 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2298 out->pcm_device_id,
2299 out->pan_scale_params);
2300
2301exit:
2302 return ret;
2303}
2304
2305int audio_extn_utils_set_downmix_params(
2306 struct stream_out *out,
2307 struct mix_matrix_params *mm_params)
2308{
2309 int ret = -EINVAL, i = 0, j = 0;
2310 struct audio_usecase *usecase = NULL;
2311
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002312 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302313 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302314 goto exit;
2315 }
2316
2317 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2318 mm_params->num_output_channels <= 0 ||
2319 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2320 mm_params->num_input_channels <= 0)
2321 goto exit;
2322
2323 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302324 if (!usecase) {
2325 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002326 goto exit;
2327 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302328 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2329 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2330
2331 out->downmix_params.has_output_channel_map =
2332 mm_params->has_output_channel_map;
2333 for (i = 0; i < mm_params->num_output_channels; i++) {
2334 out->downmix_params.output_channel_map[i] =
2335 mm_params->output_channel_map[i];
2336 }
2337
2338 out->downmix_params.has_input_channel_map =
2339 mm_params->has_input_channel_map;
2340 for (i = 0; i < mm_params->num_input_channels; i++)
2341 out->downmix_params.input_channel_map[i] =
2342 mm_params->input_channel_map[i];
2343
2344 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2345 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302346 for (j = 0; j < mm_params->num_input_channels; j++) {
2347 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302348 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302349 mm_params->mixer_coeffs[i][j] * (2 << 13);
2350 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302351
2352 ret = platform_set_stream_downmix_params(out->dev->platform,
2353 out->pcm_device_id,
2354 usecase->out_snd_device,
2355 out->downmix_params);
2356
2357exit:
2358 return ret;
2359}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302360
2361bool audio_extn_utils_is_dolby_format(audio_format_t format)
2362{
2363 if (format == AUDIO_FORMAT_AC3 ||
2364 format == AUDIO_FORMAT_E_AC3 ||
2365 format == AUDIO_FORMAT_E_AC3_JOC)
2366 return true;
2367 else
2368 return false;
2369}
2370
`Deeraj Soman676c2702017-09-18 19:25:53 +05302371int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2372{
2373 int i;
2374 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2375 {"S32_LE", 32},
2376 {"S24_LE", 24},
2377 {"S16_LE", 16}};
2378 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302379
`Deeraj Soman676c2702017-09-18 19:25:53 +05302380 for (i = 0; i < num_configs; i++) {
2381 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2382 return mixer_bitwidth_config[i].value;
2383 }
2384
2385 return -EINVAL;
2386}
2387
2388int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2389{
2390 int i;
2391 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2392 {"KHZ_48", 48000},
2393 {"KHZ_96", 96000},
2394 {"KHZ_144", 144000},
2395 {"KHZ_192", 192000},
2396 {"KHZ_384", 384000},
2397 {"KHZ_44P1", 44100},
2398 {"KHZ_88P2", 88200},
2399 {"KHZ_176P4", 176400},
2400 {"KHZ_352P8", 352800}};
2401 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2402
2403 for (i = 0; i < num_configs; i++) {
2404 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2405 return mixer_samplerate_config[i].value;
2406 }
2407
2408 return -EINVAL;
2409}
2410
2411int audio_extn_utils_get_channels_from_string(const char *id_string)
2412{
2413 int i;
2414 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2415 {"Two", 2},
2416 {"Three",3},
2417 {"Four", 4},
2418 {"Five", 5},
2419 {"Six", 6},
2420 {"Seven", 7},
2421 {"Eight", 8}};
2422 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2423
2424 for (i = 0; i < num_configs; i++) {
2425 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2426 return mixer_channels_config[i].value;
2427 }
2428
2429 return -EINVAL;
2430}