blob: 660bd6be4d30b703e17d4b777a7c63d9a3943b1c [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
Ashish Jain81eb2a82015-05-13 10:52:34 +053084#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
85#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
86#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
87#define SR_44100 (0<<0) /* 44.1kHz */
88#define SR_NOTID (1<<0) /* non indicated */
89#define SR_48000 (2<<0) /* 48kHz */
90#define SR_32000 (3<<0) /* 32kHz */
91#define SR_22050 (4<<0) /* 22.05kHz */
92#define SR_24000 (6<<0) /* 24kHz */
93#define SR_88200 (8<<0) /* 88.2kHz */
94#define SR_96000 (10<<0) /* 96kHz */
95#define SR_176400 (12<<0) /* 176.4kHz */
96#define SR_192000 (14<<0) /* 192kHz */
97
98#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053099
100/* ToDo: Check and update a proper value in msec */
101#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
102
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700103struct string_to_enum {
104 const char *name;
105 uint32_t value;
106};
107
108const struct string_to_enum s_flag_name_to_enum_table[] = {
109 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
110 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
111 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800112 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700113 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
114 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
115 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700116 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700117#ifdef INCALL_MUSIC_ENABLED
118 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
119#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700120 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530121 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530122 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
123 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
124 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
125 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
126 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530127 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128};
129
130const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530131 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700132 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530133 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
134 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530135 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700136 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
137 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
138 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700139 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
140 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700141 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700142 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530144 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700145 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530146#ifdef AUDIO_EXTN_FORMATS_ENABLED
147 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700148 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
149 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
150 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700151 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
152 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
153 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
154 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
155 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
156 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
157 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700158 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530159 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
160 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700161 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800162 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
163 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
164 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530165 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530166 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
167 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
168 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530169 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
171 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
172 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
173 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530174 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700175#endif
176};
177
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530178/* payload structure avt_device drift query */
179struct audio_avt_device_drift_stats {
180 uint32_t minor_version;
181 /* Indicates the device interface direction as either
182 * source (Tx) or sink (Rx).
183 */
184 uint16_t device_direction;
185 /*params exposed to client */
186 struct audio_avt_device_drift_param drift_param;
187};
188
Ben Rombergera04fabc2014-11-14 12:16:03 -0800189static char bTable[BASE_TABLE_SIZE] = {
190 'A','B','C','D','E','F','G','H','I','J','K','L',
191 'M','N','O','P','Q','R','S','T','U','V','W','X',
192 'Y','Z','a','b','c','d','e','f','g','h','i','j',
193 'k','l','m','n','o','p','q','r','s','t','u','v',
194 'w','x','y','z','0','1','2','3','4','5','6','7',
195 '8','9','+','/'
196};
197
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700198static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
199 const char *name)
200{
201 size_t i;
202 for (i = 0; i < size; i++) {
203 if (strcmp(table[i].name, name) == 0) {
204 ALOGV("%s found %s", __func__, table[i].name);
205 return table[i].value;
206 }
207 }
208 return 0;
209}
210
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530211static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700212{
213 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530214 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800215 char *last_r;
216 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700217 while (flag_name != NULL) {
218 if (strlen(flag_name) != 0) {
219 flag |= string_to_enum(s_flag_name_to_enum_table,
220 ARRAY_SIZE(s_flag_name_to_enum_table),
221 flag_name);
222 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800223 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700224 }
225
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800226 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530227 io_flags.in_flags = (audio_input_flags_t)flag;
228 io_flags.out_flags = (audio_output_flags_t)flag;
229 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700230}
231
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530232static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700233{
234 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800235 char *last_r;
236 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700237
238 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
239 return;
240
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530241 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700242 while (str != NULL) {
243 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
244 ARRAY_SIZE(s_format_name_to_enum_table), str);
245 ALOGV("%s: format - %d", __func__, format);
246 if (format != 0) {
247 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700248 if (sf_info == NULL)
249 break; /* return whatever was parsed */
250
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530252 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700253 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800254 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255 }
256}
257
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530258static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700259{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700260 struct stream_sample_rate *ss_info = NULL;
261 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800262 char *last_r;
263 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700264
Amit Shekhar6f461b12014-08-01 14:52:58 -0700265 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
266 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700267
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530268 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700269 while (str != NULL) {
270 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
271 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
272 if (0 != sample_rate) {
273 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800274 if (!ss_info) {
275 ALOGE("%s: memory allocation failure", __func__);
276 return;
277 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700278 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530279 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700280 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800281 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700282 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700283}
284
285static int parse_bit_width_names(char *name)
286{
287 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800288 char *last_r;
289 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700290
291 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
292 bit_width = (int)strtol(str, (char **)NULL, 10);
293
294 ALOGV("%s: bit_width - %d", __func__, bit_width);
295 return bit_width;
296}
297
298static int parse_app_type_names(void *platform, char *name)
299{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700300 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800301 char *last_r;
302 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700303
304 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
305 app_type = (int)strtol(str, (char **)NULL, 10);
306
307 ALOGV("%s: app_type - %d", __func__, app_type);
308 return app_type;
309}
310
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530311static void update_streams_cfg_list(cnode *root, void *platform,
312 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700313{
314 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530315 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700316
317 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530318 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700319
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530320 if (!s_info) {
321 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700322 return;
323 }
324
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700325 while (node) {
326 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530327 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530328 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
329 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700330 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530331 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700332 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530333 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
334 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700335 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530336 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700337 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530338 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700339 }
340 node = node->next;
341 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530342 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700343}
344
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530345static void load_cfg_list(cnode *root, void *platform,
346 struct listnode *streams_output_cfg_list,
347 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700348{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349 cnode *node = NULL;
350
351 node = config_find(root, OUTPUTS_TAG);
352 if (node != NULL) {
353 node = node->first_child;
354 while (node) {
355 ALOGV("%s: loading output %s", __func__, node->name);
356 update_streams_cfg_list(node, platform, streams_output_cfg_list);
357 node = node->next;
358 }
359 } else {
360 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700361 }
362
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530363 node = config_find(root, INPUTS_TAG);
364 if (node != NULL) {
365 node = node->first_child;
366 while (node) {
367 ALOGV("%s: loading input %s", __func__, node->name);
368 update_streams_cfg_list(node, platform, streams_input_cfg_list);
369 node = node->next;
370 }
371 } else {
372 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 }
374}
375
376static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530377 struct listnode *streams_output_cfg_list,
378 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530380 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381 int length = 0, i, num_app_types = 0;
382 struct listnode *node;
383 bool update;
384 struct mixer_ctl *ctl = NULL;
385 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530386 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700387
388 if (!mixer) {
389 ALOGE("%s: mixer is null",__func__);
390 return;
391 }
392 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
393 if (!ctl) {
394 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
395 return;
396 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530397 app_type_cfg[length++] = num_app_types;
398
399 if (list_empty(streams_output_cfg_list)) {
400 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700401 app_type_cfg[length++] = 48000;
402 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530403 num_app_types += 1;
404 }
405 if (list_empty(streams_input_cfg_list)) {
406 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
407 app_type_cfg[length++] = 48000;
408 app_type_cfg[length++] = 16;
409 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700410 }
411
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700412 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530413 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700414 update = true;
415 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530416 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700417 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530418 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530419 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530420 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530421 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530422 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700423 update = false;
424 break;
425 }
426 }
427 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530428 num_app_types += 1;
429 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
430 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
431 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
432 }
433 }
434 list_for_each(node, streams_input_cfg_list) {
435 s_info = node_to_item(node, struct streams_io_cfg, list);
436 update = true;
437 for (i=0; i<length; i=i+3) {
438 if (app_type_cfg[i+1] == 0)
439 break;
440 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530441 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530443 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530444 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
445 update = false;
446 break;
447 }
448 }
449 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
450 num_app_types += 1;
451 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
452 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
453 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 }
455 }
456 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
457 if (num_app_types) {
458 app_type_cfg[0] = num_app_types;
459 mixer_ctl_set_array(ctl, app_type_cfg, length);
460 }
461}
462
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530463void audio_extn_utils_update_streams_cfg_lists(void *platform,
464 struct mixer *mixer,
465 struct listnode *streams_output_cfg_list,
466 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700467{
468 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530469 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700470
471 ALOGV("%s", __func__);
472 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530473 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700474
475 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700476 if (root == NULL) {
477 ALOGE("cfg_list, NULL config root");
478 return;
479 }
480
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530481 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
482 if (data == NULL) {
483 ALOGD("%s: failed to open io config file(%s), trying older config file",
484 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
485 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
486 if (data == NULL) {
487 send_app_type_cfg(platform, mixer,
488 streams_output_cfg_list,
489 streams_input_cfg_list);
490 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800491 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530492 return;
493 }
494 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700495
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530496 config_load(root, data);
497 load_cfg_list(root, platform, streams_output_cfg_list,
498 streams_input_cfg_list);
499
500 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
501 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800502
503 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800504 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800505 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700506}
507
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530508static void audio_extn_utils_dump_streams_cfg_list(
509 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700510{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700511 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530512 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700513 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700514 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530515
516 list_for_each(node_i, streams_cfg_list) {
517 s_info = node_to_item(node_i, struct streams_io_cfg, list);
518 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
519 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
520 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
521 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700522 sf_info = node_to_item(node_j, struct stream_format, list);
523 ALOGV("format-%x", sf_info->format);
524 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530525 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700526 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
527 ALOGV("sample rate-%d", ss_info->sample_rate);
528 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700529 }
530}
531
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530532void audio_extn_utils_dump_streams_cfg_lists(
533 struct listnode *streams_output_cfg_list,
534 struct listnode *streams_input_cfg_list)
535{
536 ALOGV("%s", __func__);
537 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
538 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
539}
540
541static void audio_extn_utils_release_streams_cfg_list(
542 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700543{
544 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530545 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700546
547 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530548
549 while (!list_empty(streams_cfg_list)) {
550 node_i = list_head(streams_cfg_list);
551 s_info = node_to_item(node_i, struct streams_io_cfg, list);
552 while (!list_empty(&s_info->format_list)) {
553 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700554 list_remove(node_j);
555 free(node_to_item(node_j, struct stream_format, list));
556 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530557 while (!list_empty(&s_info->sample_rate_list)) {
558 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700559 list_remove(node_j);
560 free(node_to_item(node_j, struct stream_sample_rate, list));
561 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700562 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530563 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700564 }
565}
566
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530567void audio_extn_utils_release_streams_cfg_lists(
568 struct listnode *streams_output_cfg_list,
569 struct listnode *streams_input_cfg_list)
570{
571 ALOGV("%s", __func__);
572 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
573 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
574}
575
576static bool set_app_type_cfg(struct streams_io_cfg *s_info,
577 struct stream_app_type_cfg *app_type_cfg,
578 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700579 {
580 struct listnode *node_i;
581 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530582 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700583 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
584 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530585 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700586
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530587 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700588 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530589 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700590 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
591 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
592 return true;
593 }
594 }
595 /*
596 * Reiterate through the list assuming dafault sample rate.
597 * Handles scenario where input sample rate is higher
598 * than all sample rates in list for the input bit width.
599 */
600 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800601
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530602 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700603 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
604 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530605 (bit_width == s_info->app_type_cfg.bit_width)) {
606 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700607 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530608 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800609 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 -0700610 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
611 return true;
612 }
613 }
614 return false;
615}
616
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530617void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
618 struct listnode *streams_input_cfg_list,
619 audio_devices_t devices __unused,
620 audio_input_flags_t flags,
621 audio_format_t format,
622 uint32_t sample_rate,
623 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530624 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530625 struct stream_app_type_cfg *app_type_cfg)
626{
627 struct listnode *node_i, *node_j;
628 struct streams_io_cfg *s_info;
629 struct stream_format *sf_info;
630
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530631 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
632 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530633
634 list_for_each(node_i, streams_input_cfg_list) {
635 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530636 /* Along with flags do profile matching if set at either end.*/
637 if (s_info->flags.in_flags == flags &&
638 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
639 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530640 list_for_each(node_j, &s_info->format_list) {
641 sf_info = node_to_item(node_j, struct stream_format, list);
642 if (sf_info->format == format) {
643 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
644 return;
645 }
646 }
647 }
648 }
649 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
650 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
651 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
652 app_type_cfg->bit_width = 16;
653}
654
655void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700656 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700657 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700658 audio_output_flags_t flags,
659 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700660 uint32_t sample_rate,
661 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530662 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530663 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700664 struct stream_app_type_cfg *app_type_cfg)
665{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530666 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530667 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700668 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530669 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700670
Ashish Jain058165c2016-09-28 23:18:48 +0530671 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700672 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700673 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
674 if (-ENOSYS != bw)
675 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700676 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
677 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
678 }
679
Manish Dewangan837dc462015-05-27 10:17:41 +0530680 property_get("audio.playback.mch.downsample",value,"");
681 if (!strncmp("true", value, sizeof("true"))) {
682 if ((popcount(channel_mask) > 2) &&
683 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
684 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
685 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
686 ALOGD("%s: MCH session defaulting sample rate to %d",
687 __func__, sample_rate);
688 }
689 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530690
691 /* Set sampling rate to 176.4 for DSD64
692 * and 352.8Khz for DSD128.
693 * Set Bit Width to 16. output will be 16 bit
694 * post DoP in ASM.
695 */
696 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
697 (format == AUDIO_FORMAT_DSD)) {
698 bit_width = 16;
699 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
700 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
701 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
702 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
703 }
704
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530705 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
706 //TODO: Handle fractional sampling rate configuration for LL
707 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
708 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
709 __func__, sample_rate, bit_width);
710 }
711
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800712 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
713 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700714 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530715 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530716 /* Along with flags do profile matching if set at either end.*/
717 if (s_info->flags.out_flags == flags &&
718 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
719 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530720 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700721 sf_info = node_to_item(node_j, struct stream_format, list);
722 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530723 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700724 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700725 }
726 }
727 }
728 }
729 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530730 s_info = node_to_item(node_i, struct streams_io_cfg, list);
731 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700732 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530733 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
734 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
735 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700736 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530737 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700738 return;
739 }
740 }
741 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700742 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700743 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700744 app_type_cfg->bit_width = 16;
745}
746
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530747static bool audio_is_this_native_usecase(struct audio_usecase *uc)
748{
749 bool native_usecase = false;
750 struct stream_out *out = (struct stream_out*) uc->stream.out;
751
752 if (PCM_PLAYBACK == uc->type && out != NULL &&
753 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
754 is_offload_usecase(uc->id) &&
755 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
756 native_usecase = true;
757
758 return native_usecase;
759}
760
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800761
762static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
763{
764 return adev->vr_audio_mode_enabled;
765}
766
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530767void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
768 struct audio_device *adev,
769 struct audio_usecase *usecase)
770{
771 ALOGV("%s", __func__);
772
773 switch(usecase->type) {
774 case PCM_PLAYBACK:
775 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
776 &adev->streams_output_cfg_list,
777 usecase->stream.out->devices,
778 usecase->stream.out->flags,
779 usecase->stream.out->format,
780 usecase->stream.out->sample_rate,
781 usecase->stream.out->bit_width,
782 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530783 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530784 &usecase->stream.out->app_type_cfg);
785 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
786 break;
787 case PCM_CAPTURE:
788 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
789 &adev->streams_input_cfg_list,
790 usecase->stream.in->device,
791 usecase->stream.in->flags,
792 usecase->stream.in->format,
793 usecase->stream.in->sample_rate,
794 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530795 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530796 &usecase->stream.in->app_type_cfg);
797 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
798 break;
799 default:
800 ALOGE("%s: app type cfg not supported for usecase type (%d)",
801 __func__, usecase->type);
802 }
803}
804
Siena Richard7c2db772016-12-21 11:32:34 -0800805static int send_app_type_cfg_for_device(struct audio_device *adev,
806 struct audio_usecase *usecase,
807 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700808{
809 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530810 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
811 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700812 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800813 int pcm_device_id = 0, acdb_dev_id, app_type;
814 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530815 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530816 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700817
Siena Richard7c2db772016-12-21 11:32:34 -0800818 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
819 __func__, platform_get_snd_device_name(usecase->out_snd_device),
820 platform_get_snd_device_name(usecase->in_snd_device),
821 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700822
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530823 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530824 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700825 rc = 0;
826 goto exit_send_app_type_cfg;
827 }
828 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
829 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
830 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800831 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530832 (!is_offload_usecase(usecase->id)) &&
833 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530834 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 -0700835 rc = 0;
836 goto exit_send_app_type_cfg;
837 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800838
839 //if VR is active then only send the mixer control
840 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
841 ALOGI("ULL doesnt need sending app type cfg, returning");
842 rc = 0;
843 goto exit_send_app_type_cfg;
844 }
845
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530846 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700847 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530848 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
849 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700850 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700851 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530852 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
853 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530854 }
Siena Richard7c2db772016-12-21 11:32:34 -0800855
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700856 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
857 if (!ctl) {
858 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
859 mixer_ctl_name);
860 rc = -EINVAL;
861 goto exit_send_app_type_cfg;
862 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530863 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530864
Rohit Kumar1181b292017-01-31 18:07:17 +0530865 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
866 if (acdb_dev_id <= 0) {
867 ALOGE("%s: Couldn't get the acdb dev id", __func__);
868 rc = -EINVAL;
869 goto exit_send_app_type_cfg;
870 }
871
Siena Richard7c2db772016-12-21 11:32:34 -0800872 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
873 if (snd_device_be_idx < 0) {
874 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
875 __func__, platform_get_snd_device_name(snd_device),
876 snd_device_be_idx);
877 }
878
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530879 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530880
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530881 property_get("audio.playback.mch.downsample",value,"");
882 if (!strncmp("true", value, sizeof("true"))) {
883 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
884 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
885 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
886 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
887 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530888
Ashish Jain4826f6c2017-02-06 13:33:20 +0530889 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700890 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530891 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
892 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
893 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
894 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
895 /*
896 * To best utlize DSP, check if the stream sample rate is supported/multiple of
897 * configured device sample rate, if not update the COPP rate to be equal to the
898 * device sample rate, else open COPP at stream sample rate
899 */
900 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
901 usecase->stream.out->sample_rate,
902 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530903 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
904 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700905 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
906 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530907 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700908 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
909 }
910 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
911
Siena Richard7c2db772016-12-21 11:32:34 -0800912 app_type = usecase->stream.out->app_type_cfg.app_type;
913 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700914 app_type_cfg[len++] = acdb_dev_id;
915 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700916 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
917 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530918 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700919 app_type_cfg[len++] = sample_rate * 4;
920 } else {
921 app_type_cfg[len++] = sample_rate;
922 }
Siena Richard7c2db772016-12-21 11:32:34 -0800923 if (snd_device_be_idx > 0)
924 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530925
Siena Richard7c2db772016-12-21 11:32:34 -0800926 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
927 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530928
929 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800930 app_type = usecase->stream.in->app_type_cfg.app_type;
931 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530932 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800933 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
934 app_type_cfg[len++] = sample_rate;
935 if (snd_device_be_idx > 0)
936 app_type_cfg[len++] = snd_device_be_idx;
937 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
938 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530939 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800940 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
941 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530942 app_type_cfg[len++] = acdb_dev_id;
943 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800944 if (snd_device_be_idx > 0)
945 app_type_cfg[len++] = snd_device_be_idx;
946 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
947 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530948 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530949
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700950 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700951 rc = 0;
952exit_send_app_type_cfg:
953 return rc;
954}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700955
Siena Richard7c2db772016-12-21 11:32:34 -0800956int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
957 struct audio_usecase *usecase)
958{
959 int i, num_devices = 0;
960 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
961 int rc = 0;
962
963 switch (usecase->type) {
964 case PCM_PLAYBACK:
965 ALOGD("%s: usecase->out_snd_device %s",
966 __func__, platform_get_snd_device_name(usecase->out_snd_device));
967 /* check for out combo device */
968 if (platform_split_snd_device(adev->platform,
969 usecase->out_snd_device,
970 &num_devices, new_snd_devices)) {
971 new_snd_devices[0] = usecase->out_snd_device;
972 num_devices = 1;
973 }
974 break;
975 case PCM_CAPTURE:
976 ALOGD("%s: usecase->in_snd_device %s",
977 __func__, platform_get_snd_device_name(usecase->in_snd_device));
978 /* check for in combo device */
979 if (platform_split_snd_device(adev->platform,
980 usecase->in_snd_device,
981 &num_devices, new_snd_devices)) {
982 new_snd_devices[0] = usecase->in_snd_device;
983 num_devices = 1;
984 }
985 break;
986 default:
987 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
988 rc = 0;
989 break;
990 }
991
992 for (i = 0; i < num_devices; i++) {
993 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
994 if (rc)
995 break;
996 }
997
998 return rc;
999}
1000
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301001int read_line_from_file(const char *path, char *buf, size_t count)
1002{
1003 char * fgets_ret;
1004 FILE * fd;
1005 int rv;
1006
1007 fd = fopen(path, "r");
1008 if (fd == NULL)
1009 return -1;
1010
1011 fgets_ret = fgets(buf, (int)count, fd);
1012 if (NULL != fgets_ret) {
1013 rv = (int)strlen(buf);
1014 } else {
1015 rv = ferror(fd);
1016 }
1017 fclose(fd);
1018
1019 return rv;
1020}
1021
Ashish Jainf1eaa582016-05-23 20:54:24 +05301022/*Translates ALSA formats to AOSP PCM formats*/
1023audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1024{
1025 audio_format_t format;
1026
1027 switch(alsa_format) {
1028 case SNDRV_PCM_FORMAT_S16_LE:
1029 format = AUDIO_FORMAT_PCM_16_BIT;
1030 break;
1031 case SNDRV_PCM_FORMAT_S24_3LE:
1032 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1033 break;
1034 case SNDRV_PCM_FORMAT_S24_LE:
1035 format = AUDIO_FORMAT_PCM_8_24_BIT;
1036 break;
1037 case SNDRV_PCM_FORMAT_S32_LE:
1038 format = AUDIO_FORMAT_PCM_32_BIT;
1039 break;
1040 default:
1041 ALOGW("Incorrect ALSA format");
1042 format = AUDIO_FORMAT_INVALID;
1043 }
1044 return format;
1045}
1046
1047/*Translates hal format (AOSP) to alsa formats*/
1048uint32_t hal_format_to_alsa(audio_format_t hal_format)
1049{
1050 uint32_t alsa_format;
1051
1052 switch (hal_format) {
1053 case AUDIO_FORMAT_PCM_32_BIT: {
1054 if (platform_supports_true_32bit())
1055 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1056 else
1057 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1058 }
1059 break;
1060 case AUDIO_FORMAT_PCM_8_BIT:
1061 alsa_format = SNDRV_PCM_FORMAT_S8;
1062 break;
1063 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1064 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1065 break;
1066 case AUDIO_FORMAT_PCM_8_24_BIT: {
1067 if (platform_supports_true_32bit())
1068 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1069 else
1070 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1071 }
1072 break;
1073 case AUDIO_FORMAT_PCM_FLOAT:
1074 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1075 break;
1076 default:
1077 case AUDIO_FORMAT_PCM_16_BIT:
1078 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1079 break;
1080 }
1081 return alsa_format;
1082}
1083
Ashish Jain83a6cc22016-06-28 14:34:17 +05301084/*Translates PCM formats to AOSP formats*/
1085audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1086{
1087 audio_format_t format = AUDIO_FORMAT_INVALID;
1088
1089 switch(pcm_format) {
1090 case PCM_FORMAT_S16_LE:
1091 format = AUDIO_FORMAT_PCM_16_BIT;
1092 break;
1093 case PCM_FORMAT_S24_3LE:
1094 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1095 break;
1096 case PCM_FORMAT_S24_LE:
1097 format = AUDIO_FORMAT_PCM_8_24_BIT;
1098 break;
1099 case PCM_FORMAT_S32_LE:
1100 format = AUDIO_FORMAT_PCM_32_BIT;
1101 break;
1102 default:
1103 ALOGW("Incorrect PCM format");
1104 format = AUDIO_FORMAT_INVALID;
1105 }
1106 return format;
1107}
1108
1109/*Translates hal format (AOSP) to alsa formats*/
1110uint32_t hal_format_to_pcm(audio_format_t hal_format)
1111{
1112 uint32_t pcm_format;
1113
1114 switch (hal_format) {
1115 case AUDIO_FORMAT_PCM_32_BIT:
1116 case AUDIO_FORMAT_PCM_8_24_BIT:
1117 case AUDIO_FORMAT_PCM_FLOAT: {
1118 if (platform_supports_true_32bit())
1119 pcm_format = PCM_FORMAT_S32_LE;
1120 else
1121 pcm_format = PCM_FORMAT_S24_3LE;
1122 }
1123 break;
1124 case AUDIO_FORMAT_PCM_8_BIT:
1125 pcm_format = PCM_FORMAT_S8;
1126 break;
1127 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1128 pcm_format = PCM_FORMAT_S24_3LE;
1129 break;
1130 default:
1131 case AUDIO_FORMAT_PCM_16_BIT:
1132 pcm_format = PCM_FORMAT_S16_LE;
1133 break;
1134 }
1135 return pcm_format;
1136}
1137
Ashish Jainf1eaa582016-05-23 20:54:24 +05301138uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1139 uint32_t sample_rate,
1140 uint32_t noOfChannels)
1141{
1142 uint32_t fragment_size = 0;
1143 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1144
1145 fragment_size = (pcm_offload_time
1146 * sample_rate
1147 * bytes_per_sample
1148 * noOfChannels)/1000;
1149 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1150 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1151 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1152 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1153 /*To have same PCM samples for all channels, the buffer size requires to
1154 *be multiple of (number of channels * bytes per sample)
1155 *For writes to succeed, the buffer must be written at address which is multiple of 32
1156 */
1157 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1158
1159 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1160 return fragment_size;
1161}
1162
1163/* Calculates the fragment size required to configure compress session.
1164 * Based on the alsa format selected, decide if conversion is needed in
1165
1166 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1167 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1168 */
1169void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1170{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301171 audio_format_t dst_format = out->hal_op_format;
1172 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301173 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1174 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1175
1176 out->compr_config.fragment_size =
1177 get_alsa_fragment_size(hal_op_bytes_per_sample,
1178 out->sample_rate,
1179 popcount(out->channel_mask));
1180
1181 if ((src_format != dst_format) &&
1182 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1183
Ashish Jain83a6cc22016-06-28 14:34:17 +05301184 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301185 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1186 hal_op_bytes_per_sample);
1187 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301188 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301189 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301190 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301191 }
1192}
1193
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301194/* converts pcm format 24_8 to 8_24 inplace */
1195size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1196{
1197 size_t i = 0;
1198 int *int_buf_stream = buf;
1199
1200 if ((bytes % 4) != 0) {
1201 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1202 return -EINVAL;
1203 }
1204
1205 for (; i < (bytes / 4); i++)
1206 int_buf_stream[i] >>= 8;
1207
1208 return bytes;
1209}
1210
1211int get_snd_codec_id(audio_format_t format)
1212{
1213 int id = 0;
1214
1215 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1216 case AUDIO_FORMAT_MP3:
1217 id = SND_AUDIOCODEC_MP3;
1218 break;
1219 case AUDIO_FORMAT_AAC:
1220 id = SND_AUDIOCODEC_AAC;
1221 break;
1222 case AUDIO_FORMAT_AAC_ADTS:
1223 id = SND_AUDIOCODEC_AAC;
1224 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301225 case AUDIO_FORMAT_AAC_LATM:
1226 id = SND_AUDIOCODEC_AAC;
1227 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301228 case AUDIO_FORMAT_PCM:
1229 id = SND_AUDIOCODEC_PCM;
1230 break;
1231 case AUDIO_FORMAT_FLAC:
1232 id = SND_AUDIOCODEC_FLAC;
1233 break;
1234 case AUDIO_FORMAT_ALAC:
1235 id = SND_AUDIOCODEC_ALAC;
1236 break;
1237 case AUDIO_FORMAT_APE:
1238 id = SND_AUDIOCODEC_APE;
1239 break;
1240 case AUDIO_FORMAT_VORBIS:
1241 id = SND_AUDIOCODEC_VORBIS;
1242 break;
1243 case AUDIO_FORMAT_WMA:
1244 id = SND_AUDIOCODEC_WMA;
1245 break;
1246 case AUDIO_FORMAT_WMA_PRO:
1247 id = SND_AUDIOCODEC_WMA_PRO;
1248 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301249 case AUDIO_FORMAT_MP2:
1250 id = SND_AUDIOCODEC_MP2;
1251 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301252 case AUDIO_FORMAT_AC3:
1253 id = SND_AUDIOCODEC_AC3;
1254 break;
1255 case AUDIO_FORMAT_E_AC3:
1256 case AUDIO_FORMAT_E_AC3_JOC:
1257 id = SND_AUDIOCODEC_EAC3;
1258 break;
1259 case AUDIO_FORMAT_DTS:
1260 case AUDIO_FORMAT_DTS_HD:
1261 id = SND_AUDIOCODEC_DTS;
1262 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001263 case AUDIO_FORMAT_DOLBY_TRUEHD:
1264 id = SND_AUDIOCODEC_TRUEHD;
1265 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301266 case AUDIO_FORMAT_DSD:
1267 id = SND_AUDIOCODEC_DSD;
1268 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301269 case AUDIO_FORMAT_APTX:
1270 id = SND_AUDIOCODEC_APTX;
1271 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301272 default:
1273 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1274 }
1275
1276 return id;
1277}
1278
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001279void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1280 struct audio_usecase *usecase)
1281{
1282 int type = usecase->type;
1283
Dhananjay Kumar14245982017-01-16 20:21:00 +05301284 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001285 struct stream_out *out = usecase->stream.out;
1286 int snd_device = usecase->out_snd_device;
1287 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001288 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301289 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001290 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301291 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301292 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301293 platform_send_audio_calibration(adev->platform, usecase,
1294 usecase->stream.in->app_type_cfg.app_type,
1295 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001296 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301297 /* when app type is default. the sample rate is not used to send cal */
1298 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301299 platform_get_default_app_type_v2(adev->platform, usecase->type),
1300 48000);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001301 } else {
1302 /* No need to send audio calibration for voice and voip call usecases */
1303 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1304 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001305 }
1306}
1307
Ben Rombergera04fabc2014-11-14 12:16:03 -08001308// Base64 Encode and Decode
1309// Not all features supported. This must be used only with following conditions.
1310// Decode Modes: Support with and without padding
1311// CRLF not handling. So no CRLF in string to decode.
1312// Encode Modes: Supports only padding
1313int b64decode(char *inp, int ilen, uint8_t* outp)
1314{
1315 int i, j, k, ii, num;
1316 int rem, pcnt;
1317 uint32_t res=0;
1318 uint8_t getIndex[MAX_BASEINDEX_LEN];
1319 uint8_t tmp, cflag;
1320
1321 if(inp == NULL || outp == NULL || ilen <= 0) {
1322 ALOGE("[%s] received NULL pointer or zero length",__func__);
1323 return -1;
1324 }
1325
1326 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1327 for(i=0;i<BASE_TABLE_SIZE;i++) {
1328 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1329 }
1330 getIndex[(uint8_t)'=']=0;
1331
1332 j=0;k=0;
1333 num = ilen/4;
1334 rem = ilen%4;
1335 if(rem==0)
1336 num = num-1;
1337 cflag=0;
1338 for(i=0; i<num; i++) {
1339 res=0;
1340 for(ii=0;ii<4;ii++) {
1341 res = res << 6;
1342 tmp = getIndex[(uint8_t)inp[j++]];
1343 res = res | tmp;
1344 cflag = cflag | tmp;
1345 }
1346 outp[k++] = (res >> 16)&0xFF;
1347 outp[k++] = (res >> 8)&0xFF;
1348 outp[k++] = res & 0xFF;
1349 }
1350
1351 // Handle last bytes special
1352 pcnt=0;
1353 if(rem == 0) {
1354 //With padding or full data
1355 res = 0;
1356 for(ii=0;ii<4;ii++) {
1357 if(inp[j] == '=')
1358 pcnt++;
1359 res = res << 6;
1360 tmp = getIndex[(uint8_t)inp[j++]];
1361 res = res | tmp;
1362 cflag = cflag | tmp;
1363 }
1364 outp[k++] = res >> 16;
1365 if(pcnt == 2)
1366 goto done;
1367 outp[k++] = (res>>8)&0xFF;
1368 if(pcnt == 1)
1369 goto done;
1370 outp[k++] = res&0xFF;
1371 } else {
1372 //without padding
1373 res = 0;
1374 for(i=0;i<rem;i++) {
1375 res = res << 6;
1376 tmp = getIndex[(uint8_t)inp[j++]];
1377 res = res | tmp;
1378 cflag = cflag | tmp;
1379 }
1380 for(i=rem;i<4;i++) {
1381 res = res << 6;
1382 pcnt++;
1383 }
1384 outp[k++] = res >> 16;
1385 if(pcnt == 2)
1386 goto done;
1387 outp[k++] = (res>>8)&0xFF;
1388 if(pcnt == 1)
1389 goto done;
1390 outp[k++] = res&0xFF;
1391 }
1392done:
1393 if(cflag == 0xFF) {
1394 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1395 __func__, inp);
1396 return 0;
1397 }
1398 return k;
1399}
1400
1401int b64encode(uint8_t *inp, int ilen, char* outp)
1402{
1403 int i,j,k, num;
1404 int rem=0;
1405 uint32_t res=0;
1406
1407 if(inp == NULL || outp == NULL || ilen<=0) {
1408 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1409 return -1;
1410 }
1411
1412 num = ilen/3;
1413 rem = ilen%3;
1414 j=0;k=0;
1415 for(i=0; i<num; i++) {
1416 //prepare index
1417 res = inp[j++]<<16;
1418 res = res | inp[j++]<<8;
1419 res = res | inp[j++];
1420 //get output map from index
1421 outp[k++] = (char) bTable[(res>>18)&0x3F];
1422 outp[k++] = (char) bTable[(res>>12)&0x3F];
1423 outp[k++] = (char) bTable[(res>>6)&0x3F];
1424 outp[k++] = (char) bTable[res&0x3F];
1425 }
1426
1427 switch(rem) {
1428 case 1:
1429 res = inp[j++]<<16;
1430 outp[k++] = (char) bTable[res>>18];
1431 outp[k++] = (char) bTable[(res>>12)&0x3F];
1432 //outp[k++] = '=';
1433 //outp[k++] = '=';
1434 break;
1435 case 2:
1436 res = inp[j++]<<16;
1437 res = res | inp[j++]<<8;
1438 outp[k++] = (char) bTable[res>>18];
1439 outp[k++] = (char) bTable[(res>>12)&0x3F];
1440 outp[k++] = (char) bTable[(res>>6)&0x3F];
1441 //outp[k++] = '=';
1442 break;
1443 default:
1444 break;
1445 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001446 outp[k] = '\0';
1447 return k;
1448}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301449
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301450
1451int audio_extn_utils_get_codec_version(const char *snd_card_name,
1452 int card_num,
1453 char *codec_version)
1454{
1455 char procfs_path[50];
1456 FILE *fp;
1457
1458 if (strstr(snd_card_name, "tasha")) {
1459 snprintf(procfs_path, sizeof(procfs_path),
1460 "/proc/asound/card%d/codecs/tasha/version", card_num);
1461 if ((fp = fopen(procfs_path, "r")) != NULL) {
1462 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1463 fclose(fp);
1464 } else {
1465 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1466 return -ENOENT;
1467 }
1468 ALOGD("%s: codec version %s", __func__, codec_version);
1469 }
1470
1471 return 0;
1472}
1473
1474
Ashish Jain81eb2a82015-05-13 10:52:34 +05301475#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1476
1477void get_default_compressed_channel_status(
1478 unsigned char *channel_status)
1479{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301480 memset(channel_status,0,24);
1481
1482 /* block start bit in preamble bit 3 */
1483 channel_status[0] |= PROFESSIONAL;
1484 //compre out
1485 channel_status[0] |= NON_LPCM;
1486 // sample rate; fixed 48K for default/transcode
1487 channel_status[3] |= SR_48000;
1488}
1489
1490#ifdef HDMI_PASSTHROUGH_ENABLED
1491int32_t get_compressed_channel_status(void *audio_stream_data,
1492 uint32_t audio_frame_size,
1493 unsigned char *channel_status,
1494 enum audio_parser_code_type codec_type)
1495 // codec_type - AUDIO_PARSER_CODEC_AC3
1496 // - AUDIO_PARSER_CODEC_DTS
1497{
1498 unsigned char *stream;
1499 int ret = 0;
1500 stream = (unsigned char *)audio_stream_data;
1501
1502 if (audio_stream_data == NULL || audio_frame_size == 0) {
1503 ALOGW("no buffer to get channel status, return default for compress");
1504 get_default_compressed_channel_status(channel_status);
1505 return ret;
1506 }
1507
1508 memset(channel_status,0,24);
1509 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1510 {
1511 ALOGE("init audio parser failed");
1512 return -1;
1513 }
1514 ret = get_channel_status(channel_status, codec_type);
1515 return ret;
1516
1517}
1518
1519#endif
1520
1521void get_lpcm_channel_status(uint32_t sampleRate,
1522 unsigned char *channel_status)
1523{
1524 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301525 memset(channel_status,0,24);
1526 /* block start bit in preamble bit 3 */
1527 channel_status[0] |= PROFESSIONAL;
1528 //LPCM OUT
1529 channel_status[0] &= ~NON_LPCM;
1530
1531 switch (sampleRate) {
1532 case 8000:
1533 case 11025:
1534 case 12000:
1535 case 16000:
1536 case 22050:
1537 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301538 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301539 case 24000:
1540 channel_status[3] |= SR_24000;
1541 break;
1542 case 32000:
1543 channel_status[3] |= SR_32000;
1544 break;
1545 case 44100:
1546 channel_status[3] |= SR_44100;
1547 break;
1548 case 48000:
1549 channel_status[3] |= SR_48000;
1550 break;
1551 case 88200:
1552 channel_status[3] |= SR_88200;
1553 break;
1554 case 96000:
1555 channel_status[3] |= SR_96000;
1556 break;
1557 case 176400:
1558 channel_status[3] |= SR_176400;
1559 break;
1560 case 192000:
1561 channel_status[3] |= SR_192000;
1562 break;
1563 default:
1564 ALOGV("Invalid sample_rate %u\n", sampleRate);
1565 status = -1;
1566 break;
1567 }
1568}
1569
1570void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1571{
1572 unsigned char channel_status[24]={0};
1573 struct snd_aes_iec958 iec958;
1574 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1575 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301576 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301577#ifdef HDMI_PASSTHROUGH_ENABLED
1578 if (audio_extn_is_dolby_format(out->format) &&
1579 /*TODO:Extend code to support DTS passthrough*/
1580 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301581 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301582 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1583 } else
1584#endif
1585 {
1586 /*set channel status bit for LPCM*/
1587 get_lpcm_channel_status(out->sample_rate, channel_status);
1588 }
1589
1590 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1591 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1592 if (!ctl) {
1593 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1594 __func__, mixer_ctl_name);
1595 return;
1596 }
1597 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1598 ALOGE("%s: Could not set channel status for ext HDMI ",
1599 __func__);
1600 return;
1601 }
1602
1603}
1604#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301605
1606int audio_extn_utils_get_avt_device_drift(
1607 struct audio_usecase *usecase,
1608 struct audio_avt_device_drift_param *drift_param)
1609{
1610 int ret = 0, count = 0;
1611 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301612 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301613 struct mixer_ctl *ctl = NULL;
1614 struct audio_avt_device_drift_stats drift_stats;
1615 struct audio_device *adev = NULL;
1616
1617 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301618 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1619 if (!backend) {
1620 ALOGE("%s: Unsupported device %d", __func__,
1621 usecase->stream.out->devices);
1622 ret = -EINVAL;
1623 goto done;
1624 }
1625 strlcpy(avt_device_drift_mixer_ctl_name,
1626 backend,
1627 MIXER_PATH_MAX_LENGTH);
1628
1629 count = strlen(backend);
1630 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1631 strlcat(&avt_device_drift_mixer_ctl_name[count],
1632 " DRIFT",
1633 MIXER_PATH_MAX_LENGTH - count);
1634 } else {
1635 ret = -EINVAL;
1636 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301637 }
1638 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301639 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301640 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301641 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301642 }
1643
Naresh Tanniru6160c712017-04-17 15:43:48 +05301644 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301645 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1646 if (!ctl) {
1647 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1648 __func__, avt_device_drift_mixer_ctl_name);
1649
1650 ret = -EINVAL;
1651 goto done;
1652 }
1653
1654 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1655 avt_device_drift_mixer_ctl_name);
1656
1657 mixer_ctl_update(ctl);
1658 count = mixer_ctl_get_num_values(ctl);
1659 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1660 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1661 __func__);
1662
1663 ret = -EINVAL;
1664 goto done;
1665 }
1666
1667 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1668 if (ret != 0) {
1669 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1670 __func__);
1671
1672 ret = -EINVAL;
1673 goto done;
1674 }
1675 memcpy(drift_param, &drift_stats.drift_param,
1676 sizeof(struct audio_avt_device_drift_param));
1677done:
1678 return ret;
1679}
Manish Dewangan07de2142017-02-27 19:27:20 +05301680
1681#ifdef SNDRV_COMPRESS_PATH_DELAY
1682int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1683{
1684 int ret = -EINVAL;
1685 struct snd_compr_metadata metadata;
1686 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1687
1688 if (property_get_bool("audio.playback.dsp.pathdelay", false)) {
1689 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1690 if (!(is_offload_usecase(out->usecase))) {
1691 ALOGE("%s:: not supported for non offload session", __func__);
1692 goto exit;
1693 }
1694
1695 if (!out->compr) {
1696 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1697 __func__);
1698 goto exit;
1699 }
1700
1701 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1702 ret = compress_get_metadata(out->compr, &metadata);
1703 if(ret) {
1704 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1705 goto exit;
1706 }
1707 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1708 } else {
1709 ALOGD("%s:: Using Fix DSP delay",__func__);
1710 }
1711
1712exit:
1713 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1714 return delay_ms;
1715}
1716#else
1717int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1718{
1719 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1720}
1721#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301722
1723#ifdef SNDRV_COMPRESS_RENDER_MODE
1724int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1725{
1726 struct snd_compr_metadata metadata;
1727 int ret = -EINVAL;
1728
1729 if (!(is_offload_usecase(out->usecase))) {
1730 ALOGE("%s:: not supported for non offload session", __func__);
1731 goto exit;
1732 }
1733
1734 if (!out->compr) {
1735 ALOGD("%s:: Invalid compress handle",
1736 __func__);
1737 goto exit;
1738 }
1739
1740 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1741
1742 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1743 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1744 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1745 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1746 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1747 } else {
1748 ret = 0;
1749 goto exit;
1750 }
1751 ret = compress_set_metadata(out->compr, &metadata);
1752 if(ret) {
1753 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1754 }
1755exit:
1756 return ret;
1757}
1758#else
1759int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1760{
1761 ALOGD("%s:: configuring render mode not supported", __func__);
1762 return 0;
1763}
1764#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301765
1766#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1767int audio_extn_utils_compress_set_clk_rec_mode(
1768 struct audio_usecase *usecase)
1769{
1770 struct snd_compr_metadata metadata;
1771 struct stream_out *out = NULL;
1772 int ret = -EINVAL;
1773
Naresh Tanniru7586e292017-04-13 10:38:13 +05301774 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301775 ALOGE("%s:: Invalid use case", __func__);
1776 goto exit;
1777 }
1778
1779 out = usecase->stream.out;
1780 if (!out) {
1781 ALOGE("%s:: invalid stream", __func__);
1782 goto exit;
1783 }
1784
1785 if (!is_offload_usecase(out->usecase)) {
1786 ALOGE("%s:: not supported for non offload session", __func__);
1787 goto exit;
1788 }
1789
1790 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1791 ALOGD("%s:: clk recovery is only supported in STC render mode",
1792 __func__);
1793 ret = 0;
1794 goto exit;
1795 }
1796
1797 if (!out->compr) {
1798 ALOGD("%s:: Invalid compress handle",
1799 __func__);
1800 goto exit;
1801 }
1802 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1803 switch(usecase->out_snd_device) {
1804 case SND_DEVICE_OUT_HDMI:
1805 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1806 case SND_DEVICE_OUT_DISPLAY_PORT:
1807 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1808 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1809 break;
1810 default:
1811 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1812 break;
1813 }
1814
1815 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1816
1817 ret = compress_set_metadata(out->compr, &metadata);
1818 if(ret) {
1819 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1820 }
1821
1822exit:
1823 return ret;
1824}
1825#else
1826int audio_extn_utils_compress_set_clk_rec_mode(
1827 struct audio_usecase *usecase __unused)
1828{
1829 ALOGD("%s:: configuring render mode not supported", __func__);
1830 return 0;
1831}
1832#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301833
1834#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1835int audio_extn_utils_compress_set_render_window(
1836 struct stream_out *out,
1837 struct audio_out_render_window_param *render_window)
1838{
1839 struct snd_compr_metadata metadata;
1840 int ret = -EINVAL;
1841
Manish Dewangan27346042017-03-01 12:56:12 +05301842 if(render_window == NULL) {
1843 ALOGE("%s:: Invalid render_window", __func__);
1844 goto exit;
1845 }
1846
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001847 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1848 __func__,render_window->render_ws, render_window->render_we);
1849
Manish Dewangan27346042017-03-01 12:56:12 +05301850 if (!is_offload_usecase(out->usecase)) {
1851 ALOGE("%s:: not supported for non offload session", __func__);
1852 goto exit;
1853 }
1854
Naresh Tanniru6160c712017-04-17 15:43:48 +05301855 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1856 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301857 ALOGD("%s:: only supported in timestamp mode, current "
1858 "render mode mode %d", __func__, out->render_mode);
1859 goto exit;
1860 }
1861
1862 if (!out->compr) {
1863 ALOGW("%s:: offload session not yet opened,"
1864 "render window will be configure later", __func__);
1865 /* store render window to reconfigure in start_output_stream() */
1866 goto exit;
1867 }
1868
1869 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1870 /*render window start value */
1871 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1872 metadata.value[1] = \
1873 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1874 /*render window end value */
1875 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1876 metadata.value[3] = \
1877 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1878
1879 ret = compress_set_metadata(out->compr, &metadata);
1880 if(ret) {
1881 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1882 }
1883
1884exit:
1885 return ret;
1886}
1887#else
1888int audio_extn_utils_compress_set_render_window(
1889 struct stream_out *out __unused,
1890 struct audio_out_render_window_param *render_window __unused)
1891{
1892 ALOGD("%s:: configuring render window not supported", __func__);
1893 return 0;
1894}
1895#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05301896
1897#ifdef SNDRV_COMPRESS_START_DELAY
1898int audio_extn_utils_compress_set_start_delay(
1899 struct stream_out *out,
1900 struct audio_out_start_delay_param *delay_param)
1901{
1902 struct snd_compr_metadata metadata;
1903 int ret = -EINVAL;
1904
1905 if(delay_param == NULL) {
1906 ALOGE("%s:: Invalid delay_param", __func__);
1907 goto exit;
1908 }
1909
1910 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
1911 delay_param->start_delay);
1912
1913 if (!is_offload_usecase(out->usecase)) {
1914 ALOGE("%s:: not supported for non offload session", __func__);
1915 goto exit;
1916 }
1917
Naresh Tanniru6160c712017-04-17 15:43:48 +05301918 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1919 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05301920 ALOGD("%s:: only supported in timestamp mode, current "
1921 "render mode mode %d", __func__, out->render_mode);
1922 goto exit;
1923 }
1924
1925 if (!out->compr) {
1926 ALOGW("%s:: offload session not yet opened,"
1927 "start delay will be configure later", __func__);
1928 goto exit;
1929 }
1930
1931 metadata.key = SNDRV_COMPRESS_START_DELAY;
1932 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
1933 metadata.value[1] = \
1934 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
1935
1936 ret = compress_set_metadata(out->compr, &metadata);
1937 if(ret) {
1938 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1939 }
1940
1941exit:
1942 return ret;
1943}
1944#else
1945int audio_extn_utils_compress_set_start_delay(
1946 struct stream_out *out __unused,
1947 struct audio_out_start_delay_param *delay_param __unused)
1948{
1949 ALOGD("%s:: configuring render window not supported", __func__);
1950 return 0;
1951}
1952#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07001953
1954#define MAX_SND_CARD 8
1955#define RETRY_US 500000
1956#define RETRY_NUMBER 10
1957
1958int audio_extn_utils_get_snd_card_num()
1959{
1960
1961 void *hw_info = NULL;
1962 struct mixer *mixer = NULL;
1963 int retry_num = 0;
1964 int snd_card_num = 0;
1965 char* snd_card_name = NULL;
1966
1967 while (snd_card_num < MAX_SND_CARD) {
1968 mixer = mixer_open(snd_card_num);
1969
1970 while (!mixer && retry_num < RETRY_NUMBER) {
1971 usleep(RETRY_US);
1972 mixer = mixer_open(snd_card_num);
1973 retry_num++;
1974 }
1975
1976 if (!mixer) {
1977 ALOGE("%s: Unable to open the mixer card: %d", __func__,
1978 snd_card_num);
1979 retry_num = 0;
1980 snd_card_num++;
1981 continue;
1982 }
1983
1984 snd_card_name = strdup(mixer_get_name(mixer));
1985 if (!snd_card_name) {
1986 ALOGE("failed to allocate memory for snd_card_name\n");
1987 mixer_close(mixer);
1988 return -1;
1989 }
1990 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
1991
1992 hw_info = hw_info_init(snd_card_name);
1993 if (hw_info) {
1994 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
1995 break;
1996 }
1997 ALOGE("%s: Failed to init hardware info", __func__);
1998 retry_num = 0;
1999 snd_card_num++;
2000 free(snd_card_name);
2001 mixer_close(mixer);
2002 }
2003
2004 mixer_close(mixer);
2005 hw_info_deinit(hw_info);
2006 if (snd_card_name)
2007 free(snd_card_name);
2008
2009 if (snd_card_num >= MAX_SND_CARD) {
2010 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2011 return -1;
2012 }
2013
2014 return snd_card_num;
2015}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302016
2017#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2018int audio_extn_utils_compress_enable_drift_correction(
2019 struct stream_out *out,
2020 struct audio_out_enable_drift_correction *drift)
2021{
2022 struct snd_compr_metadata metadata;
2023 int ret = -EINVAL;
2024
2025 if(drift == NULL) {
2026 ALOGE("%s:: Invalid param", __func__);
2027 goto exit;
2028 }
2029
2030 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2031
2032 if (!is_offload_usecase(out->usecase)) {
2033 ALOGE("%s:: not supported for non offload session", __func__);
2034 goto exit;
2035 }
2036
2037 if (!out->compr) {
2038 ALOGW("%s:: offload session not yet opened,"
2039 "start delay will be configure later", __func__);
2040 goto exit;
2041 }
2042
2043 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2044 metadata.value[0] = drift->enable;
2045 out->drift_correction_enabled = drift->enable;
2046
2047 ret = compress_set_metadata(out->compr, &metadata);
2048 if(ret) {
2049 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2050 out->drift_correction_enabled = false;
2051 }
2052
2053exit:
2054 return ret;
2055}
2056#else
2057int audio_extn_utils_compress_enable_drift_correction(
2058 struct stream_out *out __unused,
2059 struct audio_out_enable_drift_correction *drift __unused)
2060{
2061 ALOGD("%s:: configuring drift enablement not supported", __func__);
2062 return 0;
2063}
2064#endif
2065
2066#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2067int audio_extn_utils_compress_correct_drift(
2068 struct stream_out *out,
2069 struct audio_out_correct_drift *drift_param)
2070{
2071 struct snd_compr_metadata metadata;
2072 int ret = -EINVAL;
2073
2074 if (drift_param == NULL) {
2075 ALOGE("%s:: Invalid drift_param", __func__);
2076 goto exit;
2077 }
2078
2079 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2080 drift_param->adjust_time);
2081
2082 if (!is_offload_usecase(out->usecase)) {
2083 ALOGE("%s:: not supported for non offload session", __func__);
2084 goto exit;
2085 }
2086
2087 if (!out->compr) {
2088 ALOGW("%s:: offload session not yet opened", __func__);
2089 goto exit;
2090 }
2091
2092 if (!out->drift_correction_enabled) {
2093 ALOGE("%s:: drift correction not enabled", __func__);
2094 goto exit;
2095 }
2096
2097 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2098 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2099 metadata.value[1] = \
2100 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2101
2102 ret = compress_set_metadata(out->compr, &metadata);
2103 if(ret)
2104 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2105exit:
2106 return ret;
2107}
2108#else
2109int audio_extn_utils_compress_correct_drift(
2110 struct stream_out *out __unused,
2111 struct audio_out_correct_drift *drift_param __unused)
2112{
2113 ALOGD("%s:: setting adjust clock not supported", __func__);
2114 return 0;
2115}
2116#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302117
2118int audio_extn_utils_set_channel_map(
2119 struct stream_out *out,
2120 struct audio_out_channel_map_param *channel_map_param)
2121{
2122 int ret = -EINVAL, i = 0;
2123 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2124
2125 if (channel_map_param == NULL) {
2126 ALOGE("%s:: Invalid channel_map", __func__);
2127 goto exit;
2128 }
2129
2130 if (channel_map_param->channels != channels) {
2131 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2132 __func__, channel_map_param->channels, channels);
2133 goto exit;
2134 }
2135
2136 for ( i = 0; i < channels; i++) {
2137 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2138 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2139 }
2140 ret = 0;
2141exit:
2142 return ret;
2143}