blob: b917039683e5e8b6fa27812c6657c1281f1be825 [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),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530121 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
122 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
123 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
124 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
125 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530126 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700127};
128
129const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530130 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700131 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530132 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
133 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530134 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700135 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
136 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
137 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700138 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
139 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700140 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700141 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700142 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530143 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700144 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530145#ifdef AUDIO_EXTN_FORMATS_ENABLED
146 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700147 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
148 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
149 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700150 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
151 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
152 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
153 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
154 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
155 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
156 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700157 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530158 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
159 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700160 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800161 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
162 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
163 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530164 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530165 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
166 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
167 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530168 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530169 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
170 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
171 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
172 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530173 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700174#endif
175};
176
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530177/* payload structure avt_device drift query */
178struct audio_avt_device_drift_stats {
179 uint32_t minor_version;
180 /* Indicates the device interface direction as either
181 * source (Tx) or sink (Rx).
182 */
183 uint16_t device_direction;
184 /*params exposed to client */
185 struct audio_avt_device_drift_param drift_param;
186};
187
Ben Rombergera04fabc2014-11-14 12:16:03 -0800188static char bTable[BASE_TABLE_SIZE] = {
189 'A','B','C','D','E','F','G','H','I','J','K','L',
190 'M','N','O','P','Q','R','S','T','U','V','W','X',
191 'Y','Z','a','b','c','d','e','f','g','h','i','j',
192 'k','l','m','n','o','p','q','r','s','t','u','v',
193 'w','x','y','z','0','1','2','3','4','5','6','7',
194 '8','9','+','/'
195};
196
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700197static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
198 const char *name)
199{
200 size_t i;
201 for (i = 0; i < size; i++) {
202 if (strcmp(table[i].name, name) == 0) {
203 ALOGV("%s found %s", __func__, table[i].name);
204 return table[i].value;
205 }
206 }
207 return 0;
208}
209
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530210static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700211{
212 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530213 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800214 char *last_r;
215 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700216 while (flag_name != NULL) {
217 if (strlen(flag_name) != 0) {
218 flag |= string_to_enum(s_flag_name_to_enum_table,
219 ARRAY_SIZE(s_flag_name_to_enum_table),
220 flag_name);
221 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800222 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700223 }
224
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800225 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530226 io_flags.in_flags = (audio_input_flags_t)flag;
227 io_flags.out_flags = (audio_output_flags_t)flag;
228 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700229}
230
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530231static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700232{
233 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800234 char *last_r;
235 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700236
237 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
238 return;
239
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530240 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700241 while (str != NULL) {
242 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
243 ARRAY_SIZE(s_format_name_to_enum_table), str);
244 ALOGV("%s: format - %d", __func__, format);
245 if (format != 0) {
246 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700247 if (sf_info == NULL)
248 break; /* return whatever was parsed */
249
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700250 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530251 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700252 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800253 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700254 }
255}
256
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530257static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700258{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700259 struct stream_sample_rate *ss_info = NULL;
260 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800261 char *last_r;
262 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700263
Amit Shekhar6f461b12014-08-01 14:52:58 -0700264 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
265 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700266
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530267 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700268 while (str != NULL) {
269 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
270 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
271 if (0 != sample_rate) {
272 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800273 if (!ss_info) {
274 ALOGE("%s: memory allocation failure", __func__);
275 return;
276 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700277 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530278 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700279 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800280 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700281 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700282}
283
284static int parse_bit_width_names(char *name)
285{
286 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800287 char *last_r;
288 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289
290 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
291 bit_width = (int)strtol(str, (char **)NULL, 10);
292
293 ALOGV("%s: bit_width - %d", __func__, bit_width);
294 return bit_width;
295}
296
297static int parse_app_type_names(void *platform, char *name)
298{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700299 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800300 char *last_r;
301 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302
303 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
304 app_type = (int)strtol(str, (char **)NULL, 10);
305
306 ALOGV("%s: app_type - %d", __func__, app_type);
307 return app_type;
308}
309
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530310static void update_streams_cfg_list(cnode *root, void *platform,
311 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700312{
313 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530314 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700315
316 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530317 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700318
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530319 if (!s_info) {
320 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700321 return;
322 }
323
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700324 while (node) {
325 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530326 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530327 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
328 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700329 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530330 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700331 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530332 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
333 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700334 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530335 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700336 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530337 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700338 }
339 node = node->next;
340 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530341 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700342}
343
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530344static void load_cfg_list(cnode *root, void *platform,
345 struct listnode *streams_output_cfg_list,
346 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700347{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530348 cnode *node = NULL;
349
350 node = config_find(root, OUTPUTS_TAG);
351 if (node != NULL) {
352 node = node->first_child;
353 while (node) {
354 ALOGV("%s: loading output %s", __func__, node->name);
355 update_streams_cfg_list(node, platform, streams_output_cfg_list);
356 node = node->next;
357 }
358 } else {
359 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700360 }
361
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530362 node = config_find(root, INPUTS_TAG);
363 if (node != NULL) {
364 node = node->first_child;
365 while (node) {
366 ALOGV("%s: loading input %s", __func__, node->name);
367 update_streams_cfg_list(node, platform, streams_input_cfg_list);
368 node = node->next;
369 }
370 } else {
371 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700372 }
373}
374
375static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530376 struct listnode *streams_output_cfg_list,
377 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700378{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530379 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700380 int length = 0, i, num_app_types = 0;
381 struct listnode *node;
382 bool update;
383 struct mixer_ctl *ctl = NULL;
384 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386
387 if (!mixer) {
388 ALOGE("%s: mixer is null",__func__);
389 return;
390 }
391 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
392 if (!ctl) {
393 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
394 return;
395 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530396 app_type_cfg[length++] = num_app_types;
397
398 if (list_empty(streams_output_cfg_list)) {
399 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700400 app_type_cfg[length++] = 48000;
401 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530402 num_app_types += 1;
403 }
404 if (list_empty(streams_input_cfg_list)) {
405 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
406 app_type_cfg[length++] = 48000;
407 app_type_cfg[length++] = 16;
408 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700409 }
410
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700411 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530412 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700413 update = true;
414 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530415 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700416 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530417 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530418 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530419 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530420 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530421 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700422 update = false;
423 break;
424 }
425 }
426 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530427 num_app_types += 1;
428 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
429 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
430 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
431 }
432 }
433 list_for_each(node, streams_input_cfg_list) {
434 s_info = node_to_item(node, struct streams_io_cfg, list);
435 update = true;
436 for (i=0; i<length; i=i+3) {
437 if (app_type_cfg[i+1] == 0)
438 break;
439 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530440 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530441 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530442 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530443 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
444 update = false;
445 break;
446 }
447 }
448 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
449 num_app_types += 1;
450 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
451 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
452 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700453 }
454 }
455 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
456 if (num_app_types) {
457 app_type_cfg[0] = num_app_types;
458 mixer_ctl_set_array(ctl, app_type_cfg, length);
459 }
460}
461
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462void audio_extn_utils_update_streams_cfg_lists(void *platform,
463 struct mixer *mixer,
464 struct listnode *streams_output_cfg_list,
465 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700466{
467 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530468 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700469
470 ALOGV("%s", __func__);
471 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530472 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700473
474 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700475 if (root == NULL) {
476 ALOGE("cfg_list, NULL config root");
477 return;
478 }
479
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530480 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
481 if (data == NULL) {
482 ALOGD("%s: failed to open io config file(%s), trying older config file",
483 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
484 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
485 if (data == NULL) {
486 send_app_type_cfg(platform, mixer,
487 streams_output_cfg_list,
488 streams_input_cfg_list);
489 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800490 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530491 return;
492 }
493 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700494
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530495 config_load(root, data);
496 load_cfg_list(root, platform, streams_output_cfg_list,
497 streams_input_cfg_list);
498
499 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
500 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800501
502 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800503 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800504 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700505}
506
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530507static void audio_extn_utils_dump_streams_cfg_list(
508 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700509{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700510 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530511 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700512 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700513 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530514
515 list_for_each(node_i, streams_cfg_list) {
516 s_info = node_to_item(node_i, struct streams_io_cfg, list);
517 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
518 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
519 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
520 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700521 sf_info = node_to_item(node_j, struct stream_format, list);
522 ALOGV("format-%x", sf_info->format);
523 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530524 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700525 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
526 ALOGV("sample rate-%d", ss_info->sample_rate);
527 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700528 }
529}
530
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530531void audio_extn_utils_dump_streams_cfg_lists(
532 struct listnode *streams_output_cfg_list,
533 struct listnode *streams_input_cfg_list)
534{
535 ALOGV("%s", __func__);
536 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
537 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
538}
539
540static void audio_extn_utils_release_streams_cfg_list(
541 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700542{
543 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530544 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700545
546 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530547
548 while (!list_empty(streams_cfg_list)) {
549 node_i = list_head(streams_cfg_list);
550 s_info = node_to_item(node_i, struct streams_io_cfg, list);
551 while (!list_empty(&s_info->format_list)) {
552 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700553 list_remove(node_j);
554 free(node_to_item(node_j, struct stream_format, list));
555 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530556 while (!list_empty(&s_info->sample_rate_list)) {
557 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700558 list_remove(node_j);
559 free(node_to_item(node_j, struct stream_sample_rate, list));
560 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700561 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530562 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700563 }
564}
565
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530566void audio_extn_utils_release_streams_cfg_lists(
567 struct listnode *streams_output_cfg_list,
568 struct listnode *streams_input_cfg_list)
569{
570 ALOGV("%s", __func__);
571 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
572 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
573}
574
575static bool set_app_type_cfg(struct streams_io_cfg *s_info,
576 struct stream_app_type_cfg *app_type_cfg,
577 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700578 {
579 struct listnode *node_i;
580 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530581 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700582 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
583 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530584 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700585
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530586 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700587 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530588 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700589 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
590 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
591 return true;
592 }
593 }
594 /*
595 * Reiterate through the list assuming dafault sample rate.
596 * Handles scenario where input sample rate is higher
597 * than all sample rates in list for the input bit width.
598 */
599 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800600
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530601 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700602 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
603 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530604 (bit_width == s_info->app_type_cfg.bit_width)) {
605 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700606 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530607 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800608 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 -0700609 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
610 return true;
611 }
612 }
613 return false;
614}
615
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530616void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
617 struct listnode *streams_input_cfg_list,
618 audio_devices_t devices __unused,
619 audio_input_flags_t flags,
620 audio_format_t format,
621 uint32_t sample_rate,
622 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530623 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530624 struct stream_app_type_cfg *app_type_cfg)
625{
626 struct listnode *node_i, *node_j;
627 struct streams_io_cfg *s_info;
628 struct stream_format *sf_info;
629
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530630 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
631 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530632
633 list_for_each(node_i, streams_input_cfg_list) {
634 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530635 /* Along with flags do profile matching if set at either end.*/
636 if (s_info->flags.in_flags == flags &&
637 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
638 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530639 list_for_each(node_j, &s_info->format_list) {
640 sf_info = node_to_item(node_j, struct stream_format, list);
641 if (sf_info->format == format) {
642 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
643 return;
644 }
645 }
646 }
647 }
648 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
649 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
650 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
651 app_type_cfg->bit_width = 16;
652}
653
654void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700655 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700656 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700657 audio_output_flags_t flags,
658 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700659 uint32_t sample_rate,
660 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530661 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530662 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700663 struct stream_app_type_cfg *app_type_cfg)
664{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530665 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530666 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700667 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530668 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700669
Ashish Jain058165c2016-09-28 23:18:48 +0530670 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700671 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700672 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
673 if (-ENOSYS != bw)
674 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700675 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
676 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
677 }
678
Manish Dewangan837dc462015-05-27 10:17:41 +0530679 property_get("audio.playback.mch.downsample",value,"");
680 if (!strncmp("true", value, sizeof("true"))) {
681 if ((popcount(channel_mask) > 2) &&
682 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
683 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
684 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
685 ALOGD("%s: MCH session defaulting sample rate to %d",
686 __func__, sample_rate);
687 }
688 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530689
690 /* Set sampling rate to 176.4 for DSD64
691 * and 352.8Khz for DSD128.
692 * Set Bit Width to 16. output will be 16 bit
693 * post DoP in ASM.
694 */
695 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
696 (format == AUDIO_FORMAT_DSD)) {
697 bit_width = 16;
698 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
699 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
700 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
701 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
702 }
703
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530704 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
705 //TODO: Handle fractional sampling rate configuration for LL
706 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
707 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
708 __func__, sample_rate, bit_width);
709 }
710
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800711 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
712 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700713 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530714 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530715 /* Along with flags do profile matching if set at either end.*/
716 if (s_info->flags.out_flags == flags &&
717 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
718 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530719 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700720 sf_info = node_to_item(node_j, struct stream_format, list);
721 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530722 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700723 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700724 }
725 }
726 }
727 }
728 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530729 s_info = node_to_item(node_i, struct streams_io_cfg, list);
730 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700731 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530732 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
733 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
734 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700735 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530736 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700737 return;
738 }
739 }
740 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700741 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700742 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700743 app_type_cfg->bit_width = 16;
744}
745
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530746static bool audio_is_this_native_usecase(struct audio_usecase *uc)
747{
748 bool native_usecase = false;
749 struct stream_out *out = (struct stream_out*) uc->stream.out;
750
751 if (PCM_PLAYBACK == uc->type && out != NULL &&
752 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
753 is_offload_usecase(uc->id) &&
754 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
755 native_usecase = true;
756
757 return native_usecase;
758}
759
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800760
761static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
762{
763 return adev->vr_audio_mode_enabled;
764}
765
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530766void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
767 struct audio_device *adev,
768 struct audio_usecase *usecase)
769{
770 ALOGV("%s", __func__);
771
772 switch(usecase->type) {
773 case PCM_PLAYBACK:
774 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
775 &adev->streams_output_cfg_list,
776 usecase->stream.out->devices,
777 usecase->stream.out->flags,
778 usecase->stream.out->format,
779 usecase->stream.out->sample_rate,
780 usecase->stream.out->bit_width,
781 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530782 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530783 &usecase->stream.out->app_type_cfg);
784 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
785 break;
786 case PCM_CAPTURE:
787 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
788 &adev->streams_input_cfg_list,
789 usecase->stream.in->device,
790 usecase->stream.in->flags,
791 usecase->stream.in->format,
792 usecase->stream.in->sample_rate,
793 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530794 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530795 &usecase->stream.in->app_type_cfg);
796 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
797 break;
798 default:
799 ALOGE("%s: app type cfg not supported for usecase type (%d)",
800 __func__, usecase->type);
801 }
802}
803
Siena Richard7c2db772016-12-21 11:32:34 -0800804static int send_app_type_cfg_for_device(struct audio_device *adev,
805 struct audio_usecase *usecase,
806 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700807{
808 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530809 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
810 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700811 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800812 int pcm_device_id = 0, acdb_dev_id, app_type;
813 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530814 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530815 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700816
Siena Richard7c2db772016-12-21 11:32:34 -0800817 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
818 __func__, platform_get_snd_device_name(usecase->out_snd_device),
819 platform_get_snd_device_name(usecase->in_snd_device),
820 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700821
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530822 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530823 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700824 rc = 0;
825 goto exit_send_app_type_cfg;
826 }
827 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
828 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
829 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800830 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530831 (!is_offload_usecase(usecase->id)) &&
832 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530833 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 -0700834 rc = 0;
835 goto exit_send_app_type_cfg;
836 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800837
838 //if VR is active then only send the mixer control
839 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
840 ALOGI("ULL doesnt need sending app type cfg, returning");
841 rc = 0;
842 goto exit_send_app_type_cfg;
843 }
844
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530845 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700846 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530847 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
848 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700849 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700850 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530851 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
852 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530853 }
Siena Richard7c2db772016-12-21 11:32:34 -0800854
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700855 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
856 if (!ctl) {
857 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
858 mixer_ctl_name);
859 rc = -EINVAL;
860 goto exit_send_app_type_cfg;
861 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530862 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530863
Rohit Kumar1181b292017-01-31 18:07:17 +0530864 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
865 if (acdb_dev_id <= 0) {
866 ALOGE("%s: Couldn't get the acdb dev id", __func__);
867 rc = -EINVAL;
868 goto exit_send_app_type_cfg;
869 }
870
Siena Richard7c2db772016-12-21 11:32:34 -0800871 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
872 if (snd_device_be_idx < 0) {
873 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
874 __func__, platform_get_snd_device_name(snd_device),
875 snd_device_be_idx);
876 }
877
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530878 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530879
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530880 property_get("audio.playback.mch.downsample",value,"");
881 if (!strncmp("true", value, sizeof("true"))) {
882 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
883 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
884 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
885 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
886 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530887
Ashish Jain4826f6c2017-02-06 13:33:20 +0530888 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700889 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530890 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
891 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
892 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
893 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
894 /*
895 * To best utlize DSP, check if the stream sample rate is supported/multiple of
896 * configured device sample rate, if not update the COPP rate to be equal to the
897 * device sample rate, else open COPP at stream sample rate
898 */
899 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
900 usecase->stream.out->sample_rate,
901 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530902 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
903 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700904 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
905 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530906 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700907 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
908 }
909 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
910
Siena Richard7c2db772016-12-21 11:32:34 -0800911 app_type = usecase->stream.out->app_type_cfg.app_type;
912 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700913 app_type_cfg[len++] = acdb_dev_id;
914 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700915 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
916 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530917 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700918 app_type_cfg[len++] = sample_rate * 4;
919 } else {
920 app_type_cfg[len++] = sample_rate;
921 }
Siena Richard7c2db772016-12-21 11:32:34 -0800922 if (snd_device_be_idx > 0)
923 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530924
Siena Richard7c2db772016-12-21 11:32:34 -0800925 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
926 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530927
928 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800929 app_type = usecase->stream.in->app_type_cfg.app_type;
930 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530931 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800932 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
933 app_type_cfg[len++] = sample_rate;
934 if (snd_device_be_idx > 0)
935 app_type_cfg[len++] = snd_device_be_idx;
936 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
937 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530938 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800939 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
940 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530941 app_type_cfg[len++] = acdb_dev_id;
942 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800943 if (snd_device_be_idx > 0)
944 app_type_cfg[len++] = snd_device_be_idx;
945 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
946 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530947 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530948
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700949 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700950 rc = 0;
951exit_send_app_type_cfg:
952 return rc;
953}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700954
Siena Richard7c2db772016-12-21 11:32:34 -0800955int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
956 struct audio_usecase *usecase)
957{
958 int i, num_devices = 0;
959 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
960 int rc = 0;
961
962 switch (usecase->type) {
963 case PCM_PLAYBACK:
964 ALOGD("%s: usecase->out_snd_device %s",
965 __func__, platform_get_snd_device_name(usecase->out_snd_device));
966 /* check for out combo device */
967 if (platform_split_snd_device(adev->platform,
968 usecase->out_snd_device,
969 &num_devices, new_snd_devices)) {
970 new_snd_devices[0] = usecase->out_snd_device;
971 num_devices = 1;
972 }
973 break;
974 case PCM_CAPTURE:
975 ALOGD("%s: usecase->in_snd_device %s",
976 __func__, platform_get_snd_device_name(usecase->in_snd_device));
977 /* check for in combo device */
978 if (platform_split_snd_device(adev->platform,
979 usecase->in_snd_device,
980 &num_devices, new_snd_devices)) {
981 new_snd_devices[0] = usecase->in_snd_device;
982 num_devices = 1;
983 }
984 break;
985 default:
986 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
987 rc = 0;
988 break;
989 }
990
991 for (i = 0; i < num_devices; i++) {
992 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
993 if (rc)
994 break;
995 }
996
997 return rc;
998}
999
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301000int read_line_from_file(const char *path, char *buf, size_t count)
1001{
1002 char * fgets_ret;
1003 FILE * fd;
1004 int rv;
1005
1006 fd = fopen(path, "r");
1007 if (fd == NULL)
1008 return -1;
1009
1010 fgets_ret = fgets(buf, (int)count, fd);
1011 if (NULL != fgets_ret) {
1012 rv = (int)strlen(buf);
1013 } else {
1014 rv = ferror(fd);
1015 }
1016 fclose(fd);
1017
1018 return rv;
1019}
1020
Ashish Jainf1eaa582016-05-23 20:54:24 +05301021/*Translates ALSA formats to AOSP PCM formats*/
1022audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1023{
1024 audio_format_t format;
1025
1026 switch(alsa_format) {
1027 case SNDRV_PCM_FORMAT_S16_LE:
1028 format = AUDIO_FORMAT_PCM_16_BIT;
1029 break;
1030 case SNDRV_PCM_FORMAT_S24_3LE:
1031 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1032 break;
1033 case SNDRV_PCM_FORMAT_S24_LE:
1034 format = AUDIO_FORMAT_PCM_8_24_BIT;
1035 break;
1036 case SNDRV_PCM_FORMAT_S32_LE:
1037 format = AUDIO_FORMAT_PCM_32_BIT;
1038 break;
1039 default:
1040 ALOGW("Incorrect ALSA format");
1041 format = AUDIO_FORMAT_INVALID;
1042 }
1043 return format;
1044}
1045
1046/*Translates hal format (AOSP) to alsa formats*/
1047uint32_t hal_format_to_alsa(audio_format_t hal_format)
1048{
1049 uint32_t alsa_format;
1050
1051 switch (hal_format) {
1052 case AUDIO_FORMAT_PCM_32_BIT: {
1053 if (platform_supports_true_32bit())
1054 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1055 else
1056 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1057 }
1058 break;
1059 case AUDIO_FORMAT_PCM_8_BIT:
1060 alsa_format = SNDRV_PCM_FORMAT_S8;
1061 break;
1062 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1063 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1064 break;
1065 case AUDIO_FORMAT_PCM_8_24_BIT: {
1066 if (platform_supports_true_32bit())
1067 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1068 else
1069 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1070 }
1071 break;
1072 case AUDIO_FORMAT_PCM_FLOAT:
1073 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1074 break;
1075 default:
1076 case AUDIO_FORMAT_PCM_16_BIT:
1077 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1078 break;
1079 }
1080 return alsa_format;
1081}
1082
Ashish Jain83a6cc22016-06-28 14:34:17 +05301083/*Translates PCM formats to AOSP formats*/
1084audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1085{
1086 audio_format_t format = AUDIO_FORMAT_INVALID;
1087
1088 switch(pcm_format) {
1089 case PCM_FORMAT_S16_LE:
1090 format = AUDIO_FORMAT_PCM_16_BIT;
1091 break;
1092 case PCM_FORMAT_S24_3LE:
1093 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1094 break;
1095 case PCM_FORMAT_S24_LE:
1096 format = AUDIO_FORMAT_PCM_8_24_BIT;
1097 break;
1098 case PCM_FORMAT_S32_LE:
1099 format = AUDIO_FORMAT_PCM_32_BIT;
1100 break;
1101 default:
1102 ALOGW("Incorrect PCM format");
1103 format = AUDIO_FORMAT_INVALID;
1104 }
1105 return format;
1106}
1107
1108/*Translates hal format (AOSP) to alsa formats*/
1109uint32_t hal_format_to_pcm(audio_format_t hal_format)
1110{
1111 uint32_t pcm_format;
1112
1113 switch (hal_format) {
1114 case AUDIO_FORMAT_PCM_32_BIT:
1115 case AUDIO_FORMAT_PCM_8_24_BIT:
1116 case AUDIO_FORMAT_PCM_FLOAT: {
1117 if (platform_supports_true_32bit())
1118 pcm_format = PCM_FORMAT_S32_LE;
1119 else
1120 pcm_format = PCM_FORMAT_S24_3LE;
1121 }
1122 break;
1123 case AUDIO_FORMAT_PCM_8_BIT:
1124 pcm_format = PCM_FORMAT_S8;
1125 break;
1126 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1127 pcm_format = PCM_FORMAT_S24_3LE;
1128 break;
1129 default:
1130 case AUDIO_FORMAT_PCM_16_BIT:
1131 pcm_format = PCM_FORMAT_S16_LE;
1132 break;
1133 }
1134 return pcm_format;
1135}
1136
Ashish Jainf1eaa582016-05-23 20:54:24 +05301137uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1138 uint32_t sample_rate,
1139 uint32_t noOfChannels)
1140{
1141 uint32_t fragment_size = 0;
1142 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1143
1144 fragment_size = (pcm_offload_time
1145 * sample_rate
1146 * bytes_per_sample
1147 * noOfChannels)/1000;
1148 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1149 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1150 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1151 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1152 /*To have same PCM samples for all channels, the buffer size requires to
1153 *be multiple of (number of channels * bytes per sample)
1154 *For writes to succeed, the buffer must be written at address which is multiple of 32
1155 */
1156 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1157
1158 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1159 return fragment_size;
1160}
1161
1162/* Calculates the fragment size required to configure compress session.
1163 * Based on the alsa format selected, decide if conversion is needed in
1164
1165 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1166 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1167 */
1168void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1169{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301170 audio_format_t dst_format = out->hal_op_format;
1171 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301172 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1173 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1174
1175 out->compr_config.fragment_size =
1176 get_alsa_fragment_size(hal_op_bytes_per_sample,
1177 out->sample_rate,
1178 popcount(out->channel_mask));
1179
1180 if ((src_format != dst_format) &&
1181 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1182
Ashish Jain83a6cc22016-06-28 14:34:17 +05301183 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301184 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1185 hal_op_bytes_per_sample);
1186 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301187 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301188 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301189 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301190 }
1191}
1192
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301193/* converts pcm format 24_8 to 8_24 inplace */
1194size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1195{
1196 size_t i = 0;
1197 int *int_buf_stream = buf;
1198
1199 if ((bytes % 4) != 0) {
1200 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1201 return -EINVAL;
1202 }
1203
1204 for (; i < (bytes / 4); i++)
1205 int_buf_stream[i] >>= 8;
1206
1207 return bytes;
1208}
1209
1210int get_snd_codec_id(audio_format_t format)
1211{
1212 int id = 0;
1213
1214 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1215 case AUDIO_FORMAT_MP3:
1216 id = SND_AUDIOCODEC_MP3;
1217 break;
1218 case AUDIO_FORMAT_AAC:
1219 id = SND_AUDIOCODEC_AAC;
1220 break;
1221 case AUDIO_FORMAT_AAC_ADTS:
1222 id = SND_AUDIOCODEC_AAC;
1223 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301224 case AUDIO_FORMAT_AAC_LATM:
1225 id = SND_AUDIOCODEC_AAC;
1226 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301227 case AUDIO_FORMAT_PCM:
1228 id = SND_AUDIOCODEC_PCM;
1229 break;
1230 case AUDIO_FORMAT_FLAC:
1231 id = SND_AUDIOCODEC_FLAC;
1232 break;
1233 case AUDIO_FORMAT_ALAC:
1234 id = SND_AUDIOCODEC_ALAC;
1235 break;
1236 case AUDIO_FORMAT_APE:
1237 id = SND_AUDIOCODEC_APE;
1238 break;
1239 case AUDIO_FORMAT_VORBIS:
1240 id = SND_AUDIOCODEC_VORBIS;
1241 break;
1242 case AUDIO_FORMAT_WMA:
1243 id = SND_AUDIOCODEC_WMA;
1244 break;
1245 case AUDIO_FORMAT_WMA_PRO:
1246 id = SND_AUDIOCODEC_WMA_PRO;
1247 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301248 case AUDIO_FORMAT_MP2:
1249 id = SND_AUDIOCODEC_MP2;
1250 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301251 case AUDIO_FORMAT_AC3:
1252 id = SND_AUDIOCODEC_AC3;
1253 break;
1254 case AUDIO_FORMAT_E_AC3:
1255 case AUDIO_FORMAT_E_AC3_JOC:
1256 id = SND_AUDIOCODEC_EAC3;
1257 break;
1258 case AUDIO_FORMAT_DTS:
1259 case AUDIO_FORMAT_DTS_HD:
1260 id = SND_AUDIOCODEC_DTS;
1261 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001262 case AUDIO_FORMAT_DOLBY_TRUEHD:
1263 id = SND_AUDIOCODEC_TRUEHD;
1264 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301265 case AUDIO_FORMAT_DSD:
1266 id = SND_AUDIOCODEC_DSD;
1267 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301268 case AUDIO_FORMAT_APTX:
1269 id = SND_AUDIOCODEC_APTX;
1270 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301271 default:
1272 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1273 }
1274
1275 return id;
1276}
1277
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001278void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1279 struct audio_usecase *usecase)
1280{
1281 int type = usecase->type;
1282
Dhananjay Kumar14245982017-01-16 20:21:00 +05301283 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001284 struct stream_out *out = usecase->stream.out;
1285 int snd_device = usecase->out_snd_device;
1286 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001287 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301288 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001289 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301290 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301291 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301292 platform_send_audio_calibration(adev->platform, usecase,
1293 usecase->stream.in->app_type_cfg.app_type,
1294 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001295 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301296 /* when app type is default. the sample rate is not used to send cal */
1297 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301298 platform_get_default_app_type_v2(adev->platform, usecase->type),
1299 48000);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001300 } else {
1301 /* No need to send audio calibration for voice and voip call usecases */
1302 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1303 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001304 }
1305}
1306
Ben Rombergera04fabc2014-11-14 12:16:03 -08001307// Base64 Encode and Decode
1308// Not all features supported. This must be used only with following conditions.
1309// Decode Modes: Support with and without padding
1310// CRLF not handling. So no CRLF in string to decode.
1311// Encode Modes: Supports only padding
1312int b64decode(char *inp, int ilen, uint8_t* outp)
1313{
1314 int i, j, k, ii, num;
1315 int rem, pcnt;
1316 uint32_t res=0;
1317 uint8_t getIndex[MAX_BASEINDEX_LEN];
1318 uint8_t tmp, cflag;
1319
1320 if(inp == NULL || outp == NULL || ilen <= 0) {
1321 ALOGE("[%s] received NULL pointer or zero length",__func__);
1322 return -1;
1323 }
1324
1325 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1326 for(i=0;i<BASE_TABLE_SIZE;i++) {
1327 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1328 }
1329 getIndex[(uint8_t)'=']=0;
1330
1331 j=0;k=0;
1332 num = ilen/4;
1333 rem = ilen%4;
1334 if(rem==0)
1335 num = num-1;
1336 cflag=0;
1337 for(i=0; i<num; i++) {
1338 res=0;
1339 for(ii=0;ii<4;ii++) {
1340 res = res << 6;
1341 tmp = getIndex[(uint8_t)inp[j++]];
1342 res = res | tmp;
1343 cflag = cflag | tmp;
1344 }
1345 outp[k++] = (res >> 16)&0xFF;
1346 outp[k++] = (res >> 8)&0xFF;
1347 outp[k++] = res & 0xFF;
1348 }
1349
1350 // Handle last bytes special
1351 pcnt=0;
1352 if(rem == 0) {
1353 //With padding or full data
1354 res = 0;
1355 for(ii=0;ii<4;ii++) {
1356 if(inp[j] == '=')
1357 pcnt++;
1358 res = res << 6;
1359 tmp = getIndex[(uint8_t)inp[j++]];
1360 res = res | tmp;
1361 cflag = cflag | tmp;
1362 }
1363 outp[k++] = res >> 16;
1364 if(pcnt == 2)
1365 goto done;
1366 outp[k++] = (res>>8)&0xFF;
1367 if(pcnt == 1)
1368 goto done;
1369 outp[k++] = res&0xFF;
1370 } else {
1371 //without padding
1372 res = 0;
1373 for(i=0;i<rem;i++) {
1374 res = res << 6;
1375 tmp = getIndex[(uint8_t)inp[j++]];
1376 res = res | tmp;
1377 cflag = cflag | tmp;
1378 }
1379 for(i=rem;i<4;i++) {
1380 res = res << 6;
1381 pcnt++;
1382 }
1383 outp[k++] = res >> 16;
1384 if(pcnt == 2)
1385 goto done;
1386 outp[k++] = (res>>8)&0xFF;
1387 if(pcnt == 1)
1388 goto done;
1389 outp[k++] = res&0xFF;
1390 }
1391done:
1392 if(cflag == 0xFF) {
1393 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1394 __func__, inp);
1395 return 0;
1396 }
1397 return k;
1398}
1399
1400int b64encode(uint8_t *inp, int ilen, char* outp)
1401{
1402 int i,j,k, num;
1403 int rem=0;
1404 uint32_t res=0;
1405
1406 if(inp == NULL || outp == NULL || ilen<=0) {
1407 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1408 return -1;
1409 }
1410
1411 num = ilen/3;
1412 rem = ilen%3;
1413 j=0;k=0;
1414 for(i=0; i<num; i++) {
1415 //prepare index
1416 res = inp[j++]<<16;
1417 res = res | inp[j++]<<8;
1418 res = res | inp[j++];
1419 //get output map from index
1420 outp[k++] = (char) bTable[(res>>18)&0x3F];
1421 outp[k++] = (char) bTable[(res>>12)&0x3F];
1422 outp[k++] = (char) bTable[(res>>6)&0x3F];
1423 outp[k++] = (char) bTable[res&0x3F];
1424 }
1425
1426 switch(rem) {
1427 case 1:
1428 res = inp[j++]<<16;
1429 outp[k++] = (char) bTable[res>>18];
1430 outp[k++] = (char) bTable[(res>>12)&0x3F];
1431 //outp[k++] = '=';
1432 //outp[k++] = '=';
1433 break;
1434 case 2:
1435 res = inp[j++]<<16;
1436 res = res | inp[j++]<<8;
1437 outp[k++] = (char) bTable[res>>18];
1438 outp[k++] = (char) bTable[(res>>12)&0x3F];
1439 outp[k++] = (char) bTable[(res>>6)&0x3F];
1440 //outp[k++] = '=';
1441 break;
1442 default:
1443 break;
1444 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001445 outp[k] = '\0';
1446 return k;
1447}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301448
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301449
1450int audio_extn_utils_get_codec_version(const char *snd_card_name,
1451 int card_num,
1452 char *codec_version)
1453{
1454 char procfs_path[50];
1455 FILE *fp;
1456
1457 if (strstr(snd_card_name, "tasha")) {
1458 snprintf(procfs_path, sizeof(procfs_path),
1459 "/proc/asound/card%d/codecs/tasha/version", card_num);
1460 if ((fp = fopen(procfs_path, "r")) != NULL) {
1461 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1462 fclose(fp);
1463 } else {
1464 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1465 return -ENOENT;
1466 }
1467 ALOGD("%s: codec version %s", __func__, codec_version);
1468 }
1469
1470 return 0;
1471}
1472
1473
Ashish Jain81eb2a82015-05-13 10:52:34 +05301474#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1475
1476void get_default_compressed_channel_status(
1477 unsigned char *channel_status)
1478{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301479 memset(channel_status,0,24);
1480
1481 /* block start bit in preamble bit 3 */
1482 channel_status[0] |= PROFESSIONAL;
1483 //compre out
1484 channel_status[0] |= NON_LPCM;
1485 // sample rate; fixed 48K for default/transcode
1486 channel_status[3] |= SR_48000;
1487}
1488
1489#ifdef HDMI_PASSTHROUGH_ENABLED
1490int32_t get_compressed_channel_status(void *audio_stream_data,
1491 uint32_t audio_frame_size,
1492 unsigned char *channel_status,
1493 enum audio_parser_code_type codec_type)
1494 // codec_type - AUDIO_PARSER_CODEC_AC3
1495 // - AUDIO_PARSER_CODEC_DTS
1496{
1497 unsigned char *stream;
1498 int ret = 0;
1499 stream = (unsigned char *)audio_stream_data;
1500
1501 if (audio_stream_data == NULL || audio_frame_size == 0) {
1502 ALOGW("no buffer to get channel status, return default for compress");
1503 get_default_compressed_channel_status(channel_status);
1504 return ret;
1505 }
1506
1507 memset(channel_status,0,24);
1508 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1509 {
1510 ALOGE("init audio parser failed");
1511 return -1;
1512 }
1513 ret = get_channel_status(channel_status, codec_type);
1514 return ret;
1515
1516}
1517
1518#endif
1519
1520void get_lpcm_channel_status(uint32_t sampleRate,
1521 unsigned char *channel_status)
1522{
1523 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301524 memset(channel_status,0,24);
1525 /* block start bit in preamble bit 3 */
1526 channel_status[0] |= PROFESSIONAL;
1527 //LPCM OUT
1528 channel_status[0] &= ~NON_LPCM;
1529
1530 switch (sampleRate) {
1531 case 8000:
1532 case 11025:
1533 case 12000:
1534 case 16000:
1535 case 22050:
1536 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301537 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301538 case 24000:
1539 channel_status[3] |= SR_24000;
1540 break;
1541 case 32000:
1542 channel_status[3] |= SR_32000;
1543 break;
1544 case 44100:
1545 channel_status[3] |= SR_44100;
1546 break;
1547 case 48000:
1548 channel_status[3] |= SR_48000;
1549 break;
1550 case 88200:
1551 channel_status[3] |= SR_88200;
1552 break;
1553 case 96000:
1554 channel_status[3] |= SR_96000;
1555 break;
1556 case 176400:
1557 channel_status[3] |= SR_176400;
1558 break;
1559 case 192000:
1560 channel_status[3] |= SR_192000;
1561 break;
1562 default:
1563 ALOGV("Invalid sample_rate %u\n", sampleRate);
1564 status = -1;
1565 break;
1566 }
1567}
1568
1569void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1570{
1571 unsigned char channel_status[24]={0};
1572 struct snd_aes_iec958 iec958;
1573 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1574 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301575 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301576#ifdef HDMI_PASSTHROUGH_ENABLED
1577 if (audio_extn_is_dolby_format(out->format) &&
1578 /*TODO:Extend code to support DTS passthrough*/
1579 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301580 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301581 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1582 } else
1583#endif
1584 {
1585 /*set channel status bit for LPCM*/
1586 get_lpcm_channel_status(out->sample_rate, channel_status);
1587 }
1588
1589 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1590 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1591 if (!ctl) {
1592 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1593 __func__, mixer_ctl_name);
1594 return;
1595 }
1596 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1597 ALOGE("%s: Could not set channel status for ext HDMI ",
1598 __func__);
1599 return;
1600 }
1601
1602}
1603#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301604
1605int audio_extn_utils_get_avt_device_drift(
1606 struct audio_usecase *usecase,
1607 struct audio_avt_device_drift_param *drift_param)
1608{
1609 int ret = 0, count = 0;
1610 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
1611 struct mixer_ctl *ctl = NULL;
1612 struct audio_avt_device_drift_stats drift_stats;
1613 struct audio_device *adev = NULL;
1614
1615 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
1616 adev = usecase->stream.out->dev;
1617 switch(usecase->out_snd_device) {
1618 case SND_DEVICE_OUT_HDMI:
1619 strlcpy(avt_device_drift_mixer_ctl_name,
1620 "HDMI RX Drift",
1621 MIXER_PATH_MAX_LENGTH);
1622 break;
1623 case SND_DEVICE_OUT_DISPLAY_PORT:
1624 strlcpy(avt_device_drift_mixer_ctl_name,
1625 "DISPLAY Port RX Drift",
1626 MIXER_PATH_MAX_LENGTH);
1627 break;
1628 default :
1629 ALOGE("%s: Unsupported device %d",__func__,
1630 usecase->stream.out->devices);
1631 ret = -EINVAL;
1632 }
1633 } else {
1634 ALOGE("%s: Invalid usecase %d ",__func__, usecase->type);
1635 ret = -EINVAL;
1636 }
1637
1638 if(ret)
1639 goto done;
1640
1641 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1642 if (!ctl) {
1643 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1644 __func__, avt_device_drift_mixer_ctl_name);
1645
1646 ret = -EINVAL;
1647 goto done;
1648 }
1649
1650 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1651 avt_device_drift_mixer_ctl_name);
1652
1653 mixer_ctl_update(ctl);
1654 count = mixer_ctl_get_num_values(ctl);
1655 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1656 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1657 __func__);
1658
1659 ret = -EINVAL;
1660 goto done;
1661 }
1662
1663 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1664 if (ret != 0) {
1665 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1666 __func__);
1667
1668 ret = -EINVAL;
1669 goto done;
1670 }
1671 memcpy(drift_param, &drift_stats.drift_param,
1672 sizeof(struct audio_avt_device_drift_param));
1673done:
1674 return ret;
1675}
Manish Dewangan07de2142017-02-27 19:27:20 +05301676
1677#ifdef SNDRV_COMPRESS_PATH_DELAY
1678int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1679{
1680 int ret = -EINVAL;
1681 struct snd_compr_metadata metadata;
1682 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1683
1684 if (property_get_bool("audio.playback.dsp.pathdelay", false)) {
1685 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1686 if (!(is_offload_usecase(out->usecase))) {
1687 ALOGE("%s:: not supported for non offload session", __func__);
1688 goto exit;
1689 }
1690
1691 if (!out->compr) {
1692 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1693 __func__);
1694 goto exit;
1695 }
1696
1697 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1698 ret = compress_get_metadata(out->compr, &metadata);
1699 if(ret) {
1700 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1701 goto exit;
1702 }
1703 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1704 } else {
1705 ALOGD("%s:: Using Fix DSP delay",__func__);
1706 }
1707
1708exit:
1709 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1710 return delay_ms;
1711}
1712#else
1713int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1714{
1715 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1716}
1717#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301718
1719#ifdef SNDRV_COMPRESS_RENDER_MODE
1720int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1721{
1722 struct snd_compr_metadata metadata;
1723 int ret = -EINVAL;
1724
1725 if (!(is_offload_usecase(out->usecase))) {
1726 ALOGE("%s:: not supported for non offload session", __func__);
1727 goto exit;
1728 }
1729
1730 if (!out->compr) {
1731 ALOGD("%s:: Invalid compress handle",
1732 __func__);
1733 goto exit;
1734 }
1735
1736 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1737
1738 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1739 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1740 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1741 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1742 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1743 } else {
1744 ret = 0;
1745 goto exit;
1746 }
1747 ret = compress_set_metadata(out->compr, &metadata);
1748 if(ret) {
1749 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1750 }
1751exit:
1752 return ret;
1753}
1754#else
1755int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1756{
1757 ALOGD("%s:: configuring render mode not supported", __func__);
1758 return 0;
1759}
1760#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301761
1762#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1763int audio_extn_utils_compress_set_clk_rec_mode(
1764 struct audio_usecase *usecase)
1765{
1766 struct snd_compr_metadata metadata;
1767 struct stream_out *out = NULL;
1768 int ret = -EINVAL;
1769
1770 if (usecase != NULL && usecase->type != PCM_PLAYBACK) {
1771 ALOGE("%s:: Invalid use case", __func__);
1772 goto exit;
1773 }
1774
1775 out = usecase->stream.out;
1776 if (!out) {
1777 ALOGE("%s:: invalid stream", __func__);
1778 goto exit;
1779 }
1780
1781 if (!is_offload_usecase(out->usecase)) {
1782 ALOGE("%s:: not supported for non offload session", __func__);
1783 goto exit;
1784 }
1785
1786 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1787 ALOGD("%s:: clk recovery is only supported in STC render mode",
1788 __func__);
1789 ret = 0;
1790 goto exit;
1791 }
1792
1793 if (!out->compr) {
1794 ALOGD("%s:: Invalid compress handle",
1795 __func__);
1796 goto exit;
1797 }
1798 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1799 switch(usecase->out_snd_device) {
1800 case SND_DEVICE_OUT_HDMI:
1801 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1802 case SND_DEVICE_OUT_DISPLAY_PORT:
1803 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1804 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1805 break;
1806 default:
1807 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1808 break;
1809 }
1810
1811 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1812
1813 ret = compress_set_metadata(out->compr, &metadata);
1814 if(ret) {
1815 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1816 }
1817
1818exit:
1819 return ret;
1820}
1821#else
1822int audio_extn_utils_compress_set_clk_rec_mode(
1823 struct audio_usecase *usecase __unused)
1824{
1825 ALOGD("%s:: configuring render mode not supported", __func__);
1826 return 0;
1827}
1828#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301829
1830#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1831int audio_extn_utils_compress_set_render_window(
1832 struct stream_out *out,
1833 struct audio_out_render_window_param *render_window)
1834{
1835 struct snd_compr_metadata metadata;
1836 int ret = -EINVAL;
1837
1838 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1839 __func__,render_window->render_ws, render_window->render_we);
1840
1841 if(render_window == NULL) {
1842 ALOGE("%s:: Invalid render_window", __func__);
1843 goto exit;
1844 }
1845
1846 if (!is_offload_usecase(out->usecase)) {
1847 ALOGE("%s:: not supported for non offload session", __func__);
1848 goto exit;
1849 }
1850
1851 if ((out->render_mode == RENDER_MODE_AUDIO_MASTER) ||
1852 (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER)) {
1853 memcpy(&out->render_window, render_window,
1854 sizeof(struct audio_out_render_window_param));
1855 } else {
1856 ALOGD("%s:: only supported in timestamp mode, current "
1857 "render mode mode %d", __func__, out->render_mode);
1858 goto exit;
1859 }
1860
1861 if (!out->compr) {
1862 ALOGW("%s:: offload session not yet opened,"
1863 "render window will be configure later", __func__);
1864 /* store render window to reconfigure in start_output_stream() */
1865 goto exit;
1866 }
1867
1868 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1869 /*render window start value */
1870 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1871 metadata.value[1] = \
1872 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1873 /*render window end value */
1874 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1875 metadata.value[3] = \
1876 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1877
1878 ret = compress_set_metadata(out->compr, &metadata);
1879 if(ret) {
1880 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1881 }
1882
1883exit:
1884 return ret;
1885}
1886#else
1887int audio_extn_utils_compress_set_render_window(
1888 struct stream_out *out __unused,
1889 struct audio_out_render_window_param *render_window __unused)
1890{
1891 ALOGD("%s:: configuring render window not supported", __func__);
1892 return 0;
1893}
1894#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05301895
1896#ifdef SNDRV_COMPRESS_START_DELAY
1897int audio_extn_utils_compress_set_start_delay(
1898 struct stream_out *out,
1899 struct audio_out_start_delay_param *delay_param)
1900{
1901 struct snd_compr_metadata metadata;
1902 int ret = -EINVAL;
1903
1904 if(delay_param == NULL) {
1905 ALOGE("%s:: Invalid delay_param", __func__);
1906 goto exit;
1907 }
1908
1909 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
1910 delay_param->start_delay);
1911
1912 if (!is_offload_usecase(out->usecase)) {
1913 ALOGE("%s:: not supported for non offload session", __func__);
1914 goto exit;
1915 }
1916
1917 if ((out->render_mode == RENDER_MODE_AUDIO_MASTER) ||
1918 (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER)) {
1919 /* store it to reconfigure in start_output_stream() */
1920 out->delay_param.start_delay = delay_param->start_delay;
1921 } else {
1922 ALOGD("%s:: only supported in timestamp mode, current "
1923 "render mode mode %d", __func__, out->render_mode);
1924 goto exit;
1925 }
1926
1927 if (!out->compr) {
1928 ALOGW("%s:: offload session not yet opened,"
1929 "start delay will be configure later", __func__);
1930 goto exit;
1931 }
1932
1933 metadata.key = SNDRV_COMPRESS_START_DELAY;
1934 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
1935 metadata.value[1] = \
1936 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
1937
1938 ret = compress_set_metadata(out->compr, &metadata);
1939 if(ret) {
1940 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1941 }
1942
1943exit:
1944 return ret;
1945}
1946#else
1947int audio_extn_utils_compress_set_start_delay(
1948 struct stream_out *out __unused,
1949 struct audio_out_start_delay_param *delay_param __unused)
1950{
1951 ALOGD("%s:: configuring render window not supported", __func__);
1952 return 0;
1953}
1954#endif