blob: 27bbae8281b513b2ade39d945a2666cdc44f877b [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>
Ashish Jain81eb2a82015-05-13 10:52:34 +053042#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
43#ifdef HDMI_PASSTHROUGH_ENABLED
44#include "audio_parsers.h"
45#endif
46#endif
47
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053048#ifdef LINUX_ENABLED
49#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053050#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053051#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070052#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053053#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053054#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070055
56#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053057#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070058
59#define DYNAMIC_VALUE_TAG "dynamic"
60#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053061#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062#define FORMATS_TAG "formats"
63#define SAMPLING_RATES_TAG "sampling_rates"
64#define BIT_WIDTH_TAG "bit_width"
65#define APP_TYPE_TAG "app_type"
66
67#define STRING_TO_ENUM(string) { #string, string }
68#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
69
Ben Rombergera04fabc2014-11-14 12:16:03 -080070#define BASE_TABLE_SIZE 64
71#define MAX_BASEINDEX_LEN 256
72
Ben Romberger1aaaf862017-04-06 17:49:46 -070073#ifndef SND_AUDIOCODEC_TRUEHD
74#define SND_AUDIOCODEC_TRUEHD 0x00000023
75#endif
76
Ashish Jain81eb2a82015-05-13 10:52:34 +053077#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
78#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
79#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
80#define SR_44100 (0<<0) /* 44.1kHz */
81#define SR_NOTID (1<<0) /* non indicated */
82#define SR_48000 (2<<0) /* 48kHz */
83#define SR_32000 (3<<0) /* 32kHz */
84#define SR_22050 (4<<0) /* 22.05kHz */
85#define SR_24000 (6<<0) /* 24kHz */
86#define SR_88200 (8<<0) /* 88.2kHz */
87#define SR_96000 (10<<0) /* 96kHz */
88#define SR_176400 (12<<0) /* 176.4kHz */
89#define SR_192000 (14<<0) /* 192kHz */
90
91#endif
Manish Dewangan07de2142017-02-27 19:27:20 +053092
93/* ToDo: Check and update a proper value in msec */
94#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
95
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070096struct string_to_enum {
97 const char *name;
98 uint32_t value;
99};
100
101const struct string_to_enum s_flag_name_to_enum_table[] = {
102 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
103 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
104 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800105 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700106 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
107 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
108 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700109 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700110#ifdef INCALL_MUSIC_ENABLED
111 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
112#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700113 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530114 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
115 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
116 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
117 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
118 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530119 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700120};
121
122const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530123 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700124 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530125 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
126 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530127 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
129 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
130 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700131 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
132 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700133 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700134 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700135 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530136 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700137 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530138#ifdef AUDIO_EXTN_FORMATS_ENABLED
139 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700140 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
141 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
142 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700143 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
144 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
145 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
146 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
147 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
148 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
149 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700150 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530151 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
152 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700153 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800154 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
155 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
156 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530157 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530158 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
159 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
160 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530161 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530162 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
163 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
164 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
165 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530166 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167#endif
168};
169
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530170/* payload structure avt_device drift query */
171struct audio_avt_device_drift_stats {
172 uint32_t minor_version;
173 /* Indicates the device interface direction as either
174 * source (Tx) or sink (Rx).
175 */
176 uint16_t device_direction;
177 /*params exposed to client */
178 struct audio_avt_device_drift_param drift_param;
179};
180
Ben Rombergera04fabc2014-11-14 12:16:03 -0800181static char bTable[BASE_TABLE_SIZE] = {
182 'A','B','C','D','E','F','G','H','I','J','K','L',
183 'M','N','O','P','Q','R','S','T','U','V','W','X',
184 'Y','Z','a','b','c','d','e','f','g','h','i','j',
185 'k','l','m','n','o','p','q','r','s','t','u','v',
186 'w','x','y','z','0','1','2','3','4','5','6','7',
187 '8','9','+','/'
188};
189
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700190static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
191 const char *name)
192{
193 size_t i;
194 for (i = 0; i < size; i++) {
195 if (strcmp(table[i].name, name) == 0) {
196 ALOGV("%s found %s", __func__, table[i].name);
197 return table[i].value;
198 }
199 }
200 return 0;
201}
202
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530203static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700204{
205 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530206 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800207 char *last_r;
208 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700209 while (flag_name != NULL) {
210 if (strlen(flag_name) != 0) {
211 flag |= string_to_enum(s_flag_name_to_enum_table,
212 ARRAY_SIZE(s_flag_name_to_enum_table),
213 flag_name);
214 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800215 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700216 }
217
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800218 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530219 io_flags.in_flags = (audio_input_flags_t)flag;
220 io_flags.out_flags = (audio_output_flags_t)flag;
221 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700222}
223
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530224static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700225{
226 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800227 char *last_r;
228 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700229
230 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
231 return;
232
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530233 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700234 while (str != NULL) {
235 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
236 ARRAY_SIZE(s_format_name_to_enum_table), str);
237 ALOGV("%s: format - %d", __func__, format);
238 if (format != 0) {
239 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700240 if (sf_info == NULL)
241 break; /* return whatever was parsed */
242
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700243 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530244 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700245 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800246 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700247 }
248}
249
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530250static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700252 struct stream_sample_rate *ss_info = NULL;
253 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800254 char *last_r;
255 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700256
Amit Shekhar6f461b12014-08-01 14:52:58 -0700257 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
258 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700259
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530260 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700261 while (str != NULL) {
262 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
263 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
264 if (0 != sample_rate) {
265 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800266 if (!ss_info) {
267 ALOGE("%s: memory allocation failure", __func__);
268 return;
269 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700270 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530271 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700272 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800273 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700274 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275}
276
277static int parse_bit_width_names(char *name)
278{
279 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800280 char *last_r;
281 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700282
283 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
284 bit_width = (int)strtol(str, (char **)NULL, 10);
285
286 ALOGV("%s: bit_width - %d", __func__, bit_width);
287 return bit_width;
288}
289
290static int parse_app_type_names(void *platform, char *name)
291{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700292 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800293 char *last_r;
294 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700295
296 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
297 app_type = (int)strtol(str, (char **)NULL, 10);
298
299 ALOGV("%s: app_type - %d", __func__, app_type);
300 return app_type;
301}
302
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530303static void update_streams_cfg_list(cnode *root, void *platform,
304 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305{
306 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530307 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700308
309 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530310 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700311
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530312 if (!s_info) {
313 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700314 return;
315 }
316
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700317 while (node) {
318 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530319 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530320 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
321 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700322 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530323 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700324 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530325 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
326 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700327 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530328 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700329 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530330 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700331 }
332 node = node->next;
333 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530334 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700335}
336
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530337static void load_cfg_list(cnode *root, void *platform,
338 struct listnode *streams_output_cfg_list,
339 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700340{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530341 cnode *node = NULL;
342
343 node = config_find(root, OUTPUTS_TAG);
344 if (node != NULL) {
345 node = node->first_child;
346 while (node) {
347 ALOGV("%s: loading output %s", __func__, node->name);
348 update_streams_cfg_list(node, platform, streams_output_cfg_list);
349 node = node->next;
350 }
351 } else {
352 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700353 }
354
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530355 node = config_find(root, INPUTS_TAG);
356 if (node != NULL) {
357 node = node->first_child;
358 while (node) {
359 ALOGV("%s: loading input %s", __func__, node->name);
360 update_streams_cfg_list(node, platform, streams_input_cfg_list);
361 node = node->next;
362 }
363 } else {
364 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700365 }
366}
367
368static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530369 struct listnode *streams_output_cfg_list,
370 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700371{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530372 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 int length = 0, i, num_app_types = 0;
374 struct listnode *node;
375 bool update;
376 struct mixer_ctl *ctl = NULL;
377 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530378 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379
380 if (!mixer) {
381 ALOGE("%s: mixer is null",__func__);
382 return;
383 }
384 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
385 if (!ctl) {
386 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
387 return;
388 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530389 app_type_cfg[length++] = num_app_types;
390
391 if (list_empty(streams_output_cfg_list)) {
392 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700393 app_type_cfg[length++] = 48000;
394 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530395 num_app_types += 1;
396 }
397 if (list_empty(streams_input_cfg_list)) {
398 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
399 app_type_cfg[length++] = 48000;
400 app_type_cfg[length++] = 16;
401 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700402 }
403
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700404 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530405 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700406 update = true;
407 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530408 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700409 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530410 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530411 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530412 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530413 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530414 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700415 update = false;
416 break;
417 }
418 }
419 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530420 num_app_types += 1;
421 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
422 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
423 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
424 }
425 }
426 list_for_each(node, streams_input_cfg_list) {
427 s_info = node_to_item(node, struct streams_io_cfg, list);
428 update = true;
429 for (i=0; i<length; i=i+3) {
430 if (app_type_cfg[i+1] == 0)
431 break;
432 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530433 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530434 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530435 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530436 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
437 update = false;
438 break;
439 }
440 }
441 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
442 num_app_types += 1;
443 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
444 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
445 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700446 }
447 }
448 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
449 if (num_app_types) {
450 app_type_cfg[0] = num_app_types;
451 mixer_ctl_set_array(ctl, app_type_cfg, length);
452 }
453}
454
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530455void audio_extn_utils_update_streams_cfg_lists(void *platform,
456 struct mixer *mixer,
457 struct listnode *streams_output_cfg_list,
458 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700459{
460 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530461 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700462
463 ALOGV("%s", __func__);
464 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530465 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700466
467 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700468 if (root == NULL) {
469 ALOGE("cfg_list, NULL config root");
470 return;
471 }
472
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530473 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
474 if (data == NULL) {
475 ALOGD("%s: failed to open io config file(%s), trying older config file",
476 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
477 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
478 if (data == NULL) {
479 send_app_type_cfg(platform, mixer,
480 streams_output_cfg_list,
481 streams_input_cfg_list);
482 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800483 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530484 return;
485 }
486 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700487
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530488 config_load(root, data);
489 load_cfg_list(root, platform, streams_output_cfg_list,
490 streams_input_cfg_list);
491
492 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
493 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800494
495 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800496 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800497 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700498}
499
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530500static void audio_extn_utils_dump_streams_cfg_list(
501 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700502{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700503 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530504 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700505 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700506 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530507
508 list_for_each(node_i, streams_cfg_list) {
509 s_info = node_to_item(node_i, struct streams_io_cfg, list);
510 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
511 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
512 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
513 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700514 sf_info = node_to_item(node_j, struct stream_format, list);
515 ALOGV("format-%x", sf_info->format);
516 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530517 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700518 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
519 ALOGV("sample rate-%d", ss_info->sample_rate);
520 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700521 }
522}
523
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530524void audio_extn_utils_dump_streams_cfg_lists(
525 struct listnode *streams_output_cfg_list,
526 struct listnode *streams_input_cfg_list)
527{
528 ALOGV("%s", __func__);
529 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
530 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
531}
532
533static void audio_extn_utils_release_streams_cfg_list(
534 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700535{
536 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530537 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700538
539 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530540
541 while (!list_empty(streams_cfg_list)) {
542 node_i = list_head(streams_cfg_list);
543 s_info = node_to_item(node_i, struct streams_io_cfg, list);
544 while (!list_empty(&s_info->format_list)) {
545 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700546 list_remove(node_j);
547 free(node_to_item(node_j, struct stream_format, list));
548 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549 while (!list_empty(&s_info->sample_rate_list)) {
550 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700551 list_remove(node_j);
552 free(node_to_item(node_j, struct stream_sample_rate, list));
553 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700554 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530555 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700556 }
557}
558
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530559void audio_extn_utils_release_streams_cfg_lists(
560 struct listnode *streams_output_cfg_list,
561 struct listnode *streams_input_cfg_list)
562{
563 ALOGV("%s", __func__);
564 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
565 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
566}
567
568static bool set_app_type_cfg(struct streams_io_cfg *s_info,
569 struct stream_app_type_cfg *app_type_cfg,
570 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700571 {
572 struct listnode *node_i;
573 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530574 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700575 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
576 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530577 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700578
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530579 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700580 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530581 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700582 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
583 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
584 return true;
585 }
586 }
587 /*
588 * Reiterate through the list assuming dafault sample rate.
589 * Handles scenario where input sample rate is higher
590 * than all sample rates in list for the input bit width.
591 */
592 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800593
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530594 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700595 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
596 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530597 (bit_width == s_info->app_type_cfg.bit_width)) {
598 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700599 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800601 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 -0700602 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
603 return true;
604 }
605 }
606 return false;
607}
608
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530609void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
610 struct listnode *streams_input_cfg_list,
611 audio_devices_t devices __unused,
612 audio_input_flags_t flags,
613 audio_format_t format,
614 uint32_t sample_rate,
615 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530616 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530617 struct stream_app_type_cfg *app_type_cfg)
618{
619 struct listnode *node_i, *node_j;
620 struct streams_io_cfg *s_info;
621 struct stream_format *sf_info;
622
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530623 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
624 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530625
626 list_for_each(node_i, streams_input_cfg_list) {
627 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530628 /* Along with flags do profile matching if set at either end.*/
629 if (s_info->flags.in_flags == flags &&
630 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
631 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530632 list_for_each(node_j, &s_info->format_list) {
633 sf_info = node_to_item(node_j, struct stream_format, list);
634 if (sf_info->format == format) {
635 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
636 return;
637 }
638 }
639 }
640 }
641 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
642 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
643 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
644 app_type_cfg->bit_width = 16;
645}
646
647void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700648 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700649 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700650 audio_output_flags_t flags,
651 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700652 uint32_t sample_rate,
653 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530654 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530655 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700656 struct stream_app_type_cfg *app_type_cfg)
657{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530658 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530659 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700660 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530661 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700662
Ashish Jain058165c2016-09-28 23:18:48 +0530663 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700664 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700665 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
666 if (-ENOSYS != bw)
667 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700668 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
669 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
670 }
671
Manish Dewangan837dc462015-05-27 10:17:41 +0530672 property_get("audio.playback.mch.downsample",value,"");
673 if (!strncmp("true", value, sizeof("true"))) {
674 if ((popcount(channel_mask) > 2) &&
675 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
676 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
677 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
678 ALOGD("%s: MCH session defaulting sample rate to %d",
679 __func__, sample_rate);
680 }
681 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530682
683 /* Set sampling rate to 176.4 for DSD64
684 * and 352.8Khz for DSD128.
685 * Set Bit Width to 16. output will be 16 bit
686 * post DoP in ASM.
687 */
688 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
689 (format == AUDIO_FORMAT_DSD)) {
690 bit_width = 16;
691 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
692 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
693 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
694 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
695 }
696
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530697 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
698 //TODO: Handle fractional sampling rate configuration for LL
699 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
700 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
701 __func__, sample_rate, bit_width);
702 }
703
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800704 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
705 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700706 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530707 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530708 /* Along with flags do profile matching if set at either end.*/
709 if (s_info->flags.out_flags == flags &&
710 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
711 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530712 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700713 sf_info = node_to_item(node_j, struct stream_format, list);
714 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530715 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700716 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700717 }
718 }
719 }
720 }
721 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530722 s_info = node_to_item(node_i, struct streams_io_cfg, list);
723 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700724 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530725 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
726 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
727 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700728 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530729 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700730 return;
731 }
732 }
733 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700734 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700735 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700736 app_type_cfg->bit_width = 16;
737}
738
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530739static bool audio_is_this_native_usecase(struct audio_usecase *uc)
740{
741 bool native_usecase = false;
742 struct stream_out *out = (struct stream_out*) uc->stream.out;
743
744 if (PCM_PLAYBACK == uc->type && out != NULL &&
745 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
746 is_offload_usecase(uc->id) &&
747 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
748 native_usecase = true;
749
750 return native_usecase;
751}
752
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800753
754static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
755{
756 return adev->vr_audio_mode_enabled;
757}
758
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530759void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
760 struct audio_device *adev,
761 struct audio_usecase *usecase)
762{
763 ALOGV("%s", __func__);
764
765 switch(usecase->type) {
766 case PCM_PLAYBACK:
767 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
768 &adev->streams_output_cfg_list,
769 usecase->stream.out->devices,
770 usecase->stream.out->flags,
771 usecase->stream.out->format,
772 usecase->stream.out->sample_rate,
773 usecase->stream.out->bit_width,
774 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530775 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530776 &usecase->stream.out->app_type_cfg);
777 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
778 break;
779 case PCM_CAPTURE:
780 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
781 &adev->streams_input_cfg_list,
782 usecase->stream.in->device,
783 usecase->stream.in->flags,
784 usecase->stream.in->format,
785 usecase->stream.in->sample_rate,
786 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530787 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530788 &usecase->stream.in->app_type_cfg);
789 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
790 break;
791 default:
792 ALOGE("%s: app type cfg not supported for usecase type (%d)",
793 __func__, usecase->type);
794 }
795}
796
Siena Richard7c2db772016-12-21 11:32:34 -0800797static int send_app_type_cfg_for_device(struct audio_device *adev,
798 struct audio_usecase *usecase,
799 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700800{
801 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530802 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
803 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700804 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800805 int pcm_device_id = 0, acdb_dev_id, app_type;
806 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530807 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530808 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700809
Siena Richard7c2db772016-12-21 11:32:34 -0800810 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
811 __func__, platform_get_snd_device_name(usecase->out_snd_device),
812 platform_get_snd_device_name(usecase->in_snd_device),
813 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700814
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530815 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530816 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700817 rc = 0;
818 goto exit_send_app_type_cfg;
819 }
820 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
821 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
822 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800823 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530824 (!is_offload_usecase(usecase->id)) &&
825 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530826 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 -0700827 rc = 0;
828 goto exit_send_app_type_cfg;
829 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800830
831 //if VR is active then only send the mixer control
832 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
833 ALOGI("ULL doesnt need sending app type cfg, returning");
834 rc = 0;
835 goto exit_send_app_type_cfg;
836 }
837
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530838 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700839 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530840 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
841 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700842 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700843 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530844 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
845 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530846 }
Siena Richard7c2db772016-12-21 11:32:34 -0800847
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700848 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
849 if (!ctl) {
850 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
851 mixer_ctl_name);
852 rc = -EINVAL;
853 goto exit_send_app_type_cfg;
854 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530855 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530856
Rohit Kumar1181b292017-01-31 18:07:17 +0530857 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
858 if (acdb_dev_id <= 0) {
859 ALOGE("%s: Couldn't get the acdb dev id", __func__);
860 rc = -EINVAL;
861 goto exit_send_app_type_cfg;
862 }
863
Siena Richard7c2db772016-12-21 11:32:34 -0800864 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
865 if (snd_device_be_idx < 0) {
866 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
867 __func__, platform_get_snd_device_name(snd_device),
868 snd_device_be_idx);
869 }
870
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530871 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530872
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530873 property_get("audio.playback.mch.downsample",value,"");
874 if (!strncmp("true", value, sizeof("true"))) {
875 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
876 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
877 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
878 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
879 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530880
Ashish Jain4826f6c2017-02-06 13:33:20 +0530881 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700882 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530883 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
884 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
885 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
886 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
887 /*
888 * To best utlize DSP, check if the stream sample rate is supported/multiple of
889 * configured device sample rate, if not update the COPP rate to be equal to the
890 * device sample rate, else open COPP at stream sample rate
891 */
892 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
893 usecase->stream.out->sample_rate,
894 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530895 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
896 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700897 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
898 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530899 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700900 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
901 }
902 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
903
Siena Richard7c2db772016-12-21 11:32:34 -0800904 app_type = usecase->stream.out->app_type_cfg.app_type;
905 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700906 app_type_cfg[len++] = acdb_dev_id;
907 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700908 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
909 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530910 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700911 app_type_cfg[len++] = sample_rate * 4;
912 } else {
913 app_type_cfg[len++] = sample_rate;
914 }
Siena Richard7c2db772016-12-21 11:32:34 -0800915 if (snd_device_be_idx > 0)
916 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530917
Siena Richard7c2db772016-12-21 11:32:34 -0800918 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
919 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530920
921 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800922 app_type = usecase->stream.in->app_type_cfg.app_type;
923 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530924 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800925 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
926 app_type_cfg[len++] = sample_rate;
927 if (snd_device_be_idx > 0)
928 app_type_cfg[len++] = snd_device_be_idx;
929 ALOGI("%s CAPTURE 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 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800932 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
933 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530934 app_type_cfg[len++] = acdb_dev_id;
935 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800936 if (snd_device_be_idx > 0)
937 app_type_cfg[len++] = snd_device_be_idx;
938 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
939 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530940 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530941
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700942 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700943 rc = 0;
944exit_send_app_type_cfg:
945 return rc;
946}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700947
Siena Richard7c2db772016-12-21 11:32:34 -0800948int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
949 struct audio_usecase *usecase)
950{
951 int i, num_devices = 0;
952 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
953 int rc = 0;
954
955 switch (usecase->type) {
956 case PCM_PLAYBACK:
957 ALOGD("%s: usecase->out_snd_device %s",
958 __func__, platform_get_snd_device_name(usecase->out_snd_device));
959 /* check for out combo device */
960 if (platform_split_snd_device(adev->platform,
961 usecase->out_snd_device,
962 &num_devices, new_snd_devices)) {
963 new_snd_devices[0] = usecase->out_snd_device;
964 num_devices = 1;
965 }
966 break;
967 case PCM_CAPTURE:
968 ALOGD("%s: usecase->in_snd_device %s",
969 __func__, platform_get_snd_device_name(usecase->in_snd_device));
970 /* check for in combo device */
971 if (platform_split_snd_device(adev->platform,
972 usecase->in_snd_device,
973 &num_devices, new_snd_devices)) {
974 new_snd_devices[0] = usecase->in_snd_device;
975 num_devices = 1;
976 }
977 break;
978 default:
979 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
980 rc = 0;
981 break;
982 }
983
984 for (i = 0; i < num_devices; i++) {
985 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
986 if (rc)
987 break;
988 }
989
990 return rc;
991}
992
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530993int read_line_from_file(const char *path, char *buf, size_t count)
994{
995 char * fgets_ret;
996 FILE * fd;
997 int rv;
998
999 fd = fopen(path, "r");
1000 if (fd == NULL)
1001 return -1;
1002
1003 fgets_ret = fgets(buf, (int)count, fd);
1004 if (NULL != fgets_ret) {
1005 rv = (int)strlen(buf);
1006 } else {
1007 rv = ferror(fd);
1008 }
1009 fclose(fd);
1010
1011 return rv;
1012}
1013
Ashish Jainf1eaa582016-05-23 20:54:24 +05301014/*Translates ALSA formats to AOSP PCM formats*/
1015audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1016{
1017 audio_format_t format;
1018
1019 switch(alsa_format) {
1020 case SNDRV_PCM_FORMAT_S16_LE:
1021 format = AUDIO_FORMAT_PCM_16_BIT;
1022 break;
1023 case SNDRV_PCM_FORMAT_S24_3LE:
1024 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1025 break;
1026 case SNDRV_PCM_FORMAT_S24_LE:
1027 format = AUDIO_FORMAT_PCM_8_24_BIT;
1028 break;
1029 case SNDRV_PCM_FORMAT_S32_LE:
1030 format = AUDIO_FORMAT_PCM_32_BIT;
1031 break;
1032 default:
1033 ALOGW("Incorrect ALSA format");
1034 format = AUDIO_FORMAT_INVALID;
1035 }
1036 return format;
1037}
1038
1039/*Translates hal format (AOSP) to alsa formats*/
1040uint32_t hal_format_to_alsa(audio_format_t hal_format)
1041{
1042 uint32_t alsa_format;
1043
1044 switch (hal_format) {
1045 case AUDIO_FORMAT_PCM_32_BIT: {
1046 if (platform_supports_true_32bit())
1047 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1048 else
1049 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1050 }
1051 break;
1052 case AUDIO_FORMAT_PCM_8_BIT:
1053 alsa_format = SNDRV_PCM_FORMAT_S8;
1054 break;
1055 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1056 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1057 break;
1058 case AUDIO_FORMAT_PCM_8_24_BIT: {
1059 if (platform_supports_true_32bit())
1060 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1061 else
1062 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1063 }
1064 break;
1065 case AUDIO_FORMAT_PCM_FLOAT:
1066 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1067 break;
1068 default:
1069 case AUDIO_FORMAT_PCM_16_BIT:
1070 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1071 break;
1072 }
1073 return alsa_format;
1074}
1075
Ashish Jain83a6cc22016-06-28 14:34:17 +05301076/*Translates PCM formats to AOSP formats*/
1077audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1078{
1079 audio_format_t format = AUDIO_FORMAT_INVALID;
1080
1081 switch(pcm_format) {
1082 case PCM_FORMAT_S16_LE:
1083 format = AUDIO_FORMAT_PCM_16_BIT;
1084 break;
1085 case PCM_FORMAT_S24_3LE:
1086 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1087 break;
1088 case PCM_FORMAT_S24_LE:
1089 format = AUDIO_FORMAT_PCM_8_24_BIT;
1090 break;
1091 case PCM_FORMAT_S32_LE:
1092 format = AUDIO_FORMAT_PCM_32_BIT;
1093 break;
1094 default:
1095 ALOGW("Incorrect PCM format");
1096 format = AUDIO_FORMAT_INVALID;
1097 }
1098 return format;
1099}
1100
1101/*Translates hal format (AOSP) to alsa formats*/
1102uint32_t hal_format_to_pcm(audio_format_t hal_format)
1103{
1104 uint32_t pcm_format;
1105
1106 switch (hal_format) {
1107 case AUDIO_FORMAT_PCM_32_BIT:
1108 case AUDIO_FORMAT_PCM_8_24_BIT:
1109 case AUDIO_FORMAT_PCM_FLOAT: {
1110 if (platform_supports_true_32bit())
1111 pcm_format = PCM_FORMAT_S32_LE;
1112 else
1113 pcm_format = PCM_FORMAT_S24_3LE;
1114 }
1115 break;
1116 case AUDIO_FORMAT_PCM_8_BIT:
1117 pcm_format = PCM_FORMAT_S8;
1118 break;
1119 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1120 pcm_format = PCM_FORMAT_S24_3LE;
1121 break;
1122 default:
1123 case AUDIO_FORMAT_PCM_16_BIT:
1124 pcm_format = PCM_FORMAT_S16_LE;
1125 break;
1126 }
1127 return pcm_format;
1128}
1129
Ashish Jainf1eaa582016-05-23 20:54:24 +05301130uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1131 uint32_t sample_rate,
1132 uint32_t noOfChannels)
1133{
1134 uint32_t fragment_size = 0;
1135 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1136
1137 fragment_size = (pcm_offload_time
1138 * sample_rate
1139 * bytes_per_sample
1140 * noOfChannels)/1000;
1141 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1142 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1143 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1144 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1145 /*To have same PCM samples for all channels, the buffer size requires to
1146 *be multiple of (number of channels * bytes per sample)
1147 *For writes to succeed, the buffer must be written at address which is multiple of 32
1148 */
1149 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1150
1151 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1152 return fragment_size;
1153}
1154
1155/* Calculates the fragment size required to configure compress session.
1156 * Based on the alsa format selected, decide if conversion is needed in
1157
1158 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1159 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1160 */
1161void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1162{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301163 audio_format_t dst_format = out->hal_op_format;
1164 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301165 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1166 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1167
1168 out->compr_config.fragment_size =
1169 get_alsa_fragment_size(hal_op_bytes_per_sample,
1170 out->sample_rate,
1171 popcount(out->channel_mask));
1172
1173 if ((src_format != dst_format) &&
1174 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1175
Ashish Jain83a6cc22016-06-28 14:34:17 +05301176 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301177 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1178 hal_op_bytes_per_sample);
1179 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301180 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301181 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301182 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301183 }
1184}
1185
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301186/* converts pcm format 24_8 to 8_24 inplace */
1187size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1188{
1189 size_t i = 0;
1190 int *int_buf_stream = buf;
1191
1192 if ((bytes % 4) != 0) {
1193 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1194 return -EINVAL;
1195 }
1196
1197 for (; i < (bytes / 4); i++)
1198 int_buf_stream[i] >>= 8;
1199
1200 return bytes;
1201}
1202
1203int get_snd_codec_id(audio_format_t format)
1204{
1205 int id = 0;
1206
1207 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1208 case AUDIO_FORMAT_MP3:
1209 id = SND_AUDIOCODEC_MP3;
1210 break;
1211 case AUDIO_FORMAT_AAC:
1212 id = SND_AUDIOCODEC_AAC;
1213 break;
1214 case AUDIO_FORMAT_AAC_ADTS:
1215 id = SND_AUDIOCODEC_AAC;
1216 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301217 case AUDIO_FORMAT_AAC_LATM:
1218 id = SND_AUDIOCODEC_AAC;
1219 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301220 case AUDIO_FORMAT_PCM:
1221 id = SND_AUDIOCODEC_PCM;
1222 break;
1223 case AUDIO_FORMAT_FLAC:
1224 id = SND_AUDIOCODEC_FLAC;
1225 break;
1226 case AUDIO_FORMAT_ALAC:
1227 id = SND_AUDIOCODEC_ALAC;
1228 break;
1229 case AUDIO_FORMAT_APE:
1230 id = SND_AUDIOCODEC_APE;
1231 break;
1232 case AUDIO_FORMAT_VORBIS:
1233 id = SND_AUDIOCODEC_VORBIS;
1234 break;
1235 case AUDIO_FORMAT_WMA:
1236 id = SND_AUDIOCODEC_WMA;
1237 break;
1238 case AUDIO_FORMAT_WMA_PRO:
1239 id = SND_AUDIOCODEC_WMA_PRO;
1240 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301241 case AUDIO_FORMAT_MP2:
1242 id = SND_AUDIOCODEC_MP2;
1243 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301244 case AUDIO_FORMAT_AC3:
1245 id = SND_AUDIOCODEC_AC3;
1246 break;
1247 case AUDIO_FORMAT_E_AC3:
1248 case AUDIO_FORMAT_E_AC3_JOC:
1249 id = SND_AUDIOCODEC_EAC3;
1250 break;
1251 case AUDIO_FORMAT_DTS:
1252 case AUDIO_FORMAT_DTS_HD:
1253 id = SND_AUDIOCODEC_DTS;
1254 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001255 case AUDIO_FORMAT_DOLBY_TRUEHD:
1256 id = SND_AUDIOCODEC_TRUEHD;
1257 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301258 case AUDIO_FORMAT_DSD:
1259 id = SND_AUDIOCODEC_DSD;
1260 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301261 case AUDIO_FORMAT_APTX:
1262 id = SND_AUDIOCODEC_APTX;
1263 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301264 default:
1265 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1266 }
1267
1268 return id;
1269}
1270
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001271void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1272 struct audio_usecase *usecase)
1273{
1274 int type = usecase->type;
1275
Dhananjay Kumar14245982017-01-16 20:21:00 +05301276 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001277 struct stream_out *out = usecase->stream.out;
1278 int snd_device = usecase->out_snd_device;
1279 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001280 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301281 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001282 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301283 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301284 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301285 platform_send_audio_calibration(adev->platform, usecase,
1286 usecase->stream.in->app_type_cfg.app_type,
1287 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001288 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301289 /* when app type is default. the sample rate is not used to send cal */
1290 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301291 platform_get_default_app_type_v2(adev->platform, usecase->type),
1292 48000);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001293 } else {
1294 /* No need to send audio calibration for voice and voip call usecases */
1295 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1296 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001297 }
1298}
1299
Ben Rombergera04fabc2014-11-14 12:16:03 -08001300// Base64 Encode and Decode
1301// Not all features supported. This must be used only with following conditions.
1302// Decode Modes: Support with and without padding
1303// CRLF not handling. So no CRLF in string to decode.
1304// Encode Modes: Supports only padding
1305int b64decode(char *inp, int ilen, uint8_t* outp)
1306{
1307 int i, j, k, ii, num;
1308 int rem, pcnt;
1309 uint32_t res=0;
1310 uint8_t getIndex[MAX_BASEINDEX_LEN];
1311 uint8_t tmp, cflag;
1312
1313 if(inp == NULL || outp == NULL || ilen <= 0) {
1314 ALOGE("[%s] received NULL pointer or zero length",__func__);
1315 return -1;
1316 }
1317
1318 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1319 for(i=0;i<BASE_TABLE_SIZE;i++) {
1320 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1321 }
1322 getIndex[(uint8_t)'=']=0;
1323
1324 j=0;k=0;
1325 num = ilen/4;
1326 rem = ilen%4;
1327 if(rem==0)
1328 num = num-1;
1329 cflag=0;
1330 for(i=0; i<num; i++) {
1331 res=0;
1332 for(ii=0;ii<4;ii++) {
1333 res = res << 6;
1334 tmp = getIndex[(uint8_t)inp[j++]];
1335 res = res | tmp;
1336 cflag = cflag | tmp;
1337 }
1338 outp[k++] = (res >> 16)&0xFF;
1339 outp[k++] = (res >> 8)&0xFF;
1340 outp[k++] = res & 0xFF;
1341 }
1342
1343 // Handle last bytes special
1344 pcnt=0;
1345 if(rem == 0) {
1346 //With padding or full data
1347 res = 0;
1348 for(ii=0;ii<4;ii++) {
1349 if(inp[j] == '=')
1350 pcnt++;
1351 res = res << 6;
1352 tmp = getIndex[(uint8_t)inp[j++]];
1353 res = res | tmp;
1354 cflag = cflag | tmp;
1355 }
1356 outp[k++] = res >> 16;
1357 if(pcnt == 2)
1358 goto done;
1359 outp[k++] = (res>>8)&0xFF;
1360 if(pcnt == 1)
1361 goto done;
1362 outp[k++] = res&0xFF;
1363 } else {
1364 //without padding
1365 res = 0;
1366 for(i=0;i<rem;i++) {
1367 res = res << 6;
1368 tmp = getIndex[(uint8_t)inp[j++]];
1369 res = res | tmp;
1370 cflag = cflag | tmp;
1371 }
1372 for(i=rem;i<4;i++) {
1373 res = res << 6;
1374 pcnt++;
1375 }
1376 outp[k++] = res >> 16;
1377 if(pcnt == 2)
1378 goto done;
1379 outp[k++] = (res>>8)&0xFF;
1380 if(pcnt == 1)
1381 goto done;
1382 outp[k++] = res&0xFF;
1383 }
1384done:
1385 if(cflag == 0xFF) {
1386 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1387 __func__, inp);
1388 return 0;
1389 }
1390 return k;
1391}
1392
1393int b64encode(uint8_t *inp, int ilen, char* outp)
1394{
1395 int i,j,k, num;
1396 int rem=0;
1397 uint32_t res=0;
1398
1399 if(inp == NULL || outp == NULL || ilen<=0) {
1400 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1401 return -1;
1402 }
1403
1404 num = ilen/3;
1405 rem = ilen%3;
1406 j=0;k=0;
1407 for(i=0; i<num; i++) {
1408 //prepare index
1409 res = inp[j++]<<16;
1410 res = res | inp[j++]<<8;
1411 res = res | inp[j++];
1412 //get output map from index
1413 outp[k++] = (char) bTable[(res>>18)&0x3F];
1414 outp[k++] = (char) bTable[(res>>12)&0x3F];
1415 outp[k++] = (char) bTable[(res>>6)&0x3F];
1416 outp[k++] = (char) bTable[res&0x3F];
1417 }
1418
1419 switch(rem) {
1420 case 1:
1421 res = inp[j++]<<16;
1422 outp[k++] = (char) bTable[res>>18];
1423 outp[k++] = (char) bTable[(res>>12)&0x3F];
1424 //outp[k++] = '=';
1425 //outp[k++] = '=';
1426 break;
1427 case 2:
1428 res = inp[j++]<<16;
1429 res = res | inp[j++]<<8;
1430 outp[k++] = (char) bTable[res>>18];
1431 outp[k++] = (char) bTable[(res>>12)&0x3F];
1432 outp[k++] = (char) bTable[(res>>6)&0x3F];
1433 //outp[k++] = '=';
1434 break;
1435 default:
1436 break;
1437 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001438 outp[k] = '\0';
1439 return k;
1440}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301441
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301442
1443int audio_extn_utils_get_codec_version(const char *snd_card_name,
1444 int card_num,
1445 char *codec_version)
1446{
1447 char procfs_path[50];
1448 FILE *fp;
1449
1450 if (strstr(snd_card_name, "tasha")) {
1451 snprintf(procfs_path, sizeof(procfs_path),
1452 "/proc/asound/card%d/codecs/tasha/version", card_num);
1453 if ((fp = fopen(procfs_path, "r")) != NULL) {
1454 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1455 fclose(fp);
1456 } else {
1457 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1458 return -ENOENT;
1459 }
1460 ALOGD("%s: codec version %s", __func__, codec_version);
1461 }
1462
1463 return 0;
1464}
1465
1466
Ashish Jain81eb2a82015-05-13 10:52:34 +05301467#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1468
1469void get_default_compressed_channel_status(
1470 unsigned char *channel_status)
1471{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301472 memset(channel_status,0,24);
1473
1474 /* block start bit in preamble bit 3 */
1475 channel_status[0] |= PROFESSIONAL;
1476 //compre out
1477 channel_status[0] |= NON_LPCM;
1478 // sample rate; fixed 48K for default/transcode
1479 channel_status[3] |= SR_48000;
1480}
1481
1482#ifdef HDMI_PASSTHROUGH_ENABLED
1483int32_t get_compressed_channel_status(void *audio_stream_data,
1484 uint32_t audio_frame_size,
1485 unsigned char *channel_status,
1486 enum audio_parser_code_type codec_type)
1487 // codec_type - AUDIO_PARSER_CODEC_AC3
1488 // - AUDIO_PARSER_CODEC_DTS
1489{
1490 unsigned char *stream;
1491 int ret = 0;
1492 stream = (unsigned char *)audio_stream_data;
1493
1494 if (audio_stream_data == NULL || audio_frame_size == 0) {
1495 ALOGW("no buffer to get channel status, return default for compress");
1496 get_default_compressed_channel_status(channel_status);
1497 return ret;
1498 }
1499
1500 memset(channel_status,0,24);
1501 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1502 {
1503 ALOGE("init audio parser failed");
1504 return -1;
1505 }
1506 ret = get_channel_status(channel_status, codec_type);
1507 return ret;
1508
1509}
1510
1511#endif
1512
1513void get_lpcm_channel_status(uint32_t sampleRate,
1514 unsigned char *channel_status)
1515{
1516 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301517 memset(channel_status,0,24);
1518 /* block start bit in preamble bit 3 */
1519 channel_status[0] |= PROFESSIONAL;
1520 //LPCM OUT
1521 channel_status[0] &= ~NON_LPCM;
1522
1523 switch (sampleRate) {
1524 case 8000:
1525 case 11025:
1526 case 12000:
1527 case 16000:
1528 case 22050:
1529 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301530 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301531 case 24000:
1532 channel_status[3] |= SR_24000;
1533 break;
1534 case 32000:
1535 channel_status[3] |= SR_32000;
1536 break;
1537 case 44100:
1538 channel_status[3] |= SR_44100;
1539 break;
1540 case 48000:
1541 channel_status[3] |= SR_48000;
1542 break;
1543 case 88200:
1544 channel_status[3] |= SR_88200;
1545 break;
1546 case 96000:
1547 channel_status[3] |= SR_96000;
1548 break;
1549 case 176400:
1550 channel_status[3] |= SR_176400;
1551 break;
1552 case 192000:
1553 channel_status[3] |= SR_192000;
1554 break;
1555 default:
1556 ALOGV("Invalid sample_rate %u\n", sampleRate);
1557 status = -1;
1558 break;
1559 }
1560}
1561
1562void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1563{
1564 unsigned char channel_status[24]={0};
1565 struct snd_aes_iec958 iec958;
1566 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1567 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301568 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301569#ifdef HDMI_PASSTHROUGH_ENABLED
1570 if (audio_extn_is_dolby_format(out->format) &&
1571 /*TODO:Extend code to support DTS passthrough*/
1572 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301573 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301574 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1575 } else
1576#endif
1577 {
1578 /*set channel status bit for LPCM*/
1579 get_lpcm_channel_status(out->sample_rate, channel_status);
1580 }
1581
1582 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1583 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1584 if (!ctl) {
1585 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1586 __func__, mixer_ctl_name);
1587 return;
1588 }
1589 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1590 ALOGE("%s: Could not set channel status for ext HDMI ",
1591 __func__);
1592 return;
1593 }
1594
1595}
1596#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301597
1598int audio_extn_utils_get_avt_device_drift(
1599 struct audio_usecase *usecase,
1600 struct audio_avt_device_drift_param *drift_param)
1601{
1602 int ret = 0, count = 0;
1603 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
1604 struct mixer_ctl *ctl = NULL;
1605 struct audio_avt_device_drift_stats drift_stats;
1606 struct audio_device *adev = NULL;
1607
1608 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
1609 adev = usecase->stream.out->dev;
1610 switch(usecase->out_snd_device) {
1611 case SND_DEVICE_OUT_HDMI:
1612 strlcpy(avt_device_drift_mixer_ctl_name,
1613 "HDMI RX Drift",
1614 MIXER_PATH_MAX_LENGTH);
1615 break;
1616 case SND_DEVICE_OUT_DISPLAY_PORT:
1617 strlcpy(avt_device_drift_mixer_ctl_name,
1618 "DISPLAY Port RX Drift",
1619 MIXER_PATH_MAX_LENGTH);
1620 break;
1621 default :
1622 ALOGE("%s: Unsupported device %d",__func__,
1623 usecase->stream.out->devices);
1624 ret = -EINVAL;
1625 }
1626 } else {
1627 ALOGE("%s: Invalid usecase %d ",__func__, usecase->type);
1628 ret = -EINVAL;
1629 }
1630
1631 if(ret)
1632 goto done;
1633
1634 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1635 if (!ctl) {
1636 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1637 __func__, avt_device_drift_mixer_ctl_name);
1638
1639 ret = -EINVAL;
1640 goto done;
1641 }
1642
1643 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1644 avt_device_drift_mixer_ctl_name);
1645
1646 mixer_ctl_update(ctl);
1647 count = mixer_ctl_get_num_values(ctl);
1648 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1649 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1650 __func__);
1651
1652 ret = -EINVAL;
1653 goto done;
1654 }
1655
1656 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1657 if (ret != 0) {
1658 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1659 __func__);
1660
1661 ret = -EINVAL;
1662 goto done;
1663 }
1664 memcpy(drift_param, &drift_stats.drift_param,
1665 sizeof(struct audio_avt_device_drift_param));
1666done:
1667 return ret;
1668}
Manish Dewangan07de2142017-02-27 19:27:20 +05301669
1670#ifdef SNDRV_COMPRESS_PATH_DELAY
1671int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1672{
1673 int ret = -EINVAL;
1674 struct snd_compr_metadata metadata;
1675 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1676
1677 if (property_get_bool("audio.playback.dsp.pathdelay", false)) {
1678 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1679 if (!(is_offload_usecase(out->usecase))) {
1680 ALOGE("%s:: not supported for non offload session", __func__);
1681 goto exit;
1682 }
1683
1684 if (!out->compr) {
1685 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1686 __func__);
1687 goto exit;
1688 }
1689
1690 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1691 ret = compress_get_metadata(out->compr, &metadata);
1692 if(ret) {
1693 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1694 goto exit;
1695 }
1696 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1697 } else {
1698 ALOGD("%s:: Using Fix DSP delay",__func__);
1699 }
1700
1701exit:
1702 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1703 return delay_ms;
1704}
1705#else
1706int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1707{
1708 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1709}
1710#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301711
1712#ifdef SNDRV_COMPRESS_RENDER_MODE
1713int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1714{
1715 struct snd_compr_metadata metadata;
1716 int ret = -EINVAL;
1717
1718 if (!(is_offload_usecase(out->usecase))) {
1719 ALOGE("%s:: not supported for non offload session", __func__);
1720 goto exit;
1721 }
1722
1723 if (!out->compr) {
1724 ALOGD("%s:: Invalid compress handle",
1725 __func__);
1726 goto exit;
1727 }
1728
1729 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1730
1731 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1732 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1733 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1734 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1735 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1736 } else {
1737 ret = 0;
1738 goto exit;
1739 }
1740 ret = compress_set_metadata(out->compr, &metadata);
1741 if(ret) {
1742 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1743 }
1744exit:
1745 return ret;
1746}
1747#else
1748int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1749{
1750 ALOGD("%s:: configuring render mode not supported", __func__);
1751 return 0;
1752}
1753#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301754
1755#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1756int audio_extn_utils_compress_set_clk_rec_mode(
1757 struct audio_usecase *usecase)
1758{
1759 struct snd_compr_metadata metadata;
1760 struct stream_out *out = NULL;
1761 int ret = -EINVAL;
1762
1763 if (usecase != NULL && usecase->type != PCM_PLAYBACK) {
1764 ALOGE("%s:: Invalid use case", __func__);
1765 goto exit;
1766 }
1767
1768 out = usecase->stream.out;
1769 if (!out) {
1770 ALOGE("%s:: invalid stream", __func__);
1771 goto exit;
1772 }
1773
1774 if (!is_offload_usecase(out->usecase)) {
1775 ALOGE("%s:: not supported for non offload session", __func__);
1776 goto exit;
1777 }
1778
1779 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1780 ALOGD("%s:: clk recovery is only supported in STC render mode",
1781 __func__);
1782 ret = 0;
1783 goto exit;
1784 }
1785
1786 if (!out->compr) {
1787 ALOGD("%s:: Invalid compress handle",
1788 __func__);
1789 goto exit;
1790 }
1791 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1792 switch(usecase->out_snd_device) {
1793 case SND_DEVICE_OUT_HDMI:
1794 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1795 case SND_DEVICE_OUT_DISPLAY_PORT:
1796 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1797 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1798 break;
1799 default:
1800 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1801 break;
1802 }
1803
1804 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1805
1806 ret = compress_set_metadata(out->compr, &metadata);
1807 if(ret) {
1808 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1809 }
1810
1811exit:
1812 return ret;
1813}
1814#else
1815int audio_extn_utils_compress_set_clk_rec_mode(
1816 struct audio_usecase *usecase __unused)
1817{
1818 ALOGD("%s:: configuring render mode not supported", __func__);
1819 return 0;
1820}
1821#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301822
1823#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1824int audio_extn_utils_compress_set_render_window(
1825 struct stream_out *out,
1826 struct audio_out_render_window_param *render_window)
1827{
1828 struct snd_compr_metadata metadata;
1829 int ret = -EINVAL;
1830
1831 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1832 __func__,render_window->render_ws, render_window->render_we);
1833
1834 if(render_window == NULL) {
1835 ALOGE("%s:: Invalid render_window", __func__);
1836 goto exit;
1837 }
1838
1839 if (!is_offload_usecase(out->usecase)) {
1840 ALOGE("%s:: not supported for non offload session", __func__);
1841 goto exit;
1842 }
1843
1844 if ((out->render_mode == RENDER_MODE_AUDIO_MASTER) ||
1845 (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER)) {
1846 memcpy(&out->render_window, render_window,
1847 sizeof(struct audio_out_render_window_param));
1848 } else {
1849 ALOGD("%s:: only supported in timestamp mode, current "
1850 "render mode mode %d", __func__, out->render_mode);
1851 goto exit;
1852 }
1853
1854 if (!out->compr) {
1855 ALOGW("%s:: offload session not yet opened,"
1856 "render window will be configure later", __func__);
1857 /* store render window to reconfigure in start_output_stream() */
1858 goto exit;
1859 }
1860
1861 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1862 /*render window start value */
1863 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1864 metadata.value[1] = \
1865 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1866 /*render window end value */
1867 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1868 metadata.value[3] = \
1869 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1870
1871 ret = compress_set_metadata(out->compr, &metadata);
1872 if(ret) {
1873 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1874 }
1875
1876exit:
1877 return ret;
1878}
1879#else
1880int audio_extn_utils_compress_set_render_window(
1881 struct stream_out *out __unused,
1882 struct audio_out_render_window_param *render_window __unused)
1883{
1884 ALOGD("%s:: configuring render window not supported", __func__);
1885 return 0;
1886}
1887#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05301888
1889#ifdef SNDRV_COMPRESS_START_DELAY
1890int audio_extn_utils_compress_set_start_delay(
1891 struct stream_out *out,
1892 struct audio_out_start_delay_param *delay_param)
1893{
1894 struct snd_compr_metadata metadata;
1895 int ret = -EINVAL;
1896
1897 if(delay_param == NULL) {
1898 ALOGE("%s:: Invalid delay_param", __func__);
1899 goto exit;
1900 }
1901
1902 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
1903 delay_param->start_delay);
1904
1905 if (!is_offload_usecase(out->usecase)) {
1906 ALOGE("%s:: not supported for non offload session", __func__);
1907 goto exit;
1908 }
1909
1910 if ((out->render_mode == RENDER_MODE_AUDIO_MASTER) ||
1911 (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER)) {
1912 /* store it to reconfigure in start_output_stream() */
1913 out->delay_param.start_delay = delay_param->start_delay;
1914 } else {
1915 ALOGD("%s:: only supported in timestamp mode, current "
1916 "render mode mode %d", __func__, out->render_mode);
1917 goto exit;
1918 }
1919
1920 if (!out->compr) {
1921 ALOGW("%s:: offload session not yet opened,"
1922 "start delay will be configure later", __func__);
1923 goto exit;
1924 }
1925
1926 metadata.key = SNDRV_COMPRESS_START_DELAY;
1927 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
1928 metadata.value[1] = \
1929 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
1930
1931 ret = compress_set_metadata(out->compr, &metadata);
1932 if(ret) {
1933 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1934 }
1935
1936exit:
1937 return ret;
1938}
1939#else
1940int audio_extn_utils_compress_set_start_delay(
1941 struct stream_out *out __unused,
1942 struct audio_out_start_delay_param *delay_param __unused)
1943{
1944 ALOGD("%s:: configuring render window not supported", __func__);
1945 return 0;
1946}
1947#endif