blob: b7af5f5fccea1e206a8a393e579423312f082dee [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Dhananjay Kumar14245982017-01-16 20:21:00 +05302 * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_utils"
21/* #define LOG_NDEBUG 0 */
22
Manish Dewangan27346042017-03-01 12:56:12 +053023#include <inttypes.h>
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070024#include <errno.h>
25#include <cutils/properties.h>
26#include <cutils/config_utils.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <cutils/str_parms.h>
30#include <cutils/log.h>
31#include <cutils/misc.h>
32
Manish Dewangan07de2142017-02-27 19:27:20 +053033
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070034#include "audio_hw.h"
35#include "platform.h"
36#include "platform_api.h"
37#include "audio_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080038#include "voice.h"
Manish Dewangan07de2142017-02-27 19:27:20 +053039#include <sound/compress_params.h>
40#include <sound/compress_offload.h>
41#include <tinycompress/tinycompress.h>
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053042
43#ifdef DYNAMIC_LOG_ENABLED
44#include <log_xml_parser.h>
45#define LOG_MASK HAL_MOD_FILE_UTILS
46#include <log_utils.h>
47#endif
48
Ashish Jain81eb2a82015-05-13 10:52:34 +053049#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
50#ifdef HDMI_PASSTHROUGH_ENABLED
51#include "audio_parsers.h"
52#endif
53#endif
54
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053055#ifdef LINUX_ENABLED
56#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053057#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053058#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053060#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053061#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070062
63#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053064#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070065
66#define DYNAMIC_VALUE_TAG "dynamic"
67#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053068#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070069#define FORMATS_TAG "formats"
70#define SAMPLING_RATES_TAG "sampling_rates"
71#define BIT_WIDTH_TAG "bit_width"
72#define APP_TYPE_TAG "app_type"
73
74#define STRING_TO_ENUM(string) { #string, string }
75#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
76
Ben Rombergera04fabc2014-11-14 12:16:03 -080077#define BASE_TABLE_SIZE 64
78#define MAX_BASEINDEX_LEN 256
79
Ben Romberger1aaaf862017-04-06 17:49:46 -070080#ifndef SND_AUDIOCODEC_TRUEHD
81#define SND_AUDIOCODEC_TRUEHD 0x00000023
82#endif
83
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
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700105struct string_to_enum {
106 const char *name;
107 uint32_t value;
108};
109
110const struct string_to_enum s_flag_name_to_enum_table[] = {
111 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
112 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
113 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800114 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700115 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
116 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
117 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700118 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700119#ifdef INCALL_MUSIC_ENABLED
120 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
121#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700122 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Naresh Tanniruee3499a2017-01-05 14:05:35 +0530123 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TIMESTAMP),
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700124 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530125 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
126 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
127 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
128 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
129 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530130 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700131};
132
133const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530134 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700135 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530136 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
137 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530138 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700139 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
140 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
141 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700142 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
143 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700144 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700145 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700146 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530147 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700148 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700149 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530150#ifdef AUDIO_EXTN_FORMATS_ENABLED
151 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700152 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
153 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
154 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700155 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
156 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
157 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
158 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
159 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
160 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
161 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700162 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530163 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
164 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700165 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800166 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
167 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
168 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530169 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530170 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
171 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
172 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530173 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530174 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
175 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
176 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
177 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530178 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700179#endif
180};
181
Manish Dewangan3ccdea52017-02-13 19:31:54 +0530182/* payload structure avt_device drift query */
183struct audio_avt_device_drift_stats {
184 uint32_t minor_version;
185 /* Indicates the device interface direction as either
186 * source (Tx) or sink (Rx).
187 */
188 uint16_t device_direction;
189 /*params exposed to client */
190 struct audio_avt_device_drift_param drift_param;
191};
192
Ben Rombergera04fabc2014-11-14 12:16:03 -0800193static char bTable[BASE_TABLE_SIZE] = {
194 'A','B','C','D','E','F','G','H','I','J','K','L',
195 'M','N','O','P','Q','R','S','T','U','V','W','X',
196 'Y','Z','a','b','c','d','e','f','g','h','i','j',
197 'k','l','m','n','o','p','q','r','s','t','u','v',
198 'w','x','y','z','0','1','2','3','4','5','6','7',
199 '8','9','+','/'
200};
201
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700202static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
203 const char *name)
204{
205 size_t i;
206 for (i = 0; i < size; i++) {
207 if (strcmp(table[i].name, name) == 0) {
208 ALOGV("%s found %s", __func__, table[i].name);
209 return table[i].value;
210 }
211 }
212 return 0;
213}
214
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530215static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700216{
217 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530218 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800219 char *last_r;
220 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700221 while (flag_name != NULL) {
222 if (strlen(flag_name) != 0) {
223 flag |= string_to_enum(s_flag_name_to_enum_table,
224 ARRAY_SIZE(s_flag_name_to_enum_table),
225 flag_name);
226 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800227 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700228 }
229
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800230 ALOGV("parse_flag_names: flag - %x", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530231 io_flags.in_flags = (audio_input_flags_t)flag;
232 io_flags.out_flags = (audio_output_flags_t)flag;
233 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700234}
235
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530236static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700237{
238 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800239 char *last_r;
240 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700241
242 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
243 return;
244
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530245 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700246 while (str != NULL) {
247 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
248 ARRAY_SIZE(s_format_name_to_enum_table), str);
249 ALOGV("%s: format - %d", __func__, format);
250 if (format != 0) {
251 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700252 if (sf_info == NULL)
253 break; /* return whatever was parsed */
254
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700255 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530256 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700257 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800258 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700259 }
260}
261
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530262static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700263{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700264 struct stream_sample_rate *ss_info = NULL;
265 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800266 char *last_r;
267 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700268
Amit Shekhar6f461b12014-08-01 14:52:58 -0700269 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
270 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530272 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700273 while (str != NULL) {
274 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
275 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
276 if (0 != sample_rate) {
277 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800278 if (!ss_info) {
279 ALOGE("%s: memory allocation failure", __func__);
280 return;
281 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700282 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530283 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700284 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800285 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700286 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700287}
288
289static int parse_bit_width_names(char *name)
290{
291 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800292 char *last_r;
293 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700294
295 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
296 bit_width = (int)strtol(str, (char **)NULL, 10);
297
298 ALOGV("%s: bit_width - %d", __func__, bit_width);
299 return bit_width;
300}
301
302static int parse_app_type_names(void *platform, char *name)
303{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700304 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800305 char *last_r;
306 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700307
308 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
309 app_type = (int)strtol(str, (char **)NULL, 10);
310
311 ALOGV("%s: app_type - %d", __func__, app_type);
312 return app_type;
313}
314
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530315static void update_streams_cfg_list(cnode *root, void *platform,
316 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700317{
318 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530319 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700320
321 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530322 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700323
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530324 if (!s_info) {
325 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700326 return;
327 }
328
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700329 while (node) {
330 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530331 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530332 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
333 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700334 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530335 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700336 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530337 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
338 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700339 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530340 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530342 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700343 }
344 node = node->next;
345 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530346 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700347}
348
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349static void load_cfg_list(cnode *root, void *platform,
350 struct listnode *streams_output_cfg_list,
351 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700352{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530353 cnode *node = NULL;
354
355 node = config_find(root, OUTPUTS_TAG);
356 if (node != NULL) {
357 node = node->first_child;
358 while (node) {
359 ALOGV("%s: loading output %s", __func__, node->name);
360 update_streams_cfg_list(node, platform, streams_output_cfg_list);
361 node = node->next;
362 }
363 } else {
364 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700365 }
366
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530367 node = config_find(root, INPUTS_TAG);
368 if (node != NULL) {
369 node = node->first_child;
370 while (node) {
371 ALOGV("%s: loading input %s", __func__, node->name);
372 update_streams_cfg_list(node, platform, streams_input_cfg_list);
373 node = node->next;
374 }
375 } else {
376 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700377 }
378}
379
380static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530381 struct listnode *streams_output_cfg_list,
382 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700383{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530384 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700385 int length = 0, i, num_app_types = 0;
386 struct listnode *node;
387 bool update;
388 struct mixer_ctl *ctl = NULL;
389 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530390 struct streams_io_cfg *s_info = NULL;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800391 uint32_t target_bit_width = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700392
393 if (!mixer) {
394 ALOGE("%s: mixer is null",__func__);
395 return;
396 }
397 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
398 if (!ctl) {
399 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
400 return;
401 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530402 app_type_cfg[length++] = num_app_types;
403
404 if (list_empty(streams_output_cfg_list)) {
405 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700406 app_type_cfg[length++] = 48000;
407 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530408 num_app_types += 1;
409 }
410 if (list_empty(streams_input_cfg_list)) {
411 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
412 app_type_cfg[length++] = 48000;
413 app_type_cfg[length++] = 16;
414 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700415 }
416
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800417 /* get target bit width for ADM enforce mode */
418 target_bit_width = adev_get_dsp_bit_width_enforce_mode();
419
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700420 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530421 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700422 update = true;
423 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530424 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700425 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530426 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530427 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530428 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530429 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530430 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800431 /* ADM bit width = max(enforce_bit_width, bit_width from s_info */
432 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
433 (target_bit_width > app_type_cfg[i+3]))
434 app_type_cfg[i+3] = target_bit_width;
435
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700436 update = false;
437 break;
438 }
439 }
440 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530441 num_app_types += 1;
442 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
443 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800444 app_type_cfg[length] = s_info->app_type_cfg.bit_width;
445 if (audio_extn_is_dsp_bit_width_enforce_mode_supported(s_info->flags.out_flags) &&
446 (target_bit_width > app_type_cfg[length]))
447 app_type_cfg[length] = target_bit_width;
448
449 length++;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530450 }
451 }
452 list_for_each(node, streams_input_cfg_list) {
453 s_info = node_to_item(node, struct streams_io_cfg, list);
454 update = true;
455 for (i=0; i<length; i=i+3) {
456 if (app_type_cfg[i+1] == 0)
457 break;
458 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530459 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530460 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530461 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530462 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
463 update = false;
464 break;
465 }
466 }
467 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
468 num_app_types += 1;
469 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
470 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
471 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700472 }
473 }
474 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
475 if (num_app_types) {
476 app_type_cfg[0] = num_app_types;
477 mixer_ctl_set_array(ctl, app_type_cfg, length);
478 }
479}
480
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530481void audio_extn_utils_update_streams_cfg_lists(void *platform,
482 struct mixer *mixer,
483 struct listnode *streams_output_cfg_list,
484 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700485{
486 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530487 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700488
489 ALOGV("%s", __func__);
490 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530491 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700492
493 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700494 if (root == NULL) {
495 ALOGE("cfg_list, NULL config root");
496 return;
497 }
498
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530499 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
500 if (data == NULL) {
501 ALOGD("%s: failed to open io config file(%s), trying older config file",
502 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
503 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
504 if (data == NULL) {
505 send_app_type_cfg(platform, mixer,
506 streams_output_cfg_list,
507 streams_input_cfg_list);
508 ALOGE("%s: could not load io policy config!", __func__);
yidongh6eb4f752017-04-19 18:20:57 +0800509 free(root);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530510 return;
511 }
512 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700513
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530514 config_load(root, data);
515 load_cfg_list(root, platform, streams_output_cfg_list,
516 streams_input_cfg_list);
517
518 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
519 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800520
521 config_free(root);
yidongh6eb4f752017-04-19 18:20:57 +0800522 free(root);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800523 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700524}
525
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530526static void audio_extn_utils_dump_streams_cfg_list(
527 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700528{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700529 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530530 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700531 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700532 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530533
534 list_for_each(node_i, streams_cfg_list) {
535 s_info = node_to_item(node_i, struct streams_io_cfg, list);
536 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
537 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
538 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
539 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700540 sf_info = node_to_item(node_j, struct stream_format, list);
541 ALOGV("format-%x", sf_info->format);
542 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530543 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700544 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
545 ALOGV("sample rate-%d", ss_info->sample_rate);
546 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700547 }
548}
549
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530550void audio_extn_utils_dump_streams_cfg_lists(
551 struct listnode *streams_output_cfg_list,
552 struct listnode *streams_input_cfg_list)
553{
554 ALOGV("%s", __func__);
555 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
556 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
557}
558
559static void audio_extn_utils_release_streams_cfg_list(
560 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700561{
562 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530563 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700564
565 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530566
567 while (!list_empty(streams_cfg_list)) {
568 node_i = list_head(streams_cfg_list);
569 s_info = node_to_item(node_i, struct streams_io_cfg, list);
570 while (!list_empty(&s_info->format_list)) {
571 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700572 list_remove(node_j);
573 free(node_to_item(node_j, struct stream_format, list));
574 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530575 while (!list_empty(&s_info->sample_rate_list)) {
576 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700577 list_remove(node_j);
578 free(node_to_item(node_j, struct stream_sample_rate, list));
579 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700580 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530581 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700582 }
583}
584
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530585void audio_extn_utils_release_streams_cfg_lists(
586 struct listnode *streams_output_cfg_list,
587 struct listnode *streams_input_cfg_list)
588{
589 ALOGV("%s", __func__);
590 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
591 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
592}
593
594static bool set_app_type_cfg(struct streams_io_cfg *s_info,
595 struct stream_app_type_cfg *app_type_cfg,
596 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700597 {
598 struct listnode *node_i;
599 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700601 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
602 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530603 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700604
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530605 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700606 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530607 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700608 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
609 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
610 return true;
611 }
612 }
613 /*
614 * Reiterate through the list assuming dafault sample rate.
615 * Handles scenario where input sample rate is higher
616 * than all sample rates in list for the input bit width.
617 */
618 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800619
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530620 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700621 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
622 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530623 (bit_width == s_info->app_type_cfg.bit_width)) {
624 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700625 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530626 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800627 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 -0700628 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
629 return true;
630 }
631 }
632 return false;
633}
634
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530635void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
636 struct listnode *streams_input_cfg_list,
637 audio_devices_t devices __unused,
638 audio_input_flags_t flags,
639 audio_format_t format,
640 uint32_t sample_rate,
641 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530642 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530643 struct stream_app_type_cfg *app_type_cfg)
644{
645 struct listnode *node_i, *node_j;
646 struct streams_io_cfg *s_info;
647 struct stream_format *sf_info;
648
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530649 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
650 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530651
652 list_for_each(node_i, streams_input_cfg_list) {
653 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530654 /* Along with flags do profile matching if set at either end.*/
655 if (s_info->flags.in_flags == flags &&
656 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
657 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530658 list_for_each(node_j, &s_info->format_list) {
659 sf_info = node_to_item(node_j, struct stream_format, list);
660 if (sf_info->format == format) {
661 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
662 return;
663 }
664 }
665 }
666 }
667 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
668 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
669 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
670 app_type_cfg->bit_width = 16;
671}
672
673void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700674 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700675 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700676 audio_output_flags_t flags,
677 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700678 uint32_t sample_rate,
679 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530680 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530681 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700682 struct stream_app_type_cfg *app_type_cfg)
683{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530684 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530685 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700686 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530687 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700688
Ashish Jain058165c2016-09-28 23:18:48 +0530689 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700690 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700691 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
692 if (-ENOSYS != bw)
693 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700694 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
695 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
696 }
697
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700698 property_get("vendor.audio.playback.mch.downsample",value,"");
Manish Dewangan837dc462015-05-27 10:17:41 +0530699 if (!strncmp("true", value, sizeof("true"))) {
700 if ((popcount(channel_mask) > 2) &&
701 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
702 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
703 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
704 ALOGD("%s: MCH session defaulting sample rate to %d",
705 __func__, sample_rate);
706 }
707 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530708
709 /* Set sampling rate to 176.4 for DSD64
710 * and 352.8Khz for DSD128.
711 * Set Bit Width to 16. output will be 16 bit
712 * post DoP in ASM.
713 */
714 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
715 (format == AUDIO_FORMAT_DSD)) {
716 bit_width = 16;
717 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
718 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
719 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
720 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
721 }
722
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530723 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
724 //TODO: Handle fractional sampling rate configuration for LL
725 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
726 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
727 __func__, sample_rate, bit_width);
728 }
729
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800730 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s, app_type %d",
731 __func__, flags, format, sample_rate, profile, app_type_cfg->app_type);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700732 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530733 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530734 /* Along with flags do profile matching if set at either end.*/
735 if (s_info->flags.out_flags == flags &&
736 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
737 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530738 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700739 sf_info = node_to_item(node_j, struct stream_format, list);
740 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530741 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700742 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700743 }
744 }
745 }
746 }
747 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530748 s_info = node_to_item(node_i, struct streams_io_cfg, list);
749 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700750 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530751 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
752 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
753 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700754 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530755 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700756 return;
757 }
758 }
759 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700760 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700761 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700762 app_type_cfg->bit_width = 16;
763}
764
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530765static bool audio_is_this_native_usecase(struct audio_usecase *uc)
766{
767 bool native_usecase = false;
768 struct stream_out *out = (struct stream_out*) uc->stream.out;
769
770 if (PCM_PLAYBACK == uc->type && out != NULL &&
771 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
772 is_offload_usecase(uc->id) &&
773 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
774 native_usecase = true;
775
776 return native_usecase;
777}
778
Xiaojun Sang785b5da2017-08-03 15:52:29 +0800779bool audio_extn_is_dsp_bit_width_enforce_mode_supported(audio_output_flags_t flags)
780{
781 /* DSP bitwidth enforce mode for ADM and AFE:
782 * includes:
783 * deep buffer, low latency, direct pcm and offload.
784 * excludes:
785 * ull(raw+fast), VOIP.
786 */
787 if ((flags & AUDIO_OUTPUT_FLAG_VOIP_RX) ||
788 ((flags & AUDIO_OUTPUT_FLAG_RAW) &&
789 (flags & AUDIO_OUTPUT_FLAG_FAST)))
790 return false;
791
792
793 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
794 (flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
795 (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) ||
796 (flags & AUDIO_OUTPUT_FLAG_PRIMARY))
797 return true;
798 else
799 return false;
800}
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800801
802static inline bool audio_is_vr_mode_on(struct audio_device *(__attribute__((unused)) adev))
803{
804 return adev->vr_audio_mode_enabled;
805}
806
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530807void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
808 struct audio_device *adev,
809 struct audio_usecase *usecase)
810{
811 ALOGV("%s", __func__);
812
813 switch(usecase->type) {
814 case PCM_PLAYBACK:
815 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
816 &adev->streams_output_cfg_list,
817 usecase->stream.out->devices,
818 usecase->stream.out->flags,
819 usecase->stream.out->format,
820 usecase->stream.out->sample_rate,
821 usecase->stream.out->bit_width,
822 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530823 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530824 &usecase->stream.out->app_type_cfg);
825 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
826 break;
827 case PCM_CAPTURE:
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700828 if (usecase->id == USECASE_AUDIO_RECORD_VOIP)
829 usecase->stream.in->app_type_cfg.app_type = APP_TYPE_VOIP_AUDIO;
830 else
831 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530832 &adev->streams_input_cfg_list,
833 usecase->stream.in->device,
834 usecase->stream.in->flags,
835 usecase->stream.in->format,
836 usecase->stream.in->sample_rate,
837 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530838 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530839 &usecase->stream.in->app_type_cfg);
840 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
841 break;
842 default:
843 ALOGE("%s: app type cfg not supported for usecase type (%d)",
844 __func__, usecase->type);
845 }
846}
847
Siena Richard7c2db772016-12-21 11:32:34 -0800848static int send_app_type_cfg_for_device(struct audio_device *adev,
849 struct audio_usecase *usecase,
850 int split_snd_device)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700851{
852 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530853 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
854 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700855 struct mixer_ctl *ctl;
Siena Richard7c2db772016-12-21 11:32:34 -0800856 int pcm_device_id = 0, acdb_dev_id, app_type;
857 int snd_device = split_snd_device, snd_device_be_idx = -1;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530858 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530859 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700860
Siena Richard7c2db772016-12-21 11:32:34 -0800861 ALOGV("%s: usecase->out_snd_device %s, usecase->in_snd_device %s, split_snd_device %s",
862 __func__, platform_get_snd_device_name(usecase->out_snd_device),
863 platform_get_snd_device_name(usecase->in_snd_device),
864 platform_get_snd_device_name(split_snd_device));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700865
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530866 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530867 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700868 rc = 0;
869 goto exit_send_app_type_cfg;
870 }
871 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
872 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
873 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800874 (usecase->id != USECASE_AUDIO_PLAYBACK_ULL) &&
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700875 (usecase->id != USECASE_AUDIO_PLAYBACK_VOIP) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530876 (!is_offload_usecase(usecase->id)) &&
877 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530878 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 -0700879 rc = 0;
880 goto exit_send_app_type_cfg;
881 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -0800882
883 //if VR is active then only send the mixer control
884 if (usecase->id == USECASE_AUDIO_PLAYBACK_ULL && !audio_is_vr_mode_on(adev)) {
885 ALOGI("ULL doesnt need sending app type cfg, returning");
886 rc = 0;
887 goto exit_send_app_type_cfg;
888 }
889
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530890 if (usecase->type == PCM_PLAYBACK) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700891 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530892 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
893 "Audio Stream %d App Type Cfg", pcm_device_id);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700894 } else if (usecase->type == PCM_CAPTURE) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700895 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530896 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
897 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530898 }
Siena Richard7c2db772016-12-21 11:32:34 -0800899
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700900 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
901 if (!ctl) {
902 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
903 mixer_ctl_name);
904 rc = -EINVAL;
905 goto exit_send_app_type_cfg;
906 }
Aditya Bavanari701a6992017-03-30 19:17:16 +0530907 snd_device = platform_get_spkr_prot_snd_device(snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530908
Rohit Kumar1181b292017-01-31 18:07:17 +0530909 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
910 if (acdb_dev_id <= 0) {
911 ALOGE("%s: Couldn't get the acdb dev id", __func__);
912 rc = -EINVAL;
913 goto exit_send_app_type_cfg;
914 }
915
Siena Richard7c2db772016-12-21 11:32:34 -0800916 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
917 if (snd_device_be_idx < 0) {
918 ALOGE("%s: Couldn't get the backend index for snd device %s ret=%d",
919 __func__, platform_get_snd_device_name(snd_device),
920 snd_device_be_idx);
921 }
922
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530923 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
Manish Dewangan837dc462015-05-27 10:17:41 +0530924
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700925 property_get("vendor.audio.playback.mch.downsample",value,"");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530926 if (!strncmp("true", value, sizeof("true"))) {
927 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
928 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
929 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
930 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
931 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530932
Ashish Jain4826f6c2017-02-06 13:33:20 +0530933 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Mingming Yin21854652016-04-13 11:54:02 -0700934 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530935 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
936 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
937 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
938 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
939 /*
940 * To best utlize DSP, check if the stream sample rate is supported/multiple of
941 * configured device sample rate, if not update the COPP rate to be equal to the
942 * device sample rate, else open COPP at stream sample rate
943 */
944 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
945 usecase->stream.out->sample_rate,
946 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +0530947 } else if (((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
948 !audio_is_this_native_usecase(usecase)) &&
Mingming Yin21854652016-04-13 11:54:02 -0700949 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
950 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Ashish Jain4826f6c2017-02-06 13:33:20 +0530951 /* Reset to default if no native stream is active*/
Mingming Yin21854652016-04-13 11:54:02 -0700952 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
953 }
954 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
955
Siena Richard7c2db772016-12-21 11:32:34 -0800956 app_type = usecase->stream.out->app_type_cfg.app_type;
957 app_type_cfg[len++] = app_type;
Mingming Yin21854652016-04-13 11:54:02 -0700958 app_type_cfg[len++] = acdb_dev_id;
959 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700960 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC) ||
961 (usecase->stream.out->format == AUDIO_FORMAT_DOLBY_TRUEHD))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530962 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Naresh Tanniru3a406772017-05-10 13:09:05 -0700963
964 sample_rate = sample_rate * 4;
965 if (sample_rate > HDMI_PASSTHROUGH_MAX_SAMPLE_RATE)
966 sample_rate = HDMI_PASSTHROUGH_MAX_SAMPLE_RATE;
Mingming Yin21854652016-04-13 11:54:02 -0700967 }
Naresh Tanniru3a406772017-05-10 13:09:05 -0700968 app_type_cfg[len++] = sample_rate;
969
Siena Richard7c2db772016-12-21 11:32:34 -0800970 if (snd_device_be_idx > 0)
971 app_type_cfg[len++] = snd_device_be_idx;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530972
Siena Richard7c2db772016-12-21 11:32:34 -0800973 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
974 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530975
976 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
Siena Richard7c2db772016-12-21 11:32:34 -0800977 app_type = usecase->stream.in->app_type_cfg.app_type;
978 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530979 app_type_cfg[len++] = acdb_dev_id;
Siena Richard7c2db772016-12-21 11:32:34 -0800980 sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
981 app_type_cfg[len++] = sample_rate;
982 if (snd_device_be_idx > 0)
983 app_type_cfg[len++] = snd_device_be_idx;
984 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
985 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530986 } else {
Siena Richard7c2db772016-12-21 11:32:34 -0800987 app_type = platform_get_default_app_type_v2(adev->platform, usecase->type);
988 app_type_cfg[len++] = app_type;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530989 app_type_cfg[len++] = acdb_dev_id;
990 app_type_cfg[len++] = sample_rate;
Siena Richard7c2db772016-12-21 11:32:34 -0800991 if (snd_device_be_idx > 0)
992 app_type_cfg[len++] = snd_device_be_idx;
993 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d, snd_device_be_idx %d",
994 __func__, app_type, acdb_dev_id, sample_rate, snd_device_be_idx);
Ashish Jainc02430d2016-01-04 10:42:43 +0530995 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530996
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700997 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700998 rc = 0;
999exit_send_app_type_cfg:
1000 return rc;
1001}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001002
Siena Richard7c2db772016-12-21 11:32:34 -08001003int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
1004 struct audio_usecase *usecase)
1005{
1006 int i, num_devices = 0;
1007 snd_device_t new_snd_devices[SND_DEVICE_OUT_END] = {0};
1008 int rc = 0;
1009
1010 switch (usecase->type) {
1011 case PCM_PLAYBACK:
1012 ALOGD("%s: usecase->out_snd_device %s",
1013 __func__, platform_get_snd_device_name(usecase->out_snd_device));
1014 /* check for out combo device */
1015 if (platform_split_snd_device(adev->platform,
1016 usecase->out_snd_device,
1017 &num_devices, new_snd_devices)) {
1018 new_snd_devices[0] = usecase->out_snd_device;
1019 num_devices = 1;
1020 }
1021 break;
1022 case PCM_CAPTURE:
1023 ALOGD("%s: usecase->in_snd_device %s",
1024 __func__, platform_get_snd_device_name(usecase->in_snd_device));
1025 /* check for in combo device */
1026 if (platform_split_snd_device(adev->platform,
1027 usecase->in_snd_device,
1028 &num_devices, new_snd_devices)) {
1029 new_snd_devices[0] = usecase->in_snd_device;
1030 num_devices = 1;
1031 }
1032 break;
1033 default:
1034 ALOGI("%s: not a playback/capture path, no need to cfg app type", __func__);
1035 rc = 0;
1036 break;
1037 }
1038
1039 for (i = 0; i < num_devices; i++) {
1040 rc = send_app_type_cfg_for_device(adev, usecase, new_snd_devices[i]);
1041 if (rc)
1042 break;
1043 }
1044
1045 return rc;
1046}
1047
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +05301048int read_line_from_file(const char *path, char *buf, size_t count)
1049{
1050 char * fgets_ret;
1051 FILE * fd;
1052 int rv;
1053
1054 fd = fopen(path, "r");
1055 if (fd == NULL)
1056 return -1;
1057
1058 fgets_ret = fgets(buf, (int)count, fd);
1059 if (NULL != fgets_ret) {
1060 rv = (int)strlen(buf);
1061 } else {
1062 rv = ferror(fd);
1063 }
1064 fclose(fd);
1065
1066 return rv;
1067}
1068
Ashish Jainf1eaa582016-05-23 20:54:24 +05301069/*Translates ALSA formats to AOSP PCM formats*/
1070audio_format_t alsa_format_to_hal(uint32_t alsa_format)
1071{
1072 audio_format_t format;
1073
1074 switch(alsa_format) {
1075 case SNDRV_PCM_FORMAT_S16_LE:
1076 format = AUDIO_FORMAT_PCM_16_BIT;
1077 break;
1078 case SNDRV_PCM_FORMAT_S24_3LE:
1079 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1080 break;
1081 case SNDRV_PCM_FORMAT_S24_LE:
1082 format = AUDIO_FORMAT_PCM_8_24_BIT;
1083 break;
1084 case SNDRV_PCM_FORMAT_S32_LE:
1085 format = AUDIO_FORMAT_PCM_32_BIT;
1086 break;
1087 default:
1088 ALOGW("Incorrect ALSA format");
1089 format = AUDIO_FORMAT_INVALID;
1090 }
1091 return format;
1092}
1093
1094/*Translates hal format (AOSP) to alsa formats*/
1095uint32_t hal_format_to_alsa(audio_format_t hal_format)
1096{
1097 uint32_t alsa_format;
1098
1099 switch (hal_format) {
1100 case AUDIO_FORMAT_PCM_32_BIT: {
1101 if (platform_supports_true_32bit())
1102 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1103 else
1104 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1105 }
1106 break;
1107 case AUDIO_FORMAT_PCM_8_BIT:
1108 alsa_format = SNDRV_PCM_FORMAT_S8;
1109 break;
1110 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1111 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1112 break;
1113 case AUDIO_FORMAT_PCM_8_24_BIT: {
1114 if (platform_supports_true_32bit())
1115 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
1116 else
1117 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1118 }
1119 break;
1120 case AUDIO_FORMAT_PCM_FLOAT:
1121 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
1122 break;
1123 default:
1124 case AUDIO_FORMAT_PCM_16_BIT:
1125 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
1126 break;
1127 }
1128 return alsa_format;
1129}
1130
Ashish Jain83a6cc22016-06-28 14:34:17 +05301131/*Translates PCM formats to AOSP formats*/
1132audio_format_t pcm_format_to_hal(uint32_t pcm_format)
1133{
1134 audio_format_t format = AUDIO_FORMAT_INVALID;
1135
1136 switch(pcm_format) {
1137 case PCM_FORMAT_S16_LE:
1138 format = AUDIO_FORMAT_PCM_16_BIT;
1139 break;
1140 case PCM_FORMAT_S24_3LE:
1141 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1142 break;
1143 case PCM_FORMAT_S24_LE:
1144 format = AUDIO_FORMAT_PCM_8_24_BIT;
1145 break;
1146 case PCM_FORMAT_S32_LE:
1147 format = AUDIO_FORMAT_PCM_32_BIT;
1148 break;
1149 default:
1150 ALOGW("Incorrect PCM format");
1151 format = AUDIO_FORMAT_INVALID;
1152 }
1153 return format;
1154}
1155
1156/*Translates hal format (AOSP) to alsa formats*/
1157uint32_t hal_format_to_pcm(audio_format_t hal_format)
1158{
1159 uint32_t pcm_format;
1160
1161 switch (hal_format) {
1162 case AUDIO_FORMAT_PCM_32_BIT:
1163 case AUDIO_FORMAT_PCM_8_24_BIT:
1164 case AUDIO_FORMAT_PCM_FLOAT: {
1165 if (platform_supports_true_32bit())
1166 pcm_format = PCM_FORMAT_S32_LE;
1167 else
1168 pcm_format = PCM_FORMAT_S24_3LE;
1169 }
1170 break;
1171 case AUDIO_FORMAT_PCM_8_BIT:
1172 pcm_format = PCM_FORMAT_S8;
1173 break;
1174 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1175 pcm_format = PCM_FORMAT_S24_3LE;
1176 break;
1177 default:
1178 case AUDIO_FORMAT_PCM_16_BIT:
1179 pcm_format = PCM_FORMAT_S16_LE;
1180 break;
1181 }
1182 return pcm_format;
1183}
1184
Ashish Jainf1eaa582016-05-23 20:54:24 +05301185uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1186 uint32_t sample_rate,
1187 uint32_t noOfChannels)
1188{
1189 uint32_t fragment_size = 0;
1190 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1191
1192 fragment_size = (pcm_offload_time
1193 * sample_rate
1194 * bytes_per_sample
1195 * noOfChannels)/1000;
1196 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1197 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1198 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1199 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1200 /*To have same PCM samples for all channels, the buffer size requires to
1201 *be multiple of (number of channels * bytes per sample)
1202 *For writes to succeed, the buffer must be written at address which is multiple of 32
1203 */
1204 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1205
1206 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1207 return fragment_size;
1208}
1209
1210/* Calculates the fragment size required to configure compress session.
1211 * Based on the alsa format selected, decide if conversion is needed in
1212
1213 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1214 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1215 */
1216void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1217{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301218 audio_format_t dst_format = out->hal_op_format;
1219 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301220 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1221 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1222
1223 out->compr_config.fragment_size =
1224 get_alsa_fragment_size(hal_op_bytes_per_sample,
1225 out->sample_rate,
1226 popcount(out->channel_mask));
1227
1228 if ((src_format != dst_format) &&
1229 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1230
Ashish Jain83a6cc22016-06-28 14:34:17 +05301231 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301232 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1233 hal_op_bytes_per_sample);
1234 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301235 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301236 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301237 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301238 }
1239}
1240
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301241/* converts pcm format 24_8 to 8_24 inplace */
1242size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1243{
1244 size_t i = 0;
1245 int *int_buf_stream = buf;
1246
1247 if ((bytes % 4) != 0) {
1248 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1249 return -EINVAL;
1250 }
1251
1252 for (; i < (bytes / 4); i++)
1253 int_buf_stream[i] >>= 8;
1254
1255 return bytes;
1256}
1257
1258int get_snd_codec_id(audio_format_t format)
1259{
1260 int id = 0;
1261
1262 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1263 case AUDIO_FORMAT_MP3:
1264 id = SND_AUDIOCODEC_MP3;
1265 break;
1266 case AUDIO_FORMAT_AAC:
1267 id = SND_AUDIOCODEC_AAC;
1268 break;
1269 case AUDIO_FORMAT_AAC_ADTS:
1270 id = SND_AUDIOCODEC_AAC;
1271 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301272 case AUDIO_FORMAT_AAC_LATM:
1273 id = SND_AUDIOCODEC_AAC;
1274 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301275 case AUDIO_FORMAT_PCM:
1276 id = SND_AUDIOCODEC_PCM;
1277 break;
1278 case AUDIO_FORMAT_FLAC:
1279 id = SND_AUDIOCODEC_FLAC;
1280 break;
1281 case AUDIO_FORMAT_ALAC:
1282 id = SND_AUDIOCODEC_ALAC;
1283 break;
1284 case AUDIO_FORMAT_APE:
1285 id = SND_AUDIOCODEC_APE;
1286 break;
1287 case AUDIO_FORMAT_VORBIS:
1288 id = SND_AUDIOCODEC_VORBIS;
1289 break;
1290 case AUDIO_FORMAT_WMA:
1291 id = SND_AUDIOCODEC_WMA;
1292 break;
1293 case AUDIO_FORMAT_WMA_PRO:
1294 id = SND_AUDIOCODEC_WMA_PRO;
1295 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301296 case AUDIO_FORMAT_MP2:
1297 id = SND_AUDIOCODEC_MP2;
1298 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301299 case AUDIO_FORMAT_AC3:
1300 id = SND_AUDIOCODEC_AC3;
1301 break;
1302 case AUDIO_FORMAT_E_AC3:
1303 case AUDIO_FORMAT_E_AC3_JOC:
1304 id = SND_AUDIOCODEC_EAC3;
1305 break;
1306 case AUDIO_FORMAT_DTS:
1307 case AUDIO_FORMAT_DTS_HD:
1308 id = SND_AUDIOCODEC_DTS;
1309 break;
Ben Romberger1aaaf862017-04-06 17:49:46 -07001310 case AUDIO_FORMAT_DOLBY_TRUEHD:
1311 id = SND_AUDIOCODEC_TRUEHD;
1312 break;
Naresh Tanniru928f0862017-04-07 16:44:23 -07001313 case AUDIO_FORMAT_IEC61937:
1314 id = SND_AUDIOCODEC_IEC61937;
1315 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301316 case AUDIO_FORMAT_DSD:
1317 id = SND_AUDIOCODEC_DSD;
1318 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301319 case AUDIO_FORMAT_APTX:
1320 id = SND_AUDIOCODEC_APTX;
1321 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301322 default:
1323 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1324 }
1325
1326 return id;
1327}
1328
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001329void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1330 struct audio_usecase *usecase)
1331{
1332 int type = usecase->type;
1333
Dhananjay Kumar14245982017-01-16 20:21:00 +05301334 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001335 struct stream_out *out = usecase->stream.out;
1336 int snd_device = usecase->out_snd_device;
1337 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001338 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301339 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001340 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301341 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301342 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301343 platform_send_audio_calibration(adev->platform, usecase,
1344 usecase->stream.in->app_type_cfg.app_type,
1345 usecase->stream.in->app_type_cfg.sample_rate);
Vidyakumar Athota653aaef2017-03-16 11:11:31 -07001346 } else if (type == PCM_HFP_CALL || type == PCM_CAPTURE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301347 /* when app type is default. the sample rate is not used to send cal */
1348 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301349 platform_get_default_app_type_v2(adev->platform, usecase->type),
1350 48000);
Vidyakumar Athotae57f6002017-03-01 13:13:16 -08001351 } else {
1352 /* No need to send audio calibration for voice and voip call usecases */
1353 if ((type != VOICE_CALL) && (type != VOIP_CALL))
1354 ALOGW("%s: No audio calibration for usecase type = %d", __func__, type);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001355 }
1356}
1357
Ben Rombergera04fabc2014-11-14 12:16:03 -08001358// Base64 Encode and Decode
1359// Not all features supported. This must be used only with following conditions.
1360// Decode Modes: Support with and without padding
1361// CRLF not handling. So no CRLF in string to decode.
1362// Encode Modes: Supports only padding
1363int b64decode(char *inp, int ilen, uint8_t* outp)
1364{
1365 int i, j, k, ii, num;
1366 int rem, pcnt;
1367 uint32_t res=0;
1368 uint8_t getIndex[MAX_BASEINDEX_LEN];
1369 uint8_t tmp, cflag;
1370
1371 if(inp == NULL || outp == NULL || ilen <= 0) {
1372 ALOGE("[%s] received NULL pointer or zero length",__func__);
1373 return -1;
1374 }
1375
1376 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1377 for(i=0;i<BASE_TABLE_SIZE;i++) {
1378 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1379 }
1380 getIndex[(uint8_t)'=']=0;
1381
1382 j=0;k=0;
1383 num = ilen/4;
1384 rem = ilen%4;
1385 if(rem==0)
1386 num = num-1;
1387 cflag=0;
1388 for(i=0; i<num; i++) {
1389 res=0;
1390 for(ii=0;ii<4;ii++) {
1391 res = res << 6;
1392 tmp = getIndex[(uint8_t)inp[j++]];
1393 res = res | tmp;
1394 cflag = cflag | tmp;
1395 }
1396 outp[k++] = (res >> 16)&0xFF;
1397 outp[k++] = (res >> 8)&0xFF;
1398 outp[k++] = res & 0xFF;
1399 }
1400
1401 // Handle last bytes special
1402 pcnt=0;
1403 if(rem == 0) {
1404 //With padding or full data
1405 res = 0;
1406 for(ii=0;ii<4;ii++) {
1407 if(inp[j] == '=')
1408 pcnt++;
1409 res = res << 6;
1410 tmp = getIndex[(uint8_t)inp[j++]];
1411 res = res | tmp;
1412 cflag = cflag | tmp;
1413 }
1414 outp[k++] = res >> 16;
1415 if(pcnt == 2)
1416 goto done;
1417 outp[k++] = (res>>8)&0xFF;
1418 if(pcnt == 1)
1419 goto done;
1420 outp[k++] = res&0xFF;
1421 } else {
1422 //without padding
1423 res = 0;
1424 for(i=0;i<rem;i++) {
1425 res = res << 6;
1426 tmp = getIndex[(uint8_t)inp[j++]];
1427 res = res | tmp;
1428 cflag = cflag | tmp;
1429 }
1430 for(i=rem;i<4;i++) {
1431 res = res << 6;
1432 pcnt++;
1433 }
1434 outp[k++] = res >> 16;
1435 if(pcnt == 2)
1436 goto done;
1437 outp[k++] = (res>>8)&0xFF;
1438 if(pcnt == 1)
1439 goto done;
1440 outp[k++] = res&0xFF;
1441 }
1442done:
1443 if(cflag == 0xFF) {
1444 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1445 __func__, inp);
1446 return 0;
1447 }
1448 return k;
1449}
1450
1451int b64encode(uint8_t *inp, int ilen, char* outp)
1452{
1453 int i,j,k, num;
1454 int rem=0;
1455 uint32_t res=0;
1456
1457 if(inp == NULL || outp == NULL || ilen<=0) {
1458 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1459 return -1;
1460 }
1461
1462 num = ilen/3;
1463 rem = ilen%3;
1464 j=0;k=0;
1465 for(i=0; i<num; i++) {
1466 //prepare index
1467 res = inp[j++]<<16;
1468 res = res | inp[j++]<<8;
1469 res = res | inp[j++];
1470 //get output map from index
1471 outp[k++] = (char) bTable[(res>>18)&0x3F];
1472 outp[k++] = (char) bTable[(res>>12)&0x3F];
1473 outp[k++] = (char) bTable[(res>>6)&0x3F];
1474 outp[k++] = (char) bTable[res&0x3F];
1475 }
1476
1477 switch(rem) {
1478 case 1:
1479 res = inp[j++]<<16;
1480 outp[k++] = (char) bTable[res>>18];
1481 outp[k++] = (char) bTable[(res>>12)&0x3F];
1482 //outp[k++] = '=';
1483 //outp[k++] = '=';
1484 break;
1485 case 2:
1486 res = inp[j++]<<16;
1487 res = res | inp[j++]<<8;
1488 outp[k++] = (char) bTable[res>>18];
1489 outp[k++] = (char) bTable[(res>>12)&0x3F];
1490 outp[k++] = (char) bTable[(res>>6)&0x3F];
1491 //outp[k++] = '=';
1492 break;
1493 default:
1494 break;
1495 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001496 outp[k] = '\0';
1497 return k;
1498}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301499
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301500
1501int audio_extn_utils_get_codec_version(const char *snd_card_name,
1502 int card_num,
1503 char *codec_version)
1504{
1505 char procfs_path[50];
1506 FILE *fp;
1507
1508 if (strstr(snd_card_name, "tasha")) {
1509 snprintf(procfs_path, sizeof(procfs_path),
1510 "/proc/asound/card%d/codecs/tasha/version", card_num);
1511 if ((fp = fopen(procfs_path, "r")) != NULL) {
1512 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1513 fclose(fp);
1514 } else {
1515 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1516 return -ENOENT;
1517 }
1518 ALOGD("%s: codec version %s", __func__, codec_version);
1519 }
1520
1521 return 0;
1522}
1523
1524
Ashish Jain81eb2a82015-05-13 10:52:34 +05301525#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1526
1527void get_default_compressed_channel_status(
1528 unsigned char *channel_status)
1529{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301530 memset(channel_status,0,24);
1531
1532 /* block start bit in preamble bit 3 */
1533 channel_status[0] |= PROFESSIONAL;
1534 //compre out
1535 channel_status[0] |= NON_LPCM;
1536 // sample rate; fixed 48K for default/transcode
1537 channel_status[3] |= SR_48000;
1538}
1539
1540#ifdef HDMI_PASSTHROUGH_ENABLED
1541int32_t get_compressed_channel_status(void *audio_stream_data,
1542 uint32_t audio_frame_size,
1543 unsigned char *channel_status,
1544 enum audio_parser_code_type codec_type)
1545 // codec_type - AUDIO_PARSER_CODEC_AC3
1546 // - AUDIO_PARSER_CODEC_DTS
1547{
1548 unsigned char *stream;
1549 int ret = 0;
1550 stream = (unsigned char *)audio_stream_data;
1551
1552 if (audio_stream_data == NULL || audio_frame_size == 0) {
1553 ALOGW("no buffer to get channel status, return default for compress");
1554 get_default_compressed_channel_status(channel_status);
1555 return ret;
1556 }
1557
1558 memset(channel_status,0,24);
1559 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1560 {
1561 ALOGE("init audio parser failed");
1562 return -1;
1563 }
1564 ret = get_channel_status(channel_status, codec_type);
1565 return ret;
1566
1567}
1568
1569#endif
1570
1571void get_lpcm_channel_status(uint32_t sampleRate,
1572 unsigned char *channel_status)
1573{
1574 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301575 memset(channel_status,0,24);
1576 /* block start bit in preamble bit 3 */
1577 channel_status[0] |= PROFESSIONAL;
1578 //LPCM OUT
1579 channel_status[0] &= ~NON_LPCM;
1580
1581 switch (sampleRate) {
1582 case 8000:
1583 case 11025:
1584 case 12000:
1585 case 16000:
1586 case 22050:
1587 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301588 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301589 case 24000:
1590 channel_status[3] |= SR_24000;
1591 break;
1592 case 32000:
1593 channel_status[3] |= SR_32000;
1594 break;
1595 case 44100:
1596 channel_status[3] |= SR_44100;
1597 break;
1598 case 48000:
1599 channel_status[3] |= SR_48000;
1600 break;
1601 case 88200:
1602 channel_status[3] |= SR_88200;
1603 break;
1604 case 96000:
1605 channel_status[3] |= SR_96000;
1606 break;
1607 case 176400:
1608 channel_status[3] |= SR_176400;
1609 break;
1610 case 192000:
1611 channel_status[3] |= SR_192000;
1612 break;
1613 default:
1614 ALOGV("Invalid sample_rate %u\n", sampleRate);
1615 status = -1;
1616 break;
1617 }
1618}
1619
1620void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1621{
1622 unsigned char channel_status[24]={0};
1623 struct snd_aes_iec958 iec958;
1624 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1625 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301626 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301627#ifdef HDMI_PASSTHROUGH_ENABLED
1628 if (audio_extn_is_dolby_format(out->format) &&
1629 /*TODO:Extend code to support DTS passthrough*/
1630 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301631 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301632 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1633 } else
1634#endif
1635 {
1636 /*set channel status bit for LPCM*/
1637 get_lpcm_channel_status(out->sample_rate, channel_status);
1638 }
1639
1640 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1641 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1642 if (!ctl) {
1643 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1644 __func__, mixer_ctl_name);
1645 return;
1646 }
1647 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1648 ALOGE("%s: Could not set channel status for ext HDMI ",
1649 __func__);
1650 return;
1651 }
1652
1653}
1654#endif
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301655
1656int audio_extn_utils_get_avt_device_drift(
1657 struct audio_usecase *usecase,
1658 struct audio_avt_device_drift_param *drift_param)
1659{
1660 int ret = 0, count = 0;
1661 char avt_device_drift_mixer_ctl_name[MIXER_PATH_MAX_LENGTH] = {0};
Naresh Tanniru6160c712017-04-17 15:43:48 +05301662 const char *backend = NULL;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301663 struct mixer_ctl *ctl = NULL;
1664 struct audio_avt_device_drift_stats drift_stats;
1665 struct audio_device *adev = NULL;
1666
1667 if (usecase != NULL && usecase->type == PCM_PLAYBACK) {
Naresh Tanniru6160c712017-04-17 15:43:48 +05301668 backend = platform_get_snd_device_backend_interface(usecase->out_snd_device);
1669 if (!backend) {
1670 ALOGE("%s: Unsupported device %d", __func__,
1671 usecase->stream.out->devices);
1672 ret = -EINVAL;
1673 goto done;
1674 }
1675 strlcpy(avt_device_drift_mixer_ctl_name,
1676 backend,
1677 MIXER_PATH_MAX_LENGTH);
1678
1679 count = strlen(backend);
1680 if (MIXER_PATH_MAX_LENGTH - count > 0) {
1681 strlcat(&avt_device_drift_mixer_ctl_name[count],
1682 " DRIFT",
1683 MIXER_PATH_MAX_LENGTH - count);
1684 } else {
1685 ret = -EINVAL;
1686 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301687 }
1688 } else {
Naresh Tanniru7586e292017-04-13 10:38:13 +05301689 ALOGE("%s: Invalid usecase",__func__);
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301690 ret = -EINVAL;
Naresh Tanniru6160c712017-04-17 15:43:48 +05301691 goto done;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301692 }
1693
Naresh Tanniru6160c712017-04-17 15:43:48 +05301694 adev = usecase->stream.out->dev;
Manish Dewangan3ccdea52017-02-13 19:31:54 +05301695 ctl = mixer_get_ctl_by_name(adev->mixer, avt_device_drift_mixer_ctl_name);
1696 if (!ctl) {
1697 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1698 __func__, avt_device_drift_mixer_ctl_name);
1699
1700 ret = -EINVAL;
1701 goto done;
1702 }
1703
1704 ALOGV("%s: Getting AV Timer vs Device Drift mixer ctrl name %s", __func__,
1705 avt_device_drift_mixer_ctl_name);
1706
1707 mixer_ctl_update(ctl);
1708 count = mixer_ctl_get_num_values(ctl);
1709 if (count != sizeof(struct audio_avt_device_drift_stats)) {
1710 ALOGE("%s: mixer_ctl_get_num_values() invalid drift_stats data size",
1711 __func__);
1712
1713 ret = -EINVAL;
1714 goto done;
1715 }
1716
1717 ret = mixer_ctl_get_array(ctl, (void *)&drift_stats, count);
1718 if (ret != 0) {
1719 ALOGE("%s: mixer_ctl_get_array() failed to get drift_stats Params",
1720 __func__);
1721
1722 ret = -EINVAL;
1723 goto done;
1724 }
1725 memcpy(drift_param, &drift_stats.drift_param,
1726 sizeof(struct audio_avt_device_drift_param));
1727done:
1728 return ret;
1729}
Manish Dewangan07de2142017-02-27 19:27:20 +05301730
1731#ifdef SNDRV_COMPRESS_PATH_DELAY
1732int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out)
1733{
1734 int ret = -EINVAL;
1735 struct snd_compr_metadata metadata;
1736 int delay_ms = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1737
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001738 if (property_get_bool("vendor.audio.playback.dsp.pathdelay", false)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05301739 ALOGD("%s:: Quering DSP delay %d",__func__, __LINE__);
1740 if (!(is_offload_usecase(out->usecase))) {
1741 ALOGE("%s:: not supported for non offload session", __func__);
1742 goto exit;
1743 }
1744
1745 if (!out->compr) {
1746 ALOGD("%s:: Invalid compress handle,returning default dsp latency",
1747 __func__);
1748 goto exit;
1749 }
1750
1751 metadata.key = SNDRV_COMPRESS_PATH_DELAY;
1752 ret = compress_get_metadata(out->compr, &metadata);
1753 if(ret) {
1754 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1755 goto exit;
1756 }
1757 delay_ms = metadata.value[0] / 1000; /*convert to ms*/
1758 } else {
1759 ALOGD("%s:: Using Fix DSP delay",__func__);
1760 }
1761
1762exit:
1763 ALOGD("%s:: delay in ms is %d",__func__, delay_ms);
1764 return delay_ms;
1765}
1766#else
1767int audio_extn_utils_compress_get_dsp_latency(struct stream_out *out __unused)
1768{
1769 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1770}
1771#endif
Manish Dewangan69426c82017-01-30 17:35:36 +05301772
1773#ifdef SNDRV_COMPRESS_RENDER_MODE
1774int audio_extn_utils_compress_set_render_mode(struct stream_out *out)
1775{
1776 struct snd_compr_metadata metadata;
1777 int ret = -EINVAL;
1778
1779 if (!(is_offload_usecase(out->usecase))) {
1780 ALOGE("%s:: not supported for non offload session", __func__);
1781 goto exit;
1782 }
1783
1784 if (!out->compr) {
1785 ALOGD("%s:: Invalid compress handle",
1786 __func__);
1787 goto exit;
1788 }
1789
1790 ALOGD("%s:: render mode %d", __func__, out->render_mode);
1791
1792 metadata.key = SNDRV_COMPRESS_RENDER_MODE;
1793 if (out->render_mode == RENDER_MODE_AUDIO_MASTER) {
1794 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER;
1795 } else if (out->render_mode == RENDER_MODE_AUDIO_STC_MASTER) {
1796 metadata.value[0] = SNDRV_COMPRESS_RENDER_MODE_STC_MASTER;
1797 } else {
1798 ret = 0;
1799 goto exit;
1800 }
1801 ret = compress_set_metadata(out->compr, &metadata);
1802 if(ret) {
1803 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1804 }
1805exit:
1806 return ret;
1807}
1808#else
1809int audio_extn_utils_compress_set_render_mode(struct stream_out *out __unused)
1810{
1811 ALOGD("%s:: configuring render mode not supported", __func__);
1812 return 0;
1813}
1814#endif
Manish Dewangan58229382017-02-02 15:48:41 +05301815
1816#ifdef SNDRV_COMPRESS_CLK_REC_MODE
1817int audio_extn_utils_compress_set_clk_rec_mode(
1818 struct audio_usecase *usecase)
1819{
1820 struct snd_compr_metadata metadata;
1821 struct stream_out *out = NULL;
1822 int ret = -EINVAL;
1823
Naresh Tanniru7586e292017-04-13 10:38:13 +05301824 if (usecase == NULL || usecase->type != PCM_PLAYBACK) {
Manish Dewangan58229382017-02-02 15:48:41 +05301825 ALOGE("%s:: Invalid use case", __func__);
1826 goto exit;
1827 }
1828
1829 out = usecase->stream.out;
1830 if (!out) {
1831 ALOGE("%s:: invalid stream", __func__);
1832 goto exit;
1833 }
1834
1835 if (!is_offload_usecase(out->usecase)) {
1836 ALOGE("%s:: not supported for non offload session", __func__);
1837 goto exit;
1838 }
1839
1840 if (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER) {
1841 ALOGD("%s:: clk recovery is only supported in STC render mode",
1842 __func__);
1843 ret = 0;
1844 goto exit;
1845 }
1846
1847 if (!out->compr) {
1848 ALOGD("%s:: Invalid compress handle",
1849 __func__);
1850 goto exit;
1851 }
1852 metadata.key = SNDRV_COMPRESS_CLK_REC_MODE;
1853 switch(usecase->out_snd_device) {
1854 case SND_DEVICE_OUT_HDMI:
1855 case SND_DEVICE_OUT_SPEAKER_AND_HDMI:
1856 case SND_DEVICE_OUT_DISPLAY_PORT:
1857 case SND_DEVICE_OUT_SPEAKER_AND_DISPLAY_PORT:
1858 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_NONE;
1859 break;
1860 default:
1861 metadata.value[0] = SNDRV_COMPRESS_CLK_REC_MODE_AUTO;
1862 break;
1863 }
1864
1865 ALOGD("%s:: clk recovery mode %d",__func__, metadata.value[0]);
1866
1867 ret = compress_set_metadata(out->compr, &metadata);
1868 if(ret) {
1869 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1870 }
1871
1872exit:
1873 return ret;
1874}
1875#else
1876int audio_extn_utils_compress_set_clk_rec_mode(
1877 struct audio_usecase *usecase __unused)
1878{
1879 ALOGD("%s:: configuring render mode not supported", __func__);
1880 return 0;
1881}
1882#endif
Manish Dewangan27346042017-03-01 12:56:12 +05301883
1884#ifdef SNDRV_COMPRESS_RENDER_WINDOW
1885int audio_extn_utils_compress_set_render_window(
1886 struct stream_out *out,
1887 struct audio_out_render_window_param *render_window)
1888{
1889 struct snd_compr_metadata metadata;
1890 int ret = -EINVAL;
1891
Manish Dewangan27346042017-03-01 12:56:12 +05301892 if(render_window == NULL) {
1893 ALOGE("%s:: Invalid render_window", __func__);
1894 goto exit;
1895 }
1896
Aniket Kumar Lata1e1d3662017-06-01 18:45:48 -07001897 ALOGD("%s:: render window start 0x%"PRIx64" end 0x%"PRIx64"",
1898 __func__,render_window->render_ws, render_window->render_we);
1899
Manish Dewangan27346042017-03-01 12:56:12 +05301900 if (!is_offload_usecase(out->usecase)) {
1901 ALOGE("%s:: not supported for non offload session", __func__);
1902 goto exit;
1903 }
1904
Naresh Tanniru6160c712017-04-17 15:43:48 +05301905 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1906 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan27346042017-03-01 12:56:12 +05301907 ALOGD("%s:: only supported in timestamp mode, current "
1908 "render mode mode %d", __func__, out->render_mode);
1909 goto exit;
1910 }
1911
1912 if (!out->compr) {
1913 ALOGW("%s:: offload session not yet opened,"
1914 "render window will be configure later", __func__);
1915 /* store render window to reconfigure in start_output_stream() */
1916 goto exit;
1917 }
1918
1919 metadata.key = SNDRV_COMPRESS_RENDER_WINDOW;
1920 /*render window start value */
1921 metadata.value[0] = 0xFFFFFFFF & render_window->render_ws; /* lsb */
1922 metadata.value[1] = \
1923 (0xFFFFFFFF00000000 & render_window->render_ws) >> 32; /* msb*/
1924 /*render window end value */
1925 metadata.value[2] = 0xFFFFFFFF & render_window->render_we; /* lsb */
1926 metadata.value[3] = \
1927 (0xFFFFFFFF00000000 & render_window->render_we) >> 32; /* msb*/
1928
1929 ret = compress_set_metadata(out->compr, &metadata);
1930 if(ret) {
1931 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1932 }
1933
1934exit:
1935 return ret;
1936}
1937#else
1938int audio_extn_utils_compress_set_render_window(
1939 struct stream_out *out __unused,
1940 struct audio_out_render_window_param *render_window __unused)
1941{
1942 ALOGD("%s:: configuring render window not supported", __func__);
1943 return 0;
1944}
1945#endif
Manish Dewangan14956cc2017-02-14 18:54:42 +05301946
1947#ifdef SNDRV_COMPRESS_START_DELAY
1948int audio_extn_utils_compress_set_start_delay(
1949 struct stream_out *out,
1950 struct audio_out_start_delay_param *delay_param)
1951{
1952 struct snd_compr_metadata metadata;
1953 int ret = -EINVAL;
1954
1955 if(delay_param == NULL) {
1956 ALOGE("%s:: Invalid delay_param", __func__);
1957 goto exit;
1958 }
1959
1960 ALOGD("%s:: render start delay 0x%"PRIx64" ", __func__,
1961 delay_param->start_delay);
1962
1963 if (!is_offload_usecase(out->usecase)) {
1964 ALOGE("%s:: not supported for non offload session", __func__);
1965 goto exit;
1966 }
1967
Naresh Tanniru6160c712017-04-17 15:43:48 +05301968 if ((out->render_mode != RENDER_MODE_AUDIO_MASTER) &&
1969 (out->render_mode != RENDER_MODE_AUDIO_STC_MASTER)) {
Manish Dewangan14956cc2017-02-14 18:54:42 +05301970 ALOGD("%s:: only supported in timestamp mode, current "
1971 "render mode mode %d", __func__, out->render_mode);
1972 goto exit;
1973 }
1974
1975 if (!out->compr) {
1976 ALOGW("%s:: offload session not yet opened,"
1977 "start delay will be configure later", __func__);
1978 goto exit;
1979 }
1980
1981 metadata.key = SNDRV_COMPRESS_START_DELAY;
1982 metadata.value[0] = 0xFFFFFFFF & delay_param->start_delay; /* lsb */
1983 metadata.value[1] = \
1984 (0xFFFFFFFF00000000 & delay_param->start_delay) >> 32; /* msb*/
1985
1986 ret = compress_set_metadata(out->compr, &metadata);
1987 if(ret) {
1988 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
1989 }
1990
1991exit:
1992 return ret;
1993}
1994#else
1995int audio_extn_utils_compress_set_start_delay(
1996 struct stream_out *out __unused,
1997 struct audio_out_start_delay_param *delay_param __unused)
1998{
1999 ALOGD("%s:: configuring render window not supported", __func__);
2000 return 0;
2001}
2002#endif
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002003
2004#define MAX_SND_CARD 8
2005#define RETRY_US 500000
2006#define RETRY_NUMBER 10
2007
2008int audio_extn_utils_get_snd_card_num()
2009{
2010
2011 void *hw_info = NULL;
2012 struct mixer *mixer = NULL;
2013 int retry_num = 0;
2014 int snd_card_num = 0;
2015 char* snd_card_name = NULL;
2016
2017 while (snd_card_num < MAX_SND_CARD) {
2018 mixer = mixer_open(snd_card_num);
2019
2020 while (!mixer && retry_num < RETRY_NUMBER) {
2021 usleep(RETRY_US);
2022 mixer = mixer_open(snd_card_num);
2023 retry_num++;
2024 }
2025
2026 if (!mixer) {
2027 ALOGE("%s: Unable to open the mixer card: %d", __func__,
2028 snd_card_num);
2029 retry_num = 0;
2030 snd_card_num++;
2031 continue;
2032 }
2033
2034 snd_card_name = strdup(mixer_get_name(mixer));
2035 if (!snd_card_name) {
2036 ALOGE("failed to allocate memory for snd_card_name\n");
2037 mixer_close(mixer);
2038 return -1;
2039 }
2040 ALOGD("%s: snd_card_name: %s", __func__, snd_card_name);
2041
2042 hw_info = hw_info_init(snd_card_name);
2043 if (hw_info) {
2044 ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
2045 break;
2046 }
2047 ALOGE("%s: Failed to init hardware info", __func__);
2048 retry_num = 0;
2049 snd_card_num++;
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002050
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302051 free(snd_card_name);
2052 snd_card_name = NULL;
2053
2054 mixer_close(mixer);
2055 mixer = NULL;
2056 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002057 if (snd_card_name)
2058 free(snd_card_name);
Dhananjay Kumara7e27832017-07-11 15:40:39 +05302059 if (mixer)
2060 mixer_close(mixer);
2061 if (hw_info)
2062 hw_info_deinit(hw_info);
Vignesh Kulothungan55396882017-04-20 14:37:02 -07002063
2064 if (snd_card_num >= MAX_SND_CARD) {
2065 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
2066 return -1;
2067 }
2068
2069 return snd_card_num;
2070}
Naresh Tanniru6160c712017-04-17 15:43:48 +05302071
2072#ifdef SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK
2073int audio_extn_utils_compress_enable_drift_correction(
2074 struct stream_out *out,
2075 struct audio_out_enable_drift_correction *drift)
2076{
2077 struct snd_compr_metadata metadata;
2078 int ret = -EINVAL;
2079
2080 if(drift == NULL) {
2081 ALOGE("%s:: Invalid param", __func__);
2082 goto exit;
2083 }
2084
2085 ALOGD("%s:: drift enable %d", __func__,drift->enable);
2086
2087 if (!is_offload_usecase(out->usecase)) {
2088 ALOGE("%s:: not supported for non offload session", __func__);
2089 goto exit;
2090 }
2091
2092 if (!out->compr) {
2093 ALOGW("%s:: offload session not yet opened,"
2094 "start delay will be configure later", __func__);
2095 goto exit;
2096 }
2097
2098 metadata.key = SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK;
2099 metadata.value[0] = drift->enable;
2100 out->drift_correction_enabled = drift->enable;
2101
2102 ret = compress_set_metadata(out->compr, &metadata);
2103 if(ret) {
2104 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2105 out->drift_correction_enabled = false;
2106 }
2107
2108exit:
2109 return ret;
2110}
2111#else
2112int audio_extn_utils_compress_enable_drift_correction(
2113 struct stream_out *out __unused,
2114 struct audio_out_enable_drift_correction *drift __unused)
2115{
2116 ALOGD("%s:: configuring drift enablement not supported", __func__);
2117 return 0;
2118}
2119#endif
2120
2121#ifdef SNDRV_COMPRESS_ADJUST_SESSION_CLOCK
2122int audio_extn_utils_compress_correct_drift(
2123 struct stream_out *out,
2124 struct audio_out_correct_drift *drift_param)
2125{
2126 struct snd_compr_metadata metadata;
2127 int ret = -EINVAL;
2128
2129 if (drift_param == NULL) {
2130 ALOGE("%s:: Invalid drift_param", __func__);
2131 goto exit;
2132 }
2133
2134 ALOGD("%s:: adjust time 0x%"PRIx64" ", __func__,
2135 drift_param->adjust_time);
2136
2137 if (!is_offload_usecase(out->usecase)) {
2138 ALOGE("%s:: not supported for non offload session", __func__);
2139 goto exit;
2140 }
2141
2142 if (!out->compr) {
2143 ALOGW("%s:: offload session not yet opened", __func__);
2144 goto exit;
2145 }
2146
2147 if (!out->drift_correction_enabled) {
2148 ALOGE("%s:: drift correction not enabled", __func__);
2149 goto exit;
2150 }
2151
2152 metadata.key = SNDRV_COMPRESS_ADJUST_SESSION_CLOCK;
2153 metadata.value[0] = 0xFFFFFFFF & drift_param->adjust_time; /* lsb */
2154 metadata.value[1] = \
2155 (0xFFFFFFFF00000000 & drift_param->adjust_time) >> 32; /* msb*/
2156
2157 ret = compress_set_metadata(out->compr, &metadata);
2158 if(ret)
2159 ALOGE("%s::error %s", __func__, compress_get_error(out->compr));
2160exit:
2161 return ret;
2162}
2163#else
2164int audio_extn_utils_compress_correct_drift(
2165 struct stream_out *out __unused,
2166 struct audio_out_correct_drift *drift_param __unused)
2167{
2168 ALOGD("%s:: setting adjust clock not supported", __func__);
2169 return 0;
2170}
2171#endif
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05302172
2173int audio_extn_utils_set_channel_map(
2174 struct stream_out *out,
2175 struct audio_out_channel_map_param *channel_map_param)
2176{
2177 int ret = -EINVAL, i = 0;
2178 int channels = audio_channel_count_from_out_mask(out->channel_mask);
2179
2180 if (channel_map_param == NULL) {
2181 ALOGE("%s:: Invalid channel_map", __func__);
2182 goto exit;
2183 }
2184
2185 if (channel_map_param->channels != channels) {
2186 ALOGE("%s:: Channels(%d) does not match stream channels(%d)",
2187 __func__, channel_map_param->channels, channels);
2188 goto exit;
2189 }
2190
2191 for ( i = 0; i < channels; i++) {
2192 ALOGV("%s:: channel_map[%d]- %d", __func__, i, channel_map_param->channel_map[i]);
2193 out->channel_map_param.channel_map[i] = channel_map_param->channel_map[i];
2194 }
2195 ret = 0;
2196exit:
2197 return ret;
2198}