blob: 198d8717c7a3999eff77b31ff0fab9dea2b52a15 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Divya Narayanan Poojary356e01e2018-02-06 14:25:16 +05302 * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_utils"
21/* #define LOG_NDEBUG 0 */
22
Manish Dewangan27346042017-03-01 12:56:12 +053023#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070024#include <errno.h>
25#include <cutils/properties.h>
26#include <cutils/config_utils.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <cutils/str_parms.h>
30#include <cutils/log.h>
31#include <cutils/misc.h>
32
Manish Dewangan07de2142017-02-27 19:27:20 +053033
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034#include "audio_hw.h"
35#include "platform.h"
36#include "platform_api.h"
37#include "audio_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080038#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053039#include <sound/compress_params.h>
40#include <sound/compress_offload.h>
41#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053042
43#ifdef DYNAMIC_LOG_ENABLED
44#include <log_xml_parser.h>
45#define LOG_MASK HAL_MOD_FILE_UTILS
46#include <log_utils.h>
47#endif
48
Ashish Jain81eb2a82015-05-13 10:52:34 +053049#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
50#ifdef HDMI_PASSTHROUGH_ENABLED
51#include "audio_parsers.h"
52#endif
53#endif
54
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053055#ifdef LINUX_ENABLED
56#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053058#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053060#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053061#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053064#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070065
66#define DYNAMIC_VALUE_TAG "dynamic"
67#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053068#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070069#define FORMATS_TAG "formats"
70#define SAMPLING_RATES_TAG "sampling_rates"
71#define BIT_WIDTH_TAG "bit_width"
72#define APP_TYPE_TAG "app_type"
73
74#define STRING_TO_ENUM(string) { #string, string }
75#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
76
Ben Rombergera04fabc2014-11-14 12:16:03 -080077#define BASE_TABLE_SIZE 64
78#define MAX_BASEINDEX_LEN 256
79
Ben Romberger1aaaf862017-04-06 17:49:46 -070080#ifndef SND_AUDIOCODEC_TRUEHD
81#define SND_AUDIOCODEC_TRUEHD 0x00000023
82#endif
83
Vikram Panduranga93f080e2017-06-07 18:16:14 -070084#define APP_TYPE_VOIP_AUDIO 0x1113A
85
Ashish Jain81eb2a82015-05-13 10:52:34 +053086#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
87#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
88#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
89#define SR_44100 (0<<0) /* 44.1kHz */
90#define SR_NOTID (1<<0) /* non indicated */
91#define SR_48000 (2<<0) /* 48kHz */
92#define SR_32000 (3<<0) /* 32kHz */
93#define SR_22050 (4<<0) /* 22.05kHz */
94#define SR_24000 (6<<0) /* 24kHz */
95#define SR_88200 (8<<0) /* 88.2kHz */
96#define SR_96000 (10<<0) /* 96kHz */
97#define SR_176400 (12<<0) /* 176.4kHz */
98#define SR_192000 (14<<0) /* 192kHz */
99
100#endif
Manish Dewangan07de2142017-02-27 19:27:20 +0530101
102/* ToDo: Check and update a proper value in msec */
103#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
104
Varun Balaraje49253e2017-07-06 19:48:56 +0530105#ifndef MAX_CHANNELS_SUPPORTED
106#define MAX_CHANNELS_SUPPORTED 8
107#endif
108
`Deeraj Soman676c2702017-09-18 19:25:53 +0530109typedef struct {
110 const char *id_string;
111 const int value;
112} mixer_config_lookup;
113
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700114struct string_to_enum {
115 const char *name;
116 uint32_t value;
117};
118
119const struct string_to_enum s_flag_name_to_enum_table[] = {
120 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
121 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
122 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800123 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700124 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
125 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
126 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700127 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128#ifdef INCALL_MUSIC_ENABLED
129 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
130#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700131 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530132 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700133 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Ben Romberger6c4d3812017-06-13 17:46:45 -0700134 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_BD),
Varun Balaraje49253e2017-07-06 19:48:56 +0530135 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INTERACTIVE),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530136 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
137 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
138 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
139 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
140 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530141 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Dhananjay Kumar704ce6f2017-09-28 22:08:00 +0530142 STRING_TO_ENUM(AUDIO_INPUT_FLAG_COMPRESS),
Ralf Herzaec80262018-07-03 07:08:49 +0200143 STRING_TO_ENUM(AUDIO_INPUT_FLAG_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700144};
145
146const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530147 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700148 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530149 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
150 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530151 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700152 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
153 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
154 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700155 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
156 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700157 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700158 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700159 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530160 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700161 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700162 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530163#ifdef AUDIO_EXTN_FORMATS_ENABLED
164 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700165 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
166 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
167 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700168 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
169 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
170 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
171 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
172 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
173 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
174 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700175 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530176 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
177 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700178 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800179 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
180 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
181 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530182 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530183 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
184 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
185 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530186 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530187 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
188 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
189 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
190 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530191 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700192#endif
193};
194
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530195/* payload structure avt_device drift query */
196struct audio_avt_device_drift_stats {
197 uint32_t minor_version;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530198
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530199 /* Indicates the device interface direction as either
200 * source (Tx) or sink (Rx).
201 */
202 uint16_t device_direction;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530203
204 /* Reference timer for drift accumulation and time stamp information.
205 * currently it only support AFE_REF_TIMER_TYPE_AVTIMER
206 */
207 uint16_t reference_timer;
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530208 struct audio_avt_device_drift_param drift_param;
Manish Dewangan338c50a2017-09-12 15:22:03 +0530209} __attribute__((packed));
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530210
Ben Rombergera04fabc2014-11-14 12:16:03 -0800211static char bTable[BASE_TABLE_SIZE] = {
212 'A','B','C','D','E','F','G','H','I','J','K','L',
213 'M','N','O','P','Q','R','S','T','U','V','W','X',
214 'Y','Z','a','b','c','d','e','f','g','h','i','j',
215 'k','l','m','n','o','p','q','r','s','t','u','v',
216 'w','x','y','z','0','1','2','3','4','5','6','7',
217 '8','9','+','/'
218};
219
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700220static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
221 const char *name)
222{
223 size_t i;
224 for (i = 0; i < size; i++) {
225 if (strcmp(table[i].name, name) == 0) {
226 ALOGV("%s found %s", __func__, table[i].name);
227 return table[i].value;
228 }
229 }
230 return 0;
231}
232
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530233static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700234{
235 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530236 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800237 char *last_r;
238 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700239 while (flag_name != NULL) {
240 if (strlen(flag_name) != 0) {
241 flag |= string_to_enum(s_flag_name_to_enum_table,
242 ARRAY_SIZE(s_flag_name_to_enum_table),
243 flag_name);
244 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800245 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700246 }
247
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800248 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530249 io_flags.in_flags = (audio_input_flags_t)flag;
250 io_flags.out_flags = (audio_output_flags_t)flag;
251 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700252}
253
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530254static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255{
256 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800257 char *last_r;
258 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700259
260 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
261 return;
262
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530263 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700264 while (str != NULL) {
265 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
266 ARRAY_SIZE(s_format_name_to_enum_table), str);
267 ALOGV("%s: format - %d", __func__, format);
268 if (format != 0) {
269 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700270 if (sf_info == NULL)
271 break; /* return whatever was parsed */
272
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700273 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530274 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700275 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800276 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700277 }
278}
279
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530280static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700281{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700282 struct stream_sample_rate *ss_info = NULL;
283 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800284 char *last_r;
285 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700286
Amit Shekhar6f461b12014-08-01 14:52:58 -0700287 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
288 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530290 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700291 while (str != NULL) {
292 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
293 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
294 if (0 != sample_rate) {
295 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800296 if (!ss_info) {
297 ALOGE("%s: memory allocation failure", __func__);
298 return;
299 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700300 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530301 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700302 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800303 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700304 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305}
306
307static int parse_bit_width_names(char *name)
308{
309 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800310 char *last_r;
311 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700312
313 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
314 bit_width = (int)strtol(str, (char **)NULL, 10);
315
316 ALOGV("%s: bit_width - %d", __func__, bit_width);
317 return bit_width;
318}
319
320static int parse_app_type_names(void *platform, char *name)
321{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700322 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800323 char *last_r;
324 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700325
326 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
327 app_type = (int)strtol(str, (char **)NULL, 10);
328
329 ALOGV("%s: app_type - %d", __func__, app_type);
330 return app_type;
331}
332
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530333static void update_streams_cfg_list(cnode *root, void *platform,
334 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700335{
336 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530337 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700338
339 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530340 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700341
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530342 if (!s_info) {
343 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700344 return;
345 }
346
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700347 while (node) {
348 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530350 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
351 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700352 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530353 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700354 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530355 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
356 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700357 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530358 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700359 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530360 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700361 }
362 node = node->next;
363 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530364 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700365}
366
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530367static void load_cfg_list(cnode *root, void *platform,
368 struct listnode *streams_output_cfg_list,
369 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530371 cnode *node = NULL;
372
373 node = config_find(root, OUTPUTS_TAG);
374 if (node != NULL) {
375 node = node->first_child;
376 while (node) {
377 ALOGV("%s: loading output %s", __func__, node->name);
378 update_streams_cfg_list(node, platform, streams_output_cfg_list);
379 node = node->next;
380 }
381 } else {
382 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700383 }
384
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 node = config_find(root, INPUTS_TAG);
386 if (node != NULL) {
387 node = node->first_child;
388 while (node) {
389 ALOGV("%s: loading input %s", __func__, node->name);
390 update_streams_cfg_list(node, platform, streams_input_cfg_list);
391 node = node->next;
392 }
393 } else {
394 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700395 }
396}
397
398static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530399 struct listnode *streams_output_cfg_list,
400 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700401{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530402 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700403 int length = 0, i, num_app_types = 0;
404 struct listnode *node;
405 bool update;
406 struct mixer_ctl *ctl = NULL;
407 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530408 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800409 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700410
411 if (!mixer) {
412 ALOGE("%s: mixer is null",__func__);
413 return;
414 }
415 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
416 if (!ctl) {
417 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
418 return;
419 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530420 app_type_cfg[length++] = num_app_types;
421
422 if (list_empty(streams_output_cfg_list)) {
423 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700424 app_type_cfg[length++] = 48000;
425 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530426 num_app_types += 1;
427 }
428 if (list_empty(streams_input_cfg_list)) {
429 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
430 app_type_cfg[length++] = 48000;
431 app_type_cfg[length++] = 16;
432 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700433 }
434
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800435 /* get target bit width for ADM enforce mode */
436 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
437
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700438 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530439 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700440 update = true;
441 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700443 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530444 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530445 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530446 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530447 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530448 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800449 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
450 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
451 (target_bit_width > app_type_cfg[i+3]))
452 app_type_cfg[i+3] = target_bit_width;
453
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700454 update = false;
455 break;
456 }
457 }
458 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530459 num_app_types += 1;
460 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
461 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800462 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
463 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
464 (target_bit_width > app_type_cfg[length]))
465 app_type_cfg[length] = target_bit_width;
466
467 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530468 }
469 }
470 list_for_each(node, streams_input_cfg_list) {
471 s_info = node_to_item(node, struct streams_io_cfg, list);
472 update = true;
473 for (i=0; i<length; i=i+3) {
474 if (app_type_cfg[i+1] == 0)
475 break;
476 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530477 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530478 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530479 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530480 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
481 update = false;
482 break;
483 }
484 }
485 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
486 num_app_types += 1;
487 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
488 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
489 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700490 }
491 }
492 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
493 if (num_app_types) {
494 app_type_cfg[0] = num_app_types;
495 mixer_ctl_set_array(ctl, app_type_cfg, length);
496 }
497}
498
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530499void audio_extn_utils_update_streams_cfg_lists(void *platform,
500 struct mixer *mixer,
501 struct listnode *streams_output_cfg_list,
502 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700503{
504 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530505 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700506
507 ALOGV("%s", __func__);
508 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530509 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700510
511 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700512 if (root == NULL) {
513 ALOGE("cfg_list, NULL config root");
514 return;
515 }
516
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530517 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
518 if (data == NULL) {
519 ALOGD("%s: failed to open io config file(%s), trying older config file",
520 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
521 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
522 if (data == NULL) {
523 send_app_type_cfg(platform, mixer,
524 streams_output_cfg_list,
525 streams_input_cfg_list);
526 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800527 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530528 return;
529 }
530 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700531
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530532 config_load(root, data);
533 load_cfg_list(root, platform, streams_output_cfg_list,
534 streams_input_cfg_list);
535
536 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
537 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800538
539 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800540 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800541 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700542}
543
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530544static void audio_extn_utils_dump_streams_cfg_list(
545 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700546{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700547 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530548 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700549 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700550 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530551
552 list_for_each(node_i, streams_cfg_list) {
553 s_info = node_to_item(node_i, struct streams_io_cfg, list);
554 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
555 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
556 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
557 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700558 sf_info = node_to_item(node_j, struct stream_format, list);
559 ALOGV("format-%x", sf_info->format);
560 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530561 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700562 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
563 ALOGV("sample rate-%d", ss_info->sample_rate);
564 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700565 }
566}
567
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530568void audio_extn_utils_dump_streams_cfg_lists(
569 struct listnode *streams_output_cfg_list,
570 struct listnode *streams_input_cfg_list)
571{
572 ALOGV("%s", __func__);
573 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
574 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
575}
576
577static void audio_extn_utils_release_streams_cfg_list(
578 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700579{
580 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530581 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700582
583 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530584
585 while (!list_empty(streams_cfg_list)) {
586 node_i = list_head(streams_cfg_list);
587 s_info = node_to_item(node_i, struct streams_io_cfg, list);
588 while (!list_empty(&s_info->format_list)) {
589 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700590 list_remove(node_j);
591 free(node_to_item(node_j, struct stream_format, list));
592 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530593 while (!list_empty(&s_info->sample_rate_list)) {
594 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700595 list_remove(node_j);
596 free(node_to_item(node_j, struct stream_sample_rate, list));
597 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700598 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530599 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700600 }
601}
602
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530603void audio_extn_utils_release_streams_cfg_lists(
604 struct listnode *streams_output_cfg_list,
605 struct listnode *streams_input_cfg_list)
606{
607 ALOGV("%s", __func__);
608 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
609 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
610}
611
612static bool set_app_type_cfg(struct streams_io_cfg *s_info,
613 struct stream_app_type_cfg *app_type_cfg,
614 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700615 {
616 struct listnode *node_i;
617 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530618 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700619 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
620 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530621 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700622
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530623 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700624 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530625 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700626 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
627 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
628 return true;
629 }
630 }
631 /*
632 * Reiterate through the list assuming dafault sample rate.
633 * Handles scenario where input sample rate is higher
634 * than all sample rates in list for the input bit width.
635 */
636 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800637
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530638 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700639 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
640 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530641 (bit_width == s_info->app_type_cfg.bit_width)) {
642 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700643 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530644 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800645 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 -0700646 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
647 return true;
648 }
649 }
650 return false;
651}
652
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530653void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
654 struct listnode *streams_input_cfg_list,
655 audio_devices_t devices __unused,
656 audio_input_flags_t flags,
657 audio_format_t format,
658 uint32_t sample_rate,
659 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530660 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530661 struct stream_app_type_cfg *app_type_cfg)
662{
663 struct listnode *node_i, *node_j;
664 struct streams_io_cfg *s_info;
665 struct stream_format *sf_info;
666
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530667 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
668 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530669
670 list_for_each(node_i, streams_input_cfg_list) {
671 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530672 /* Along with flags do profile matching if set at either end.*/
673 if (s_info->flags.in_flags == flags &&
674 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
675 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530676 list_for_each(node_j, &s_info->format_list) {
677 sf_info = node_to_item(node_j, struct stream_format, list);
678 if (sf_info->format == format) {
679 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
680 return;
681 }
682 }
683 }
684 }
685 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
686 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
687 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
688 app_type_cfg->bit_width = 16;
689}
690
691void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700692 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700693 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700694 audio_output_flags_t flags,
695 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700696 uint32_t sample_rate,
697 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530698 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530699 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700700 struct stream_app_type_cfg *app_type_cfg)
701{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530702 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530703 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700704 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530705 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700706
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530707 if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
708 int bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
709 if ((-ENOSYS != bw) && (bit_width > (uint32_t)bw))
Amit Shekhar5a39c912014-10-14 15:39:30 -0700710 bit_width = (uint32_t)bw;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530711 else if (-ENOSYS == bw)
712 bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Amit Shekhar1d896042014-10-03 13:16:09 -0700713 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ramu Gottipati074fa4d2018-09-11 20:05:51 +0530714 ALOGI("%s Allowing 24 and above bits playback on speaker ONLY at default sampling rate", __func__);
Amit Shekhar1d896042014-10-03 13:16:09 -0700715 }
716
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700717 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530718 if (!strncmp("true", value, sizeof("true"))) {
719 if ((popcount(channel_mask) > 2) &&
720 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700721 !(flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530722 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
723 ALOGD("%s: MCH session defaulting sample rate to %d",
724 __func__, sample_rate);
725 }
726 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530727
728 /* Set sampling rate to 176.4 for DSD64
729 * and 352.8Khz for DSD128.
730 * Set Bit Width to 16. output will be 16 bit
731 * post DoP in ASM.
732 */
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700733 if ((flags & (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530734 (format == AUDIO_FORMAT_DSD)) {
735 bit_width = 16;
736 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
737 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
738 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
739 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
740 }
741
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530742
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800743 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
744 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700745 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530746 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530747 /* Along with flags do profile matching if set at either end.*/
748 if (s_info->flags.out_flags == flags &&
749 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
750 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530751 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700752 sf_info = node_to_item(node_j, struct stream_format, list);
753 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530754 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700755 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700756 }
757 }
758 }
759 }
760 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530761 s_info = node_to_item(node_i, struct streams_io_cfg, list);
762 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700763 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530764 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
765 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
766 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700767 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530768 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700769 return;
770 }
771 }
772 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700773 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700774 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700775 app_type_cfg->bit_width = 16;
776}
777
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530778static bool audio_is_this_native_usecase(struct audio_usecase *uc)
779{
780 bool native_usecase = false;
781 struct stream_out *out = (struct stream_out*) uc->stream.out;
782
783 if (PCM_PLAYBACK == uc->type && out != NULL &&
784 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
785 is_offload_usecase(uc->id) &&
786 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
787 native_usecase = true;
788
789 return native_usecase;
790}
791
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800792bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
793{
794 /* DSP bitwidth enforce mode for ADM and AFE:
795 * includes:
796 * deep buffer, low latency, direct pcm and offload.
797 * excludes:
798 * ull(raw+fast), VOIP.
799 */
800 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
801 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
802 (flags & AUDIO_OUTPUT_FLAG_FAST)))
803 return false;
804
805
806 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
807 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
808 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
809 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
810 return true;
811 else
812 return false;
813}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800814
815static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
816{
817 return adev->vr_audio_mode_enabled;
818}
819
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530820void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
821 struct audio_device *adev,
822 struct audio_usecase *usecase)
823{
824 ALOGV("%s", __func__);
825
826 switch(usecase->type) {
827 case PCM_PLAYBACK:
828 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
829 &adev->streams_output_cfg_list,
830 usecase->stream.out->devices,
831 usecase->stream.out->flags,
Aalique Grahame65780b52017-09-27 14:59:56 -0700832 usecase->stream.out->hal_op_format,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530833 usecase->stream.out->sample_rate,
834 usecase->stream.out->bit_width,
835 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530836 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530837 &usecase->stream.out->app_type_cfg);
838 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
839 break;
840 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700841 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
842 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
843 else
844 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530845 &adev->streams_input_cfg_list,
846 usecase->stream.in->device,
847 usecase->stream.in->flags,
848 usecase->stream.in->format,
849 usecase->stream.in->sample_rate,
850 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530851 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530852 &usecase->stream.in->app_type_cfg);
853 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
854 break;
Surendar Karka93cd25a2018-08-28 14:21:37 +0530855 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik343abc62017-08-08 11:15:25 +0530856 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
857 &adev->streams_output_cfg_list,
858 usecase->stream.inout->out_config.devices,
859 0,
860 usecase->stream.inout->out_config.format,
861 usecase->stream.inout->out_config.sample_rate,
862 usecase->stream.inout->out_config.bit_width,
863 usecase->stream.inout->out_config.channel_mask,
864 usecase->stream.inout->profile,
865 &usecase->stream.inout->out_app_type_cfg);
866 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.inout->out_app_type_cfg.app_type);
867 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530868 default:
869 ALOGE("%s: app type cfg not supported for usecase type (%d)",
870 __func__, usecase->type);
871 }
872}
873
Divya Narayanan Poojary356e01e2018-02-06 14:25:16 +0530874void audio_extn_btsco_get_sample_rate(int snd_device, int *sample_rate)
875{
876 switch (snd_device) {
877 case SND_DEVICE_OUT_BT_SCO:
878 case SND_DEVICE_IN_BT_SCO_MIC:
879 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
880 *sample_rate = 8000;
881 break;
882 case SND_DEVICE_OUT_BT_SCO_WB:
883 case SND_DEVICE_IN_BT_SCO_MIC_WB:
884 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
885 *sample_rate = 16000;
886 break;
887 default:
888 ALOGD("%s:Not a BT SCO device, need not update sampling rate\n", __func__);
889 break;
890 }
891}
892
Siena Richard7c2db772016-12-21 11:32:34 -0800893static int send_app_type_cfg_for_device(struct audio_device *adev,
894 struct audio_usecase *usecase,
895 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700896{
897 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530898 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
899 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700900 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800901 int pcm_device_id = 0, acdb_dev_id, app_type;
902 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530903 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530904 char value[PROPERTY_VALUE_MAX] = {0};
Varun Balaraje49253e2017-07-06 19:48:56 +0530905 struct streams_io_cfg *s_info = NULL;
906 struct listnode *node = NULL;
Nikhil Laturkarac4a7492017-08-31 18:27:19 +0530907 int bd_app_type = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700908
Siena Richard7c2db772016-12-21 11:32:34 -0800909 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
910 __func__, platform_get_snd_device_name(usecase->out_snd_device),
911 platform_get_snd_device_name(usecase->in_snd_device),
912 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700913
Siddartha Shaik343abc62017-08-08 11:15:25 +0530914 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE &&
Surendar Karka93cd25a2018-08-28 14:21:37 +0530915 usecase->type != TRANSCODE_LOOPBACK_RX) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530916 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700917 rc = 0;
918 goto exit_send_app_type_cfg;
919 }
920 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
921 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
922 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800923 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700924 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Surendar Karka93cd25a2018-08-28 14:21:37 +0530925 (usecase->id != USECASE_AUDIO_TRANSCODE_LOOPBACK_RX) &&
Varun Balaraje49253e2017-07-06 19:48:56 +0530926 (!is_interactive_usecase(usecase->id)) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530927 (!is_offload_usecase(usecase->id)) &&
928 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530929 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 -0700930 rc = 0;
931 goto exit_send_app_type_cfg;
932 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800933
934 //if VR is active then only send the mixer control
935 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
936 ALOGI("ULL doesnt need sending app type cfg, returning");
937 rc = 0;
938 goto exit_send_app_type_cfg;
939 }
940
Surendar Karka93cd25a2018-08-28 14:21:37 +0530941 if (usecase->type == PCM_PLAYBACK || usecase->type == TRANSCODE_LOOPBACK_RX) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700942 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530943 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
944 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700945 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700946 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530947 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
948 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530949 }
Siena Richard7c2db772016-12-21 11:32:34 -0800950
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700951 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
952 if (!ctl) {
953 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
954 mixer_ctl_name);
955 rc = -EINVAL;
956 goto exit_send_app_type_cfg;
957 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530958 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Divya Narayanan Poojary356e01e2018-02-06 14:25:16 +0530959 if (voice_is_in_call_rec_stream(usecase->stream.in) && usecase->type == PCM_CAPTURE) {
960 snd_device_t voice_device = voice_get_incall_rec_snd_device(usecase->in_snd_device);
961 acdb_dev_id = platform_get_snd_device_acdb_id(voice_device);
962 ALOGV("acdb id for voice call use case %d", acdb_dev_id);
963 } else {
964 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
965 }
Rohit Kumar1181b292017-01-31 18:07:17 +0530966 if (acdb_dev_id <= 0) {
967 ALOGE("%s: Couldn't get the acdb dev id", __func__);
968 rc = -EINVAL;
969 goto exit_send_app_type_cfg;
970 }
971
Siena Richard7c2db772016-12-21 11:32:34 -0800972 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
973 if (snd_device_be_idx < 0) {
974 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
975 __func__, platform_get_snd_device_name(snd_device),
976 snd_device_be_idx);
977 }
978
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530979 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530980
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700981 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530982 if (!strncmp("true", value, sizeof("true"))) {
983 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
984 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
Aalique Grahame5cd12ff2017-10-19 10:57:00 -0700985 !(usecase->stream.out->flags &
986 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530987 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
988 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530989
kunleizcdf25942017-09-20 14:01:21 +0800990 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
991 usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
992 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Dieter Luecking5d57def2018-09-07 14:23:37 +0200993 if (platform_spkr_use_default_sample_rate(adev->platform)) {
994 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
995 } else {
996 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
997 usecase->stream.out->sample_rate,
998 &usecase->stream.out->app_type_cfg.sample_rate);
999 }
1000
Ashish Jaina052e572016-11-07 16:41:07 +05301001 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
1002 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
1003 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
1004 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
1005 /*
1006 * To best utlize DSP, check if the stream sample rate is supported/multiple of
1007 * configured device sample rate, if not update the COPP rate to be equal to the
1008 * device sample rate, else open COPP at stream sample rate
1009 */
1010 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
1011 usecase->stream.out->sample_rate,
1012 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05301013 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
1014 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -07001015 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
1016 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +05301017 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -07001018 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301019 } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
1020 /*
1021 * For a2dp playback get encoder sampling rate and set copp sampling rate,
1022 * for bit width use the stream param only.
1023 */
1024 audio_extn_a2dp_get_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
1025 ALOGI("%s using %d sample rate rate for A2DP CoPP",
1026 __func__, usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001027 }
Divya Narayanan Poojary356e01e2018-02-06 14:25:16 +05301028 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -07001029 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
1030
Varun Balaraje49253e2017-07-06 19:48:56 +05301031 /* Interactive streams are supported with only direct app type id.
1032 * Get Direct profile app type and use it for interactive streams
1033 */
1034 list_for_each(node, &adev->streams_output_cfg_list) {
1035 s_info = node_to_item(node, struct streams_io_cfg, list);
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001036 if (s_info->flags.out_flags == (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_BD |
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301037 AUDIO_OUTPUT_FLAG_DIRECT_PCM |
1038 AUDIO_OUTPUT_FLAG_DIRECT))
1039 bd_app_type = s_info->app_type_cfg.app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301040 }
Aalique Grahame5cd12ff2017-10-19 10:57:00 -07001041 if (usecase->stream.out->flags == (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INTERACTIVE)
Nikhil Laturkarac4a7492017-08-31 18:27:19 +05301042 app_type = bd_app_type;
Varun Balaraje49253e2017-07-06 19:48:56 +05301043 else
1044 app_type = usecase->stream.out->app_type_cfg.app_type;
Siena Richard7c2db772016-12-21 11:32:34 -08001045 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -07001046 app_type_cfg[len++] = acdb_dev_id;
1047 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001048 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
1049 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Harsh Bansal026d97f2017-08-17 17:44:49 +05301050 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)
1051 && !audio_extn_passthru_is_convert_supported(adev, usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -07001052
1053 sample_rate = sample_rate * 4;
1054 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
1055 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -07001056 }
Naresh Tanniru3a406772017-05-10 13:09:05 -07001057 app_type_cfg[len++] = sample_rate;
1058
Siena Richard7c2db772016-12-21 11:32:34 -08001059 if (snd_device_be_idx > 0)
1060 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301061
Siena Richard7c2db772016-12-21 11:32:34 -08001062 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1063 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301064
1065 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -08001066 app_type = usecase->stream.in->app_type_cfg.app_type;
1067 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301068 app_type_cfg[len++] = acdb_dev_id;
kunleizcdf25942017-09-20 14:01:21 +08001069 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
1070 usecase->stream.in->app_type_cfg.sample_rate = usecase->stream.in->sample_rate;
Divya Narayanan Poojary356e01e2018-02-06 14:25:16 +05301071 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1072 audio_extn_btsco_get_sample_rate(usecase->in_snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1073 } else {
1074 audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
1075 }
Siena Richard7c2db772016-12-21 11:32:34 -08001076 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
1077 app_type_cfg[len++] = sample_rate;
1078 if (snd_device_be_idx > 0)
1079 app_type_cfg[len++] = snd_device_be_idx;
1080 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1081 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301082 } else {
Siena Richard7c2db772016-12-21 11:32:34 -08001083 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301084 if(usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301085 sample_rate = usecase->stream.inout->out_config.sample_rate;
1086 app_type = usecase->stream.inout->out_app_type_cfg.app_type;
1087 }
Siena Richard7c2db772016-12-21 11:32:34 -08001088 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301089 app_type_cfg[len++] = acdb_dev_id;
1090 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -08001091 if (snd_device_be_idx > 0)
1092 app_type_cfg[len++] = snd_device_be_idx;
1093 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
1094 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +05301095 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301096
Siddartha Shaik343abc62017-08-08 11:15:25 +05301097 if(ctl)
1098 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001099 rc = 0;
1100exit_send_app_type_cfg:
1101 return rc;
1102}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001103
Siena Richard7c2db772016-12-21 11:32:34 -08001104int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1105 struct audio_usecase *usecase)
1106{
1107 int i, num_devices = 0;
1108 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
Divya Narayanan Poojary356e01e2018-02-06 14:25:16 +05301109 snd_device_t in_snd_device = usecase->in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001110 int rc = 0;
1111
1112 switch (usecase->type) {
1113 case PCM_PLAYBACK:
Surendar Karka93cd25a2018-08-28 14:21:37 +05301114 case TRANSCODE_LOOPBACK_RX:
Siena Richard7c2db772016-12-21 11:32:34 -08001115 ALOGD("%s: usecase->out_snd_device %s",
1116 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1117 /* check for out combo device */
1118 if (platform_split_snd_device(adev->platform,
1119 usecase->out_snd_device,
1120 &num_devices, new_snd_devices)) {
1121 new_snd_devices[0] = usecase->out_snd_device;
1122 num_devices = 1;
1123 }
1124 break;
1125 case PCM_CAPTURE:
1126 ALOGD("%s: usecase->in_snd_device %s",
1127 __func__, platform_get_snd_device_name(usecase->in_snd_device));
Divya Narayanan Poojary356e01e2018-02-06 14:25:16 +05301128 if (voice_is_in_call_rec_stream(usecase->stream.in)) {
1129 in_snd_device = voice_get_incall_rec_backend_device(usecase->stream.in);
1130 }
Siena Richard7c2db772016-12-21 11:32:34 -08001131 /* check for in combo device */
1132 if (platform_split_snd_device(adev->platform,
Divya Narayanan Poojary356e01e2018-02-06 14:25:16 +05301133 in_snd_device,
Siena Richard7c2db772016-12-21 11:32:34 -08001134 &num_devices, new_snd_devices)) {
Divya Narayanan Poojary356e01e2018-02-06 14:25:16 +05301135 new_snd_devices[0] = in_snd_device;
Siena Richard7c2db772016-12-21 11:32:34 -08001136 num_devices = 1;
1137 }
1138 break;
1139 default:
1140 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1141 rc = 0;
1142 break;
1143 }
1144
1145 for (i = 0; i < num_devices; i++) {
1146 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1147 if (rc)
1148 break;
1149 }
1150
1151 return rc;
1152}
1153
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301154int read_line_from_file(const char *path, char *buf, size_t count)
1155{
1156 char * fgets_ret;
1157 FILE * fd;
1158 int rv;
1159
1160 fd = fopen(path, "r");
1161 if (fd == NULL)
1162 return -1;
1163
1164 fgets_ret = fgets(buf, (int)count, fd);
1165 if (NULL != fgets_ret) {
1166 rv = (int)strlen(buf);
1167 } else {
1168 rv = ferror(fd);
1169 }
1170 fclose(fd);
1171
1172 return rv;
1173}
1174
Ashish Jainf1eaa582016-05-23 20:54:24 +05301175/*Translates ALSA formats to AOSP PCM formats*/
1176audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1177{
1178 audio_format_t format;
1179
1180 switch(alsa_format) {
1181 case SNDRV_PCM_FORMAT_S16_LE:
1182 format = AUDIO_FORMAT_PCM_16_BIT;
1183 break;
1184 case SNDRV_PCM_FORMAT_S24_3LE:
1185 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1186 break;
1187 case SNDRV_PCM_FORMAT_S24_LE:
1188 format = AUDIO_FORMAT_PCM_8_24_BIT;
1189 break;
1190 case SNDRV_PCM_FORMAT_S32_LE:
1191 format = AUDIO_FORMAT_PCM_32_BIT;
1192 break;
1193 default:
1194 ALOGW("Incorrect ALSA format");
1195 format = AUDIO_FORMAT_INVALID;
1196 }
1197 return format;
1198}
1199
1200/*Translates hal format (AOSP) to alsa formats*/
1201uint32_t hal_format_to_alsa(audio_format_t hal_format)
1202{
1203 uint32_t alsa_format;
1204
1205 switch (hal_format) {
1206 case AUDIO_FORMAT_PCM_32_BIT: {
1207 if (platform_supports_true_32bit())
1208 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1209 else
1210 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1211 }
1212 break;
1213 case AUDIO_FORMAT_PCM_8_BIT:
1214 alsa_format = SNDRV_PCM_FORMAT_S8;
1215 break;
1216 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1217 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1218 break;
1219 case AUDIO_FORMAT_PCM_8_24_BIT: {
1220 if (platform_supports_true_32bit())
1221 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1222 else
1223 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1224 }
1225 break;
1226 case AUDIO_FORMAT_PCM_FLOAT:
1227 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1228 break;
1229 default:
1230 case AUDIO_FORMAT_PCM_16_BIT:
1231 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1232 break;
1233 }
1234 return alsa_format;
1235}
1236
Ashish Jain83a6cc22016-06-28 14:34:17 +05301237/*Translates PCM formats to AOSP formats*/
1238audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1239{
1240 audio_format_t format = AUDIO_FORMAT_INVALID;
1241
1242 switch(pcm_format) {
1243 case PCM_FORMAT_S16_LE:
1244 format = AUDIO_FORMAT_PCM_16_BIT;
1245 break;
1246 case PCM_FORMAT_S24_3LE:
1247 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1248 break;
1249 case PCM_FORMAT_S24_LE:
1250 format = AUDIO_FORMAT_PCM_8_24_BIT;
1251 break;
1252 case PCM_FORMAT_S32_LE:
1253 format = AUDIO_FORMAT_PCM_32_BIT;
1254 break;
1255 default:
1256 ALOGW("Incorrect PCM format");
1257 format = AUDIO_FORMAT_INVALID;
1258 }
1259 return format;
1260}
1261
1262/*Translates hal format (AOSP) to alsa formats*/
1263uint32_t hal_format_to_pcm(audio_format_t hal_format)
1264{
1265 uint32_t pcm_format;
1266
1267 switch (hal_format) {
1268 case AUDIO_FORMAT_PCM_32_BIT:
1269 case AUDIO_FORMAT_PCM_8_24_BIT:
1270 case AUDIO_FORMAT_PCM_FLOAT: {
1271 if (platform_supports_true_32bit())
1272 pcm_format = PCM_FORMAT_S32_LE;
1273 else
1274 pcm_format = PCM_FORMAT_S24_3LE;
1275 }
1276 break;
1277 case AUDIO_FORMAT_PCM_8_BIT:
1278 pcm_format = PCM_FORMAT_S8;
1279 break;
1280 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1281 pcm_format = PCM_FORMAT_S24_3LE;
1282 break;
1283 default:
1284 case AUDIO_FORMAT_PCM_16_BIT:
1285 pcm_format = PCM_FORMAT_S16_LE;
1286 break;
1287 }
1288 return pcm_format;
1289}
1290
Ashish Jainf1eaa582016-05-23 20:54:24 +05301291uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1292 uint32_t sample_rate,
1293 uint32_t noOfChannels)
1294{
1295 uint32_t fragment_size = 0;
1296 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1297
1298 fragment_size = (pcm_offload_time
1299 * sample_rate
1300 * bytes_per_sample
1301 * noOfChannels)/1000;
1302 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1303 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1304 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1305 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1306 /*To have same PCM samples for all channels, the buffer size requires to
1307 *be multiple of (number of channels * bytes per sample)
1308 *For writes to succeed, the buffer must be written at address which is multiple of 32
1309 */
Aalique Grahameb85f2d92017-10-16 17:53:23 -07001310 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
Ashish Jainf1eaa582016-05-23 20:54:24 +05301311
1312 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1313 return fragment_size;
1314}
1315
1316/* Calculates the fragment size required to configure compress session.
1317 * Based on the alsa format selected, decide if conversion is needed in
1318
1319 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1320 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1321 */
1322void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1323{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301324 audio_format_t dst_format = out->hal_op_format;
1325 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301326 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1327 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1328
1329 out->compr_config.fragment_size =
1330 get_alsa_fragment_size(hal_op_bytes_per_sample,
1331 out->sample_rate,
1332 popcount(out->channel_mask));
1333
1334 if ((src_format != dst_format) &&
1335 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1336
Ashish Jain83a6cc22016-06-28 14:34:17 +05301337 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301338 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1339 hal_op_bytes_per_sample);
1340 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301341 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301342 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301343 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301344 }
1345}
1346
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301347/* converts pcm format 24_8 to 8_24 inplace */
1348size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1349{
1350 size_t i = 0;
1351 int *int_buf_stream = buf;
1352
1353 if ((bytes % 4) != 0) {
1354 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1355 return -EINVAL;
1356 }
1357
1358 for (; i < (bytes / 4); i++)
1359 int_buf_stream[i] >>= 8;
1360
1361 return bytes;
1362}
1363
1364int get_snd_codec_id(audio_format_t format)
1365{
1366 int id = 0;
1367
1368 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1369 case AUDIO_FORMAT_MP3:
1370 id = SND_AUDIOCODEC_MP3;
1371 break;
1372 case AUDIO_FORMAT_AAC:
1373 id = SND_AUDIOCODEC_AAC;
1374 break;
1375 case AUDIO_FORMAT_AAC_ADTS:
1376 id = SND_AUDIOCODEC_AAC;
1377 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301378 case AUDIO_FORMAT_AAC_LATM:
1379 id = SND_AUDIOCODEC_AAC;
1380 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301381 case AUDIO_FORMAT_PCM:
1382 id = SND_AUDIOCODEC_PCM;
1383 break;
1384 case AUDIO_FORMAT_FLAC:
1385 id = SND_AUDIOCODEC_FLAC;
1386 break;
1387 case AUDIO_FORMAT_ALAC:
1388 id = SND_AUDIOCODEC_ALAC;
1389 break;
1390 case AUDIO_FORMAT_APE:
1391 id = SND_AUDIOCODEC_APE;
1392 break;
1393 case AUDIO_FORMAT_VORBIS:
1394 id = SND_AUDIOCODEC_VORBIS;
1395 break;
1396 case AUDIO_FORMAT_WMA:
1397 id = SND_AUDIOCODEC_WMA;
1398 break;
1399 case AUDIO_FORMAT_WMA_PRO:
1400 id = SND_AUDIOCODEC_WMA_PRO;
1401 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301402 case AUDIO_FORMAT_MP2:
1403 id = SND_AUDIOCODEC_MP2;
1404 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301405 case AUDIO_FORMAT_AC3:
1406 id = SND_AUDIOCODEC_AC3;
1407 break;
1408 case AUDIO_FORMAT_E_AC3:
1409 case AUDIO_FORMAT_E_AC3_JOC:
1410 id = SND_AUDIOCODEC_EAC3;
1411 break;
1412 case AUDIO_FORMAT_DTS:
1413 case AUDIO_FORMAT_DTS_HD:
1414 id = SND_AUDIOCODEC_DTS;
1415 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001416 case AUDIO_FORMAT_DOLBY_TRUEHD:
1417 id = SND_AUDIOCODEC_TRUEHD;
1418 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001419 case AUDIO_FORMAT_IEC61937:
1420 id = SND_AUDIOCODEC_IEC61937;
1421 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301422 case AUDIO_FORMAT_DSD:
1423 id = SND_AUDIOCODEC_DSD;
1424 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301425 case AUDIO_FORMAT_APTX:
1426 id = SND_AUDIOCODEC_APTX;
1427 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301428 default:
1429 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1430 }
1431
1432 return id;
1433}
1434
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001435void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1436 struct audio_usecase *usecase)
1437{
1438 int type = usecase->type;
1439
Dhananjay Kumar14245982017-01-16 20:21:00 +05301440 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001441 struct stream_out *out = usecase->stream.out;
1442 int snd_device = usecase->out_snd_device;
1443 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001444 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301445 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001446 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301447 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301448 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301449 platform_send_audio_calibration(adev->platform, usecase,
1450 usecase->stream.in->app_type_cfg.app_type,
1451 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001452 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301453 /* when app type is default. the sample rate is not used to send cal */
1454 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301455 platform_get_default_app_type_v2(adev->platform, usecase->type),
1456 48000);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301457 } else if (type == TRANSCODE_LOOPBACK_RX && usecase->stream.inout != NULL) {
Siddartha Shaik343abc62017-08-08 11:15:25 +05301458 int snd_device = usecase->out_snd_device;
1459 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
1460 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
1461 platform_send_audio_calibration(adev->platform, usecase,
1462 platform_get_default_app_type_v2(adev->platform, usecase->type),
1463 usecase->stream.inout->out_config.sample_rate);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001464 } else {
1465 /* No need to send audio calibration for voice and voip call usecases */
1466 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1467 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001468 }
1469}
1470
Ben Rombergera04fabc2014-11-14 12:16:03 -08001471// Base64 Encode and Decode
1472// Not all features supported. This must be used only with following conditions.
1473// Decode Modes: Support with and without padding
1474// CRLF not handling. So no CRLF in string to decode.
1475// Encode Modes: Supports only padding
1476int b64decode(char *inp, int ilen, uint8_t* outp)
1477{
1478 int i, j, k, ii, num;
1479 int rem, pcnt;
1480 uint32_t res=0;
1481 uint8_t getIndex[MAX_BASEINDEX_LEN];
1482 uint8_t tmp, cflag;
1483
1484 if(inp == NULL || outp == NULL || ilen <= 0) {
1485 ALOGE("[%s] received NULL pointer or zero length",__func__);
1486 return -1;
1487 }
1488
1489 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1490 for(i=0;i<BASE_TABLE_SIZE;i++) {
1491 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1492 }
1493 getIndex[(uint8_t)'=']=0;
1494
1495 j=0;k=0;
1496 num = ilen/4;
1497 rem = ilen%4;
1498 if(rem==0)
1499 num = num-1;
1500 cflag=0;
1501 for(i=0; i<num; i++) {
1502 res=0;
1503 for(ii=0;ii<4;ii++) {
1504 res = res << 6;
1505 tmp = getIndex[(uint8_t)inp[j++]];
1506 res = res | tmp;
1507 cflag = cflag | tmp;
1508 }
1509 outp[k++] = (res >> 16)&0xFF;
1510 outp[k++] = (res >> 8)&0xFF;
1511 outp[k++] = res & 0xFF;
1512 }
1513
1514 // Handle last bytes special
1515 pcnt=0;
1516 if(rem == 0) {
1517 //With padding or full data
1518 res = 0;
1519 for(ii=0;ii<4;ii++) {
1520 if(inp[j] == '=')
1521 pcnt++;
1522 res = res << 6;
1523 tmp = getIndex[(uint8_t)inp[j++]];
1524 res = res | tmp;
1525 cflag = cflag | tmp;
1526 }
1527 outp[k++] = res >> 16;
1528 if(pcnt == 2)
1529 goto done;
1530 outp[k++] = (res>>8)&0xFF;
1531 if(pcnt == 1)
1532 goto done;
1533 outp[k++] = res&0xFF;
1534 } else {
1535 //without padding
1536 res = 0;
1537 for(i=0;i<rem;i++) {
1538 res = res << 6;
1539 tmp = getIndex[(uint8_t)inp[j++]];
1540 res = res | tmp;
1541 cflag = cflag | tmp;
1542 }
1543 for(i=rem;i<4;i++) {
1544 res = res << 6;
1545 pcnt++;
1546 }
1547 outp[k++] = res >> 16;
1548 if(pcnt == 2)
1549 goto done;
1550 outp[k++] = (res>>8)&0xFF;
1551 if(pcnt == 1)
1552 goto done;
1553 outp[k++] = res&0xFF;
1554 }
1555done:
1556 if(cflag == 0xFF) {
1557 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1558 __func__, inp);
1559 return 0;
1560 }
1561 return k;
1562}
1563
1564int b64encode(uint8_t *inp, int ilen, char* outp)
1565{
1566 int i,j,k, num;
1567 int rem=0;
1568 uint32_t res=0;
1569
1570 if(inp == NULL || outp == NULL || ilen<=0) {
1571 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1572 return -1;
1573 }
1574
1575 num = ilen/3;
1576 rem = ilen%3;
1577 j=0;k=0;
1578 for(i=0; i<num; i++) {
1579 //prepare index
1580 res = inp[j++]<<16;
1581 res = res | inp[j++]<<8;
1582 res = res | inp[j++];
1583 //get output map from index
1584 outp[k++] = (char) bTable[(res>>18)&0x3F];
1585 outp[k++] = (char) bTable[(res>>12)&0x3F];
1586 outp[k++] = (char) bTable[(res>>6)&0x3F];
1587 outp[k++] = (char) bTable[res&0x3F];
1588 }
1589
1590 switch(rem) {
1591 case 1:
1592 res = inp[j++]<<16;
1593 outp[k++] = (char) bTable[res>>18];
1594 outp[k++] = (char) bTable[(res>>12)&0x3F];
1595 //outp[k++] = '=';
1596 //outp[k++] = '=';
1597 break;
1598 case 2:
1599 res = inp[j++]<<16;
1600 res = res | inp[j++]<<8;
1601 outp[k++] = (char) bTable[res>>18];
1602 outp[k++] = (char) bTable[(res>>12)&0x3F];
1603 outp[k++] = (char) bTable[(res>>6)&0x3F];
1604 //outp[k++] = '=';
1605 break;
1606 default:
1607 break;
1608 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001609 outp[k] = '\0';
1610 return k;
1611}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301612
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301613
1614int audio_extn_utils_get_codec_version(const char *snd_card_name,
1615 int card_num,
1616 char *codec_version)
1617{
1618 char procfs_path[50];
1619 FILE *fp;
1620
1621 if (strstr(snd_card_name, "tasha")) {
1622 snprintf(procfs_path, sizeof(procfs_path),
1623 "/proc/asound/card%d/codecs/tasha/version", card_num);
1624 if ((fp = fopen(procfs_path, "r")) != NULL) {
1625 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1626 fclose(fp);
1627 } else {
1628 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1629 return -ENOENT;
1630 }
1631 ALOGD("%s: codec version %s", __func__, codec_version);
1632 }
1633
1634 return 0;
1635}
1636
1637
Ashish Jain81eb2a82015-05-13 10:52:34 +05301638#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1639
1640void get_default_compressed_channel_status(
1641 unsigned char *channel_status)
1642{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301643 memset(channel_status,0,24);
1644
1645 /* block start bit in preamble bit 3 */
1646 channel_status[0] |= PROFESSIONAL;
1647 //compre out
1648 channel_status[0] |= NON_LPCM;
1649 // sample rate; fixed 48K for default/transcode
1650 channel_status[3] |= SR_48000;
1651}
1652
1653#ifdef HDMI_PASSTHROUGH_ENABLED
1654int32_t get_compressed_channel_status(void *audio_stream_data,
1655 uint32_t audio_frame_size,
1656 unsigned char *channel_status,
1657 enum audio_parser_code_type codec_type)
1658 // codec_type - AUDIO_PARSER_CODEC_AC3
1659 // - AUDIO_PARSER_CODEC_DTS
1660{
1661 unsigned char *stream;
1662 int ret = 0;
1663 stream = (unsigned char *)audio_stream_data;
1664
1665 if (audio_stream_data == NULL || audio_frame_size == 0) {
1666 ALOGW("no buffer to get channel status, return default for compress");
1667 get_default_compressed_channel_status(channel_status);
1668 return ret;
1669 }
1670
1671 memset(channel_status,0,24);
1672 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1673 {
1674 ALOGE("init audio parser failed");
1675 return -1;
1676 }
1677 ret = get_channel_status(channel_status, codec_type);
1678 return ret;
1679
1680}
1681
1682#endif
1683
1684void get_lpcm_channel_status(uint32_t sampleRate,
1685 unsigned char *channel_status)
1686{
1687 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301688 memset(channel_status,0,24);
1689 /* block start bit in preamble bit 3 */
1690 channel_status[0] |= PROFESSIONAL;
1691 //LPCM OUT
1692 channel_status[0] &= ~NON_LPCM;
1693
1694 switch (sampleRate) {
1695 case 8000:
1696 case 11025:
1697 case 12000:
1698 case 16000:
1699 case 22050:
1700 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301701 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301702 case 24000:
1703 channel_status[3] |= SR_24000;
1704 break;
1705 case 32000:
1706 channel_status[3] |= SR_32000;
1707 break;
1708 case 44100:
1709 channel_status[3] |= SR_44100;
1710 break;
1711 case 48000:
1712 channel_status[3] |= SR_48000;
1713 break;
1714 case 88200:
1715 channel_status[3] |= SR_88200;
1716 break;
1717 case 96000:
1718 channel_status[3] |= SR_96000;
1719 break;
1720 case 176400:
1721 channel_status[3] |= SR_176400;
1722 break;
1723 case 192000:
1724 channel_status[3] |= SR_192000;
1725 break;
1726 default:
1727 ALOGV("Invalid sample_rate %u\n", sampleRate);
1728 status = -1;
1729 break;
1730 }
1731}
1732
1733void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1734{
1735 unsigned char channel_status[24]={0};
1736 struct snd_aes_iec958 iec958;
1737 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1738 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301739 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301740#ifdef HDMI_PASSTHROUGH_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05301741 if (audio_extn_utils_is_dolby_format(out->format) &&
Ashish Jain81eb2a82015-05-13 10:52:34 +05301742 /*TODO:Extend code to support DTS passthrough*/
1743 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301744 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301745 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1746 } else
1747#endif
1748 {
1749 /*set channel status bit for LPCM*/
1750 get_lpcm_channel_status(out->sample_rate, channel_status);
1751 }
1752
1753 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1754 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1755 if (!ctl) {
1756 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1757 __func__, mixer_ctl_name);
1758 return;
1759 }
1760 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1761 ALOGE("%s: Could not set channel status for ext HDMI ",
1762 __func__);
1763 return;
1764 }
1765
1766}
1767#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301768
1769int audio_extn_utils_get_avt_device_drift(
1770 struct audio_usecase *usecase,
1771 struct audio_avt_device_drift_param *drift_param)
1772{
1773 int ret = 0, count = 0;
1774 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301775 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301776 struct mixer_ctl *ctl = NULL;
1777 struct audio_avt_device_drift_stats drift_stats;
1778 struct audio_device *adev = NULL;
1779
1780 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301781 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1782 if (!backend) {
1783 ALOGE("%s: Unsupported device %d", __func__,
1784 usecase->stream.out->devices);
1785 ret = -EINVAL;
1786 goto done;
1787 }
1788 strlcpy(avt_device_drift_mixer_ctl_name,
1789 backend,
1790 MIXER_PATH_MAX_LENGTH);
1791
1792 count = strlen(backend);
1793 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1794 strlcat(&avt_device_drift_mixer_ctl_name[count],
1795 " DRIFT",
1796 MIXER_PATH_MAX_LENGTH - count);
1797 } else {
1798 ret = -EINVAL;
1799 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301800 }
1801 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301802 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301803 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301804 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301805 }
1806
Naresh Tanniru6160c712017-04-17 15:43:48 +05301807 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301808 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1809 if (!ctl) {
1810 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1811 __func__, avt_device_drift_mixer_ctl_name);
1812
1813 ret = -EINVAL;
1814 goto done;
1815 }
1816
1817 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1818 avt_device_drift_mixer_ctl_name);
1819
1820 mixer_ctl_update(ctl);
1821 count = mixer_ctl_get_num_values(ctl);
1822 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1823 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1824 __func__);
1825
1826 ret = -EINVAL;
1827 goto done;
1828 }
1829
1830 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1831 if (ret != 0) {
1832 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1833 __func__);
1834
1835 ret = -EINVAL;
1836 goto done;
1837 }
1838 memcpy(drift_param, &drift_stats.drift_param,
1839 sizeof(struct audio_avt_device_drift_param));
1840done:
1841 return ret;
1842}
Manish Dewangan07de2142017-02-27 19:27:20 +05301843
1844#ifdef SNDRV_COMPRESS_PATH_DELAY
1845int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1846{
1847 int ret = -EINVAL;
1848 struct snd_compr_metadata metadata;
1849 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1850
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001851 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301852 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1853 if (!(is_offload_usecase(out->usecase))) {
1854 ALOGE("%s:: not supported for non offload session", __func__);
1855 goto exit;
1856 }
1857
1858 if (!out->compr) {
1859 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1860 __func__);
1861 goto exit;
1862 }
1863
1864 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1865 ret = compress_get_metadata(out->compr, &metadata);
1866 if(ret) {
1867 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1868 goto exit;
1869 }
1870 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1871 } else {
1872 ALOGD("%s:: Using Fix DSP delay",__func__);
1873 }
1874
1875exit:
1876 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1877 return delay_ms;
1878}
1879#else
1880int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1881{
1882 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1883}
1884#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301885
1886#ifdef SNDRV_COMPRESS_RENDER_MODE
1887int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1888{
1889 struct snd_compr_metadata metadata;
1890 int ret = -EINVAL;
1891
1892 if (!(is_offload_usecase(out->usecase))) {
1893 ALOGE("%s:: not supported for non offload session", __func__);
1894 goto exit;
1895 }
1896
1897 if (!out->compr) {
1898 ALOGD("%s:: Invalid compress handle",
1899 __func__);
1900 goto exit;
1901 }
1902
1903 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1904
1905 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1906 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1907 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1908 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1909 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1910 } else {
1911 ret = 0;
1912 goto exit;
1913 }
1914 ret = compress_set_metadata(out->compr, &metadata);
1915 if(ret) {
1916 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1917 }
1918exit:
1919 return ret;
1920}
1921#else
1922int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1923{
1924 ALOGD("%s:: configuring render mode not supported", __func__);
1925 return 0;
1926}
1927#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301928
1929#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1930int audio_extn_utils_compress_set_clk_rec_mode(
1931 struct audio_usecase *usecase)
1932{
1933 struct snd_compr_metadata metadata;
1934 struct stream_out *out = NULL;
1935 int ret = -EINVAL;
1936
Naresh Tanniru7586e292017-04-13 10:38:13 +05301937 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301938 ALOGE("%s:: Invalid use case", __func__);
1939 goto exit;
1940 }
1941
1942 out = usecase->stream.out;
1943 if (!out) {
1944 ALOGE("%s:: invalid stream", __func__);
1945 goto exit;
1946 }
1947
1948 if (!is_offload_usecase(out->usecase)) {
1949 ALOGE("%s:: not supported for non offload session", __func__);
1950 goto exit;
1951 }
1952
1953 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1954 ALOGD("%s:: clk recovery is only supported in STC render mode",
1955 __func__);
1956 ret = 0;
1957 goto exit;
1958 }
1959
1960 if (!out->compr) {
1961 ALOGD("%s:: Invalid compress handle",
1962 __func__);
1963 goto exit;
1964 }
1965 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1966 switch(usecase->out_snd_device) {
1967 case SND_DEVICE_OUT_HDMI:
1968 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1969 case SND_DEVICE_OUT_DISPLAY_PORT:
1970 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1971 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1972 break;
1973 default:
1974 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1975 break;
1976 }
1977
1978 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1979
1980 ret = compress_set_metadata(out->compr, &metadata);
1981 if(ret) {
1982 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1983 }
1984
1985exit:
1986 return ret;
1987}
1988#else
1989int audio_extn_utils_compress_set_clk_rec_mode(
1990 struct audio_usecase *usecase __unused)
1991{
1992 ALOGD("%s:: configuring render mode not supported", __func__);
1993 return 0;
1994}
1995#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301996
1997#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1998int audio_extn_utils_compress_set_render_window(
1999 struct stream_out *out,
2000 struct audio_out_render_window_param *render_window)
2001{
2002 struct snd_compr_metadata metadata;
2003 int ret = -EINVAL;
2004
Manish Dewangan27346042017-03-01 12:56:12 +05302005 if(render_window == NULL) {
2006 ALOGE("%s:: Invalid render_window", __func__);
2007 goto exit;
2008 }
2009
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07002010 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
2011 __func__,render_window->render_ws, render_window->render_we);
2012
Manish Dewangan27346042017-03-01 12:56:12 +05302013 if (!is_offload_usecase(out->usecase)) {
2014 ALOGE("%s:: not supported for non offload session", __func__);
2015 goto exit;
2016 }
2017
Naresh Tanniru6160c712017-04-17 15:43:48 +05302018 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2019 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05302020 ALOGD("%s:: only supported in timestamp mode, current "
2021 "render mode mode %d", __func__, out->render_mode);
2022 goto exit;
2023 }
2024
2025 if (!out->compr) {
2026 ALOGW("%s:: offload session not yet opened,"
2027 "render window will be configure later", __func__);
2028 /* store render window to reconfigure in start_output_stream() */
2029 goto exit;
2030 }
2031
2032 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
2033 /*render window start value */
2034 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
2035 metadata.value[1] = \
2036 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
2037 /*render window end value */
2038 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
2039 metadata.value[3] = \
2040 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
2041
2042 ret = compress_set_metadata(out->compr, &metadata);
2043 if(ret) {
2044 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2045 }
2046
2047exit:
2048 return ret;
2049}
2050#else
2051int audio_extn_utils_compress_set_render_window(
2052 struct stream_out *out __unused,
2053 struct audio_out_render_window_param *render_window __unused)
2054{
2055 ALOGD("%s:: configuring render window not supported", __func__);
2056 return 0;
2057}
2058#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05302059
2060#ifdef SNDRV_COMPRESS_START_DELAY
2061int audio_extn_utils_compress_set_start_delay(
2062 struct stream_out *out,
2063 struct audio_out_start_delay_param *delay_param)
2064{
2065 struct snd_compr_metadata metadata;
2066 int ret = -EINVAL;
2067
2068 if(delay_param == NULL) {
2069 ALOGE("%s:: Invalid delay_param", __func__);
2070 goto exit;
2071 }
2072
2073 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
2074 delay_param->start_delay);
2075
2076 if (!is_offload_usecase(out->usecase)) {
2077 ALOGE("%s:: not supported for non offload session", __func__);
2078 goto exit;
2079 }
2080
Naresh Tanniru6160c712017-04-17 15:43:48 +05302081 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
2082 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05302083 ALOGD("%s:: only supported in timestamp mode, current "
2084 "render mode mode %d", __func__, out->render_mode);
2085 goto exit;
2086 }
2087
2088 if (!out->compr) {
2089 ALOGW("%s:: offload session not yet opened,"
2090 "start delay will be configure later", __func__);
2091 goto exit;
2092 }
2093
2094 metadata.key = SNDRV_COMPRESS_START_DELAY;
2095 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
2096 metadata.value[1] = \
2097 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
2098
2099 ret = compress_set_metadata(out->compr, &metadata);
2100 if(ret) {
2101 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2102 }
2103
2104exit:
2105 return ret;
2106}
2107#else
2108int audio_extn_utils_compress_set_start_delay(
2109 struct stream_out *out __unused,
2110 struct audio_out_start_delay_param *delay_param __unused)
2111{
2112 ALOGD("%s:: configuring render window not supported", __func__);
2113 return 0;
2114}
2115#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002116
2117#define MAX_SND_CARD 8
Ashish Jain30ae8942017-11-05 17:21:23 +05302118#define RETRY_US 1000000
2119#define RETRY_NUMBER 40
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002120
2121int audio_extn_utils_get_snd_card_num()
2122{
Soumya Managolid4a9c962018-04-06 16:21:50 +05302123 int snd_card_num = 0;
2124 struct mixer *mixer = NULL;
2125
2126 snd_card_num = audio_extn_utils_open_snd_mixer(&mixer);
2127 if (mixer)
2128 mixer_close(mixer);
2129 return snd_card_num;
2130}
2131
2132int audio_extn_utils_open_snd_mixer(struct mixer **mixer_handle)
2133{
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002134
2135 void *hw_info = NULL;
2136 struct mixer *mixer = NULL;
2137 int retry_num = 0;
Ashish Jain30ae8942017-11-05 17:21:23 +05302138 int snd_card_num = 0, min_snd_card_num = 0;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002139 char* snd_card_name = NULL;
2140
Soumya Managolid4a9c962018-04-06 16:21:50 +05302141 if (!mixer_handle) {
2142 ALOGE("invalid mixer handle");
2143 return -1;
2144 }
2145 *mixer_handle = NULL;
Ashish Jain30ae8942017-11-05 17:21:23 +05302146 /*
2147 * Try with all the sound cards ( 0 to 8 ) and if none of them were detected
2148 * sleep for 1 sec and try detections with sound card 0 again.
2149 * If sound card gets detected, check if it is relevant, if not check with the
2150 * other sound cards. To ensure that the irrelevant sound card is not check again,
2151 * we maintain it in min_snd_card_num.
2152 */
2153 while (retry_num < RETRY_NUMBER) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002154
Ashish Jain30ae8942017-11-05 17:21:23 +05302155 while (snd_card_num <= MAX_SND_CARD) {
2156 mixer = mixer_open(snd_card_num);
2157 if (!mixer)
2158 snd_card_num++;
2159 else
2160 break;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002161 }
2162
2163 if (!mixer) {
Ashish Jain30ae8942017-11-05 17:21:23 +05302164 usleep(RETRY_US);
2165 snd_card_num = min_snd_card_num;
2166 retry_num++;
2167 continue;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002168 }
2169
2170 snd_card_name = strdup(mixer_get_name(mixer));
2171 if (!snd_card_name) {
2172 ALOGE("failed to allocate memory for snd_card_name\n");
2173 mixer_close(mixer);
2174 return -1;
2175 }
2176 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2177
2178 hw_info = hw_info_init(snd_card_name);
2179 if (hw_info) {
2180 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2181 break;
2182 }
2183 ALOGE("%s: Failed to init hardware info", __func__);
Ashish Jain30ae8942017-11-05 17:21:23 +05302184 min_snd_card_num++;
2185 snd_card_num = min_snd_card_num;
2186
2187 if (snd_card_num >= MAX_SND_CARD)
2188 break;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002189
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302190 free(snd_card_name);
2191 snd_card_name = NULL;
2192
2193 mixer_close(mixer);
2194 mixer = NULL;
2195 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002196 if (snd_card_name)
2197 free(snd_card_name);
Ashish Jain30ae8942017-11-05 17:21:23 +05302198
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302199 if (hw_info)
2200 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002201
Ashish Jain30ae8942017-11-05 17:21:23 +05302202 if ((snd_card_num >= MAX_SND_CARD) || (retry_num >= RETRY_NUMBER)) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002203 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2204 return -1;
2205 }
2206
Soumya Managolid4a9c962018-04-06 16:21:50 +05302207 if (mixer)
2208 *mixer_handle = mixer;
2209
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002210 return snd_card_num;
2211}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302212
Soumya Managolid4a9c962018-04-06 16:21:50 +05302213void audio_extn_utils_close_snd_mixer(struct mixer *mixer)
2214{
2215 if (mixer)
2216 mixer_close(mixer);
2217}
2218
Naresh Tanniru6160c712017-04-17 15:43:48 +05302219#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2220int audio_extn_utils_compress_enable_drift_correction(
2221 struct stream_out *out,
2222 struct audio_out_enable_drift_correction *drift)
2223{
2224 struct snd_compr_metadata metadata;
2225 int ret = -EINVAL;
2226
2227 if(drift == NULL) {
2228 ALOGE("%s:: Invalid param", __func__);
2229 goto exit;
2230 }
2231
2232 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2233
2234 if (!is_offload_usecase(out->usecase)) {
2235 ALOGE("%s:: not supported for non offload session", __func__);
2236 goto exit;
2237 }
2238
2239 if (!out->compr) {
2240 ALOGW("%s:: offload session not yet opened,"
2241 "start delay will be configure later", __func__);
2242 goto exit;
2243 }
2244
2245 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2246 metadata.value[0] = drift->enable;
2247 out->drift_correction_enabled = drift->enable;
2248
2249 ret = compress_set_metadata(out->compr, &metadata);
2250 if(ret) {
2251 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2252 out->drift_correction_enabled = false;
2253 }
2254
2255exit:
2256 return ret;
2257}
2258#else
2259int audio_extn_utils_compress_enable_drift_correction(
2260 struct stream_out *out __unused,
2261 struct audio_out_enable_drift_correction *drift __unused)
2262{
2263 ALOGD("%s:: configuring drift enablement not supported", __func__);
2264 return 0;
2265}
2266#endif
2267
2268#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2269int audio_extn_utils_compress_correct_drift(
2270 struct stream_out *out,
2271 struct audio_out_correct_drift *drift_param)
2272{
2273 struct snd_compr_metadata metadata;
2274 int ret = -EINVAL;
2275
2276 if (drift_param == NULL) {
2277 ALOGE("%s:: Invalid drift_param", __func__);
2278 goto exit;
2279 }
2280
2281 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2282 drift_param->adjust_time);
2283
2284 if (!is_offload_usecase(out->usecase)) {
2285 ALOGE("%s:: not supported for non offload session", __func__);
2286 goto exit;
2287 }
2288
2289 if (!out->compr) {
2290 ALOGW("%s:: offload session not yet opened", __func__);
2291 goto exit;
2292 }
2293
2294 if (!out->drift_correction_enabled) {
2295 ALOGE("%s:: drift correction not enabled", __func__);
2296 goto exit;
2297 }
2298
2299 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2300 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2301 metadata.value[1] = \
2302 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2303
2304 ret = compress_set_metadata(out->compr, &metadata);
2305 if(ret)
2306 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2307exit:
2308 return ret;
2309}
2310#else
2311int audio_extn_utils_compress_correct_drift(
2312 struct stream_out *out __unused,
2313 struct audio_out_correct_drift *drift_param __unused)
2314{
2315 ALOGD("%s:: setting adjust clock not supported", __func__);
2316 return 0;
2317}
2318#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302319
2320int audio_extn_utils_set_channel_map(
2321 struct stream_out *out,
2322 struct audio_out_channel_map_param *channel_map_param)
2323{
2324 int ret = -EINVAL, i = 0;
2325 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2326
2327 if (channel_map_param == NULL) {
2328 ALOGE("%s:: Invalid channel_map", __func__);
2329 goto exit;
2330 }
2331
2332 if (channel_map_param->channels != channels) {
2333 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2334 __func__, channel_map_param->channels, channels);
2335 goto exit;
2336 }
2337
2338 for ( i = 0; i < channels; i++) {
2339 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2340 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2341 }
2342 ret = 0;
2343exit:
2344 return ret;
2345}
Varun Balaraje49253e2017-07-06 19:48:56 +05302346
2347int audio_extn_utils_set_pan_scale_params(
2348 struct stream_out *out,
2349 struct mix_matrix_params *mm_params)
2350{
2351 int ret = -EINVAL, i = 0, j = 0;
2352
Varun Balaraj003ee752017-08-07 17:54:55 +05302353 if (mm_params == NULL || out == NULL) {
2354 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302355 goto exit;
2356 }
2357
2358 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2359 mm_params->num_output_channels <= 0 ||
2360 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2361 mm_params->num_input_channels <= 0)
2362 goto exit;
2363
2364 out->pan_scale_params.num_output_channels = mm_params->num_output_channels;
2365 out->pan_scale_params.num_input_channels = mm_params->num_input_channels;
2366 out->pan_scale_params.has_output_channel_map =
2367 mm_params->has_output_channel_map;
2368 for (i = 0; i < mm_params->num_output_channels; i++)
2369 out->pan_scale_params.output_channel_map[i] =
2370 mm_params->output_channel_map[i];
2371
2372 out->pan_scale_params.has_input_channel_map =
2373 mm_params->has_input_channel_map;
2374 for (i = 0; i < mm_params->num_input_channels; i++)
2375 out->pan_scale_params.input_channel_map[i] =
2376 mm_params->input_channel_map[i];
2377
2378 out->pan_scale_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2379 for (i = 0; i < mm_params->num_output_channels; i++)
2380 for (j = 0; j < mm_params->num_input_channels; j++) {
2381 //Convert the channel coefficient gains in Q14 format
2382 out->pan_scale_params.mixer_coeffs[i][j] =
2383 mm_params->mixer_coeffs[i][j] * (2 << 13);
2384 }
2385
2386 ret = platform_set_stream_pan_scale_params(out->dev->platform,
2387 out->pcm_device_id,
2388 out->pan_scale_params);
2389
2390exit:
2391 return ret;
2392}
2393
2394int audio_extn_utils_set_downmix_params(
2395 struct stream_out *out,
2396 struct mix_matrix_params *mm_params)
2397{
2398 int ret = -EINVAL, i = 0, j = 0;
2399 struct audio_usecase *usecase = NULL;
2400
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002401 if (mm_params == NULL || out == NULL) {
Varun Balaraj003ee752017-08-07 17:54:55 +05302402 ALOGE("%s:: Invalid mix matrix or out param", __func__);
Varun Balaraje49253e2017-07-06 19:48:56 +05302403 goto exit;
2404 }
2405
2406 if (mm_params->num_output_channels > MAX_CHANNELS_SUPPORTED ||
2407 mm_params->num_output_channels <= 0 ||
2408 mm_params->num_input_channels > MAX_CHANNELS_SUPPORTED ||
2409 mm_params->num_input_channels <= 0)
2410 goto exit;
2411
2412 usecase = get_usecase_from_list(out->dev, out->usecase);
Varun Balaraj003ee752017-08-07 17:54:55 +05302413 if (!usecase) {
2414 ALOGE("%s: Get usecase list failed!", __func__);
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07002415 goto exit;
2416 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302417 out->downmix_params.num_output_channels = mm_params->num_output_channels;
2418 out->downmix_params.num_input_channels = mm_params->num_input_channels;
2419
2420 out->downmix_params.has_output_channel_map =
2421 mm_params->has_output_channel_map;
2422 for (i = 0; i < mm_params->num_output_channels; i++) {
2423 out->downmix_params.output_channel_map[i] =
2424 mm_params->output_channel_map[i];
2425 }
2426
2427 out->downmix_params.has_input_channel_map =
2428 mm_params->has_input_channel_map;
2429 for (i = 0; i < mm_params->num_input_channels; i++)
2430 out->downmix_params.input_channel_map[i] =
2431 mm_params->input_channel_map[i];
2432
2433 out->downmix_params.has_mixer_coeffs = mm_params->has_mixer_coeffs;
2434 for (i = 0; i < mm_params->num_output_channels; i++)
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302435 for (j = 0; j < mm_params->num_input_channels; j++) {
2436 //Convert the channel coefficient gains in Q14 format
Varun Balaraje49253e2017-07-06 19:48:56 +05302437 out->downmix_params.mixer_coeffs[i][j] =
Nikhil Latukar2e6f6242017-08-15 13:37:07 +05302438 mm_params->mixer_coeffs[i][j] * (2 << 13);
2439 }
Varun Balaraje49253e2017-07-06 19:48:56 +05302440
2441 ret = platform_set_stream_downmix_params(out->dev->platform,
2442 out->pcm_device_id,
2443 usecase->out_snd_device,
2444 out->downmix_params);
2445
2446exit:
2447 return ret;
2448}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302449
2450bool audio_extn_utils_is_dolby_format(audio_format_t format)
2451{
2452 if (format == AUDIO_FORMAT_AC3 ||
2453 format == AUDIO_FORMAT_E_AC3 ||
2454 format == AUDIO_FORMAT_E_AC3_JOC)
2455 return true;
2456 else
2457 return false;
2458}
2459
`Deeraj Soman676c2702017-09-18 19:25:53 +05302460int audio_extn_utils_get_bit_width_from_string(const char *id_string)
2461{
2462 int i;
2463 const mixer_config_lookup mixer_bitwidth_config[] = {{"S24_3LE", 24},
2464 {"S32_LE", 32},
2465 {"S24_LE", 24},
2466 {"S16_LE", 16}};
2467 int num_configs = sizeof(mixer_bitwidth_config) / sizeof(mixer_bitwidth_config[0]);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05302468
`Deeraj Soman676c2702017-09-18 19:25:53 +05302469 for (i = 0; i < num_configs; i++) {
2470 if (!strcmp(id_string, mixer_bitwidth_config[i].id_string))
2471 return mixer_bitwidth_config[i].value;
2472 }
2473
2474 return -EINVAL;
2475}
2476
2477int audio_extn_utils_get_sample_rate_from_string(const char *id_string)
2478{
2479 int i;
2480 const mixer_config_lookup mixer_samplerate_config[] = {{"KHZ_32", 32000},
2481 {"KHZ_48", 48000},
2482 {"KHZ_96", 96000},
2483 {"KHZ_144", 144000},
2484 {"KHZ_192", 192000},
2485 {"KHZ_384", 384000},
2486 {"KHZ_44P1", 44100},
2487 {"KHZ_88P2", 88200},
2488 {"KHZ_176P4", 176400},
2489 {"KHZ_352P8", 352800}};
2490 int num_configs = sizeof(mixer_samplerate_config) / sizeof(mixer_samplerate_config[0]);
2491
2492 for (i = 0; i < num_configs; i++) {
2493 if (!strcmp(id_string, mixer_samplerate_config[i].id_string))
2494 return mixer_samplerate_config[i].value;
2495 }
2496
2497 return -EINVAL;
2498}
2499
2500int audio_extn_utils_get_channels_from_string(const char *id_string)
2501{
2502 int i;
2503 const mixer_config_lookup mixer_channels_config[] = {{"One", 1},
2504 {"Two", 2},
2505 {"Three",3},
2506 {"Four", 4},
2507 {"Five", 5},
2508 {"Six", 6},
2509 {"Seven", 7},
2510 {"Eight", 8}};
2511 int num_configs = sizeof(mixer_channels_config) / sizeof(mixer_channels_config[0]);
2512
2513 for (i = 0; i < num_configs; i++) {
2514 if (!strcmp(id_string, mixer_channels_config[i].id_string))
2515 return mixer_channels_config[i].value;
2516 }
2517
2518 return -EINVAL;
2519}
Surendar karka30569792018-05-08 12:02:21 +05302520
2521int audio_extn_utils_get_license_params
2522(
2523const struct audio_device *adev,
2524struct audio_license_params *license_params
2525)
2526{
2527 if(!license_params)
2528 return -EINVAL;
2529 return platform_get_license_by_product(adev->platform, (const char*)license_params->product, &license_params->key, license_params->license);
2530}
2531