blob: 45ea3b778c063ac2b1e3988ced3dc1f9dd2b93b5 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Alexy Josephaee4fdd2016-01-29 13:02:07 -08002 * Copyright (c) 2014-2016, 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
23#include <errno.h>
24#include <cutils/properties.h>
25#include <cutils/config_utils.h>
26#include <stdlib.h>
27#include <dlfcn.h>
28#include <cutils/str_parms.h>
29#include <cutils/log.h>
30#include <cutils/misc.h>
31
32#include "audio_hw.h"
33#include "platform.h"
34#include "platform_api.h"
35#include "audio_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080036#include "voice.h"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070037
Ashish Jain81eb2a82015-05-13 10:52:34 +053038#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
39#ifdef HDMI_PASSTHROUGH_ENABLED
40#include "audio_parsers.h"
41#endif
42#endif
43
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053044#ifdef LINUX_ENABLED
45#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053046#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053047#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070048#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053049#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053050#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070051
52#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053053#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070054
55#define DYNAMIC_VALUE_TAG "dynamic"
56#define FLAGS_TAG "flags"
57#define FORMATS_TAG "formats"
58#define SAMPLING_RATES_TAG "sampling_rates"
59#define BIT_WIDTH_TAG "bit_width"
60#define APP_TYPE_TAG "app_type"
61
62#define STRING_TO_ENUM(string) { #string, string }
63#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
64
Ben Rombergera04fabc2014-11-14 12:16:03 -080065#define BASE_TABLE_SIZE 64
66#define MAX_BASEINDEX_LEN 256
67
Ashish Jain81eb2a82015-05-13 10:52:34 +053068#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
69#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
70#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
71#define SR_44100 (0<<0) /* 44.1kHz */
72#define SR_NOTID (1<<0) /* non indicated */
73#define SR_48000 (2<<0) /* 48kHz */
74#define SR_32000 (3<<0) /* 32kHz */
75#define SR_22050 (4<<0) /* 22.05kHz */
76#define SR_24000 (6<<0) /* 24kHz */
77#define SR_88200 (8<<0) /* 88.2kHz */
78#define SR_96000 (10<<0) /* 96kHz */
79#define SR_176400 (12<<0) /* 176.4kHz */
80#define SR_192000 (14<<0) /* 192kHz */
81
82#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070083struct string_to_enum {
84 const char *name;
85 uint32_t value;
86};
87
88const struct string_to_enum s_flag_name_to_enum_table[] = {
89 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
vivek mehta0ea887a2015-08-26 14:01:20 -070090 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT_PCM),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070091 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
92 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
93 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
94 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
95 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070096 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070097#ifdef INCALL_MUSIC_ENABLED
98 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
99#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700100 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530101 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
102 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
103 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
104 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
105 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700106};
107
108const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530109 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700110 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530111 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
112 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530113 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700114 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
115 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
116 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700117 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
118 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700119 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700120 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700121 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530122 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
123#ifdef AUDIO_EXTN_FORMATS_ENABLED
124 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700125 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
126 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
127 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
129 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
130 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
131 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
132 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
133 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
134 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700135 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530136 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
137 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700138 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800139 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
140 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
141 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530142 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530143 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
144 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
145 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530146 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700147#endif
148};
149
Ben Rombergera04fabc2014-11-14 12:16:03 -0800150static char bTable[BASE_TABLE_SIZE] = {
151 'A','B','C','D','E','F','G','H','I','J','K','L',
152 'M','N','O','P','Q','R','S','T','U','V','W','X',
153 'Y','Z','a','b','c','d','e','f','g','h','i','j',
154 'k','l','m','n','o','p','q','r','s','t','u','v',
155 'w','x','y','z','0','1','2','3','4','5','6','7',
156 '8','9','+','/'
157};
158
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700159static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
160 const char *name)
161{
162 size_t i;
163 for (i = 0; i < size; i++) {
164 if (strcmp(table[i].name, name) == 0) {
165 ALOGV("%s found %s", __func__, table[i].name);
166 return table[i].value;
167 }
168 }
169 return 0;
170}
171
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530172static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700173{
174 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530175 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800176 char *last_r;
177 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700178 while (flag_name != NULL) {
179 if (strlen(flag_name) != 0) {
180 flag |= string_to_enum(s_flag_name_to_enum_table,
181 ARRAY_SIZE(s_flag_name_to_enum_table),
182 flag_name);
183 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800184 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700185 }
186
187 ALOGV("parse_flag_names: flag - %d", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530188 io_flags.in_flags = (audio_input_flags_t)flag;
189 io_flags.out_flags = (audio_output_flags_t)flag;
190 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700191}
192
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530193static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700194{
195 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800196 char *last_r;
197 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700198
199 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
200 return;
201
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530202 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700203 while (str != NULL) {
204 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
205 ARRAY_SIZE(s_format_name_to_enum_table), str);
206 ALOGV("%s: format - %d", __func__, format);
207 if (format != 0) {
208 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700209 if (sf_info == NULL)
210 break; /* return whatever was parsed */
211
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700212 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530213 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700214 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800215 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700216 }
217}
218
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530219static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700220{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700221 struct stream_sample_rate *ss_info = NULL;
222 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800223 char *last_r;
224 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700225
Amit Shekhar6f461b12014-08-01 14:52:58 -0700226 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
227 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700228
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530229 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700230 while (str != NULL) {
231 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
232 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
233 if (0 != sample_rate) {
234 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800235 if (!ss_info) {
236 ALOGE("%s: memory allocation failure", __func__);
237 return;
238 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700239 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530240 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700241 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800242 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700243 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700244}
245
246static int parse_bit_width_names(char *name)
247{
248 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800249 char *last_r;
250 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251
252 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
253 bit_width = (int)strtol(str, (char **)NULL, 10);
254
255 ALOGV("%s: bit_width - %d", __func__, bit_width);
256 return bit_width;
257}
258
259static int parse_app_type_names(void *platform, char *name)
260{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700261 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800262 char *last_r;
263 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700264
265 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
266 app_type = (int)strtol(str, (char **)NULL, 10);
267
268 ALOGV("%s: app_type - %d", __func__, app_type);
269 return app_type;
270}
271
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530272static void update_streams_cfg_list(cnode *root, void *platform,
273 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700274{
275 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530276 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700277
278 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530279 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700280
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530281 if (!s_info) {
282 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700283 return;
284 }
285
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700286 while (node) {
287 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530288 s_info->flags = parse_flag_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530290 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700291 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530292 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
293 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700294 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530295 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530297 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700298 }
299 node = node->next;
300 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530301 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700302}
303
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530304static void load_cfg_list(cnode *root, void *platform,
305 struct listnode *streams_output_cfg_list,
306 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700307{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530308 cnode *node = NULL;
309
310 node = config_find(root, OUTPUTS_TAG);
311 if (node != NULL) {
312 node = node->first_child;
313 while (node) {
314 ALOGV("%s: loading output %s", __func__, node->name);
315 update_streams_cfg_list(node, platform, streams_output_cfg_list);
316 node = node->next;
317 }
318 } else {
319 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700320 }
321
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530322 node = config_find(root, INPUTS_TAG);
323 if (node != NULL) {
324 node = node->first_child;
325 while (node) {
326 ALOGV("%s: loading input %s", __func__, node->name);
327 update_streams_cfg_list(node, platform, streams_input_cfg_list);
328 node = node->next;
329 }
330 } else {
331 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700332 }
333}
334
335static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530336 struct listnode *streams_output_cfg_list,
337 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700338{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530339 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700340 int length = 0, i, num_app_types = 0;
341 struct listnode *node;
342 bool update;
343 struct mixer_ctl *ctl = NULL;
344 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530345 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700346
347 if (!mixer) {
348 ALOGE("%s: mixer is null",__func__);
349 return;
350 }
351 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
352 if (!ctl) {
353 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
354 return;
355 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530356 app_type_cfg[length++] = num_app_types;
357
358 if (list_empty(streams_output_cfg_list)) {
359 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700360 app_type_cfg[length++] = 48000;
361 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530362 num_app_types += 1;
363 }
364 if (list_empty(streams_input_cfg_list)) {
365 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
366 app_type_cfg[length++] = 48000;
367 app_type_cfg[length++] = 16;
368 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700369 }
370
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700371 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530372 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700373 update = true;
374 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530375 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700376 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530377 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
378 if (app_type_cfg[i+2] < s_info->app_type_cfg.sample_rate)
379 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
380 if (app_type_cfg[i+3] < s_info->app_type_cfg.bit_width)
381 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 update = false;
383 break;
384 }
385 }
386 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387 num_app_types += 1;
388 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
389 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
390 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
391 }
392 }
393 list_for_each(node, streams_input_cfg_list) {
394 s_info = node_to_item(node, struct streams_io_cfg, list);
395 update = true;
396 for (i=0; i<length; i=i+3) {
397 if (app_type_cfg[i+1] == 0)
398 break;
399 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
400 if (app_type_cfg[i+2] < s_info->app_type_cfg.sample_rate)
401 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
402 if (app_type_cfg[i+3] < s_info->app_type_cfg.bit_width)
403 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
404 update = false;
405 break;
406 }
407 }
408 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
409 num_app_types += 1;
410 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
411 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
412 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700413 }
414 }
415 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
416 if (num_app_types) {
417 app_type_cfg[0] = num_app_types;
418 mixer_ctl_set_array(ctl, app_type_cfg, length);
419 }
420}
421
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530422void audio_extn_utils_update_streams_cfg_lists(void *platform,
423 struct mixer *mixer,
424 struct listnode *streams_output_cfg_list,
425 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700426{
427 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530428 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700429
430 ALOGV("%s", __func__);
431 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530432 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700433
434 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700435 if (root == NULL) {
436 ALOGE("cfg_list, NULL config root");
437 return;
438 }
439
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530440 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
441 if (data == NULL) {
442 ALOGD("%s: failed to open io config file(%s), trying older config file",
443 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
444 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
445 if (data == NULL) {
446 send_app_type_cfg(platform, mixer,
447 streams_output_cfg_list,
448 streams_input_cfg_list);
449 ALOGE("%s: could not load io policy config!", __func__);
450 return;
451 }
452 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700453
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530454 config_load(root, data);
455 load_cfg_list(root, platform, streams_output_cfg_list,
456 streams_input_cfg_list);
457
458 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
459 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800460
461 config_free(root);
462 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700463}
464
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530465static void audio_extn_utils_dump_streams_cfg_list(
466 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700467{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700468 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530469 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700470 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700471 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530472
473 list_for_each(node_i, streams_cfg_list) {
474 s_info = node_to_item(node_i, struct streams_io_cfg, list);
475 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
476 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
477 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
478 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700479 sf_info = node_to_item(node_j, struct stream_format, list);
480 ALOGV("format-%x", sf_info->format);
481 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530482 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700483 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
484 ALOGV("sample rate-%d", ss_info->sample_rate);
485 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700486 }
487}
488
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530489void audio_extn_utils_dump_streams_cfg_lists(
490 struct listnode *streams_output_cfg_list,
491 struct listnode *streams_input_cfg_list)
492{
493 ALOGV("%s", __func__);
494 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
495 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
496}
497
498static void audio_extn_utils_release_streams_cfg_list(
499 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700500{
501 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530502 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700503
504 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530505
506 while (!list_empty(streams_cfg_list)) {
507 node_i = list_head(streams_cfg_list);
508 s_info = node_to_item(node_i, struct streams_io_cfg, list);
509 while (!list_empty(&s_info->format_list)) {
510 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700511 list_remove(node_j);
512 free(node_to_item(node_j, struct stream_format, list));
513 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530514 while (!list_empty(&s_info->sample_rate_list)) {
515 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700516 list_remove(node_j);
517 free(node_to_item(node_j, struct stream_sample_rate, list));
518 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700519 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530520 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700521 }
522}
523
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530524void audio_extn_utils_release_streams_cfg_lists(
525 struct listnode *streams_output_cfg_list,
526 struct listnode *streams_input_cfg_list)
527{
528 ALOGV("%s", __func__);
529 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
530 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
531}
532
533static bool set_app_type_cfg(struct streams_io_cfg *s_info,
534 struct stream_app_type_cfg *app_type_cfg,
535 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700536 {
537 struct listnode *node_i;
538 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530539 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700540 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
541 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530542 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700543
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530544 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700545 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530546 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700547 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
548 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
549 return true;
550 }
551 }
552 /*
553 * Reiterate through the list assuming dafault sample rate.
554 * Handles scenario where input sample rate is higher
555 * than all sample rates in list for the input bit width.
556 */
557 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800558
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530559 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700560 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
561 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530562 (bit_width == s_info->app_type_cfg.bit_width)) {
563 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700564 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530565 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800566 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 -0700567 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
568 return true;
569 }
570 }
571 return false;
572}
573
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530574void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
575 struct listnode *streams_input_cfg_list,
576 audio_devices_t devices __unused,
577 audio_input_flags_t flags,
578 audio_format_t format,
579 uint32_t sample_rate,
580 uint32_t bit_width,
581 struct stream_app_type_cfg *app_type_cfg)
582{
583 struct listnode *node_i, *node_j;
584 struct streams_io_cfg *s_info;
585 struct stream_format *sf_info;
586
587 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d",
588 __func__, flags, format, sample_rate);
589
590 list_for_each(node_i, streams_input_cfg_list) {
591 s_info = node_to_item(node_i, struct streams_io_cfg, list);
592 if (s_info->flags.in_flags == flags) {
593 list_for_each(node_j, &s_info->format_list) {
594 sf_info = node_to_item(node_j, struct stream_format, list);
595 if (sf_info->format == format) {
596 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
597 return;
598 }
599 }
600 }
601 }
602 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
603 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
604 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
605 app_type_cfg->bit_width = 16;
606}
607
608void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700609 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700610 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700611 audio_output_flags_t flags,
612 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700613 uint32_t sample_rate,
614 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530615 audio_channel_mask_t channel_mask,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700616 struct stream_app_type_cfg *app_type_cfg)
617{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530618 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530619 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700620 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530621 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700622
Ashish Jain058165c2016-09-28 23:18:48 +0530623 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700624 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700625 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
626 if (-ENOSYS != bw)
627 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700628 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
629 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
630 }
631
Manish Dewangan837dc462015-05-27 10:17:41 +0530632 property_get("audio.playback.mch.downsample",value,"");
633 if (!strncmp("true", value, sizeof("true"))) {
634 if ((popcount(channel_mask) > 2) &&
635 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
636 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
637 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
638 ALOGD("%s: MCH session defaulting sample rate to %d",
639 __func__, sample_rate);
640 }
641 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530642
643 /* Set sampling rate to 176.4 for DSD64
644 * and 352.8Khz for DSD128.
645 * Set Bit Width to 16. output will be 16 bit
646 * post DoP in ASM.
647 */
648 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
649 (format == AUDIO_FORMAT_DSD)) {
650 bit_width = 16;
651 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
652 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
653 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
654 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
655 }
656
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530657 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
658 //TODO: Handle fractional sampling rate configuration for LL
659 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
660 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
661 __func__, sample_rate, bit_width);
662 }
663
Amit Shekhar1d896042014-10-03 13:16:09 -0700664 ALOGV("%s: flags: %x, format: %x sample_rate %d",
665 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700666 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530667 s_info = node_to_item(node_i, struct streams_io_cfg, list);
668 if (s_info->flags.out_flags == flags) {
669 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700670 sf_info = node_to_item(node_j, struct stream_format, list);
671 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530672 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700673 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700674 }
675 }
676 }
677 }
678 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530679 s_info = node_to_item(node_i, struct streams_io_cfg, list);
680 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700681 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530682 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
683 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
684 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700685 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530686 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700687 return;
688 }
689 }
690 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700691 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700692 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700693 app_type_cfg->bit_width = 16;
694}
695
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530696static bool audio_is_this_native_usecase(struct audio_usecase *uc)
697{
698 bool native_usecase = false;
699 struct stream_out *out = (struct stream_out*) uc->stream.out;
700
701 if (PCM_PLAYBACK == uc->type && out != NULL &&
702 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
703 is_offload_usecase(uc->id) &&
704 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
705 native_usecase = true;
706
707 return native_usecase;
708}
709
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530710void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
711 struct audio_device *adev,
712 struct audio_usecase *usecase)
713{
714 ALOGV("%s", __func__);
715
716 switch(usecase->type) {
717 case PCM_PLAYBACK:
718 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
719 &adev->streams_output_cfg_list,
720 usecase->stream.out->devices,
721 usecase->stream.out->flags,
722 usecase->stream.out->format,
723 usecase->stream.out->sample_rate,
724 usecase->stream.out->bit_width,
725 usecase->stream.out->channel_mask,
726 &usecase->stream.out->app_type_cfg);
727 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
728 break;
729 case PCM_CAPTURE:
730 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
731 &adev->streams_input_cfg_list,
732 usecase->stream.in->device,
733 usecase->stream.in->flags,
734 usecase->stream.in->format,
735 usecase->stream.in->sample_rate,
736 usecase->stream.in->bit_width,
737 &usecase->stream.in->app_type_cfg);
738 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
739 break;
740 default:
741 ALOGE("%s: app type cfg not supported for usecase type (%d)",
742 __func__, usecase->type);
743 }
744}
745
Ben Romberger1fafdde2015-09-09 19:43:15 -0700746int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
747 struct audio_usecase *usecase)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700748{
749 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530750 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
751 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700752 struct mixer_ctl *ctl;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530753 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530754 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530755 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700756
757 ALOGV("%s", __func__);
758
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530759 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530760 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700761 rc = 0;
762 goto exit_send_app_type_cfg;
763 }
764 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
765 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
766 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530767 (!is_offload_usecase(usecase->id)) &&
768 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530769 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 -0700770 rc = 0;
771 goto exit_send_app_type_cfg;
772 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530773 if (usecase->type == PCM_PLAYBACK) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530774 snd_device = usecase->out_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700775 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530776 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
777 "Audio Stream %d App Type Cfg", pcm_device_id);
778 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->out_snd_device);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700779 } else if (usecase->type == PCM_CAPTURE) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530780 snd_device = usecase->in_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700781 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530782 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
783 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
784 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->in_snd_device);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530785 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530786 if (acdb_dev_id <= 0) {
787 ALOGE("%s: Couldn't get the acdb dev id", __func__);
788 rc = -EINVAL;
789 goto exit_send_app_type_cfg;
790 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700791
792 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
793 if (!ctl) {
794 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
795 mixer_ctl_name);
796 rc = -EINVAL;
797 goto exit_send_app_type_cfg;
798 }
799 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800800 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530801
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530802 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
803 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
804 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
805 } else if ((usecase->stream.out->app_type_cfg.sample_rate == OUTPUT_SAMPLING_RATE_44100 &&
806 !(audio_is_this_native_usecase(usecase))) ||
807 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
808 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
809 }
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530810
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530811 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
Manish Dewangan837dc462015-05-27 10:17:41 +0530812
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530813 property_get("audio.playback.mch.downsample",value,"");
814 if (!strncmp("true", value, sizeof("true"))) {
815 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
816 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
817 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
818 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
819 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530820
Mingming Yin21854652016-04-13 11:54:02 -0700821 if ((24 == usecase->stream.out->bit_width) &&
822 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
823 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530824 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
825 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
826 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
827 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
828 /*
829 * To best utlize DSP, check if the stream sample rate is supported/multiple of
830 * configured device sample rate, if not update the COPP rate to be equal to the
831 * device sample rate, else open COPP at stream sample rate
832 */
833 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
834 usecase->stream.out->sample_rate,
835 &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -0700836 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
837 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
838 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
839 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
840 }
841 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
842
843 app_type_cfg[len++] = usecase->stream.out->app_type_cfg.app_type;
844 app_type_cfg[len++] = acdb_dev_id;
845 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
846 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530847 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700848 app_type_cfg[len++] = sample_rate * 4;
849 } else {
850 app_type_cfg[len++] = sample_rate;
851 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530852
853 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
854 __func__, usecase->stream.out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
855
856 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
857 app_type_cfg[len++] = usecase->stream.in->app_type_cfg.app_type;
858 app_type_cfg[len++] = acdb_dev_id;
859 app_type_cfg[len++] = usecase->stream.in->app_type_cfg.sample_rate;
860 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d",
861 __func__, usecase->stream.in->app_type_cfg.app_type, acdb_dev_id,
862 usecase->stream.in->app_type_cfg.sample_rate);
863 } else {
864 app_type_cfg[len++] = platform_get_default_app_type_v2(adev->platform, usecase->type);
865 app_type_cfg[len++] = acdb_dev_id;
866 app_type_cfg[len++] = sample_rate;
867 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d",
868 __func__, platform_get_default_app_type_v2(adev->platform, usecase->type),
869 acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530870 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530871
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700872 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700873 rc = 0;
874exit_send_app_type_cfg:
875 return rc;
876}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700877
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530878int read_line_from_file(const char *path, char *buf, size_t count)
879{
880 char * fgets_ret;
881 FILE * fd;
882 int rv;
883
884 fd = fopen(path, "r");
885 if (fd == NULL)
886 return -1;
887
888 fgets_ret = fgets(buf, (int)count, fd);
889 if (NULL != fgets_ret) {
890 rv = (int)strlen(buf);
891 } else {
892 rv = ferror(fd);
893 }
894 fclose(fd);
895
896 return rv;
897}
898
Ashish Jainf1eaa582016-05-23 20:54:24 +0530899/*Translates ALSA formats to AOSP PCM formats*/
900audio_format_t alsa_format_to_hal(uint32_t alsa_format)
901{
902 audio_format_t format;
903
904 switch(alsa_format) {
905 case SNDRV_PCM_FORMAT_S16_LE:
906 format = AUDIO_FORMAT_PCM_16_BIT;
907 break;
908 case SNDRV_PCM_FORMAT_S24_3LE:
909 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
910 break;
911 case SNDRV_PCM_FORMAT_S24_LE:
912 format = AUDIO_FORMAT_PCM_8_24_BIT;
913 break;
914 case SNDRV_PCM_FORMAT_S32_LE:
915 format = AUDIO_FORMAT_PCM_32_BIT;
916 break;
917 default:
918 ALOGW("Incorrect ALSA format");
919 format = AUDIO_FORMAT_INVALID;
920 }
921 return format;
922}
923
924/*Translates hal format (AOSP) to alsa formats*/
925uint32_t hal_format_to_alsa(audio_format_t hal_format)
926{
927 uint32_t alsa_format;
928
929 switch (hal_format) {
930 case AUDIO_FORMAT_PCM_32_BIT: {
931 if (platform_supports_true_32bit())
932 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
933 else
934 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
935 }
936 break;
937 case AUDIO_FORMAT_PCM_8_BIT:
938 alsa_format = SNDRV_PCM_FORMAT_S8;
939 break;
940 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
941 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
942 break;
943 case AUDIO_FORMAT_PCM_8_24_BIT: {
944 if (platform_supports_true_32bit())
945 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
946 else
947 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
948 }
949 break;
950 case AUDIO_FORMAT_PCM_FLOAT:
951 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
952 break;
953 default:
954 case AUDIO_FORMAT_PCM_16_BIT:
955 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
956 break;
957 }
958 return alsa_format;
959}
960
Ashish Jain83a6cc22016-06-28 14:34:17 +0530961/*Translates PCM formats to AOSP formats*/
962audio_format_t pcm_format_to_hal(uint32_t pcm_format)
963{
964 audio_format_t format = AUDIO_FORMAT_INVALID;
965
966 switch(pcm_format) {
967 case PCM_FORMAT_S16_LE:
968 format = AUDIO_FORMAT_PCM_16_BIT;
969 break;
970 case PCM_FORMAT_S24_3LE:
971 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
972 break;
973 case PCM_FORMAT_S24_LE:
974 format = AUDIO_FORMAT_PCM_8_24_BIT;
975 break;
976 case PCM_FORMAT_S32_LE:
977 format = AUDIO_FORMAT_PCM_32_BIT;
978 break;
979 default:
980 ALOGW("Incorrect PCM format");
981 format = AUDIO_FORMAT_INVALID;
982 }
983 return format;
984}
985
986/*Translates hal format (AOSP) to alsa formats*/
987uint32_t hal_format_to_pcm(audio_format_t hal_format)
988{
989 uint32_t pcm_format;
990
991 switch (hal_format) {
992 case AUDIO_FORMAT_PCM_32_BIT:
993 case AUDIO_FORMAT_PCM_8_24_BIT:
994 case AUDIO_FORMAT_PCM_FLOAT: {
995 if (platform_supports_true_32bit())
996 pcm_format = PCM_FORMAT_S32_LE;
997 else
998 pcm_format = PCM_FORMAT_S24_3LE;
999 }
1000 break;
1001 case AUDIO_FORMAT_PCM_8_BIT:
1002 pcm_format = PCM_FORMAT_S8;
1003 break;
1004 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1005 pcm_format = PCM_FORMAT_S24_3LE;
1006 break;
1007 default:
1008 case AUDIO_FORMAT_PCM_16_BIT:
1009 pcm_format = PCM_FORMAT_S16_LE;
1010 break;
1011 }
1012 return pcm_format;
1013}
1014
Ashish Jainf1eaa582016-05-23 20:54:24 +05301015uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1016 uint32_t sample_rate,
1017 uint32_t noOfChannels)
1018{
1019 uint32_t fragment_size = 0;
1020 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1021
1022 fragment_size = (pcm_offload_time
1023 * sample_rate
1024 * bytes_per_sample
1025 * noOfChannels)/1000;
1026 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1027 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1028 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1029 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1030 /*To have same PCM samples for all channels, the buffer size requires to
1031 *be multiple of (number of channels * bytes per sample)
1032 *For writes to succeed, the buffer must be written at address which is multiple of 32
1033 */
1034 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1035
1036 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1037 return fragment_size;
1038}
1039
1040/* Calculates the fragment size required to configure compress session.
1041 * Based on the alsa format selected, decide if conversion is needed in
1042
1043 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1044 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1045 */
1046void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1047{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301048 audio_format_t dst_format = out->hal_op_format;
1049 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301050 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1051 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1052
1053 out->compr_config.fragment_size =
1054 get_alsa_fragment_size(hal_op_bytes_per_sample,
1055 out->sample_rate,
1056 popcount(out->channel_mask));
1057
1058 if ((src_format != dst_format) &&
1059 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1060
Ashish Jain83a6cc22016-06-28 14:34:17 +05301061 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301062 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1063 hal_op_bytes_per_sample);
1064 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301065 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301066 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301067 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301068 }
1069}
1070
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001071void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1072 struct audio_usecase *usecase)
1073{
1074 int type = usecase->type;
1075
1076 if (type == PCM_PLAYBACK) {
1077 struct stream_out *out = usecase->stream.out;
1078 int snd_device = usecase->out_snd_device;
1079 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001080 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301081 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001082 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301083 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301084 } else if (type == PCM_CAPTURE) {
1085 platform_send_audio_calibration(adev->platform, usecase,
1086 usecase->stream.in->app_type_cfg.app_type,
1087 usecase->stream.in->app_type_cfg.sample_rate);
1088 } else {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301089 /* when app type is default. the sample rate is not used to send cal */
1090 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301091 platform_get_default_app_type_v2(adev->platform, usecase->type),
1092 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001093 }
1094}
1095
Ben Rombergera04fabc2014-11-14 12:16:03 -08001096// Base64 Encode and Decode
1097// Not all features supported. This must be used only with following conditions.
1098// Decode Modes: Support with and without padding
1099// CRLF not handling. So no CRLF in string to decode.
1100// Encode Modes: Supports only padding
1101int b64decode(char *inp, int ilen, uint8_t* outp)
1102{
1103 int i, j, k, ii, num;
1104 int rem, pcnt;
1105 uint32_t res=0;
1106 uint8_t getIndex[MAX_BASEINDEX_LEN];
1107 uint8_t tmp, cflag;
1108
1109 if(inp == NULL || outp == NULL || ilen <= 0) {
1110 ALOGE("[%s] received NULL pointer or zero length",__func__);
1111 return -1;
1112 }
1113
1114 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1115 for(i=0;i<BASE_TABLE_SIZE;i++) {
1116 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1117 }
1118 getIndex[(uint8_t)'=']=0;
1119
1120 j=0;k=0;
1121 num = ilen/4;
1122 rem = ilen%4;
1123 if(rem==0)
1124 num = num-1;
1125 cflag=0;
1126 for(i=0; i<num; i++) {
1127 res=0;
1128 for(ii=0;ii<4;ii++) {
1129 res = res << 6;
1130 tmp = getIndex[(uint8_t)inp[j++]];
1131 res = res | tmp;
1132 cflag = cflag | tmp;
1133 }
1134 outp[k++] = (res >> 16)&0xFF;
1135 outp[k++] = (res >> 8)&0xFF;
1136 outp[k++] = res & 0xFF;
1137 }
1138
1139 // Handle last bytes special
1140 pcnt=0;
1141 if(rem == 0) {
1142 //With padding or full data
1143 res = 0;
1144 for(ii=0;ii<4;ii++) {
1145 if(inp[j] == '=')
1146 pcnt++;
1147 res = res << 6;
1148 tmp = getIndex[(uint8_t)inp[j++]];
1149 res = res | tmp;
1150 cflag = cflag | tmp;
1151 }
1152 outp[k++] = res >> 16;
1153 if(pcnt == 2)
1154 goto done;
1155 outp[k++] = (res>>8)&0xFF;
1156 if(pcnt == 1)
1157 goto done;
1158 outp[k++] = res&0xFF;
1159 } else {
1160 //without padding
1161 res = 0;
1162 for(i=0;i<rem;i++) {
1163 res = res << 6;
1164 tmp = getIndex[(uint8_t)inp[j++]];
1165 res = res | tmp;
1166 cflag = cflag | tmp;
1167 }
1168 for(i=rem;i<4;i++) {
1169 res = res << 6;
1170 pcnt++;
1171 }
1172 outp[k++] = res >> 16;
1173 if(pcnt == 2)
1174 goto done;
1175 outp[k++] = (res>>8)&0xFF;
1176 if(pcnt == 1)
1177 goto done;
1178 outp[k++] = res&0xFF;
1179 }
1180done:
1181 if(cflag == 0xFF) {
1182 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1183 __func__, inp);
1184 return 0;
1185 }
1186 return k;
1187}
1188
1189int b64encode(uint8_t *inp, int ilen, char* outp)
1190{
1191 int i,j,k, num;
1192 int rem=0;
1193 uint32_t res=0;
1194
1195 if(inp == NULL || outp == NULL || ilen<=0) {
1196 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1197 return -1;
1198 }
1199
1200 num = ilen/3;
1201 rem = ilen%3;
1202 j=0;k=0;
1203 for(i=0; i<num; i++) {
1204 //prepare index
1205 res = inp[j++]<<16;
1206 res = res | inp[j++]<<8;
1207 res = res | inp[j++];
1208 //get output map from index
1209 outp[k++] = (char) bTable[(res>>18)&0x3F];
1210 outp[k++] = (char) bTable[(res>>12)&0x3F];
1211 outp[k++] = (char) bTable[(res>>6)&0x3F];
1212 outp[k++] = (char) bTable[res&0x3F];
1213 }
1214
1215 switch(rem) {
1216 case 1:
1217 res = inp[j++]<<16;
1218 outp[k++] = (char) bTable[res>>18];
1219 outp[k++] = (char) bTable[(res>>12)&0x3F];
1220 //outp[k++] = '=';
1221 //outp[k++] = '=';
1222 break;
1223 case 2:
1224 res = inp[j++]<<16;
1225 res = res | inp[j++]<<8;
1226 outp[k++] = (char) bTable[res>>18];
1227 outp[k++] = (char) bTable[(res>>12)&0x3F];
1228 outp[k++] = (char) bTable[(res>>6)&0x3F];
1229 //outp[k++] = '=';
1230 break;
1231 default:
1232 break;
1233 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001234 outp[k] = '\0';
1235 return k;
1236}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301237
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301238
1239int audio_extn_utils_get_codec_version(const char *snd_card_name,
1240 int card_num,
1241 char *codec_version)
1242{
1243 char procfs_path[50];
1244 FILE *fp;
1245
1246 if (strstr(snd_card_name, "tasha")) {
1247 snprintf(procfs_path, sizeof(procfs_path),
1248 "/proc/asound/card%d/codecs/tasha/version", card_num);
1249 if ((fp = fopen(procfs_path, "r")) != NULL) {
1250 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1251 fclose(fp);
1252 } else {
1253 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1254 return -ENOENT;
1255 }
1256 ALOGD("%s: codec version %s", __func__, codec_version);
1257 }
1258
1259 return 0;
1260}
1261
1262
Ashish Jain81eb2a82015-05-13 10:52:34 +05301263#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1264
1265void get_default_compressed_channel_status(
1266 unsigned char *channel_status)
1267{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301268 memset(channel_status,0,24);
1269
1270 /* block start bit in preamble bit 3 */
1271 channel_status[0] |= PROFESSIONAL;
1272 //compre out
1273 channel_status[0] |= NON_LPCM;
1274 // sample rate; fixed 48K for default/transcode
1275 channel_status[3] |= SR_48000;
1276}
1277
1278#ifdef HDMI_PASSTHROUGH_ENABLED
1279int32_t get_compressed_channel_status(void *audio_stream_data,
1280 uint32_t audio_frame_size,
1281 unsigned char *channel_status,
1282 enum audio_parser_code_type codec_type)
1283 // codec_type - AUDIO_PARSER_CODEC_AC3
1284 // - AUDIO_PARSER_CODEC_DTS
1285{
1286 unsigned char *stream;
1287 int ret = 0;
1288 stream = (unsigned char *)audio_stream_data;
1289
1290 if (audio_stream_data == NULL || audio_frame_size == 0) {
1291 ALOGW("no buffer to get channel status, return default for compress");
1292 get_default_compressed_channel_status(channel_status);
1293 return ret;
1294 }
1295
1296 memset(channel_status,0,24);
1297 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1298 {
1299 ALOGE("init audio parser failed");
1300 return -1;
1301 }
1302 ret = get_channel_status(channel_status, codec_type);
1303 return ret;
1304
1305}
1306
1307#endif
1308
1309void get_lpcm_channel_status(uint32_t sampleRate,
1310 unsigned char *channel_status)
1311{
1312 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301313 memset(channel_status,0,24);
1314 /* block start bit in preamble bit 3 */
1315 channel_status[0] |= PROFESSIONAL;
1316 //LPCM OUT
1317 channel_status[0] &= ~NON_LPCM;
1318
1319 switch (sampleRate) {
1320 case 8000:
1321 case 11025:
1322 case 12000:
1323 case 16000:
1324 case 22050:
1325 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301326 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301327 case 24000:
1328 channel_status[3] |= SR_24000;
1329 break;
1330 case 32000:
1331 channel_status[3] |= SR_32000;
1332 break;
1333 case 44100:
1334 channel_status[3] |= SR_44100;
1335 break;
1336 case 48000:
1337 channel_status[3] |= SR_48000;
1338 break;
1339 case 88200:
1340 channel_status[3] |= SR_88200;
1341 break;
1342 case 96000:
1343 channel_status[3] |= SR_96000;
1344 break;
1345 case 176400:
1346 channel_status[3] |= SR_176400;
1347 break;
1348 case 192000:
1349 channel_status[3] |= SR_192000;
1350 break;
1351 default:
1352 ALOGV("Invalid sample_rate %u\n", sampleRate);
1353 status = -1;
1354 break;
1355 }
1356}
1357
1358void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1359{
1360 unsigned char channel_status[24]={0};
1361 struct snd_aes_iec958 iec958;
1362 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1363 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301364 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301365#ifdef HDMI_PASSTHROUGH_ENABLED
1366 if (audio_extn_is_dolby_format(out->format) &&
1367 /*TODO:Extend code to support DTS passthrough*/
1368 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301369 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301370 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1371 } else
1372#endif
1373 {
1374 /*set channel status bit for LPCM*/
1375 get_lpcm_channel_status(out->sample_rate, channel_status);
1376 }
1377
1378 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1379 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1380 if (!ctl) {
1381 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1382 __func__, mixer_ctl_name);
1383 return;
1384 }
1385 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1386 ALOGE("%s: Could not set channel status for ext HDMI ",
1387 __func__);
1388 return;
1389 }
1390
1391}
1392#endif