blob: 38cfe35371fb15e3c2d33be35843863daba78c2f [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)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -0700919
920 sample_rate = sample_rate * 4;
921 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
922 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -0700923 }
Naresh Tanniru3a406772017-05-10 13:09:05 -0700924 app_type_cfg[len++] = sample_rate;
925
Siena Richard7c2db772016-12-21 11:32:34 -0800926 if (snd_device_be_idx > 0)
927 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530928
Siena Richard7c2db772016-12-21 11:32:34 -0800929 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
930 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530931
932 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800933 app_type = usecase->stream.in->app_type_cfg.app_type;
934 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530935 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800936 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
937 app_type_cfg[len++] = sample_rate;
938 if (snd_device_be_idx > 0)
939 app_type_cfg[len++] = snd_device_be_idx;
940 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
941 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530942 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800943 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
944 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530945 app_type_cfg[len++] = acdb_dev_id;
946 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800947 if (snd_device_be_idx > 0)
948 app_type_cfg[len++] = snd_device_be_idx;
949 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
950 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530951 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530952
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700953 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700954 rc = 0;
955exit_send_app_type_cfg:
956 return rc;
957}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700958
Siena Richard7c2db772016-12-21 11:32:34 -0800959int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
960 struct audio_usecase *usecase)
961{
962 int i, num_devices = 0;
963 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
964 int rc = 0;
965
966 switch (usecase->type) {
967 case PCM_PLAYBACK:
968 ALOGD("%s: usecase->out_snd_device %s",
969 __func__, platform_get_snd_device_name(usecase->out_snd_device));
970 /* check for out combo device */
971 if (platform_split_snd_device(adev->platform,
972 usecase->out_snd_device,
973 &num_devices, new_snd_devices)) {
974 new_snd_devices[0] = usecase->out_snd_device;
975 num_devices = 1;
976 }
977 break;
978 case PCM_CAPTURE:
979 ALOGD("%s: usecase->in_snd_device %s",
980 __func__, platform_get_snd_device_name(usecase->in_snd_device));
981 /* check for in combo device */
982 if (platform_split_snd_device(adev->platform,
983 usecase->in_snd_device,
984 &num_devices, new_snd_devices)) {
985 new_snd_devices[0] = usecase->in_snd_device;
986 num_devices = 1;
987 }
988 break;
989 default:
990 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
991 rc = 0;
992 break;
993 }
994
995 for (i = 0; i < num_devices; i++) {
996 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
997 if (rc)
998 break;
999 }
1000
1001 return rc;
1002}
1003
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301004int read_line_from_file(const char *path, char *buf, size_t count)
1005{
1006 char * fgets_ret;
1007 FILE * fd;
1008 int rv;
1009
1010 fd = fopen(path, "r");
1011 if (fd == NULL)
1012 return -1;
1013
1014 fgets_ret = fgets(buf, (int)count, fd);
1015 if (NULL != fgets_ret) {
1016 rv = (int)strlen(buf);
1017 } else {
1018 rv = ferror(fd);
1019 }
1020 fclose(fd);
1021
1022 return rv;
1023}
1024
Ashish Jainf1eaa582016-05-23 20:54:24 +05301025/*Translates ALSA formats to AOSP PCM formats*/
1026audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1027{
1028 audio_format_t format;
1029
1030 switch(alsa_format) {
1031 case SNDRV_PCM_FORMAT_S16_LE:
1032 format = AUDIO_FORMAT_PCM_16_BIT;
1033 break;
1034 case SNDRV_PCM_FORMAT_S24_3LE:
1035 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1036 break;
1037 case SNDRV_PCM_FORMAT_S24_LE:
1038 format = AUDIO_FORMAT_PCM_8_24_BIT;
1039 break;
1040 case SNDRV_PCM_FORMAT_S32_LE:
1041 format = AUDIO_FORMAT_PCM_32_BIT;
1042 break;
1043 default:
1044 ALOGW("Incorrect ALSA format");
1045 format = AUDIO_FORMAT_INVALID;
1046 }
1047 return format;
1048}
1049
1050/*Translates hal format (AOSP) to alsa formats*/
1051uint32_t hal_format_to_alsa(audio_format_t hal_format)
1052{
1053 uint32_t alsa_format;
1054
1055 switch (hal_format) {
1056 case AUDIO_FORMAT_PCM_32_BIT: {
1057 if (platform_supports_true_32bit())
1058 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1059 else
1060 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1061 }
1062 break;
1063 case AUDIO_FORMAT_PCM_8_BIT:
1064 alsa_format = SNDRV_PCM_FORMAT_S8;
1065 break;
1066 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1067 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1068 break;
1069 case AUDIO_FORMAT_PCM_8_24_BIT: {
1070 if (platform_supports_true_32bit())
1071 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1072 else
1073 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1074 }
1075 break;
1076 case AUDIO_FORMAT_PCM_FLOAT:
1077 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1078 break;
1079 default:
1080 case AUDIO_FORMAT_PCM_16_BIT:
1081 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1082 break;
1083 }
1084 return alsa_format;
1085}
1086
Ashish Jain83a6cc22016-06-28 14:34:17 +05301087/*Translates PCM formats to AOSP formats*/
1088audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1089{
1090 audio_format_t format = AUDIO_FORMAT_INVALID;
1091
1092 switch(pcm_format) {
1093 case PCM_FORMAT_S16_LE:
1094 format = AUDIO_FORMAT_PCM_16_BIT;
1095 break;
1096 case PCM_FORMAT_S24_3LE:
1097 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1098 break;
1099 case PCM_FORMAT_S24_LE:
1100 format = AUDIO_FORMAT_PCM_8_24_BIT;
1101 break;
1102 case PCM_FORMAT_S32_LE:
1103 format = AUDIO_FORMAT_PCM_32_BIT;
1104 break;
1105 default:
1106 ALOGW("Incorrect PCM format");
1107 format = AUDIO_FORMAT_INVALID;
1108 }
1109 return format;
1110}
1111
1112/*Translates hal format (AOSP) to alsa formats*/
1113uint32_t hal_format_to_pcm(audio_format_t hal_format)
1114{
1115 uint32_t pcm_format;
1116
1117 switch (hal_format) {
1118 case AUDIO_FORMAT_PCM_32_BIT:
1119 case AUDIO_FORMAT_PCM_8_24_BIT:
1120 case AUDIO_FORMAT_PCM_FLOAT: {
1121 if (platform_supports_true_32bit())
1122 pcm_format = PCM_FORMAT_S32_LE;
1123 else
1124 pcm_format = PCM_FORMAT_S24_3LE;
1125 }
1126 break;
1127 case AUDIO_FORMAT_PCM_8_BIT:
1128 pcm_format = PCM_FORMAT_S8;
1129 break;
1130 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1131 pcm_format = PCM_FORMAT_S24_3LE;
1132 break;
1133 default:
1134 case AUDIO_FORMAT_PCM_16_BIT:
1135 pcm_format = PCM_FORMAT_S16_LE;
1136 break;
1137 }
1138 return pcm_format;
1139}
1140
Ashish Jainf1eaa582016-05-23 20:54:24 +05301141uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1142 uint32_t sample_rate,
1143 uint32_t noOfChannels)
1144{
1145 uint32_t fragment_size = 0;
1146 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1147
1148 fragment_size = (pcm_offload_time
1149 * sample_rate
1150 * bytes_per_sample
1151 * noOfChannels)/1000;
1152 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1153 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1154 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1155 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1156 /*To have same PCM samples for all channels, the buffer size requires to
1157 *be multiple of (number of channels * bytes per sample)
1158 *For writes to succeed, the buffer must be written at address which is multiple of 32
1159 */
1160 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1161
1162 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1163 return fragment_size;
1164}
1165
1166/* Calculates the fragment size required to configure compress session.
1167 * Based on the alsa format selected, decide if conversion is needed in
1168
1169 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1170 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1171 */
1172void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1173{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301174 audio_format_t dst_format = out->hal_op_format;
1175 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301176 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1177 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1178
1179 out->compr_config.fragment_size =
1180 get_alsa_fragment_size(hal_op_bytes_per_sample,
1181 out->sample_rate,
1182 popcount(out->channel_mask));
1183
1184 if ((src_format != dst_format) &&
1185 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1186
Ashish Jain83a6cc22016-06-28 14:34:17 +05301187 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301188 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1189 hal_op_bytes_per_sample);
1190 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301191 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301192 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301193 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301194 }
1195}
1196
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301197/* converts pcm format 24_8 to 8_24 inplace */
1198size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1199{
1200 size_t i = 0;
1201 int *int_buf_stream = buf;
1202
1203 if ((bytes % 4) != 0) {
1204 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1205 return -EINVAL;
1206 }
1207
1208 for (; i < (bytes / 4); i++)
1209 int_buf_stream[i] >>= 8;
1210
1211 return bytes;
1212}
1213
1214int get_snd_codec_id(audio_format_t format)
1215{
1216 int id = 0;
1217
1218 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1219 case AUDIO_FORMAT_MP3:
1220 id = SND_AUDIOCODEC_MP3;
1221 break;
1222 case AUDIO_FORMAT_AAC:
1223 id = SND_AUDIOCODEC_AAC;
1224 break;
1225 case AUDIO_FORMAT_AAC_ADTS:
1226 id = SND_AUDIOCODEC_AAC;
1227 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301228 case AUDIO_FORMAT_AAC_LATM:
1229 id = SND_AUDIOCODEC_AAC;
1230 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301231 case AUDIO_FORMAT_PCM:
1232 id = SND_AUDIOCODEC_PCM;
1233 break;
1234 case AUDIO_FORMAT_FLAC:
1235 id = SND_AUDIOCODEC_FLAC;
1236 break;
1237 case AUDIO_FORMAT_ALAC:
1238 id = SND_AUDIOCODEC_ALAC;
1239 break;
1240 case AUDIO_FORMAT_APE:
1241 id = SND_AUDIOCODEC_APE;
1242 break;
1243 case AUDIO_FORMAT_VORBIS:
1244 id = SND_AUDIOCODEC_VORBIS;
1245 break;
1246 case AUDIO_FORMAT_WMA:
1247 id = SND_AUDIOCODEC_WMA;
1248 break;
1249 case AUDIO_FORMAT_WMA_PRO:
1250 id = SND_AUDIOCODEC_WMA_PRO;
1251 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301252 case AUDIO_FORMAT_MP2:
1253 id = SND_AUDIOCODEC_MP2;
1254 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301255 case AUDIO_FORMAT_AC3:
1256 id = SND_AUDIOCODEC_AC3;
1257 break;
1258 case AUDIO_FORMAT_E_AC3:
1259 case AUDIO_FORMAT_E_AC3_JOC:
1260 id = SND_AUDIOCODEC_EAC3;
1261 break;
1262 case AUDIO_FORMAT_DTS:
1263 case AUDIO_FORMAT_DTS_HD:
1264 id = SND_AUDIOCODEC_DTS;
1265 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001266 case AUDIO_FORMAT_DOLBY_TRUEHD:
1267 id = SND_AUDIOCODEC_TRUEHD;
1268 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301269 case AUDIO_FORMAT_DSD:
1270 id = SND_AUDIOCODEC_DSD;
1271 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301272 case AUDIO_FORMAT_APTX:
1273 id = SND_AUDIOCODEC_APTX;
1274 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301275 default:
1276 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1277 }
1278
1279 return id;
1280}
1281
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001282void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1283 struct audio_usecase *usecase)
1284{
1285 int type = usecase->type;
1286
Dhananjay Kumar14245982017-01-16 20:21:00 +05301287 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001288 struct stream_out *out = usecase->stream.out;
1289 int snd_device = usecase->out_snd_device;
1290 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001291 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301292 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001293 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301294 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301295 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301296 platform_send_audio_calibration(adev->platform, usecase,
1297 usecase->stream.in->app_type_cfg.app_type,
1298 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001299 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301300 /* when app type is default. the sample rate is not used to send cal */
1301 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301302 platform_get_default_app_type_v2(adev->platform, usecase->type),
1303 48000);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001304 } else {
1305 /* No need to send audio calibration for voice and voip call usecases */
1306 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1307 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001308 }
1309}
1310
Ben Rombergera04fabc2014-11-14 12:16:03 -08001311// Base64 Encode and Decode
1312// Not all features supported. This must be used only with following conditions.
1313// Decode Modes: Support with and without padding
1314// CRLF not handling. So no CRLF in string to decode.
1315// Encode Modes: Supports only padding
1316int b64decode(char *inp, int ilen, uint8_t* outp)
1317{
1318 int i, j, k, ii, num;
1319 int rem, pcnt;
1320 uint32_t res=0;
1321 uint8_t getIndex[MAX_BASEINDEX_LEN];
1322 uint8_t tmp, cflag;
1323
1324 if(inp == NULL || outp == NULL || ilen <= 0) {
1325 ALOGE("[%s] received NULL pointer or zero length",__func__);
1326 return -1;
1327 }
1328
1329 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1330 for(i=0;i<BASE_TABLE_SIZE;i++) {
1331 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1332 }
1333 getIndex[(uint8_t)'=']=0;
1334
1335 j=0;k=0;
1336 num = ilen/4;
1337 rem = ilen%4;
1338 if(rem==0)
1339 num = num-1;
1340 cflag=0;
1341 for(i=0; i<num; i++) {
1342 res=0;
1343 for(ii=0;ii<4;ii++) {
1344 res = res << 6;
1345 tmp = getIndex[(uint8_t)inp[j++]];
1346 res = res | tmp;
1347 cflag = cflag | tmp;
1348 }
1349 outp[k++] = (res >> 16)&0xFF;
1350 outp[k++] = (res >> 8)&0xFF;
1351 outp[k++] = res & 0xFF;
1352 }
1353
1354 // Handle last bytes special
1355 pcnt=0;
1356 if(rem == 0) {
1357 //With padding or full data
1358 res = 0;
1359 for(ii=0;ii<4;ii++) {
1360 if(inp[j] == '=')
1361 pcnt++;
1362 res = res << 6;
1363 tmp = getIndex[(uint8_t)inp[j++]];
1364 res = res | tmp;
1365 cflag = cflag | tmp;
1366 }
1367 outp[k++] = res >> 16;
1368 if(pcnt == 2)
1369 goto done;
1370 outp[k++] = (res>>8)&0xFF;
1371 if(pcnt == 1)
1372 goto done;
1373 outp[k++] = res&0xFF;
1374 } else {
1375 //without padding
1376 res = 0;
1377 for(i=0;i<rem;i++) {
1378 res = res << 6;
1379 tmp = getIndex[(uint8_t)inp[j++]];
1380 res = res | tmp;
1381 cflag = cflag | tmp;
1382 }
1383 for(i=rem;i<4;i++) {
1384 res = res << 6;
1385 pcnt++;
1386 }
1387 outp[k++] = res >> 16;
1388 if(pcnt == 2)
1389 goto done;
1390 outp[k++] = (res>>8)&0xFF;
1391 if(pcnt == 1)
1392 goto done;
1393 outp[k++] = res&0xFF;
1394 }
1395done:
1396 if(cflag == 0xFF) {
1397 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1398 __func__, inp);
1399 return 0;
1400 }
1401 return k;
1402}
1403
1404int b64encode(uint8_t *inp, int ilen, char* outp)
1405{
1406 int i,j,k, num;
1407 int rem=0;
1408 uint32_t res=0;
1409
1410 if(inp == NULL || outp == NULL || ilen<=0) {
1411 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1412 return -1;
1413 }
1414
1415 num = ilen/3;
1416 rem = ilen%3;
1417 j=0;k=0;
1418 for(i=0; i<num; i++) {
1419 //prepare index
1420 res = inp[j++]<<16;
1421 res = res | inp[j++]<<8;
1422 res = res | inp[j++];
1423 //get output map from index
1424 outp[k++] = (char) bTable[(res>>18)&0x3F];
1425 outp[k++] = (char) bTable[(res>>12)&0x3F];
1426 outp[k++] = (char) bTable[(res>>6)&0x3F];
1427 outp[k++] = (char) bTable[res&0x3F];
1428 }
1429
1430 switch(rem) {
1431 case 1:
1432 res = inp[j++]<<16;
1433 outp[k++] = (char) bTable[res>>18];
1434 outp[k++] = (char) bTable[(res>>12)&0x3F];
1435 //outp[k++] = '=';
1436 //outp[k++] = '=';
1437 break;
1438 case 2:
1439 res = inp[j++]<<16;
1440 res = res | inp[j++]<<8;
1441 outp[k++] = (char) bTable[res>>18];
1442 outp[k++] = (char) bTable[(res>>12)&0x3F];
1443 outp[k++] = (char) bTable[(res>>6)&0x3F];
1444 //outp[k++] = '=';
1445 break;
1446 default:
1447 break;
1448 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001449 outp[k] = '\0';
1450 return k;
1451}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301452
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301453
1454int audio_extn_utils_get_codec_version(const char *snd_card_name,
1455 int card_num,
1456 char *codec_version)
1457{
1458 char procfs_path[50];
1459 FILE *fp;
1460
1461 if (strstr(snd_card_name, "tasha")) {
1462 snprintf(procfs_path, sizeof(procfs_path),
1463 "/proc/asound/card%d/codecs/tasha/version", card_num);
1464 if ((fp = fopen(procfs_path, "r")) != NULL) {
1465 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1466 fclose(fp);
1467 } else {
1468 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1469 return -ENOENT;
1470 }
1471 ALOGD("%s: codec version %s", __func__, codec_version);
1472 }
1473
1474 return 0;
1475}
1476
1477
Ashish Jain81eb2a82015-05-13 10:52:34 +05301478#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1479
1480void get_default_compressed_channel_status(
1481 unsigned char *channel_status)
1482{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301483 memset(channel_status,0,24);
1484
1485 /* block start bit in preamble bit 3 */
1486 channel_status[0] |= PROFESSIONAL;
1487 //compre out
1488 channel_status[0] |= NON_LPCM;
1489 // sample rate; fixed 48K for default/transcode
1490 channel_status[3] |= SR_48000;
1491}
1492
1493#ifdef HDMI_PASSTHROUGH_ENABLED
1494int32_t get_compressed_channel_status(void *audio_stream_data,
1495 uint32_t audio_frame_size,
1496 unsigned char *channel_status,
1497 enum audio_parser_code_type codec_type)
1498 // codec_type - AUDIO_PARSER_CODEC_AC3
1499 // - AUDIO_PARSER_CODEC_DTS
1500{
1501 unsigned char *stream;
1502 int ret = 0;
1503 stream = (unsigned char *)audio_stream_data;
1504
1505 if (audio_stream_data == NULL || audio_frame_size == 0) {
1506 ALOGW("no buffer to get channel status, return default for compress");
1507 get_default_compressed_channel_status(channel_status);
1508 return ret;
1509 }
1510
1511 memset(channel_status,0,24);
1512 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1513 {
1514 ALOGE("init audio parser failed");
1515 return -1;
1516 }
1517 ret = get_channel_status(channel_status, codec_type);
1518 return ret;
1519
1520}
1521
1522#endif
1523
1524void get_lpcm_channel_status(uint32_t sampleRate,
1525 unsigned char *channel_status)
1526{
1527 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301528 memset(channel_status,0,24);
1529 /* block start bit in preamble bit 3 */
1530 channel_status[0] |= PROFESSIONAL;
1531 //LPCM OUT
1532 channel_status[0] &= ~NON_LPCM;
1533
1534 switch (sampleRate) {
1535 case 8000:
1536 case 11025:
1537 case 12000:
1538 case 16000:
1539 case 22050:
1540 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301541 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301542 case 24000:
1543 channel_status[3] |= SR_24000;
1544 break;
1545 case 32000:
1546 channel_status[3] |= SR_32000;
1547 break;
1548 case 44100:
1549 channel_status[3] |= SR_44100;
1550 break;
1551 case 48000:
1552 channel_status[3] |= SR_48000;
1553 break;
1554 case 88200:
1555 channel_status[3] |= SR_88200;
1556 break;
1557 case 96000:
1558 channel_status[3] |= SR_96000;
1559 break;
1560 case 176400:
1561 channel_status[3] |= SR_176400;
1562 break;
1563 case 192000:
1564 channel_status[3] |= SR_192000;
1565 break;
1566 default:
1567 ALOGV("Invalid sample_rate %u\n", sampleRate);
1568 status = -1;
1569 break;
1570 }
1571}
1572
1573void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1574{
1575 unsigned char channel_status[24]={0};
1576 struct snd_aes_iec958 iec958;
1577 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1578 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301579 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301580#ifdef HDMI_PASSTHROUGH_ENABLED
1581 if (audio_extn_is_dolby_format(out->format) &&
1582 /*TODO:Extend code to support DTS passthrough*/
1583 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301584 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301585 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1586 } else
1587#endif
1588 {
1589 /*set channel status bit for LPCM*/
1590 get_lpcm_channel_status(out->sample_rate, channel_status);
1591 }
1592
1593 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1594 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1595 if (!ctl) {
1596 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1597 __func__, mixer_ctl_name);
1598 return;
1599 }
1600 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1601 ALOGE("%s: Could not set channel status for ext HDMI ",
1602 __func__);
1603 return;
1604 }
1605
1606}
1607#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301608
1609int audio_extn_utils_get_avt_device_drift(
1610 struct audio_usecase *usecase,
1611 struct audio_avt_device_drift_param *drift_param)
1612{
1613 int ret = 0, count = 0;
1614 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301615 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301616 struct mixer_ctl *ctl = NULL;
1617 struct audio_avt_device_drift_stats drift_stats;
1618 struct audio_device *adev = NULL;
1619
1620 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301621 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1622 if (!backend) {
1623 ALOGE("%s: Unsupported device %d", __func__,
1624 usecase->stream.out->devices);
1625 ret = -EINVAL;
1626 goto done;
1627 }
1628 strlcpy(avt_device_drift_mixer_ctl_name,
1629 backend,
1630 MIXER_PATH_MAX_LENGTH);
1631
1632 count = strlen(backend);
1633 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1634 strlcat(&avt_device_drift_mixer_ctl_name[count],
1635 " DRIFT",
1636 MIXER_PATH_MAX_LENGTH - count);
1637 } else {
1638 ret = -EINVAL;
1639 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301640 }
1641 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301642 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301643 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301644 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301645 }
1646
Naresh Tanniru6160c712017-04-17 15:43:48 +05301647 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301648 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1649 if (!ctl) {
1650 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1651 __func__, avt_device_drift_mixer_ctl_name);
1652
1653 ret = -EINVAL;
1654 goto done;
1655 }
1656
1657 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1658 avt_device_drift_mixer_ctl_name);
1659
1660 mixer_ctl_update(ctl);
1661 count = mixer_ctl_get_num_values(ctl);
1662 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1663 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1664 __func__);
1665
1666 ret = -EINVAL;
1667 goto done;
1668 }
1669
1670 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1671 if (ret != 0) {
1672 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1673 __func__);
1674
1675 ret = -EINVAL;
1676 goto done;
1677 }
1678 memcpy(drift_param, &drift_stats.drift_param,
1679 sizeof(struct audio_avt_device_drift_param));
1680done:
1681 return ret;
1682}
Manish Dewangan07de2142017-02-27 19:27:20 +05301683
1684#ifdef SNDRV_COMPRESS_PATH_DELAY
1685int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1686{
1687 int ret = -EINVAL;
1688 struct snd_compr_metadata metadata;
1689 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1690
1691 if (property_get_bool("audio.playback.dsp.pathdelay", false)) {
1692 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1693 if (!(is_offload_usecase(out->usecase))) {
1694 ALOGE("%s:: not supported for non offload session", __func__);
1695 goto exit;
1696 }
1697
1698 if (!out->compr) {
1699 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1700 __func__);
1701 goto exit;
1702 }
1703
1704 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1705 ret = compress_get_metadata(out->compr, &metadata);
1706 if(ret) {
1707 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1708 goto exit;
1709 }
1710 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1711 } else {
1712 ALOGD("%s:: Using Fix DSP delay",__func__);
1713 }
1714
1715exit:
1716 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1717 return delay_ms;
1718}
1719#else
1720int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1721{
1722 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1723}
1724#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301725
1726#ifdef SNDRV_COMPRESS_RENDER_MODE
1727int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1728{
1729 struct snd_compr_metadata metadata;
1730 int ret = -EINVAL;
1731
1732 if (!(is_offload_usecase(out->usecase))) {
1733 ALOGE("%s:: not supported for non offload session", __func__);
1734 goto exit;
1735 }
1736
1737 if (!out->compr) {
1738 ALOGD("%s:: Invalid compress handle",
1739 __func__);
1740 goto exit;
1741 }
1742
1743 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1744
1745 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1746 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1747 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1748 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1749 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1750 } else {
1751 ret = 0;
1752 goto exit;
1753 }
1754 ret = compress_set_metadata(out->compr, &metadata);
1755 if(ret) {
1756 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1757 }
1758exit:
1759 return ret;
1760}
1761#else
1762int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1763{
1764 ALOGD("%s:: configuring render mode not supported", __func__);
1765 return 0;
1766}
1767#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301768
1769#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1770int audio_extn_utils_compress_set_clk_rec_mode(
1771 struct audio_usecase *usecase)
1772{
1773 struct snd_compr_metadata metadata;
1774 struct stream_out *out = NULL;
1775 int ret = -EINVAL;
1776
Naresh Tanniru7586e292017-04-13 10:38:13 +05301777 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301778 ALOGE("%s:: Invalid use case", __func__);
1779 goto exit;
1780 }
1781
1782 out = usecase->stream.out;
1783 if (!out) {
1784 ALOGE("%s:: invalid stream", __func__);
1785 goto exit;
1786 }
1787
1788 if (!is_offload_usecase(out->usecase)) {
1789 ALOGE("%s:: not supported for non offload session", __func__);
1790 goto exit;
1791 }
1792
1793 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1794 ALOGD("%s:: clk recovery is only supported in STC render mode",
1795 __func__);
1796 ret = 0;
1797 goto exit;
1798 }
1799
1800 if (!out->compr) {
1801 ALOGD("%s:: Invalid compress handle",
1802 __func__);
1803 goto exit;
1804 }
1805 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1806 switch(usecase->out_snd_device) {
1807 case SND_DEVICE_OUT_HDMI:
1808 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1809 case SND_DEVICE_OUT_DISPLAY_PORT:
1810 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1811 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1812 break;
1813 default:
1814 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1815 break;
1816 }
1817
1818 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1819
1820 ret = compress_set_metadata(out->compr, &metadata);
1821 if(ret) {
1822 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1823 }
1824
1825exit:
1826 return ret;
1827}
1828#else
1829int audio_extn_utils_compress_set_clk_rec_mode(
1830 struct audio_usecase *usecase __unused)
1831{
1832 ALOGD("%s:: configuring render mode not supported", __func__);
1833 return 0;
1834}
1835#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301836
1837#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1838int audio_extn_utils_compress_set_render_window(
1839 struct stream_out *out,
1840 struct audio_out_render_window_param *render_window)
1841{
1842 struct snd_compr_metadata metadata;
1843 int ret = -EINVAL;
1844
Manish Dewangan27346042017-03-01 12:56:12 +05301845 if(render_window == NULL) {
1846 ALOGE("%s:: Invalid render_window", __func__);
1847 goto exit;
1848 }
1849
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001850 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1851 __func__,render_window->render_ws, render_window->render_we);
1852
Manish Dewangan27346042017-03-01 12:56:12 +05301853 if (!is_offload_usecase(out->usecase)) {
1854 ALOGE("%s:: not supported for non offload session", __func__);
1855 goto exit;
1856 }
1857
Naresh Tanniru6160c712017-04-17 15:43:48 +05301858 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1859 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301860 ALOGD("%s:: only supported in timestamp mode, current "
1861 "render mode mode %d", __func__, out->render_mode);
1862 goto exit;
1863 }
1864
1865 if (!out->compr) {
1866 ALOGW("%s:: offload session not yet opened,"
1867 "render window will be configure later", __func__);
1868 /* store render window to reconfigure in start_output_stream() */
1869 goto exit;
1870 }
1871
1872 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1873 /*render window start value */
1874 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1875 metadata.value[1] = \
1876 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1877 /*render window end value */
1878 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1879 metadata.value[3] = \
1880 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1881
1882 ret = compress_set_metadata(out->compr, &metadata);
1883 if(ret) {
1884 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1885 }
1886
1887exit:
1888 return ret;
1889}
1890#else
1891int audio_extn_utils_compress_set_render_window(
1892 struct stream_out *out __unused,
1893 struct audio_out_render_window_param *render_window __unused)
1894{
1895 ALOGD("%s:: configuring render window not supported", __func__);
1896 return 0;
1897}
1898#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05301899
1900#ifdef SNDRV_COMPRESS_START_DELAY
1901int audio_extn_utils_compress_set_start_delay(
1902 struct stream_out *out,
1903 struct audio_out_start_delay_param *delay_param)
1904{
1905 struct snd_compr_metadata metadata;
1906 int ret = -EINVAL;
1907
1908 if(delay_param == NULL) {
1909 ALOGE("%s:: Invalid delay_param", __func__);
1910 goto exit;
1911 }
1912
1913 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
1914 delay_param->start_delay);
1915
1916 if (!is_offload_usecase(out->usecase)) {
1917 ALOGE("%s:: not supported for non offload session", __func__);
1918 goto exit;
1919 }
1920
Naresh Tanniru6160c712017-04-17 15:43:48 +05301921 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1922 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05301923 ALOGD("%s:: only supported in timestamp mode, current "
1924 "render mode mode %d", __func__, out->render_mode);
1925 goto exit;
1926 }
1927
1928 if (!out->compr) {
1929 ALOGW("%s:: offload session not yet opened,"
1930 "start delay will be configure later", __func__);
1931 goto exit;
1932 }
1933
1934 metadata.key = SNDRV_COMPRESS_START_DELAY;
1935 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
1936 metadata.value[1] = \
1937 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
1938
1939 ret = compress_set_metadata(out->compr, &metadata);
1940 if(ret) {
1941 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1942 }
1943
1944exit:
1945 return ret;
1946}
1947#else
1948int audio_extn_utils_compress_set_start_delay(
1949 struct stream_out *out __unused,
1950 struct audio_out_start_delay_param *delay_param __unused)
1951{
1952 ALOGD("%s:: configuring render window not supported", __func__);
1953 return 0;
1954}
1955#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07001956
1957#define MAX_SND_CARD 8
1958#define RETRY_US 500000
1959#define RETRY_NUMBER 10
1960
1961int audio_extn_utils_get_snd_card_num()
1962{
1963
1964 void *hw_info = NULL;
1965 struct mixer *mixer = NULL;
1966 int retry_num = 0;
1967 int snd_card_num = 0;
1968 char* snd_card_name = NULL;
1969
1970 while (snd_card_num < MAX_SND_CARD) {
1971 mixer = mixer_open(snd_card_num);
1972
1973 while (!mixer && retry_num < RETRY_NUMBER) {
1974 usleep(RETRY_US);
1975 mixer = mixer_open(snd_card_num);
1976 retry_num++;
1977 }
1978
1979 if (!mixer) {
1980 ALOGE("%s: Unable to open the mixer card: %d", __func__,
1981 snd_card_num);
1982 retry_num = 0;
1983 snd_card_num++;
1984 continue;
1985 }
1986
1987 snd_card_name = strdup(mixer_get_name(mixer));
1988 if (!snd_card_name) {
1989 ALOGE("failed to allocate memory for snd_card_name\n");
1990 mixer_close(mixer);
1991 return -1;
1992 }
1993 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
1994
1995 hw_info = hw_info_init(snd_card_name);
1996 if (hw_info) {
1997 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
1998 break;
1999 }
2000 ALOGE("%s: Failed to init hardware info", __func__);
2001 retry_num = 0;
2002 snd_card_num++;
2003 free(snd_card_name);
2004 mixer_close(mixer);
2005 }
2006
2007 mixer_close(mixer);
2008 hw_info_deinit(hw_info);
2009 if (snd_card_name)
2010 free(snd_card_name);
2011
2012 if (snd_card_num >= MAX_SND_CARD) {
2013 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2014 return -1;
2015 }
2016
2017 return snd_card_num;
2018}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302019
2020#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2021int audio_extn_utils_compress_enable_drift_correction(
2022 struct stream_out *out,
2023 struct audio_out_enable_drift_correction *drift)
2024{
2025 struct snd_compr_metadata metadata;
2026 int ret = -EINVAL;
2027
2028 if(drift == NULL) {
2029 ALOGE("%s:: Invalid param", __func__);
2030 goto exit;
2031 }
2032
2033 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2034
2035 if (!is_offload_usecase(out->usecase)) {
2036 ALOGE("%s:: not supported for non offload session", __func__);
2037 goto exit;
2038 }
2039
2040 if (!out->compr) {
2041 ALOGW("%s:: offload session not yet opened,"
2042 "start delay will be configure later", __func__);
2043 goto exit;
2044 }
2045
2046 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2047 metadata.value[0] = drift->enable;
2048 out->drift_correction_enabled = drift->enable;
2049
2050 ret = compress_set_metadata(out->compr, &metadata);
2051 if(ret) {
2052 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2053 out->drift_correction_enabled = false;
2054 }
2055
2056exit:
2057 return ret;
2058}
2059#else
2060int audio_extn_utils_compress_enable_drift_correction(
2061 struct stream_out *out __unused,
2062 struct audio_out_enable_drift_correction *drift __unused)
2063{
2064 ALOGD("%s:: configuring drift enablement not supported", __func__);
2065 return 0;
2066}
2067#endif
2068
2069#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2070int audio_extn_utils_compress_correct_drift(
2071 struct stream_out *out,
2072 struct audio_out_correct_drift *drift_param)
2073{
2074 struct snd_compr_metadata metadata;
2075 int ret = -EINVAL;
2076
2077 if (drift_param == NULL) {
2078 ALOGE("%s:: Invalid drift_param", __func__);
2079 goto exit;
2080 }
2081
2082 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2083 drift_param->adjust_time);
2084
2085 if (!is_offload_usecase(out->usecase)) {
2086 ALOGE("%s:: not supported for non offload session", __func__);
2087 goto exit;
2088 }
2089
2090 if (!out->compr) {
2091 ALOGW("%s:: offload session not yet opened", __func__);
2092 goto exit;
2093 }
2094
2095 if (!out->drift_correction_enabled) {
2096 ALOGE("%s:: drift correction not enabled", __func__);
2097 goto exit;
2098 }
2099
2100 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2101 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2102 metadata.value[1] = \
2103 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2104
2105 ret = compress_set_metadata(out->compr, &metadata);
2106 if(ret)
2107 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2108exit:
2109 return ret;
2110}
2111#else
2112int audio_extn_utils_compress_correct_drift(
2113 struct stream_out *out __unused,
2114 struct audio_out_correct_drift *drift_param __unused)
2115{
2116 ALOGD("%s:: setting adjust clock not supported", __func__);
2117 return 0;
2118}
2119#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302120
2121int audio_extn_utils_set_channel_map(
2122 struct stream_out *out,
2123 struct audio_out_channel_map_param *channel_map_param)
2124{
2125 int ret = -EINVAL, i = 0;
2126 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2127
2128 if (channel_map_param == NULL) {
2129 ALOGE("%s:: Invalid channel_map", __func__);
2130 goto exit;
2131 }
2132
2133 if (channel_map_param->channels != channels) {
2134 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2135 __func__, channel_map_param->channels, channels);
2136 goto exit;
2137 }
2138
2139 for ( i = 0; i < channels; i++) {
2140 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2141 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2142 }
2143 ret = 0;
2144exit:
2145 return ret;
2146}