blob: 989eb52a2da8ac401ebad5e15444a2e3fd3daf11 [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 * 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"
36
37#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
38
39#define OUTPUTS_TAG "outputs"
40
41#define DYNAMIC_VALUE_TAG "dynamic"
42#define FLAGS_TAG "flags"
43#define FORMATS_TAG "formats"
44#define SAMPLING_RATES_TAG "sampling_rates"
45#define BIT_WIDTH_TAG "bit_width"
46#define APP_TYPE_TAG "app_type"
47
48#define STRING_TO_ENUM(string) { #string, string }
49#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
50
51struct string_to_enum {
52 const char *name;
53 uint32_t value;
54};
55
56const struct string_to_enum s_flag_name_to_enum_table[] = {
57 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
58 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
59 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
60 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
61 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
62 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
63#ifdef INCALL_MUSIC_ENABLED
64 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
65#endif
66#ifdef COMPRESS_VOIP_ENABLED
67 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_VOIP_RX),
68#endif
69};
70
71const struct string_to_enum s_format_name_to_enum_table[] = {
72 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
73 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
74 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
75 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
76 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -070077 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
78 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070079 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -070080 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
81#ifdef FORMATS_ENABLED
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070082 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
83 STRING_TO_ENUM(AUDIO_FORMAT_DTS_LBR),
84 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
85 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
86 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070087 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
88 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
89 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
90 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
91 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
92 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
93 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
94 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT_OFFLOAD),
95 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_OFFLOAD),
Amit Shekhar6f461b12014-08-01 14:52:58 -070096 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070097#endif
98};
99
100static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
101 const char *name)
102{
103 size_t i;
104 for (i = 0; i < size; i++) {
105 if (strcmp(table[i].name, name) == 0) {
106 ALOGV("%s found %s", __func__, table[i].name);
107 return table[i].value;
108 }
109 }
110 return 0;
111}
112
113static audio_output_flags_t parse_flag_names(char *name)
114{
115 uint32_t flag = 0;
116 char *flag_name = strtok(name, "|");
117 while (flag_name != NULL) {
118 if (strlen(flag_name) != 0) {
119 flag |= string_to_enum(s_flag_name_to_enum_table,
120 ARRAY_SIZE(s_flag_name_to_enum_table),
121 flag_name);
122 }
123 flag_name = strtok(NULL, "|");
124 }
125
126 ALOGV("parse_flag_names: flag - %d", flag);
127 return (audio_output_flags_t)flag;
128}
129
130static void parse_format_names(char *name, struct streams_output_cfg *so_info)
131{
132 struct stream_format *sf_info = NULL;
133 char *str = strtok(name, "|");
134
135 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
136 return;
137
138 list_init(&so_info->format_list);
139 while (str != NULL) {
140 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
141 ARRAY_SIZE(s_format_name_to_enum_table), str);
142 ALOGV("%s: format - %d", __func__, format);
143 if (format != 0) {
144 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700145 if (sf_info == NULL)
146 break; /* return whatever was parsed */
147
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700148 sf_info->format = format;
149 list_add_tail(&so_info->format_list, &sf_info->list);
150 }
151 str = strtok(NULL, "|");
152 }
153}
154
Amit Shekhar6f461b12014-08-01 14:52:58 -0700155static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700156{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700157 struct stream_sample_rate *ss_info = NULL;
158 uint32_t sample_rate = 48000;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700159 char *str = strtok(name, "|");
160
Amit Shekhar6f461b12014-08-01 14:52:58 -0700161 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
162 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700163
Amit Shekhar6f461b12014-08-01 14:52:58 -0700164 list_init(&so_info->sample_rate_list);
165 while (str != NULL) {
166 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
167 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
168 if (0 != sample_rate) {
169 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
170 ss_info->sample_rate = sample_rate;
171 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
172 }
173 str = strtok(NULL, "|");
174 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700175}
176
177static int parse_bit_width_names(char *name)
178{
179 int bit_width = 16;
180 char *str = strtok(name, "|");
181
182 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
183 bit_width = (int)strtol(str, (char **)NULL, 10);
184
185 ALOGV("%s: bit_width - %d", __func__, bit_width);
186 return bit_width;
187}
188
189static int parse_app_type_names(void *platform, char *name)
190{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700191 int app_type = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700192 char *str = strtok(name, "|");
193
194 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
195 app_type = (int)strtol(str, (char **)NULL, 10);
196
197 ALOGV("%s: app_type - %d", __func__, app_type);
198 return app_type;
199}
200
201static void update_streams_output_cfg_list(cnode *root, void *platform,
202 struct listnode *streams_output_cfg_list)
203{
204 cnode *node = root->first_child;
205 struct streams_output_cfg *so_info;
206
207 ALOGV("%s", __func__);
208 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700209
210 if (!so_info) {
211 ALOGE("failed to allocate mem for so_info list element");
212 return;
213 }
214
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700215 while (node) {
216 if (strcmp(node->name, FLAGS_TAG) == 0) {
217 so_info->flags = parse_flag_names((char *)node->value);
218 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
219 parse_format_names((char *)node->value, so_info);
220 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700221 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
222 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700223 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
224 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
225 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
226 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
227 }
228 node = node->next;
229 }
230 list_add_tail(streams_output_cfg_list, &so_info->list);
231}
232
233static void load_output(cnode *root, void *platform,
234 struct listnode *streams_output_cfg_list)
235{
236 cnode *node = config_find(root, OUTPUTS_TAG);
237 if (node == NULL) {
238 ALOGE("%s: could not load output, node is NULL", __func__);
239 return;
240 }
241
242 node = node->first_child;
243 while (node) {
244 ALOGV("%s: loading output %s", __func__, node->name);
245 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
246 node = node->next;
247 }
248}
249
250static void send_app_type_cfg(void *platform, struct mixer *mixer,
251 struct listnode *streams_output_cfg_list)
252{
253 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
254 int length = 0, i, num_app_types = 0;
255 struct listnode *node;
256 bool update;
257 struct mixer_ctl *ctl = NULL;
258 const char *mixer_ctl_name = "App Type Config";
259 struct streams_output_cfg *so_info;
260
261 if (!mixer) {
262 ALOGE("%s: mixer is null",__func__);
263 return;
264 }
265 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
266 if (!ctl) {
267 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
268 return;
269 }
270 if (streams_output_cfg_list == NULL) {
271 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700272 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700273 app_type_cfg[length++] = 48000;
274 app_type_cfg[length++] = 16;
275 mixer_ctl_set_array(ctl, app_type_cfg, length);
276 return;
277 }
278
279 app_type_cfg[length++] = num_app_types;
280 list_for_each(node, streams_output_cfg_list) {
281 so_info = node_to_item(node, struct streams_output_cfg, list);
282 update = true;
283 for (i=0; i<length; i=i+3) {
284 if (app_type_cfg[i+1] == -1)
285 break;
286 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
287 update = false;
288 break;
289 }
290 }
291 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
292 num_app_types += 1 ;
293 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
294 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
295 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
296 }
297 }
298 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
299 if (num_app_types) {
300 app_type_cfg[0] = num_app_types;
301 mixer_ctl_set_array(ctl, app_type_cfg, length);
302 }
303}
304
305void audio_extn_utils_update_streams_output_cfg_list(void *platform,
306 struct mixer *mixer,
307 struct listnode *streams_output_cfg_list)
308{
309 cnode *root;
310 char *data;
311
312 ALOGV("%s", __func__);
313 list_init(streams_output_cfg_list);
314 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
315 if (data == NULL) {
316 send_app_type_cfg(platform, mixer, NULL);
317 ALOGE("%s: could not load output policy config file", __func__);
318 return;
319 }
320
321 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700322 if (root == NULL) {
323 ALOGE("cfg_list, NULL config root");
324 return;
325 }
326
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700327 config_load(root, data);
328 load_output(root, platform, streams_output_cfg_list);
329
330 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
331}
332
333void audio_extn_utils_dump_streams_output_cfg_list(
334 struct listnode *streams_output_cfg_list)
335{
336 int i=0;
337 struct listnode *node_i, *node_j;
338 struct streams_output_cfg *so_info;
339 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700340 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341 ALOGV("%s", __func__);
342 list_for_each(node_i, streams_output_cfg_list) {
343 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700344 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
345 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700346 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
347 list_for_each(node_j, &so_info->format_list) {
348 sf_info = node_to_item(node_j, struct stream_format, list);
349 ALOGV("format-%x", sf_info->format);
350 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700351 list_for_each(node_j, &so_info->sample_rate_list) {
352 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
353 ALOGV("sample rate-%d", ss_info->sample_rate);
354 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700355 }
356}
357
358void audio_extn_utils_release_streams_output_cfg_list(
359 struct listnode *streams_output_cfg_list)
360{
361 struct listnode *node_i, *node_j;
362 struct streams_output_cfg *so_info;
363 struct stream_format *sf_info;
364
365 ALOGV("%s", __func__);
366 while (!list_empty(streams_output_cfg_list)) {
367 node_i = list_head(streams_output_cfg_list);
368 so_info = node_to_item(node_i, struct streams_output_cfg, list);
369 while (!list_empty(&so_info->format_list)) {
370 node_j = list_head(&so_info->format_list);
371 list_remove(node_j);
372 free(node_to_item(node_j, struct stream_format, list));
373 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700374 while (!list_empty(&so_info->sample_rate_list)) {
375 node_j = list_head(&so_info->sample_rate_list);
376 list_remove(node_j);
377 free(node_to_item(node_j, struct stream_sample_rate, list));
378 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379 list_remove(node_i);
380 free(node_to_item(node_i, struct streams_output_cfg, list));
381 }
382}
383
Amit Shekhar6f461b12014-08-01 14:52:58 -0700384static bool set_output_cfg(struct streams_output_cfg *so_info,
385 struct stream_app_type_cfg *app_type_cfg,
386 uint32_t sample_rate, uint32_t bit_width)
387 {
388 struct listnode *node_i;
389 struct stream_sample_rate *ss_info;
390 list_for_each(node_i, &so_info->sample_rate_list) {
391 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
392 if ((sample_rate <= ss_info->sample_rate) &&
393 (bit_width == so_info->app_type_cfg.bit_width)) {
394 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
395 app_type_cfg->sample_rate = ss_info->sample_rate;
396 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
397 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
398 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
399 return true;
400 }
401 }
402 /*
403 * Reiterate through the list assuming dafault sample rate.
404 * Handles scenario where input sample rate is higher
405 * than all sample rates in list for the input bit width.
406 */
407 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
408 list_for_each(node_i, &so_info->sample_rate_list) {
409 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
410 if ((sample_rate <= ss_info->sample_rate) &&
411 (bit_width == so_info->app_type_cfg.bit_width)) {
412 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
413 app_type_cfg->sample_rate = sample_rate;
414 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
415 ALOGV("%s Assuming default sample rate. app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
416 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
417 return true;
418 }
419 }
420 return false;
421}
422
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700423void audio_extn_utils_update_stream_app_type_cfg(void *platform,
424 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700425 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700426 audio_output_flags_t flags,
427 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700428 uint32_t sample_rate,
429 uint32_t bit_width,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700430 struct stream_app_type_cfg *app_type_cfg)
431{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700432 struct listnode *node_i, *node_j, *node_k;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700433 struct streams_output_cfg *so_info;
434 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700435 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700436
Amit Shekhar1d896042014-10-03 13:16:09 -0700437 if ((24 == bit_width) &&
438 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700439 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
440 if (-ENOSYS != bw)
441 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700442 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
443 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
444 }
445
446 ALOGV("%s: flags: %x, format: %x sample_rate %d",
447 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700448 list_for_each(node_i, streams_output_cfg_list) {
449 so_info = node_to_item(node_i, struct streams_output_cfg, list);
450 if (so_info->flags == flags) {
451 list_for_each(node_j, &so_info->format_list) {
452 sf_info = node_to_item(node_j, struct stream_format, list);
453 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700454 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
455 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700456 }
457 }
458 }
459 }
460 list_for_each(node_i, streams_output_cfg_list) {
461 so_info = node_to_item(node_i, struct streams_output_cfg, list);
462 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
463 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700464 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
465 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
466 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700467 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
468 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700469 return;
470 }
471 }
472 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700473 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700474 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700475 app_type_cfg->bit_width = 16;
476}
477
478int audio_extn_utils_send_app_type_cfg(struct audio_usecase *usecase)
479{
480 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
481 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Anish Kumarc8599f22014-07-20 09:36:07 -0700482 struct stream_out *out;
483 struct audio_device *adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700484 struct mixer_ctl *ctl;
485 int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530486 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700487
488 ALOGV("%s", __func__);
489
490 if (usecase->type != PCM_PLAYBACK) {
491 ALOGV("%s: not a playback path, no need to cfg app type", __func__);
492 rc = 0;
493 goto exit_send_app_type_cfg;
494 }
495 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
496 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
497 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Alexy Joseph2f89cfa2014-10-06 12:15:01 -0700498 (!is_offload_usecase(usecase->id))) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700499 ALOGV("%s: a playback path where app type cfg is not required", __func__);
500 rc = 0;
501 goto exit_send_app_type_cfg;
502 }
Anish Kumarc8599f22014-07-20 09:36:07 -0700503 out = usecase->stream.out;
504 adev = out->dev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700505
506 snd_device = usecase->out_snd_device;
507
508 pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
509
510 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
511 "Audio Stream %d App Type Cfg", pcm_device_id);
512
513 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
514 if (!ctl) {
515 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
516 mixer_ctl_name);
517 rc = -EINVAL;
518 goto exit_send_app_type_cfg;
519 }
520 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
521 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
522 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
523 if (acdb_dev_id < 0) {
524 ALOGE("%s: Couldn't get the acdb dev id", __func__);
525 rc = -EINVAL;
526 goto exit_send_app_type_cfg;
527 }
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530528
529 if ((24 == usecase->stream.out->bit_width) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700530 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530531 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
532 } else {
533 sample_rate = out->app_type_cfg.sample_rate;
534 }
535
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700536 app_type_cfg[len++] = out->app_type_cfg.app_type;
537 app_type_cfg[len++] = acdb_dev_id;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530538 app_type_cfg[len++] = sample_rate;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700539
540 mixer_ctl_set_array(ctl, app_type_cfg, len);
Amit Shekhar278e3362014-09-08 14:08:19 -0700541 ALOGI("%s app_type %d, acdb_dev_id %d, sample_rate %d",
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530542 __func__, out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700543 rc = 0;
544exit_send_app_type_cfg:
545 return rc;
546}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700547
548void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
549 struct audio_usecase *usecase)
550{
551 int type = usecase->type;
552
553 if (type == PCM_PLAYBACK) {
554 struct stream_out *out = usecase->stream.out;
555 int snd_device = usecase->out_snd_device;
556 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
557 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
558 platform_send_audio_calibration(adev->platform, usecase->out_snd_device,
559 out->app_type_cfg.app_type,
560 out->app_type_cfg.sample_rate);
561 }
562 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
563 /* when app type is default. the sample rate is not used to send cal */
564 platform_send_audio_calibration(adev->platform, usecase->in_snd_device,
565 platform_get_default_app_type(adev->platform),
566 48000);
567 }
568}
569