blob: 04a6c21a2dbdfdc4aeddedcb62b267d50abf1d9f [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003 * Not a Contribution.
4 *
Shiv Maliyappanahalli8911f282014-01-10 15:56:19 -08005 * Copyright (C) 2013 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006 *
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_primary"
Steve Kondik5a447012014-12-02 16:04:20 -080021//#define LOG_NDEBUG 0
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070022/*#define VERY_VERY_VERBOSE_LOGGING*/
23#ifdef VERY_VERY_VERBOSE_LOGGING
24#define ALOGVV ALOGV
25#else
26#define ALOGVV(a...) do { } while(0)
27#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080028#include <errno.h>
29#include <pthread.h>
30#include <stdint.h>
31#include <sys/time.h>
32#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080033#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070034#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070035#include <sys/resource.h>
36#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080037
38#include <cutils/log.h>
39#include <cutils/str_parms.h>
40#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070041#include <cutils/atomic.h>
42#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080043
Eric Laurentb23d5282013-05-14 15:27:20 -070044#include <hardware/audio_effect.h>
Steve Kondik3abbbc82014-11-29 14:14:43 -080045#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070050#include "platform_api.h"
51#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070052#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080053#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include "sound/compress_params.h"
ApurupaPattapu0c566872014-01-10 14:46:02 -080056#include "sound/asound.h"
57
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +053058#ifdef USES_AUDIO_AMPLIFIER
59#include <audio_amplifier.h>
60#endif
61
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070062#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
63/* ToDo: Check and update a proper value in msec */
Alexy Joseph62babb92015-03-03 19:01:03 -080064#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 50
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070065#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
66
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +053067#define PROXY_OPEN_RETRY_COUNT 100
68#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapu0c566872014-01-10 14:46:02 -080069
Steve Kondikb045f472014-11-12 23:24:07 -080070#ifdef LOW_LATENCY_PRIMARY
71#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_LOW_LATENCY
72#else
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080073#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
Steve Kondikb045f472014-11-12 23:24:07 -080074#endif
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080075
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -070076static unsigned int configured_low_latency_capture_period_size =
77 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
78
Steve Kondik3abbbc82014-11-29 14:14:43 -080079/* This constant enables extended precision handling.
Steve Kondik3abbbc82014-11-29 14:14:43 -080080 */
81static const bool k_enable_extended_precision = true;
82
Eric Laurentb23d5282013-05-14 15:27:20 -070083struct pcm_config pcm_config_deep_buffer = {
84 .channels = 2,
85 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
86 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
87 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
88 .format = PCM_FORMAT_S16_LE,
89 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
90 .stop_threshold = INT_MAX,
91 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
92};
93
94struct pcm_config pcm_config_low_latency = {
95 .channels = 2,
96 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
97 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
98 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
99 .format = PCM_FORMAT_S16_LE,
100 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
101 .stop_threshold = INT_MAX,
102 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
103};
104
105struct pcm_config pcm_config_hdmi_multi = {
106 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
107 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
108 .period_size = HDMI_MULTI_PERIOD_SIZE,
109 .period_count = HDMI_MULTI_PERIOD_COUNT,
110 .format = PCM_FORMAT_S16_LE,
111 .start_threshold = 0,
112 .stop_threshold = INT_MAX,
113 .avail_min = 0,
114};
115
116struct pcm_config pcm_config_audio_capture = {
117 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700118 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
119 .format = PCM_FORMAT_S16_LE,
120};
121
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530122#define AFE_PROXY_CHANNEL_COUNT 2
123#define AFE_PROXY_SAMPLING_RATE 48000
124
125#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
126#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
127
128struct pcm_config pcm_config_afe_proxy_playback = {
129 .channels = AFE_PROXY_CHANNEL_COUNT,
130 .rate = AFE_PROXY_SAMPLING_RATE,
131 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
132 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
133 .format = PCM_FORMAT_S16_LE,
134 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
135 .stop_threshold = INT_MAX,
136 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
137};
138
139#define AFE_PROXY_RECORD_PERIOD_SIZE 768
140#define AFE_PROXY_RECORD_PERIOD_COUNT 4
141
142struct pcm_config pcm_config_afe_proxy_record = {
143 .channels = AFE_PROXY_CHANNEL_COUNT,
144 .rate = AFE_PROXY_SAMPLING_RATE,
145 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
146 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
147 .format = PCM_FORMAT_S16_LE,
148 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
149 .stop_threshold = INT_MAX,
150 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
151};
152
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800153const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700154 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
155 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
156 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700157 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700158#ifdef MULTIPLE_OFFLOAD_ENABLED
159 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
160 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
161 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
162 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
163 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
164 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
165 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
166 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
167#endif
Eric Laurentb23d5282013-05-14 15:27:20 -0700168 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700169 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700170 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700171 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700172 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800173 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800174 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700175 [USECASE_VOICE_CALL] = "voice-call",
Mingming Yinee733602014-04-03 17:47:22 -0700176
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700177 [USECASE_VOICE2_CALL] = "voice2-call",
178 [USECASE_VOLTE_CALL] = "volte-call",
179 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat111aeb32014-02-12 17:58:59 -0800180 [USECASE_VOWLAN_CALL] = "vowlan-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800181 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700182 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
183 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
184 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800185 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
186 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
187 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
188
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700189 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
190 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700191 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
192 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530193
194 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
195 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700196};
197
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700198static const audio_usecase_t offload_usecases[] = {
199 USECASE_AUDIO_PLAYBACK_OFFLOAD,
200#ifdef MULTIPLE_OFFLOAD_ENABLED
201 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
202 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
Steve Kondikac72feb2014-07-20 23:15:34 -0700203#ifndef PLATFORM_MSM8974
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700204 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
205 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
206 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
207 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
Steve Kondikac72feb2014-07-20 23:15:34 -0700208#endif
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700209 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
Steve Kondikac72feb2014-07-20 23:15:34 -0700210#ifndef PLATFORM_MSM8974
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700211 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
212#endif
Steve Kondikac72feb2014-07-20 23:15:34 -0700213#endif
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700214};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800215
216#define STRING_TO_ENUM(string) { #string, string }
217
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800218struct string_to_enum {
219 const char *name;
220 uint32_t value;
221};
222
223static const struct string_to_enum out_channels_name_to_enum_table[] = {
224 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
225 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
226 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
227};
228
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700229static struct audio_device *adev = NULL;
230static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700231static unsigned int audio_device_ref_count;
232
Haynes Mathew George5191a852013-09-11 14:19:36 -0700233static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -0800234
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -0800235static int check_and_set_gapless_mode(struct audio_device *adev) {
236
237
238 char value[PROPERTY_VALUE_MAX] = {0};
239 bool gapless_enabled = false;
240 const char *mixer_ctl_name = "Compress Gapless Playback";
241 struct mixer_ctl *ctl;
242
243 ALOGV("%s:", __func__);
244 property_get("audio.offload.gapless.enabled", value, NULL);
245 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
246
247 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
248 if (!ctl) {
249 ALOGE("%s: Could not get ctl for mixer cmd - %s",
250 __func__, mixer_ctl_name);
251 return -EINVAL;
252 }
253
254 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
255 ALOGE("%s: Could not set gapless mode %d",
256 __func__, gapless_enabled);
257 return -EINVAL;
258 }
259 return 0;
260}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700261
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700262static bool is_supported_format(audio_format_t format)
263{
Steve Kondik3fdf4fc2014-12-10 21:15:43 -0800264 switch (format) {
Steve Kondik3abbbc82014-11-29 14:14:43 -0800265 case AUDIO_FORMAT_MP3:
Steve Kondik3fdf4fc2014-12-10 21:15:43 -0800266 case AUDIO_FORMAT_PCM_16_BIT_OFFLOAD:
267 case AUDIO_FORMAT_PCM_24_BIT_OFFLOAD:
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700268#ifdef FLAC_OFFLOAD_ENABLED
Steve Kondik3abbbc82014-11-29 14:14:43 -0800269 case AUDIO_FORMAT_FLAC:
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700270#endif
Steve Kondik3abbbc82014-11-29 14:14:43 -0800271#ifdef WMA_OFFLOAD_ENABLED
272 case AUDIO_FORMAT_WMA:
273 case AUDIO_FORMAT_WMA_PRO:
274#endif
275#ifdef MP2_OFFLOAD_ENABLED
276 case AUDIO_FORMAT_MP2:
277#endif
278 case AUDIO_FORMAT_AAC_LC:
279 case AUDIO_FORMAT_AAC_HE_V1:
280 case AUDIO_FORMAT_AAC_HE_V2:
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530281 return true;
Steve Kondik3abbbc82014-11-29 14:14:43 -0800282
Steve Kondik3fdf4fc2014-12-10 21:15:43 -0800283 default:
284 return false;
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530285 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700286 return false;
287}
288
289static int get_snd_codec_id(audio_format_t format)
290{
291 int id = 0;
292
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530293 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700294 case AUDIO_FORMAT_MP3:
295 id = SND_AUDIOCODEC_MP3;
296 break;
297 case AUDIO_FORMAT_AAC:
298 id = SND_AUDIOCODEC_AAC;
299 break;
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530300 case AUDIO_FORMAT_PCM_OFFLOAD:
ApurupaPattapu0c566872014-01-10 14:46:02 -0800301 id = SND_AUDIOCODEC_PCM;
302 break;
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700303#ifdef FLAC_OFFLOAD_ENABLED
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800304 case AUDIO_FORMAT_FLAC:
305 id = SND_AUDIOCODEC_FLAC;
306 break;
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700307#endif
Ethan Chenc856d972014-07-25 14:57:15 -0700308#ifdef WMA_OFFLOAD_ENABLED
Steve Kondikba3b35d2014-07-18 01:49:48 -0700309 case AUDIO_FORMAT_WMA:
310 id = SND_AUDIOCODEC_WMA;
311 break;
312 case AUDIO_FORMAT_WMA_PRO:
313 id = SND_AUDIOCODEC_WMA_PRO;
314 break;
Ethan Chenc856d972014-07-25 14:57:15 -0700315#endif
316#ifdef MP2_OFFLOAD_ENABLED
Steve Kondikba3b35d2014-07-18 01:49:48 -0700317 case AUDIO_FORMAT_MP2:
318 id = SND_AUDIOCODEC_MP2;
319 break;
Ethan Chenc856d972014-07-25 14:57:15 -0700320#endif
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700321 default:
Mingming Yin90310102013-11-13 16:57:00 -0800322 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700323 }
324
325 return id;
326}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800327
Avinash Vaish8005dc82014-07-24 15:36:33 +0530328static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
329 struct audio_usecase *uc_info)
330{
331 struct listnode *node;
332 struct audio_usecase *usecase;
333
334 if (uc_info == NULL)
335 return -EINVAL;
336
337 /* Re-route all voice usecases on the shared backend other than the
338 specified usecase to new snd devices */
339 list_for_each(node, &adev->usecase_list) {
340 usecase = node_to_item(node, struct audio_usecase, list);
341 if ((usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) &&
342 (usecase != uc_info))
343 enable_audio_route(adev, usecase);
344 }
345 return 0;
346}
347
Venkata Narendra Kumar Guttac9e9e2c2014-07-09 16:29:28 +0530348int get_snd_card_state(struct audio_device *adev)
Naresh Tannirucef332d2014-06-04 18:17:56 +0530349{
350 int snd_scard_state;
351
352 if (!adev)
353 return SND_CARD_STATE_OFFLINE;
354
355 pthread_mutex_lock(&adev->snd_card_status.lock);
356 snd_scard_state = adev->snd_card_status.state;
357 pthread_mutex_unlock(&adev->snd_card_status.lock);
358
359 return snd_scard_state;
360}
361
362static int set_snd_card_state(struct audio_device *adev, int snd_scard_state)
363{
364 if (!adev)
365 return -ENOSYS;
366
367 pthread_mutex_lock(&adev->snd_card_status.lock);
368 adev->snd_card_status.state = snd_scard_state;
369 pthread_mutex_unlock(&adev->snd_card_status.lock);
370
371 return 0;
372}
373
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -0700374int pcm_ioctl(struct pcm *pcm, int request, ...)
375{
376 va_list ap;
377 void * arg;
378 int pcm_fd = *(int*)pcm;
379
380 va_start(ap, request);
381 arg = va_arg(ap, void *);
382 va_end(ap);
383
384 return ioctl(pcm_fd, request, arg);
385}
386
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700387int enable_audio_route(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700388 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800389{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700390 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700391 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800392
393 if (usecase == NULL)
394 return -EINVAL;
395
396 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
397
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800398 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700399 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800400 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700401 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800402
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -0800403#ifdef DS1_DOLBY_DAP_ENABLED
404 audio_extn_dolby_set_dmid(adev);
405 audio_extn_dolby_set_endpoint(adev);
406#endif
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800407 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700408 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700409 ALOGV("%s: apply mixer and update path: %s", __func__, mixer_path);
410 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800411 ALOGV("%s: exit", __func__);
412 return 0;
413}
414
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700415int disable_audio_route(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700416 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800417{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700418 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700419 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800420
421 if (usecase == NULL)
422 return -EINVAL;
423
424 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700425 if (usecase->type == PCM_CAPTURE)
426 snd_device = usecase->in_snd_device;
427 else
428 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800429 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700430 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700431 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
432 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800433 ALOGV("%s: exit", __func__);
434 return 0;
435}
436
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700437int enable_snd_device(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700438 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800439{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700440 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
441
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800442 if (snd_device < SND_DEVICE_MIN ||
443 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800444 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800445 return -EINVAL;
446 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700447
448 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700449
450 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
451 ALOGE("%s: Invalid sound device returned", __func__);
452 return -EINVAL;
453 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700454 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700455 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700456 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700457 return 0;
458 }
459
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700460 /* start usb playback thread */
461 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
462 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
463 audio_extn_usb_start_playback(adev);
464
465 /* start usb capture thread */
466 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
467 audio_extn_usb_start_capture(adev);
468
Tony Layher692f19a2015-01-03 13:16:45 -0500469 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
470 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700471 audio_extn_spkr_prot_is_enabled()) {
472 if (audio_extn_spkr_prot_start_processing(snd_device)) {
473 ALOGE("%s: spkr_start_processing failed", __func__);
474 return -EINVAL;
475 }
476 } else {
477 ALOGV("%s: snd_device(%d: %s)", __func__,
Steve Kondik6288f9e2014-12-27 13:36:30 -0800478 snd_device, device_name);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700479 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
480 adev->snd_dev_ref_cnt[snd_device]--;
481 return -EINVAL;
482 }
Steve Kondik6288f9e2014-12-27 13:36:30 -0800483 audio_extn_listen_update_status(snd_device,
484 LISTEN_EVENT_SND_DEVICE_BUSY);
Kiran Kandide144c82013-11-20 15:58:32 -0800485
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700486 audio_route_apply_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800487 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800488 return 0;
489}
490
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700491int disable_snd_device(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700492 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800493{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700494 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
495
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800496 if (snd_device < SND_DEVICE_MIN ||
497 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800498 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800499 return -EINVAL;
500 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700501 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
502 ALOGE("%s: device ref cnt is already 0", __func__);
503 return -EINVAL;
504 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700505
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700506 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700507
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700508 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
509 ALOGE("%s: Invalid sound device returned", __func__);
510 return -EINVAL;
511 }
512
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700513 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700514 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700515 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800516 /* exit usb play back thread */
517 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
518 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
519 audio_extn_usb_stop_playback();
520
521 /* exit usb capture thread */
522 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
Rajshekar Eashwarappa4dfa0752014-10-02 20:18:20 +0530523 audio_extn_usb_stop_capture();
Tony Layher692f19a2015-01-03 13:16:45 -0500524
525 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
526 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700527 audio_extn_spkr_prot_is_enabled()) {
528 audio_extn_spkr_prot_stop_processing();
529 } else
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700530 audio_route_reset_and_update_path(adev->audio_route, device_name);
Kiran Kandide144c82013-11-20 15:58:32 -0800531
Steve Kondik6288f9e2014-12-27 13:36:30 -0800532 audio_extn_listen_update_status(snd_device,
Kiran Kandide144c82013-11-20 15:58:32 -0800533 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700534 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700535
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800536 return 0;
537}
538
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700539static void check_usecases_codec_backend(struct audio_device *adev,
540 struct audio_usecase *uc_info,
541 snd_device_t snd_device)
542{
543 struct listnode *node;
544 struct audio_usecase *usecase;
545 bool switch_device[AUDIO_USECASE_MAX];
546 int i, num_uc_to_switch = 0;
547
548 /*
549 * This function is to make sure that all the usecases that are active on
550 * the hardware codec backend are always routed to any one device that is
551 * handled by the hardware codec.
552 * For example, if low-latency and deep-buffer usecases are currently active
553 * on speaker and out_set_parameters(headset) is received on low-latency
554 * output, then we have to make sure deep-buffer is also switched to headset,
555 * because of the limitation that both the devices cannot be enabled
556 * at the same time as they share the same backend.
557 */
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800558 /*
559 * This call is to check if we need to force routing for a particular stream
560 * If there is a backend configuration change for the device when a
561 * new stream starts, then ADM needs to be closed and re-opened with the new
562 * configuraion. This call check if we need to re-route all the streams
563 * associated with the backend. Touch tone + 24 bit playback.
564 */
565 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info);
566
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700567 /* Disable all the usecases on the shared backend other than the
568 specified usecase */
569 for (i = 0; i < AUDIO_USECASE_MAX; i++)
570 switch_device[i] = false;
571
572 list_for_each(node, &adev->usecase_list) {
573 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800574 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700575 usecase != uc_info &&
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800576 (usecase->out_snd_device != snd_device || force_routing) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700577 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
578 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
579 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700580 platform_get_snd_device_name(usecase->out_snd_device));
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700581 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700582 switch_device[usecase->id] = true;
583 num_uc_to_switch++;
584 }
585 }
586
587 if (num_uc_to_switch) {
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700588 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700589
590 list_for_each(node, &adev->usecase_list) {
591 usecase = node_to_item(node, struct audio_usecase, list);
592 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700593 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700594 }
595 }
596
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700597 list_for_each(node, &adev->usecase_list) {
598 usecase = node_to_item(node, struct audio_usecase, list);
599 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700600 enable_snd_device(adev, snd_device);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700601 }
602 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700603
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700604 /* Re-route all the usecases on the shared backend other than the
605 specified usecase to new snd devices */
606 list_for_each(node, &adev->usecase_list) {
607 usecase = node_to_item(node, struct audio_usecase, list);
608 /* Update the out_snd_device only before enabling the audio route */
609 if (switch_device[usecase->id] ) {
610 usecase->out_snd_device = snd_device;
Avinash Vaish8005dc82014-07-24 15:36:33 +0530611 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
612 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700613 }
614 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700615 }
616}
617
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700618static void check_and_route_capture_usecases(struct audio_device *adev,
619 struct audio_usecase *uc_info,
620 snd_device_t snd_device)
621{
622 struct listnode *node;
623 struct audio_usecase *usecase;
624 bool switch_device[AUDIO_USECASE_MAX];
625 int i, num_uc_to_switch = 0;
626
627 /*
628 * This function is to make sure that all the active capture usecases
629 * are always routed to the same input sound device.
630 * For example, if audio-record and voice-call usecases are currently
631 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
632 * is received for voice call then we have to make sure that audio-record
633 * usecase is also switched to earpiece i.e. voice-dmic-ef,
634 * because of the limitation that two devices cannot be enabled
635 * at the same time if they share the same backend.
636 */
637 for (i = 0; i < AUDIO_USECASE_MAX; i++)
638 switch_device[i] = false;
639
640 list_for_each(node, &adev->usecase_list) {
641 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800642 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700643 usecase != uc_info &&
Avinash Vaish44014792015-03-27 14:46:04 +0530644 usecase->in_snd_device != snd_device &&
645 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700646 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
647 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700648 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700649 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700650 switch_device[usecase->id] = true;
651 num_uc_to_switch++;
652 }
653 }
654
655 if (num_uc_to_switch) {
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700656 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700657
658 list_for_each(node, &adev->usecase_list) {
659 usecase = node_to_item(node, struct audio_usecase, list);
660 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700661 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800662 }
663 }
664
665 list_for_each(node, &adev->usecase_list) {
666 usecase = node_to_item(node, struct audio_usecase, list);
667 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700668 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700669 }
670 }
671
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700672 /* Re-route all the usecases on the shared backend other than the
673 specified usecase to new snd devices */
674 list_for_each(node, &adev->usecase_list) {
675 usecase = node_to_item(node, struct audio_usecase, list);
676 /* Update the in_snd_device only before enabling the audio route */
677 if (switch_device[usecase->id] ) {
678 usecase->in_snd_device = snd_device;
Avinash Vaish8005dc82014-07-24 15:36:33 +0530679 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
680 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700681 }
682 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700683 }
684}
685
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800686/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700687static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800688{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700689 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700690 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691
692 switch (channels) {
693 /*
694 * Do not handle stereo output in Multi-channel cases
695 * Stereo case is handled in normal playback path
696 */
697 case 6:
698 ALOGV("%s: HDMI supports 5.1", __func__);
699 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
700 break;
701 case 8:
702 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
703 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
704 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
705 break;
706 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700707 ALOGE("HDMI does not support multi channel playback");
708 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800709 break;
710 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700711 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800712}
713
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700714static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
715{
716 struct audio_usecase *usecase;
717 struct listnode *node;
718
719 list_for_each(node, &adev->usecase_list) {
720 usecase = node_to_item(node, struct audio_usecase, list);
721 if (usecase->type == VOICE_CALL) {
722 ALOGV("%s: usecase id %d", __func__, usecase->id);
723 return usecase->id;
724 }
725 }
726 return USECASE_INVALID;
727}
728
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700729struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700730 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700731{
732 struct audio_usecase *usecase;
733 struct listnode *node;
734
735 list_for_each(node, &adev->usecase_list) {
736 usecase = node_to_item(node, struct audio_usecase, list);
737 if (usecase->id == uc_id)
738 return usecase;
739 }
740 return NULL;
741}
742
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700743int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800744{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800745 snd_device_t out_snd_device = SND_DEVICE_NONE;
746 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700747 struct audio_usecase *usecase = NULL;
748 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800749 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800750 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed739e7152014-01-23 15:56:53 -0800751 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800752 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700753 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800754
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700755 usecase = get_usecase_from_list(adev, uc_id);
756 if (usecase == NULL) {
757 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
758 return -EINVAL;
759 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800760
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800761 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800762 (usecase->type == VOIP_CALL) ||
763 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700764 out_snd_device = platform_get_output_snd_device(adev->platform,
765 usecase->stream.out->devices);
766 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700767 usecase->devices = usecase->stream.out->devices;
768 } else {
769 /*
770 * If the voice call is active, use the sound devices of voice call usecase
771 * so that it would not result any device switch. All the usecases will
772 * be switched to new device when select_devices() is called for voice call
773 * usecase. This is to avoid switching devices for voice call when
774 * check_usecases_codec_backend() is called below.
775 */
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -0700776 if (adev->voice.in_call && adev->mode == AUDIO_MODE_IN_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700777 vc_usecase = get_usecase_from_list(adev,
778 get_voice_usecase_id_from_list(adev));
Mingming Yin6c23f382014-08-14 00:00:51 -0700779 if ((vc_usecase) && ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
780 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700781 in_snd_device = vc_usecase->in_snd_device;
782 out_snd_device = vc_usecase->out_snd_device;
783 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800784 } else if (voice_extn_compress_voip_is_active(adev)) {
785 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Mingming Yin6c23f382014-08-14 00:00:51 -0700786 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Avinash Vaish6a4c3372014-06-25 12:20:37 +0530787 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Mingming Yin6c23f382014-08-14 00:00:51 -0700788 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800789 in_snd_device = voip_usecase->in_snd_device;
790 out_snd_device = voip_usecase->out_snd_device;
791 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800792 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed739e7152014-01-23 15:56:53 -0800793 hfp_ucid = audio_extn_hfp_get_usecase();
794 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin6c23f382014-08-14 00:00:51 -0700795 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800796 in_snd_device = hfp_usecase->in_snd_device;
797 out_snd_device = hfp_usecase->out_snd_device;
798 }
Vimal Puthanveed8fa9eab2014-01-07 16:47:47 -0800799 } else if (audio_extn_hfp_is_active(adev)) {
800 hfp_usecase = get_usecase_from_list(adev, USECASE_AUDIO_HFP_SCO);
801 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
802 in_snd_device = hfp_usecase->in_snd_device;
803 out_snd_device = hfp_usecase->out_snd_device;
804 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700805 }
806 if (usecase->type == PCM_PLAYBACK) {
807 usecase->devices = usecase->stream.out->devices;
808 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700809 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700810 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700811 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700812 if (usecase->stream.out == adev->primary_output &&
Shreyas Nagasandra Chandrasekhar23b3af22014-09-03 19:10:01 +0530813 adev->active_input ) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700814 select_devices(adev, adev->active_input->usecase);
815 }
816 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700817 } else if (usecase->type == PCM_CAPTURE) {
818 usecase->devices = usecase->stream.in->device;
819 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700820 if (in_snd_device == SND_DEVICE_NONE) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530821 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Shreyas Nagasandra Chandrasekhar23b3af22014-09-03 19:10:01 +0530822 if ((adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION || (adev->mode == AUDIO_MODE_IN_COMMUNICATION && adev->active_input->source == AUDIO_SOURCE_MIC)) &&
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700823 adev->primary_output && !adev->primary_output->standby) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530824 out_device = adev->primary_output->devices;
825 } else if(usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
826 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700827 }
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530828 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700829 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700830 }
831 }
832
833 if (out_snd_device == usecase->out_snd_device &&
834 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800835 return 0;
836 }
837
sangwoobc677242013-08-08 16:53:43 +0900838 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700839 out_snd_device, platform_get_snd_device_name(out_snd_device),
840 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800841
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800842 /*
843 * Limitation: While in call, to do a device switch we need to disable
844 * and enable both RX and TX devices though one of them is same as current
845 * device.
846 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800847 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700848 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800849 }
850
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700851 /* Disable current sound devices */
852 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700853 disable_audio_route(adev, usecase);
854 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800855 }
856
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700857 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700858 disable_audio_route(adev, usecase);
859 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800860 }
861
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700862 /* Enable new sound devices */
863 if (out_snd_device != SND_DEVICE_NONE) {
864 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
865 check_usecases_codec_backend(adev, usecase, out_snd_device);
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700866 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800867 }
868
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700869 if (in_snd_device != SND_DEVICE_NONE) {
870 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700871 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700872 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700873
Avinash Vaish8005dc82014-07-24 15:36:33 +0530874 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700875 status = platform_switch_voice_call_device_post(adev->platform,
876 out_snd_device,
877 in_snd_device);
Avinash Vaish8005dc82014-07-24 15:36:33 +0530878 enable_audio_route_for_voice_usecases(adev, usecase);
879 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800880
sangwoo170731f2013-06-08 15:36:36 +0900881 usecase->in_snd_device = in_snd_device;
882 usecase->out_snd_device = out_snd_device;
883
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700884 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900885
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +0530886#ifdef USES_AUDIO_AMPLIFIER
887 /* Rely on amplifier_set_devices to distinguish between in/out devices */
888 amplifier_set_devices(in_snd_device);
889 amplifier_set_devices(out_snd_device);
890#endif
891
892
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800893 /* Applicable only on the targets that has external modem.
894 * Enable device command should be sent to modem only after
895 * enabling voice call mixer controls
896 */
897 if (usecase->type == VOICE_CALL)
898 status = platform_switch_voice_call_usecase_route_post(adev->platform,
899 out_snd_device,
900 in_snd_device);
901
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +0530902 ALOGD("%s: done",__func__);
903
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800904 return status;
905}
906
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800907static int stop_input_stream(struct stream_in *in)
908{
909 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800910 struct audio_usecase *uc_info;
911 struct audio_device *adev = in->dev;
912
Eric Laurentc8400632013-02-14 19:04:54 -0800913 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800914
Eric Laurent994a6932013-07-17 11:51:42 -0700915 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700916 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800917 uc_info = get_usecase_from_list(adev, in->usecase);
918 if (uc_info == NULL) {
919 ALOGE("%s: Could not find the usecase (%d) in the list",
920 __func__, in->usecase);
921 return -EINVAL;
922 }
923
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800924 /* Close in-call recording streams */
925 voice_check_and_stop_incall_rec_usecase(adev, in);
926
Eric Laurent150dbfe2013-02-27 14:31:02 -0800927 /* 1. Disable stream specific mixer controls */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700928 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700929
930 /* 2. Disable the tx device */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700931 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800932
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800933 list_remove(&uc_info->list);
934 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800935
Eric Laurent994a6932013-07-17 11:51:42 -0700936 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800937 return ret;
938}
939
940int start_input_stream(struct stream_in *in)
941{
942 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800943 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800944 struct audio_usecase *uc_info;
945 struct audio_device *adev = in->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +0530946 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800947
Ramjee Singh240ac652015-07-31 16:12:29 +0530948 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
949 if (get_usecase_from_list(adev, usecase) == NULL)
950 in->usecase = usecase;
951
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +0530952 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
953 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700954
Naresh Tannirucef332d2014-06-04 18:17:56 +0530955 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
956 ALOGE("%s: sound card is not active/SSR returning error ", __func__);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530957 ret = -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530958 goto error_config;
959 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530960
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700961 /* Check if source matches incall recording usecase criteria */
962 ret = voice_check_and_set_incall_rec_usecase(adev, in);
963 if (ret)
964 goto error_config;
965 else
966 ALOGV("%s: usecase(%d)", __func__, in->usecase);
967
Ramjee Singh240ac652015-07-31 16:12:29 +0530968 if (get_usecase_from_list(adev, in->usecase) != NULL) {
969 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
970 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
971 goto error_config;
972 }
973
Eric Laurentb23d5282013-05-14 15:27:20 -0700974 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800975 if (in->pcm_device_id < 0) {
976 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
977 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800978 ret = -EINVAL;
979 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800980 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700981
982 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800983 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew George9f0254a2014-06-30 13:56:18 -0700984
985 if (!uc_info) {
986 ret = -ENOMEM;
987 goto error_config;
988 }
989
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800990 uc_info->id = in->usecase;
991 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800992 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700993 uc_info->devices = in->device;
994 uc_info->in_snd_device = SND_DEVICE_NONE;
995 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800996
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800997 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700998 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800999
Eric Laurentc8400632013-02-14 19:04:54 -08001000 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301001 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
1002
1003 unsigned int flags = PCM_IN;
1004 unsigned int pcm_open_retry_entry_count = 0;
1005
1006 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1007 flags |= PCM_MMAP | PCM_NOIRQ;
1008 pcm_open_retry_entry_count = PROXY_OPEN_RETRY_COUNT;
1009 }
1010
1011 while(1) {
1012 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
1013 flags, &in->config);
1014 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
1015 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
1016 if (in->pcm != NULL) {
1017 pcm_close(in->pcm);
1018 in->pcm = NULL;
1019 }
1020 if (pcm_open_retry_entry_count-- == 0) {
1021 ret = -EIO;
1022 goto error_open;
1023 }
1024 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1025 continue;
1026 }
1027 break;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001028 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301029
Eric Laurent994a6932013-07-17 11:51:42 -07001030 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -08001031 return ret;
1032
1033error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001034 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -08001035
1036error_config:
1037 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001038 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -08001039
1040 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001041}
1042
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001043/* must be called with out->lock locked */
1044static int send_offload_cmd_l(struct stream_out* out, int command)
1045{
1046 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
1047
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001048 if (!cmd) {
1049 ALOGE("failed to allocate mem for command 0x%x", command);
1050 return -ENOMEM;
1051 }
1052
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001053 ALOGVV("%s %d", __func__, command);
1054
1055 cmd->cmd = command;
1056 list_add_tail(&out->offload_cmd_list, &cmd->node);
1057 pthread_cond_signal(&out->offload_cond);
1058 return 0;
1059}
1060
1061/* must be called iwth out->lock locked */
1062static void stop_compressed_output_l(struct stream_out *out)
1063{
1064 out->offload_state = OFFLOAD_STATE_IDLE;
1065 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001066 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001067 if (out->compr != NULL) {
1068 compress_stop(out->compr);
1069 while (out->offload_thread_blocked) {
1070 pthread_cond_wait(&out->cond, &out->lock);
1071 }
1072 }
1073}
1074
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001075bool is_offload_usecase(audio_usecase_t uc_id)
1076{
1077 unsigned int i;
1078 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1079 if (uc_id == offload_usecases[i])
1080 return true;
1081 }
1082 return false;
1083}
1084
1085static audio_usecase_t get_offload_usecase(struct audio_device *adev)
1086{
1087 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1088 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
1089 char value[PROPERTY_VALUE_MAX] = {0};
1090
1091 property_get("audio.offload.multiple.enabled", value, NULL);
1092 if (!(atoi(value) || !strncmp("true", value, 4)))
1093 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
1094
1095 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
1096 for (i = 0; i < num_usecase; i++) {
1097 if (!(adev->offload_usecases_state & (0x1<<i))) {
1098 adev->offload_usecases_state |= 0x1 << i;
1099 ret = offload_usecases[i];
1100 break;
1101 }
1102 }
1103 ALOGV("%s: offload usecase is %d", __func__, ret);
1104 return ret;
1105}
1106
1107static void free_offload_usecase(struct audio_device *adev,
1108 audio_usecase_t uc_id)
1109{
1110 unsigned int i;
1111 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1112 if (offload_usecases[i] == uc_id) {
1113 adev->offload_usecases_state &= ~(0x1<<i);
1114 break;
1115 }
1116 }
1117 ALOGV("%s: free offload usecase %d", __func__, uc_id);
1118}
1119
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001120static void *offload_thread_loop(void *context)
1121{
1122 struct stream_out *out = (struct stream_out *) context;
1123 struct listnode *item;
Krishnankutty Kolathappilly0fe78f02014-01-06 18:33:58 -08001124 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001125
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001126 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1127 set_sched_policy(0, SP_FOREGROUND);
1128 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1129
1130 ALOGV("%s", __func__);
1131 pthread_mutex_lock(&out->lock);
1132 for (;;) {
1133 struct offload_cmd *cmd = NULL;
1134 stream_callback_event_t event;
1135 bool send_callback = false;
1136
1137 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1138 __func__, list_empty(&out->offload_cmd_list),
1139 out->offload_state);
1140 if (list_empty(&out->offload_cmd_list)) {
1141 ALOGV("%s SLEEPING", __func__);
1142 pthread_cond_wait(&out->offload_cond, &out->lock);
1143 ALOGV("%s RUNNING", __func__);
1144 continue;
1145 }
1146
1147 item = list_head(&out->offload_cmd_list);
1148 cmd = node_to_item(item, struct offload_cmd, node);
1149 list_remove(item);
1150
1151 ALOGVV("%s STATE %d CMD %d out->compr %p",
1152 __func__, out->offload_state, cmd->cmd, out->compr);
1153
1154 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1155 free(cmd);
1156 break;
1157 }
1158
1159 if (out->compr == NULL) {
1160 ALOGE("%s: Compress handle is NULL", __func__);
1161 pthread_cond_signal(&out->cond);
1162 continue;
1163 }
1164 out->offload_thread_blocked = true;
1165 pthread_mutex_unlock(&out->lock);
1166 send_callback = false;
1167 switch(cmd->cmd) {
1168 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Steve Kondik05c17962015-02-25 00:55:50 -08001169 ALOGV("copl(%x):calling compress_wait", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001170 compress_wait(out->compr, -1);
Steve Kondik05c17962015-02-25 00:55:50 -08001171 ALOGV("copl(%x):out of compress_wait", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001172 send_callback = true;
1173 event = STREAM_CBK_EVENT_WRITE_READY;
1174 break;
1175 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappilly0fe78f02014-01-06 18:33:58 -08001176 ret = compress_next_track(out->compr);
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05301177 if(ret == 0) {
Steve Kondik05c17962015-02-25 00:55:50 -08001178 ALOGV("copl(%p):calling compress_partial_drain", out);
Dhanalakshmi Siddani5754e2c2014-12-23 16:57:43 +05301179 ret = compress_partial_drain(out->compr);
Steve Kondik05c17962015-02-25 00:55:50 -08001180 ALOGV("copl(%p):out of compress_partial_drain", out);
Dhanalakshmi Siddani5754e2c2014-12-23 16:57:43 +05301181 if (ret < 0)
1182 ret = -errno;
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05301183 }
Dhanalakshmi Siddani5754e2c2014-12-23 16:57:43 +05301184 else if (ret == -ETIMEDOUT)
Krishnankutty Kolathappilly0fe78f02014-01-06 18:33:58 -08001185 compress_drain(out->compr);
1186 else
1187 ALOGE("%s: Next track returned error %d",__func__, ret);
Dhanalakshmi Siddani5754e2c2014-12-23 16:57:43 +05301188
1189 if (ret != -ENETRESET) {
1190 send_callback = true;
1191 event = STREAM_CBK_EVENT_DRAIN_READY;
1192
1193 /* Resend the metadata for next iteration */
1194 out->send_new_metadata = 1;
1195 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
1196 } else
1197 ALOGE("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001198 break;
1199 case OFFLOAD_CMD_DRAIN:
Steve Kondik05c17962015-02-25 00:55:50 -08001200 ALOGV("copl(%x):calling compress_drain", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001201 compress_drain(out->compr);
Steve Kondik05c17962015-02-25 00:55:50 -08001202 ALOGV("copl(%x):out of compress_drain", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001203 send_callback = true;
1204 event = STREAM_CBK_EVENT_DRAIN_READY;
1205 break;
1206 default:
1207 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1208 break;
1209 }
1210 pthread_mutex_lock(&out->lock);
1211 out->offload_thread_blocked = false;
1212 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001213 if (send_callback) {
Ravi Kumar Alamanda3d316172014-12-02 19:21:51 -08001214 ALOGVV("%s: sending offload_callback event %d", __func__, event);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001215 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001216 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001217 free(cmd);
1218 }
1219
1220 pthread_cond_signal(&out->cond);
1221 while (!list_empty(&out->offload_cmd_list)) {
1222 item = list_head(&out->offload_cmd_list);
1223 list_remove(item);
1224 free(node_to_item(item, struct offload_cmd, node));
1225 }
1226 pthread_mutex_unlock(&out->lock);
1227
1228 return NULL;
1229}
1230
1231static int create_offload_callback_thread(struct stream_out *out)
1232{
1233 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1234 list_init(&out->offload_cmd_list);
1235 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1236 offload_thread_loop, out);
1237 return 0;
1238}
1239
1240static int destroy_offload_callback_thread(struct stream_out *out)
1241{
1242 pthread_mutex_lock(&out->lock);
1243 stop_compressed_output_l(out);
1244 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1245
1246 pthread_mutex_unlock(&out->lock);
1247 pthread_join(out->offload_thread, (void **) NULL);
1248 pthread_cond_destroy(&out->offload_cond);
1249
1250 return 0;
1251}
1252
Eric Laurent07eeafd2013-10-06 12:52:49 -07001253static bool allow_hdmi_channel_config(struct audio_device *adev)
1254{
1255 struct listnode *node;
1256 struct audio_usecase *usecase;
1257 bool ret = true;
1258
1259 list_for_each(node, &adev->usecase_list) {
1260 usecase = node_to_item(node, struct audio_usecase, list);
1261 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1262 /*
1263 * If voice call is already existing, do not proceed further to avoid
1264 * disabling/enabling both RX and TX devices, CSD calls, etc.
1265 * Once the voice call done, the HDMI channels can be configured to
1266 * max channels of remaining use cases.
1267 */
1268 if (usecase->id == USECASE_VOICE_CALL) {
1269 ALOGD("%s: voice call is active, no change in HDMI channels",
1270 __func__);
1271 ret = false;
1272 break;
1273 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1274 ALOGD("%s: multi channel playback is active, "
1275 "no change in HDMI channels", __func__);
1276 ret = false;
1277 break;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001278 } else if (is_offload_usecase(usecase->id) &&
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05301279 audio_channel_count_from_out_mask(usecase->stream.out->channel_mask) > 2) {
Mingming Yin3ed162b2014-02-24 17:56:01 -08001280 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1281 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1282 ret = false;
1283 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001284 }
1285 }
1286 }
1287 return ret;
1288}
1289
1290static int check_and_set_hdmi_channels(struct audio_device *adev,
1291 unsigned int channels)
1292{
1293 struct listnode *node;
1294 struct audio_usecase *usecase;
1295
1296 /* Check if change in HDMI channel config is allowed */
1297 if (!allow_hdmi_channel_config(adev))
1298 return 0;
1299
1300 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001301 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001302 return 0;
1303 }
1304
1305 platform_set_hdmi_channels(adev->platform, channels);
1306 adev->cur_hdmi_channels = channels;
1307
1308 /*
1309 * Deroute all the playback streams routed to HDMI so that
1310 * the back end is deactivated. Note that backend will not
1311 * be deactivated if any one stream is connected to it.
1312 */
1313 list_for_each(node, &adev->usecase_list) {
1314 usecase = node_to_item(node, struct audio_usecase, list);
1315 if (usecase->type == PCM_PLAYBACK &&
1316 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001317 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001318 }
1319 }
1320
1321 /*
1322 * Enable all the streams disabled above. Now the HDMI backend
1323 * will be activated with new channel configuration
1324 */
1325 list_for_each(node, &adev->usecase_list) {
1326 usecase = node_to_item(node, struct audio_usecase, list);
1327 if (usecase->type == PCM_PLAYBACK &&
1328 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001329 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001330 }
1331 }
1332
1333 return 0;
1334}
1335
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001336static int stop_output_stream(struct stream_out *out)
1337{
1338 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001339 struct audio_usecase *uc_info;
1340 struct audio_device *adev = out->dev;
1341
Eric Laurent994a6932013-07-17 11:51:42 -07001342 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001343 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001344 uc_info = get_usecase_from_list(adev, out->usecase);
1345 if (uc_info == NULL) {
1346 ALOGE("%s: Could not find the usecase (%d) in the list",
1347 __func__, out->usecase);
1348 return -EINVAL;
1349 }
1350
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001351 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001352 if (adev->visualizer_stop_output != NULL)
1353 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1354 if (adev->offload_effects_stop_output != NULL)
1355 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1356 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001357
Eric Laurent150dbfe2013-02-27 14:31:02 -08001358 /* 1. Get and set stream specific mixer controls */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001359 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001360
1361 /* 2. Disable the rx device */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001362 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001363
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001364 list_remove(&uc_info->list);
1365 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001366
Eric Laurentda46bfb2014-08-25 22:39:29 -05001367 audio_extn_extspk_update(adev->extspk);
1368
Eric Laurent07eeafd2013-10-06 12:52:49 -07001369 /* Must be called after removing the usecase from list */
1370 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1371 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1372
Eric Laurent994a6932013-07-17 11:51:42 -07001373 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001374 return ret;
1375}
1376
1377int start_output_stream(struct stream_out *out)
1378{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001379 int ret = 0;
Mingming Yin6c344432014-05-01 15:37:31 -07001380 int sink_channels = 0;
1381 char prop_value[PROPERTY_VALUE_MAX] = {0};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001382 struct audio_usecase *uc_info;
1383 struct audio_device *adev = out->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +05301384 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001385
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001386 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
1387 ret = -EINVAL;
1388 goto error_config;
1389 }
1390
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301391 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1392 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1393 out->devices);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301394
Naresh Tannirucef332d2014-06-04 18:17:56 +05301395 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301396 ALOGE("%s: sound card is not active/SSR returning error", __func__);
1397 ret = -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301398 goto error_config;
1399 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301400
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05301401 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
1402 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001403 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404 if (out->pcm_device_id < 0) {
1405 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1406 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001407 ret = -EINVAL;
1408 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409 }
1410
1411 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001412
1413 if (!uc_info) {
1414 ret = -ENOMEM;
1415 goto error_config;
1416 }
1417
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001418 uc_info->id = out->usecase;
1419 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001420 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001421 uc_info->devices = out->devices;
1422 uc_info->in_snd_device = SND_DEVICE_NONE;
1423 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001424
Eric Laurent07eeafd2013-10-06 12:52:49 -07001425 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001426 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Mingming Yin6c344432014-05-01 15:37:31 -07001427 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1428 if (!strncmp("true", prop_value, 4)) {
1429 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1430 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1431 check_and_set_hdmi_channels(adev, sink_channels);
1432 } else {
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001433 if (is_offload_usecase(out->usecase))
Mingming Yin6c344432014-05-01 15:37:31 -07001434 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1435 else
1436 check_and_set_hdmi_channels(adev, out->config.channels);
1437 }
Mingming Yin10fef6a2013-11-26 17:17:01 -08001438 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001439
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001440 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001441
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001442 select_devices(adev, out->usecase);
1443
Eric Laurentda46bfb2014-08-25 22:39:29 -05001444 audio_extn_extspk_update(adev->extspk);
1445
Steve Kondik3abbbc82014-11-29 14:14:43 -08001446 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
1447 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
1448
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001449 if (!is_offload_usecase(out->usecase)) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301450 unsigned int flags = PCM_OUT;
1451 unsigned int pcm_open_retry_count = 0;
1452 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1453 flags |= PCM_MMAP | PCM_NOIRQ;
1454 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1455 } else
1456 flags |= PCM_MONOTONIC;
1457
1458 while (1) {
1459 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1460 flags, &out->config);
1461 if (out->pcm && !pcm_is_ready(out->pcm)) {
1462 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1463 if (out->pcm != NULL) {
1464 pcm_close(out->pcm);
1465 out->pcm = NULL;
1466 }
1467 if (pcm_open_retry_count-- == 0) {
1468 ret = -EIO;
1469 goto error_open;
1470 }
1471 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1472 continue;
1473 }
1474 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001475 }
1476 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001477 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001478 out->compr = compress_open(adev->snd_card,
1479 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001480 COMPRESS_IN, &out->compr_config);
1481 if (out->compr && !is_compress_ready(out->compr)) {
1482 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1483 compress_close(out->compr);
1484 out->compr = NULL;
1485 ret = -EIO;
1486 goto error_open;
1487 }
1488 if (out->offload_callback)
1489 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001490
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08001491#ifdef DS1_DOLBY_DDP_ENABLED
1492 if (audio_extn_is_dolby_format(out->format))
1493 audio_extn_dolby_send_ddp_endp_params(adev);
1494#endif
1495
Eric Laurentc4aef752013-09-12 17:45:53 -07001496 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001497 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1498 if (adev->offload_effects_start_output != NULL)
1499 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001500 }
Eric Laurent994a6932013-07-17 11:51:42 -07001501 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001502 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001503error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001504 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001505error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001506 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001507}
1508
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001509static int check_input_parameters(uint32_t sample_rate,
1510 audio_format_t format,
1511 int channel_count)
1512{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001513 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001514
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001515 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001516 !voice_extn_compress_voip_is_format_supported(format) &&
1517 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001518
1519 switch (channel_count) {
1520 case 1:
1521 case 2:
1522 case 6:
1523 break;
1524 default:
1525 ret = -EINVAL;
1526 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001527
1528 switch (sample_rate) {
1529 case 8000:
1530 case 11025:
1531 case 12000:
1532 case 16000:
1533 case 22050:
1534 case 24000:
1535 case 32000:
1536 case 44100:
1537 case 48000:
1538 break;
1539 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001540 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001541 }
1542
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001543 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001544}
1545
1546static size_t get_input_buffer_size(uint32_t sample_rate,
1547 audio_format_t format,
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07001548 int channel_count,
1549 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001550{
1551 size_t size = 0;
1552
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001553 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1554 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001555
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001556 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07001557 if (is_low_latency)
1558 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001559 /* ToDo: should use frame_size computed based on the format and
1560 channel_count here. */
1561 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001562
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07001563 /* make sure the size is multiple of 32 bytes
1564 * At 48 kHz mono 16-bit PCM:
1565 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1566 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1567 */
1568 size += 0x1f;
1569 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001570
1571 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001572}
1573
1574static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1575{
1576 struct stream_out *out = (struct stream_out *)stream;
1577
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001578 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001579}
1580
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301581static int out_set_sample_rate(struct audio_stream *stream __unused,
1582 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001583{
1584 return -ENOSYS;
1585}
1586
1587static size_t out_get_buffer_size(const struct audio_stream *stream)
1588{
1589 struct stream_out *out = (struct stream_out *)stream;
1590
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001591 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001592 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001593 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1594 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001595
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05301596 return out->config.period_size *
1597 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001598}
1599
1600static uint32_t out_get_channels(const struct audio_stream *stream)
1601{
1602 struct stream_out *out = (struct stream_out *)stream;
1603
1604 return out->channel_mask;
1605}
1606
1607static audio_format_t out_get_format(const struct audio_stream *stream)
1608{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001609 struct stream_out *out = (struct stream_out *)stream;
1610
1611 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001612}
1613
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301614static int out_set_format(struct audio_stream *stream __unused,
1615 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001616{
1617 return -ENOSYS;
1618}
1619
1620static int out_standby(struct audio_stream *stream)
1621{
1622 struct stream_out *out = (struct stream_out *)stream;
1623 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001624
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301625 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1626 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001627 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1628 /* Ignore standby in case of voip call because the voip output
1629 * stream is closed in adev_close_output_stream()
1630 */
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301631 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001632 return 0;
1633 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001634
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001635 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001636 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001637 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001638 out->standby = true;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001639 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001640 if (out->pcm) {
1641 pcm_close(out->pcm);
1642 out->pcm = NULL;
1643 }
1644 } else {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05301645 ALOGD("copl(%x):standby", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001646 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001647 out->gapless_mdata.encoder_delay = 0;
1648 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001649 if (out->compr != NULL) {
1650 compress_close(out->compr);
1651 out->compr = NULL;
1652 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001653 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001654 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001655 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001656 }
1657 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001658 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001659 return 0;
1660}
1661
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301662static int out_dump(const struct audio_stream *stream __unused,
1663 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001664{
1665 return 0;
1666}
1667
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001668static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1669{
1670 int ret = 0;
1671 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001672 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001673 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001674 tmp_mdata.encoder_delay = 0;
1675 tmp_mdata.encoder_padding = 0;
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001676
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001677 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001678 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001679 return -EINVAL;
1680 }
1681
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001682 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1683 if (ret >= 0) {
1684 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1685 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1686 ALOGV("ADTS format is set in offload mode");
1687 }
1688 out->send_new_metadata = 1;
1689 }
1690
Steve Kondik6bedcdf2014-07-21 11:48:30 -07001691#ifdef FLAC_OFFLOAD_ENABLED
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08001692 if (out->format == AUDIO_FORMAT_FLAC) {
1693 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_BLK_SIZE, value, sizeof(value));
1694 if (ret >= 0) {
1695 out->compr_config.codec->options.flac_dec.min_blk_size = atoi(value);
1696 out->send_new_metadata = 1;
1697 }
1698 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_BLK_SIZE, value, sizeof(value));
1699 if (ret >= 0) {
1700 out->compr_config.codec->options.flac_dec.max_blk_size = atoi(value);
1701 out->send_new_metadata = 1;
1702 }
1703 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_FRAME_SIZE, value, sizeof(value));
1704 if (ret >= 0) {
1705 out->compr_config.codec->options.flac_dec.min_frame_size = atoi(value);
1706 out->send_new_metadata = 1;
1707 }
1708 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_FRAME_SIZE, value, sizeof(value));
1709 if (ret >= 0) {
1710 out->compr_config.codec->options.flac_dec.max_frame_size = atoi(value);
1711 out->send_new_metadata = 1;
1712 }
1713 }
Steve Kondik6bedcdf2014-07-21 11:48:30 -07001714#endif
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08001715
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001716 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1717 if(ret >= 0)
1718 is_meta_data_params = true;
1719 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1720 if(ret >= 0 )
1721 is_meta_data_params = true;
1722 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1723 if(ret >= 0 )
1724 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001725 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1726 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001727 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001728 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001729 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001730 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1731 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001732 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001733 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001734 }
1735
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001736 if(!is_meta_data_params) {
1737 ALOGV("%s: Not gapless meta data params", __func__);
1738 return 0;
1739 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001740 out->gapless_mdata = tmp_mdata;
1741 out->send_new_metadata = 1;
1742 ALOGV("%s new encoder delay %u and padding %u", __func__,
1743 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1744
Steve Kondikba3b35d2014-07-18 01:49:48 -07001745 if(out->format == AUDIO_FORMAT_WMA || out->format == AUDIO_FORMAT_WMA_PRO) {
1746 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_FORMAT_TAG, value, sizeof(value));
1747 if (ret >= 0) {
1748 out->compr_config.codec->format = atoi(value);
1749 }
1750 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_BLOCK_ALIGN, value, sizeof(value));
1751 if (ret >= 0) {
1752 out->compr_config.codec->options.wma.super_block_align = atoi(value);
1753 }
1754 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_BIT_PER_SAMPLE, value, sizeof(value));
1755 if (ret >= 0) {
1756 out->compr_config.codec->options.wma.bits_per_sample = atoi(value);
1757 }
1758 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_CHANNEL_MASK, value, sizeof(value));
1759 if (ret >= 0) {
1760 out->compr_config.codec->options.wma.channelmask = atoi(value);
1761 }
1762 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_ENCODE_OPTION, value, sizeof(value));
1763 if (ret >= 0) {
1764 out->compr_config.codec->options.wma.encodeopt = atoi(value);
1765 }
1766 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_ENCODE_OPTION1, value, sizeof(value));
1767 if (ret >= 0) {
1768 out->compr_config.codec->options.wma.encodeopt1 = atoi(value);
1769 }
1770 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_ENCODE_OPTION2, value, sizeof(value));
1771 if (ret >= 0) {
1772 out->compr_config.codec->options.wma.encodeopt2 = atoi(value);
1773 }
1774 ALOGV("WMA params: fmt %x, balgn %x, sr %d, chmsk %x, encop %x, op1 %x, op2 %x",
1775 out->compr_config.codec->format,
1776 out->compr_config.codec->options.wma.super_block_align,
1777 out->compr_config.codec->options.wma.bits_per_sample,
1778 out->compr_config.codec->options.wma.channelmask,
1779 out->compr_config.codec->options.wma.encodeopt,
1780 out->compr_config.codec->options.wma.encodeopt1,
1781 out->compr_config.codec->options.wma.encodeopt2);
1782 }
1783
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001784 return 0;
1785}
1786
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301787static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1788{
1789 return out == adev->primary_output || out == adev->voice_tx_output;
1790}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001791
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001792static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1793{
1794 struct stream_out *out = (struct stream_out *)stream;
1795 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001796 struct audio_usecase *usecase;
1797 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001798 struct str_parms *parms;
1799 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001800 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001801 bool select_new_device = false;
Steve Kondik3abbbc82014-11-29 14:14:43 -08001802 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001803
sangwoobc677242013-08-08 16:53:43 +09001804 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001805 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001806 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001807 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1808 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001809 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001810 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001811 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001812
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001813 /*
Dhanalakshmi Siddani523ae412014-04-18 22:26:56 +05301814 * When HDMI cable is unplugged/usb hs is disconnected the
1815 * music playback is paused and the policy manager sends routing=0
1816 * But the audioflingercontinues to write data until standby time
1817 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001818 * Avoid this by routing audio to speaker until standby.
1819 */
Dhanalakshmi Siddani523ae412014-04-18 22:26:56 +05301820 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
Steve Kondikeadb6c82014-09-03 18:40:06 -07001821 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
1822 out->devices == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001823 val == AUDIO_DEVICE_NONE) {
1824 val = AUDIO_DEVICE_OUT_SPEAKER;
1825 }
1826
1827 /*
1828 * select_devices() call below switches all the usecases on the same
1829 * backend to the new device. Refer to check_usecases_codec_backend() in
1830 * the select_devices(). But how do we undo this?
1831 *
1832 * For example, music playback is active on headset (deep-buffer usecase)
1833 * and if we go to ringtones and select a ringtone, low-latency usecase
1834 * will be started on headset+speaker. As we can't enable headset+speaker
1835 * and headset devices at the same time, select_devices() switches the music
1836 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1837 * So when the ringtone playback is completed, how do we undo the same?
1838 *
1839 * We are relying on the out_set_parameters() call on deep-buffer output,
1840 * once the ringtone playback is ended.
1841 * NOTE: We should not check if the current devices are same as new devices.
1842 * Because select_devices() must be called to switch back the music
1843 * playback to headset.
1844 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001845 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001846 out->devices = val;
1847
1848 if (!out->standby)
1849 select_devices(adev, out->usecase);
1850
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001851 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301852 output_drives_call(adev, out)) {
1853 adev->current_call_output = out;
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001854 if (!adev->voice.in_call)
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301855 ret = voice_start_call(adev);
1856 else
1857 voice_update_devices_for_all_voice_usecases(adev);
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001858 }
1859 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001860
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001861 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1862 adev->voice.in_call &&
1863 output_drives_call(adev, out)) {
1864 ret = voice_stop_call(adev);
1865 adev->current_call_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001866 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001867
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001868 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001869 pthread_mutex_unlock(&out->lock);
Eric Laurentda46bfb2014-08-25 22:39:29 -05001870
1871 /*handles device and call state changes*/
1872 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001873 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001874
1875 if (out == adev->primary_output) {
1876 pthread_mutex_lock(&adev->lock);
1877 audio_extn_set_parameters(adev, parms);
1878 pthread_mutex_unlock(&adev->lock);
1879 }
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001880 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001881 pthread_mutex_lock(&out->lock);
Steve Kondikba3b35d2014-07-18 01:49:48 -07001882 ret = parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001883 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001884 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001885
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001886 str_parms_destroy(parms);
Steve Kondik3abbbc82014-11-29 14:14:43 -08001887 ALOGV("%s: exit: code(%d)", __func__, status);
1888 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001889}
1890
1891static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1892{
1893 struct stream_out *out = (struct stream_out *)stream;
1894 struct str_parms *query = str_parms_create_str(keys);
1895 char *str;
1896 char value[256];
1897 struct str_parms *reply = str_parms_create();
1898 size_t i, j;
1899 int ret;
1900 bool first = true;
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001901
1902 if (!query || !reply) {
1903 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
1904 return NULL;
1905 }
1906
Eric Laurent994a6932013-07-17 11:51:42 -07001907 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001908 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1909 if (ret >= 0) {
1910 value[0] = '\0';
1911 i = 0;
1912 while (out->supported_channel_masks[i] != 0) {
1913 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1914 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1915 if (!first) {
1916 strcat(value, "|");
1917 }
1918 strcat(value, out_channels_name_to_enum_table[j].name);
1919 first = false;
1920 break;
1921 }
1922 }
1923 i++;
1924 }
1925 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1926 str = str_parms_to_str(reply);
1927 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001928 voice_extn_out_get_parameters(out, query, reply);
1929 str = str_parms_to_str(reply);
1930 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella2e032352014-01-29 12:52:19 -08001931 free(str);
1932 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001933 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001934 }
1935 str_parms_destroy(query);
1936 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001937 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001938 return str;
1939}
1940
1941static uint32_t out_get_latency(const struct audio_stream_out *stream)
1942{
1943 struct stream_out *out = (struct stream_out *)stream;
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001944 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001945
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001946 if (is_offload_usecase(out->usecase)) {
Alexy Joseph62babb92015-03-03 19:01:03 -08001947 latency = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001948 } else {
1949 latency = (out->config.period_count * out->config.period_size * 1000) /
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001950 (out->config.rate);
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001951 }
1952
Anish Kumar1a0594f2014-12-09 04:01:39 +05301953 ALOGV("%s: Latency %d", __func__, latency);
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001954 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001955}
1956
1957static int out_set_volume(struct audio_stream_out *stream, float left,
1958 float right)
1959{
Eric Laurenta9024de2013-04-04 09:19:12 -07001960 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001961 int volume[2];
1962
Eric Laurenta9024de2013-04-04 09:19:12 -07001963 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1964 /* only take left channel into account: the API is for stereo anyway */
1965 out->muted = (left == 0.0f);
1966 return 0;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001967 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001968 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001969 struct audio_device *adev = out->dev;
1970 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001971 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1972 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001973
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001974 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1975 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001976 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1977 if (!ctl) {
1978 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1979 __func__, mixer_ctl_name);
1980 return -EINVAL;
1981 }
1982 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1983 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1984 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1985 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001986 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001987
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001988 return -ENOSYS;
1989}
1990
1991static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1992 size_t bytes)
1993{
1994 struct stream_out *out = (struct stream_out *)stream;
1995 struct audio_device *adev = out->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +05301996 int snd_scard_state = get_snd_card_state(adev);
Eric Laurent6e895242013-09-05 16:10:57 -07001997 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001998
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001999 pthread_mutex_lock(&out->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302000
Naresh Tannirucef332d2014-06-04 18:17:56 +05302001 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
2002 if (out->pcm) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302003 ALOGD(" %s: sound card is not active/SSR state", __func__);
2004 ret= -ENETRESET;
2005 goto exit;
Alexy Joseph55204352014-10-06 12:15:01 -07002006 } else if (is_offload_usecase(out->usecase)) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302007 //during SSR for compress usecase we should return error to flinger
2008 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
2009 pthread_mutex_unlock(&out->lock);
2010 return -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302011 }
2012 }
2013
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002014 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002015 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002016 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002017 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
2018 ret = voice_extn_compress_voip_start_output_stream(out);
2019 else
2020 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002021 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002022 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002023 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002024 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002025 goto exit;
2026 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002027 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002028
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002029 if (is_offload_usecase(out->usecase)) {
Alexy Joseph62babb92015-03-03 19:01:03 -08002030 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002031 if (out->send_new_metadata) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302032 ALOGD("copl(%x):send new gapless metadata", (unsigned int)out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002033 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
2034 out->send_new_metadata = 0;
2035 }
2036
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002037 ret = compress_write(out->compr, buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302038 if (ret < 0)
2039 ret = -errno;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002040 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07002041 if (ret >= 0 && ret < (ssize_t)bytes) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302042 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002043 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302044 } else if (-ENETRESET == ret) {
2045 ALOGE("copl %s: received sound card offline state on compress write", __func__);
2046 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2047 pthread_mutex_unlock(&out->lock);
2048 out_standby(&out->stream.common);
2049 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002050 }
Naresh Tannirucef332d2014-06-04 18:17:56 +05302051 if (!out->playback_started && ret >= 0) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002052 compress_start(out->compr);
2053 out->playback_started = 1;
2054 out->offload_state = OFFLOAD_STATE_PLAYING;
2055 }
2056 pthread_mutex_unlock(&out->lock);
2057 return ret;
2058 } else {
2059 if (out->pcm) {
2060 if (out->muted)
2061 memset((void *)buffer, 0, bytes);
2062 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302063 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY)
2064 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
2065 else
2066 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302067 if (ret < 0)
2068 ret = -errno;
2069 else if (ret == 0)
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002070 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002071 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002072 }
2073
2074exit:
Dhanalakshmi Siddani4fe3e512014-05-26 18:03:42 +05302075 /* ToDo: There may be a corner case when SSR happens back to back during
2076 start/stop. Need to post different error to handle that. */
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302077 if (-ENETRESET == ret) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302078 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302079 }
2080
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002081 pthread_mutex_unlock(&out->lock);
2082
2083 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002084 if (out->pcm)
2085 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302086 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302087 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302088 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302089 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302090 out->standby = true;
2091 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002092 out_standby(&out->stream.common);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002093 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
2094 out_get_sample_rate(&out->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002095 }
2096 return bytes;
2097}
2098
2099static int out_get_render_position(const struct audio_stream_out *stream,
2100 uint32_t *dsp_frames)
2101{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002102 struct stream_out *out = (struct stream_out *)stream;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002103 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
2104 ssize_t ret = 0;
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002105 *dsp_frames = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002106 pthread_mutex_lock(&out->lock);
2107 if (out->compr != NULL) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302108 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002109 &out->sample_rate);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302110 if (ret < 0)
2111 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002112 ALOGVV("%s rendered frames %d sample_rate %d",
2113 __func__, *dsp_frames, out->sample_rate);
2114 }
2115 pthread_mutex_unlock(&out->lock);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302116 if (-ENETRESET == ret) {
2117 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
2118 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2119 return -EINVAL;
2120 } else if(ret < 0) {
Steve Kondik81506242014-07-21 16:12:57 -07002121 if (out->compr == NULL) {
2122 return 0;
2123 }
2124 ALOGE(" ERROR: Unable to get time stamp from compress driver ret=%d", ret);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302125 return -EINVAL;
2126 } else {
2127 return 0;
2128 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002129 } else
2130 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002131}
2132
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302133static int out_add_audio_effect(const struct audio_stream *stream __unused,
2134 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002135{
2136 return 0;
2137}
2138
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302139static int out_remove_audio_effect(const struct audio_stream *stream __unused,
2140 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002141{
2142 return 0;
2143}
2144
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302145static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
2146 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002147{
2148 return -EINVAL;
2149}
2150
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002151static int out_get_presentation_position(const struct audio_stream_out *stream,
2152 uint64_t *frames, struct timespec *timestamp)
2153{
2154 struct stream_out *out = (struct stream_out *)stream;
2155 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07002156 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002157
2158 pthread_mutex_lock(&out->lock);
2159
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002160 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07002161 if (out->compr != NULL) {
2162 compress_get_tstamp(out->compr, &dsp_frames,
2163 &out->sample_rate);
2164 ALOGVV("%s rendered frames %ld sample_rate %d",
2165 __func__, dsp_frames, out->sample_rate);
2166 *frames = dsp_frames;
2167 ret = 0;
2168 /* this is the best we can do */
2169 clock_gettime(CLOCK_MONOTONIC, timestamp);
2170 }
2171 } else {
2172 if (out->pcm) {
2173 size_t avail;
2174 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
2175 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07002176 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07002177 // This adjustment accounts for buffering after app processor.
2178 // It is based on estimated DSP latency per use case, rather than exact.
2179 signed_frames -=
2180 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
2181
Eric Laurent949a0892013-09-20 09:20:13 -07002182 // It would be unusual for this value to be negative, but check just in case ...
2183 if (signed_frames >= 0) {
2184 *frames = signed_frames;
2185 ret = 0;
2186 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002187 }
2188 }
2189 }
2190
2191 pthread_mutex_unlock(&out->lock);
2192
2193 return ret;
2194}
2195
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002196static int out_set_callback(struct audio_stream_out *stream,
2197 stream_callback_t callback, void *cookie)
2198{
2199 struct stream_out *out = (struct stream_out *)stream;
2200
2201 ALOGV("%s", __func__);
2202 pthread_mutex_lock(&out->lock);
2203 out->offload_callback = callback;
2204 out->offload_cookie = cookie;
2205 pthread_mutex_unlock(&out->lock);
2206 return 0;
2207}
2208
2209static int out_pause(struct audio_stream_out* stream)
2210{
2211 struct stream_out *out = (struct stream_out *)stream;
2212 int status = -ENOSYS;
2213 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002214 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302215 ALOGD("copl(%x):pause compress driver", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002216 pthread_mutex_lock(&out->lock);
2217 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302218 struct audio_device *adev = out->dev;
2219 int snd_scard_state = get_snd_card_state(adev);
2220
2221 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2222 status = compress_pause(out->compr);
2223
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002224 out->offload_state = OFFLOAD_STATE_PAUSED;
2225 }
2226 pthread_mutex_unlock(&out->lock);
2227 }
2228 return status;
2229}
2230
2231static int out_resume(struct audio_stream_out* stream)
2232{
2233 struct stream_out *out = (struct stream_out *)stream;
2234 int status = -ENOSYS;
2235 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002236 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302237 ALOGD("copl(%x):resume compress driver", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002238 status = 0;
2239 pthread_mutex_lock(&out->lock);
2240 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302241 struct audio_device *adev = out->dev;
2242 int snd_scard_state = get_snd_card_state(adev);
2243
2244 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2245 status = compress_resume(out->compr);
2246
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002247 out->offload_state = OFFLOAD_STATE_PLAYING;
2248 }
2249 pthread_mutex_unlock(&out->lock);
2250 }
2251 return status;
2252}
2253
2254static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2255{
2256 struct stream_out *out = (struct stream_out *)stream;
2257 int status = -ENOSYS;
2258 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002259 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002260 pthread_mutex_lock(&out->lock);
2261 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2262 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2263 else
2264 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2265 pthread_mutex_unlock(&out->lock);
2266 }
2267 return status;
2268}
2269
2270static int out_flush(struct audio_stream_out* stream)
2271{
2272 struct stream_out *out = (struct stream_out *)stream;
2273 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002274 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302275 ALOGD("copl(%x):calling compress flush", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002276 pthread_mutex_lock(&out->lock);
2277 stop_compressed_output_l(out);
2278 pthread_mutex_unlock(&out->lock);
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302279 ALOGD("copl(%x):out of compress flush", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002280 return 0;
2281 }
2282 return -ENOSYS;
2283}
2284
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002285/** audio_stream_in implementation **/
2286static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2287{
2288 struct stream_in *in = (struct stream_in *)stream;
2289
2290 return in->config.rate;
2291}
2292
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302293static int in_set_sample_rate(struct audio_stream *stream __unused,
2294 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002295{
2296 return -ENOSYS;
2297}
2298
2299static size_t in_get_buffer_size(const struct audio_stream *stream)
2300{
2301 struct stream_in *in = (struct stream_in *)stream;
2302
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002303 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
2304 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07002305 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
2306 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002307
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302308 return in->config.period_size *
Steve Kondik3abbbc82014-11-29 14:14:43 -08002309 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002310}
2311
2312static uint32_t in_get_channels(const struct audio_stream *stream)
2313{
2314 struct stream_in *in = (struct stream_in *)stream;
2315
2316 return in->channel_mask;
2317}
2318
2319static audio_format_t in_get_format(const struct audio_stream *stream)
2320{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002321 struct stream_in *in = (struct stream_in *)stream;
2322
2323 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002324}
2325
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302326static int in_set_format(struct audio_stream *stream __unused,
2327 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002328{
2329 return -ENOSYS;
2330}
2331
2332static int in_standby(struct audio_stream *stream)
2333{
2334 struct stream_in *in = (struct stream_in *)stream;
2335 struct audio_device *adev = in->dev;
2336 int status = 0;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302337 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2338 stream, in->usecase, use_case_table[in->usecase]);
2339
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002340
2341 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2342 /* Ignore standby in case of voip call because the voip input
2343 * stream is closed in adev_close_input_stream()
2344 */
2345 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2346 return status;
2347 }
2348
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002349 pthread_mutex_lock(&in->lock);
2350 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002351 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002352 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002353 if (in->pcm) {
2354 pcm_close(in->pcm);
2355 in->pcm = NULL;
2356 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002357 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002358 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002359 }
2360 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002361 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002362 return status;
2363}
2364
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302365static int in_dump(const struct audio_stream *stream __unused,
2366 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002367{
2368 return 0;
2369}
2370
2371static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2372{
2373 struct stream_in *in = (struct stream_in *)stream;
2374 struct audio_device *adev = in->dev;
2375 struct str_parms *parms;
2376 char *str;
2377 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002378 int ret = 0, val = 0, err;
Steve Kondik3abbbc82014-11-29 14:14:43 -08002379 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002380
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302381 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002382 parms = str_parms_create_str(kvpairs);
2383
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002384 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002385 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002386
2387 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2388 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002389 val = atoi(value);
2390 /* no audio source uses val == 0 */
2391 if ((in->source != val) && (val != 0)) {
2392 in->source = val;
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002393 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2394 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2395 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2396 (in->config.rate == 8000 || in->config.rate == 16000) &&
Steve Kondikb6ab1fa2015-02-06 16:55:26 -08002397 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08002398 err = voice_extn_compress_voip_open_input_stream(in);
2399 if (err != 0) {
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002400 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08002401 __func__, err);
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002402 }
2403 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002404 }
2405 }
2406
Steve Kondik3abbbc82014-11-29 14:14:43 -08002407 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2408
2409 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002410 val = atoi(value);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302411 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002412 in->device = val;
2413 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002414 if (!in->standby)
Steve Kondik3abbbc82014-11-29 14:14:43 -08002415 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002416 }
2417 }
2418
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002419done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002420 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002421 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002422
2423 str_parms_destroy(parms);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002424 ALOGV("%s: exit: status(%d)", __func__, status);
2425 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002426}
2427
2428static char* in_get_parameters(const struct audio_stream *stream,
2429 const char *keys)
2430{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002431 struct stream_in *in = (struct stream_in *)stream;
2432 struct str_parms *query = str_parms_create_str(keys);
2433 char *str;
2434 char value[256];
2435 struct str_parms *reply = str_parms_create();
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002436
2437 if (!query || !reply) {
2438 ALOGE("in_get_parameters: failed to create query or reply");
2439 return NULL;
2440 }
2441
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002442 ALOGV("%s: enter: keys - %s", __func__, keys);
2443
2444 voice_extn_in_get_parameters(in, query, reply);
2445
2446 str = str_parms_to_str(reply);
2447 str_parms_destroy(query);
2448 str_parms_destroy(reply);
2449
2450 ALOGV("%s: exit: returns - %s", __func__, str);
2451 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002452}
2453
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302454static int in_set_gain(struct audio_stream_in *stream __unused,
2455 float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002456{
2457 return 0;
2458}
2459
2460static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2461 size_t bytes)
2462{
2463 struct stream_in *in = (struct stream_in *)stream;
2464 struct audio_device *adev = in->dev;
2465 int i, ret = -1;
Naresh Tannirucef332d2014-06-04 18:17:56 +05302466 int snd_scard_state = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002467
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002468 pthread_mutex_lock(&in->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302469
2470 if (in->pcm) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302471 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302472 ALOGD(" %s: sound card is not active/SSR state", __func__);
2473 ret= -ENETRESET;
2474 goto exit;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302475 }
2476 }
2477
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002478 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002479 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002480 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2481 ret = voice_extn_compress_voip_start_input_stream(in);
2482 else
2483 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002484 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002485 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002486 goto exit;
2487 }
2488 in->standby = 0;
2489 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002490
2491 if (in->pcm) {
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302492 if (audio_extn_ssr_get_enabled() &&
2493 audio_channel_count_from_in_mask(in->channel_mask) == 6)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002494 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002495 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2496 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302497 else if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY)
2498 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002499 else
2500 ret = pcm_read(in->pcm, buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302501 if (ret < 0)
2502 ret = -errno;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002503 }
2504
2505 /*
2506 * Instead of writing zeroes here, we could trust the hardware
2507 * to always provide zeroes when muted.
2508 */
Pavan Chikkala6c183fd2014-12-04 10:48:28 +05302509 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in) &&
2510 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002511 memset(buffer, 0, bytes);
2512
2513exit:
Dhanalakshmi Siddani4fe3e512014-05-26 18:03:42 +05302514 /* ToDo: There may be a corner case when SSR happens back to back during
2515 start/stop. Need to post different error to handle that. */
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302516 if (-ENETRESET == ret) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302517 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302518 memset(buffer, 0, bytes);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302519 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002520 pthread_mutex_unlock(&in->lock);
2521
2522 if (ret != 0) {
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302523 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302524 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302525 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302526 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302527 in->standby = true;
2528 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002529 in_standby(&in->stream.common);
2530 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002531 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
2532 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002533 }
2534 return bytes;
2535}
2536
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302537static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002538{
2539 return 0;
2540}
2541
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002542static int add_remove_audio_effect(const struct audio_stream *stream,
2543 effect_handle_t effect,
2544 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002545{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002546 struct stream_in *in = (struct stream_in *)stream;
2547 int status = 0;
2548 effect_descriptor_t desc;
2549
2550 status = (*effect)->get_descriptor(effect, &desc);
2551 if (status != 0)
2552 return status;
2553
2554 pthread_mutex_lock(&in->lock);
2555 pthread_mutex_lock(&in->dev->lock);
2556 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2557 in->enable_aec != enable &&
2558 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2559 in->enable_aec = enable;
2560 if (!in->standby)
2561 select_devices(in->dev, in->usecase);
2562 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002563 if (in->enable_ns != enable &&
2564 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2565 in->enable_ns = enable;
2566 if (!in->standby)
2567 select_devices(in->dev, in->usecase);
2568 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002569 pthread_mutex_unlock(&in->dev->lock);
2570 pthread_mutex_unlock(&in->lock);
2571
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002572 return 0;
2573}
2574
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002575static int in_add_audio_effect(const struct audio_stream *stream,
2576 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002577{
Eric Laurent994a6932013-07-17 11:51:42 -07002578 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002579 return add_remove_audio_effect(stream, effect, true);
2580}
2581
2582static int in_remove_audio_effect(const struct audio_stream *stream,
2583 effect_handle_t effect)
2584{
Eric Laurent994a6932013-07-17 11:51:42 -07002585 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002586 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002587}
2588
2589static int adev_open_output_stream(struct audio_hw_device *dev,
2590 audio_io_handle_t handle,
2591 audio_devices_t devices,
2592 audio_output_flags_t flags,
2593 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302594 struct audio_stream_out **stream_out,
2595 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002596{
2597 struct audio_device *adev = (struct audio_device *)dev;
2598 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002599 int i, ret = 0;
Steve Kondik5a447012014-12-02 16:04:20 -08002600 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002601
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002602 *stream_out = NULL;
Naresh Tannirucef332d2014-06-04 18:17:56 +05302603
2604 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2605 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2606 ALOGE(" sound card is not active rejecting compress output open request");
2607 return -EINVAL;
2608 }
2609
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002610 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2611
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302612 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2613 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2614 devices, flags, &out->stream);
2615
2616
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002617 if (!out) {
2618 return -ENOMEM;
2619 }
2620
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002621 if (devices == AUDIO_DEVICE_NONE)
2622 devices = AUDIO_DEVICE_OUT_SPEAKER;
2623
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002624 out->flags = flags;
2625 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002626 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002627 out->format = config->format;
2628 out->sample_rate = config->sample_rate;
2629 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2630 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002631 out->handle = handle;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002632 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Joseph3a2fec32014-12-03 02:46:47 -08002633 out->non_blocking = 0;
2634 out->use_small_bufs = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002635
2636 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002637 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
Mingming Yinee733602014-04-03 17:47:22 -07002638 (
2639#ifdef AFE_PROXY_ENABLED
2640 out->devices & AUDIO_DEVICE_OUT_PROXY ||
2641#endif
2642 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002643
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002644 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002645 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2646 ret = read_hdmi_channel_masks(out);
2647
Mingming Yinee733602014-04-03 17:47:22 -07002648#ifdef AFE_PROXY_ENABLED
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002649 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2650 ret = audio_extn_read_afe_proxy_channel_masks(out);
Mingming Yinee733602014-04-03 17:47:22 -07002651#endif
2652
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002653 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002654 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002655 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002656
2657 if (config->sample_rate == 0)
2658 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2659 if (config->channel_mask == 0)
2660 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2661
2662 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002663 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002664 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2665 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002666 out->config.rate = config->sample_rate;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302667 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002668 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Mingming Yinee733602014-04-03 17:47:22 -07002669#ifdef COMPRESS_VOIP_ENABLED
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002670 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2671 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002672 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002673 ret = voice_extn_compress_voip_open_output_stream(out);
2674 if (ret != 0) {
2675 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2676 __func__, ret);
2677 goto error_open;
2678 }
Mingming Yinee733602014-04-03 17:47:22 -07002679#endif
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002680 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302681 ALOGD("%s: copl(%x): sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
2682 __func__, (unsigned int)out, config->sample_rate, config->channel_mask, devices, flags);
2683
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002684 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2685 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2686 ALOGE("%s: Unsupported Offload information", __func__);
2687 ret = -EINVAL;
2688 goto error_open;
2689 }
Mingming Yin90310102013-11-13 16:57:00 -08002690 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002691 !audio_extn_is_dolby_format(config->offload_info.format)) {
Steve Kondik3fdf4fc2014-12-10 21:15:43 -08002692 ALOGE("%s: Unsupported offload audio format %x", __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002693 ret = -EINVAL;
2694 goto error_open;
2695 }
2696
2697 out->compr_config.codec = (struct snd_codec *)
2698 calloc(1, sizeof(struct snd_codec));
2699
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002700 if (!out->compr_config.codec) {
2701 ret = -ENOMEM;
2702 goto error_open;
2703 }
2704
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002705 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002706 if (config->offload_info.channel_mask)
2707 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002708 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002709 out->channel_mask = config->channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002710 config->offload_info.channel_mask = config->channel_mask;
2711 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002712 out->format = config->offload_info.format;
2713 out->sample_rate = config->offload_info.sample_rate;
2714
2715 out->stream.set_callback = out_set_callback;
2716 out->stream.pause = out_pause;
2717 out->stream.resume = out_resume;
2718 out->stream.drain = out_drain;
2719 out->stream.flush = out_flush;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002720 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002721
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002722 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002723 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002724 audio_extn_dolby_get_snd_codec_id(adev, out,
2725 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002726 else
2727 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002728 get_snd_codec_id(config->offload_info.format);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002729
ApurupaPattapu0c566872014-01-10 14:46:02 -08002730 if (audio_is_offload_pcm(config->offload_info.format)) {
2731 out->compr_config.fragment_size =
2732 platform_get_pcm_offload_buffer_size(&config->offload_info);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002733 } else {
ApurupaPattapu0c566872014-01-10 14:46:02 -08002734 out->compr_config.fragment_size =
2735 platform_get_compress_offload_buffer_size(&config->offload_info);
2736 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002737 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2738 out->compr_config.codec->sample_rate =
2739 compress_get_alsa_rate(config->offload_info.sample_rate);
2740 out->compr_config.codec->bit_rate =
2741 config->offload_info.bit_rate;
2742 out->compr_config.codec->ch_in =
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302743 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002744 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002745 out->bit_width = config->offload_info.bit_width;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002746
Steve Kondik3fdf4fc2014-12-10 21:15:43 -08002747 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
Steve Kondik37454012014-11-23 19:54:21 -08002748 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002749 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2750 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002751
Steve Kondik3abbbc82014-11-29 14:14:43 -08002752 if (config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002753 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002754
Steve Kondik3abbbc82014-11-29 14:14:43 -08002755 if (out->bit_width == 24)
2756 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002757
Steve Kondik6bedcdf2014-07-21 11:48:30 -07002758#ifdef FLAC_OFFLOAD_ENABLED
Steve Kondik37454012014-11-23 19:54:21 -08002759 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
2760 out->compr_config.codec->options.flac_dec.sample_size = config->offload_info.bit_width;
Steve Kondik6bedcdf2014-07-21 11:48:30 -07002761#endif
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002762
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002763 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2764 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002765
Alexy Joseph3a2fec32014-12-03 02:46:47 -08002766 if (config->offload_info.use_small_bufs) {
2767 //this flag is set from framework only if its for PCM formats
2768 //no need to check for PCM format again
2769 out->non_blocking = 0;
2770 out->use_small_bufs = true;
2771 ALOGI("Keep write blocking for small buff: non_blockling %d",
2772 out->non_blocking);
2773 }
2774
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002775 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002776 out->offload_state = OFFLOAD_STATE_IDLE;
2777 out->playback_started = 0;
2778
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002779 create_offload_callback_thread(out);
2780 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2781 __func__, config->offload_info.version,
2782 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002783 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -08002784 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002785
Mingming Yinee733602014-04-03 17:47:22 -07002786#ifdef INCALL_MUSIC_ENABLED
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002787 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2788 ret = voice_check_and_set_incall_music_usecase(adev, out);
2789 if (ret != 0) {
2790 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2791 __func__, ret);
2792 goto error_open;
2793 }
Mingming Yinee733602014-04-03 17:47:22 -07002794#endif
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302795 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2796 if (config->sample_rate == 0)
2797 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2798 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2799 config->sample_rate != 8000) {
2800 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2801 ret = -EINVAL;
2802 goto error_open;
2803 }
2804 out->sample_rate = config->sample_rate;
2805 out->config.rate = config->sample_rate;
2806 if (config->format == AUDIO_FORMAT_DEFAULT)
2807 config->format = AUDIO_FORMAT_PCM_16_BIT;
2808 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2809 config->format = AUDIO_FORMAT_PCM_16_BIT;
2810 ret = -EINVAL;
2811 goto error_open;
2812 }
2813 out->format = config->format;
2814 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2815 out->config = pcm_config_afe_proxy_playback;
2816 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002817 } else {
Steve Kondik3abbbc82014-11-29 14:14:43 -08002818#ifndef LOW_LATENCY_PRIMARY
2819 if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
2820 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2821 out->config = pcm_config_low_latency;
Steve Kondikb045f472014-11-12 23:24:07 -08002822#endif
Steve Kondik3abbbc82014-11-29 14:14:43 -08002823#ifdef LOW_LATENCY_PRIMARY
2824 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2825 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2826 out->config = pcm_config_deep_buffer;
2827#endif
2828 } else {
2829 /* primary path is the default path selected if no other outputs are available/suitable */
2830 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
2831#ifdef LOW_LATENCY_PRIMARY
2832 out->config = pcm_config_low_latency;
2833#else
2834 out->config = pcm_config_deep_buffer;
2835#endif
2836 }
2837 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2838 if (k_enable_extended_precision
2839 && pcm_params_format_test(adev->use_case_table[out->usecase],
2840 pcm_format_from_audio_format(config->format))) {
2841 out->config.format = pcm_format_from_audio_format(config->format);
2842 /* out->format already set to config->format */
2843 } else {
2844 /* deny the externally proposed config format
2845 * and use the one specified in audio_hw layer configuration.
2846 * Note: out->format is returned by out->stream.common.get_format()
2847 * and is used to set config->format in the code several lines below.
2848 */
Steve Kondik5a447012014-12-02 16:04:20 -08002849 ALOGW("Audio format %x is not available on this output", out->format);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002850 out->format = audio_format_from_pcm_format(out->config.format);
2851 }
2852 }
2853
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002854 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002855 }
2856
Steve Kondik5a447012014-12-02 16:04:20 -08002857 ALOGV("%s flags %x, format %x, sample_rate %d, out->bit_width %d",
2858 __func__, flags, out->format, out->sample_rate, out->bit_width);
2859
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002860 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
Steve Kondik3abbbc82014-11-29 14:14:43 -08002861 flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002862 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002863 if(adev->primary_output == NULL)
2864 adev->primary_output = out;
2865 else {
2866 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002867 ret = -EEXIST;
2868 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002869 }
2870 }
2871
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002872 /* Check if this usecase is already existing */
2873 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella0d06c8e2014-04-17 20:00:41 -07002874 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2875 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002876 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002877 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002878 ret = -EEXIST;
2879 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002880 }
2881 pthread_mutex_unlock(&adev->lock);
2882
2883 out->stream.common.get_sample_rate = out_get_sample_rate;
2884 out->stream.common.set_sample_rate = out_set_sample_rate;
2885 out->stream.common.get_buffer_size = out_get_buffer_size;
2886 out->stream.common.get_channels = out_get_channels;
2887 out->stream.common.get_format = out_get_format;
2888 out->stream.common.set_format = out_set_format;
2889 out->stream.common.standby = out_standby;
2890 out->stream.common.dump = out_dump;
2891 out->stream.common.set_parameters = out_set_parameters;
2892 out->stream.common.get_parameters = out_get_parameters;
2893 out->stream.common.add_audio_effect = out_add_audio_effect;
2894 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2895 out->stream.get_latency = out_get_latency;
2896 out->stream.set_volume = out_set_volume;
2897 out->stream.write = out_write;
2898 out->stream.get_render_position = out_get_render_position;
2899 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002900 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002901
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002902 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002903 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002904 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002905
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302906 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2907 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2908
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002909 config->format = out->stream.common.get_format(&out->stream.common);
2910 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2911 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2912
2913 *stream_out = &out->stream;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302914 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2915 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002916 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002917 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002918
2919error_open:
2920 free(out);
2921 *stream_out = NULL;
2922 ALOGD("%s: exit: ret %d", __func__, ret);
2923 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002924}
2925
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302926static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002927 struct audio_stream_out *stream)
2928{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002929 struct stream_out *out = (struct stream_out *)stream;
2930 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002931 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002932
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302933 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2934
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002935 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302936 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002937 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302938 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002939 if(ret != 0)
2940 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2941 __func__, ret);
2942 }
2943 else
2944 out_standby(&stream->common);
2945
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002946 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002947 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002948 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002949 if (out->compr_config.codec != NULL)
2950 free(out->compr_config.codec);
2951 }
2952 pthread_cond_destroy(&out->cond);
2953 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002954 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002955 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002956}
2957
Alexy Joseph55204352014-10-06 12:15:01 -07002958static void close_compress_sessions(struct audio_device *adev)
2959{
2960 struct stream_out *out = NULL;
2961 struct listnode *node = NULL;
2962 struct listnode *tmp = NULL;
2963 struct audio_usecase *usecase = NULL;
2964 pthread_mutex_lock(&adev->lock);
2965 list_for_each_safe(node, tmp, &adev->usecase_list) {
2966 usecase = node_to_item(node, struct audio_usecase, list);
2967 if (is_offload_usecase(usecase->id)) {
2968 if (usecase && usecase->stream.out) {
2969 ALOGI(" %s closing compress session %d on OFFLINE state", __func__, usecase->id);
2970 out = usecase->stream.out;
2971 pthread_mutex_unlock(&adev->lock);
2972 out_standby(&out->stream.common);
2973 pthread_mutex_lock(&adev->lock);
2974 }
2975 }
2976 }
2977 pthread_mutex_unlock(&adev->lock);
2978}
2979
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002980static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2981{
2982 struct audio_device *adev = (struct audio_device *)dev;
2983 struct str_parms *parms;
2984 char *str;
2985 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002986 int val;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302987 int ret;
2988 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002989
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002990 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002991 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002992
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302993 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
2994 if (ret >= 0) {
2995 char *snd_card_status = value+2;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302996 if (strstr(snd_card_status, "OFFLINE")) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302997 struct listnode *node;
2998 struct audio_usecase *usecase;
2999
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303000 ALOGD("Received sound card OFFLINE status");
Naresh Tannirucef332d2014-06-04 18:17:56 +05303001 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
3002
Alexy Joseph55204352014-10-06 12:15:01 -07003003 //close compress sessions on OFFLINE status
3004 close_compress_sessions(adev);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303005 } else if (strstr(snd_card_status, "ONLINE")) {
3006 ALOGD("Received sound card ONLINE status");
Naresh Tannirucef332d2014-06-04 18:17:56 +05303007 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303008 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303009 }
3010
3011 pthread_mutex_lock(&adev->lock);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303012 status = voice_set_parameters(adev, parms);
3013 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003014 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003015
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303016 status = platform_set_parameters(adev->platform, parms);
3017 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003018 goto done;
3019
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303020 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
3021 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003022 /* When set to false, HAL should disable EC and NS
3023 * But it is currently not supported.
3024 */
3025 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
3026 adev->bluetooth_nrec = true;
3027 else
3028 adev->bluetooth_nrec = false;
3029 }
3030
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303031 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
3032 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003033 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
3034 adev->screen_off = false;
3035 else
3036 adev->screen_off = true;
3037 }
3038
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303039 ret = str_parms_get_int(parms, "rotation", &val);
3040 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003041 bool reverse_speakers = false;
3042 switch(val) {
3043 // FIXME: note that the code below assumes that the speakers are in the correct placement
3044 // relative to the user when the device is rotated 90deg from its default rotation. This
3045 // assumption is device-specific, not platform-specific like this code.
3046 case 270:
3047 reverse_speakers = true;
3048 break;
3049 case 0:
3050 case 90:
3051 case 180:
3052 break;
3053 default:
3054 ALOGE("%s: unexpected rotation of %d", __func__, val);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303055 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003056 }
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303057 if (status == 0) {
3058 if (adev->speaker_lr_swap != reverse_speakers) {
3059 adev->speaker_lr_swap = reverse_speakers;
3060 // only update the selected device if there is active pcm playback
3061 struct audio_usecase *usecase;
3062 struct listnode *node;
3063 list_for_each(node, &adev->usecase_list) {
3064 usecase = node_to_item(node, struct audio_usecase, list);
3065 if (usecase->type == PCM_PLAYBACK) {
Steve Kondik3abbbc82014-11-29 14:14:43 -08003066 status = select_devices(adev, usecase->id);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303067 break;
3068 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003069 }
3070 }
3071 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003072 }
3073
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003074 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003075
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003076done:
3077 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003078 pthread_mutex_unlock(&adev->lock);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303079 ALOGV("%s: exit with code(%d)", __func__, status);
3080 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003081}
3082
3083static char* adev_get_parameters(const struct audio_hw_device *dev,
3084 const char *keys)
3085{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003086 struct audio_device *adev = (struct audio_device *)dev;
3087 struct str_parms *reply = str_parms_create();
3088 struct str_parms *query = str_parms_create_str(keys);
3089 char *str;
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303090 char value[256] = {0};
3091 int ret = 0;
3092
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003093 if (!query || !reply) {
3094 ALOGE("adev_get_parameters: failed to create query or reply");
3095 return NULL;
3096 }
3097
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303098 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_SND_CARD_STATUS, value,
3099 sizeof(value));
3100 if (ret >=0) {
3101 int val = 1;
3102 pthread_mutex_lock(&adev->snd_card_status.lock);
3103 if (SND_CARD_STATE_OFFLINE == adev->snd_card_status.state)
3104 val = 0;
3105 pthread_mutex_unlock(&adev->snd_card_status.lock);
3106 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_SND_CARD_STATUS, val);
3107 goto exit;
3108 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003109
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003110 pthread_mutex_lock(&adev->lock);
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303111
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003112 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08003113 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003114 platform_get_parameters(adev->platform, query, reply);
Naresh Tannirucef332d2014-06-04 18:17:56 +05303115 pthread_mutex_unlock(&adev->lock);
3116
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303117exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003118 str = str_parms_to_str(reply);
3119 str_parms_destroy(query);
3120 str_parms_destroy(reply);
3121
3122 ALOGV("%s: exit: returns - %s", __func__, str);
3123 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003124}
3125
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303126static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003127{
3128 return 0;
3129}
3130
3131static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
3132{
Haynes Mathew George5191a852013-09-11 14:19:36 -07003133 int ret;
3134 struct audio_device *adev = (struct audio_device *)dev;
Eric Laurent321b96d2014-09-10 13:21:01 -05003135
3136 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
3137
Haynes Mathew George5191a852013-09-11 14:19:36 -07003138 pthread_mutex_lock(&adev->lock);
3139 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07003140 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07003141 pthread_mutex_unlock(&adev->lock);
3142 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003143}
3144
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303145static int adev_set_master_volume(struct audio_hw_device *dev __unused,
3146 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003147{
3148 return -ENOSYS;
3149}
3150
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303151static int adev_get_master_volume(struct audio_hw_device *dev __unused,
3152 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003153{
3154 return -ENOSYS;
3155}
3156
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303157static int adev_set_master_mute(struct audio_hw_device *dev __unused,
3158 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003159{
3160 return -ENOSYS;
3161}
3162
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303163static int adev_get_master_mute(struct audio_hw_device *dev __unused,
3164 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003165{
3166 return -ENOSYS;
3167}
3168
3169static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
3170{
3171 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003172 pthread_mutex_lock(&adev->lock);
3173 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07003174 ALOGD("%s mode %d\n", __func__, mode);
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303175#ifdef USES_AUDIO_AMPLIFIER
3176 if (amplifier_set_mode(mode) != 0)
3177 ALOGE("Failed setting amplifier mode");
3178#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003179 adev->mode = mode;
3180 }
3181 pthread_mutex_unlock(&adev->lock);
Eric Laurentda46bfb2014-08-25 22:39:29 -05003182
3183 audio_extn_extspk_set_mode(adev->extspk, mode);
3184
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003185 return 0;
3186}
3187
3188static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
3189{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003190 int ret;
3191
3192 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08003193 ALOGD("%s state %d\n", __func__, state);
Shreyas Nagasandra Chandrasekhar9781c6c2014-12-01 05:49:35 -08003194 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003195 pthread_mutex_unlock(&adev->lock);
3196
3197 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003198}
3199
3200static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
3201{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003202 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003203 return 0;
3204}
3205
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303206static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003207 const struct audio_config *config)
3208{
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303209 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003210
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003211 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
3212 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003213}
3214
3215static int adev_open_input_stream(struct audio_hw_device *dev,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303216 audio_io_handle_t handle __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003217 audio_devices_t devices,
3218 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303219 struct audio_stream_in **stream_in,
3220 audio_input_flags_t flags __unused,
3221 const char *address __unused,
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003222 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003223{
3224 struct audio_device *adev = (struct audio_device *)dev;
3225 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003226 int ret = 0, buffer_size, frame_size;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303227 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003228 bool is_low_latency = false;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303229
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003230 *stream_in = NULL;
3231 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
3232 return -EINVAL;
3233
3234 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003235
3236 if (!in) {
3237 ALOGE("failed to allocate input stream");
3238 return -ENOMEM;
3239 }
3240
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303241 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003242 stream_handle(%p) io_handle(%d) source(%d)",__func__, config->sample_rate, config->channel_mask,
3243 devices, &in->stream, handle, source);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003244
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07003245 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
3246
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003247 in->stream.common.get_sample_rate = in_get_sample_rate;
3248 in->stream.common.set_sample_rate = in_set_sample_rate;
3249 in->stream.common.get_buffer_size = in_get_buffer_size;
3250 in->stream.common.get_channels = in_get_channels;
3251 in->stream.common.get_format = in_get_format;
3252 in->stream.common.set_format = in_set_format;
3253 in->stream.common.standby = in_standby;
3254 in->stream.common.dump = in_dump;
3255 in->stream.common.set_parameters = in_set_parameters;
3256 in->stream.common.get_parameters = in_get_parameters;
3257 in->stream.common.add_audio_effect = in_add_audio_effect;
3258 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3259 in->stream.set_gain = in_set_gain;
3260 in->stream.read = in_read;
3261 in->stream.get_input_frames_lost = in_get_input_frames_lost;
3262
3263 in->device = devices;
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003264 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003265 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003266 in->standby = 1;
3267 in->channel_mask = config->channel_mask;
3268
3269 /* Update config params with the requested sample rate and channels */
3270 in->usecase = USECASE_AUDIO_RECORD;
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003271 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
3272 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
3273 is_low_latency = true;
3274#if LOW_LATENCY_CAPTURE_USE_CASE
3275 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
3276#endif
3277 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003278 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003279 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003280 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003281
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05303282 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
Pavan Chikkala3c0036d2014-12-17 16:45:10 +05303283 if (adev->mode != AUDIO_MODE_IN_CALL) {
3284 ret = -EINVAL;
3285 goto err_open;
Pavan Chikkala3ec42ef2014-11-21 20:57:48 +05303286 }
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05303287 if (config->sample_rate == 0)
3288 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3289 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
3290 config->sample_rate != 8000) {
3291 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3292 ret = -EINVAL;
3293 goto err_open;
3294 }
3295 if (config->format == AUDIO_FORMAT_DEFAULT)
3296 config->format = AUDIO_FORMAT_PCM_16_BIT;
3297 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
3298 config->format = AUDIO_FORMAT_PCM_16_BIT;
3299 ret = -EINVAL;
3300 goto err_open;
3301 }
3302 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
3303 in->config = pcm_config_afe_proxy_record;
3304 in->config.channels = channel_count;
3305 in->config.rate = config->sample_rate;
3306 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003307 if(audio_extn_ssr_get_enabled()) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303308 if(audio_extn_ssr_init(in)) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003309 ALOGE("%s: audio_extn_ssr_init failed", __func__);
3310 ret = -EINVAL;
3311 goto err_open;
3312 }
3313 } else {
Mingming Yindaf9c542014-09-16 17:41:33 -07003314 ALOGW("%s: surround sound recording is not supported", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003315 }
Mingming Yine62d7842013-10-25 16:26:03 -07003316 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08003317 audio_extn_compr_cap_format_supported(config->format) &&
3318 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303319 audio_extn_compr_cap_init(in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003320 } else {
3321 in->config.channels = channel_count;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303322 frame_size = audio_stream_in_frame_size(&in->stream);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003323 buffer_size = get_input_buffer_size(config->sample_rate,
3324 config->format,
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003325 channel_count,
3326 is_low_latency);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003327 in->config.period_size = buffer_size / frame_size;
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003328 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
3329 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
3330 (voice_extn_compress_voip_is_format_supported(in->format)) &&
3331 (in->config.rate == 8000 || in->config.rate == 16000) &&
3332 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
3333 voice_extn_compress_voip_open_input_stream(in);
3334 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003335 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003336
3337 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07003338 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003339 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003340
3341err_open:
3342 free(in);
3343 *stream_in = NULL;
3344 return ret;
3345}
3346
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303347static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003348 struct audio_stream_in *stream)
3349{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003350 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003351 struct stream_in *in = (struct stream_in *)stream;
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303352 struct audio_device *adev = in->dev;
3353
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303354 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003355
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003356 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303357 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003358 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303359 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003360 if (ret != 0)
3361 ALOGE("%s: Compress voip input cannot be closed, error:%d",
3362 __func__, ret);
3363 } else
3364 in_standby(&stream->common);
3365
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303366 if (audio_extn_ssr_get_enabled() &&
3367 (audio_channel_count_from_in_mask(in->channel_mask) == 6)) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003368 audio_extn_ssr_deinit();
3369 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003370 free(stream);
3371
Mingming Yine62d7842013-10-25 16:26:03 -07003372 if(audio_extn_compr_cap_enabled() &&
3373 audio_extn_compr_cap_format_supported(in->config.format))
3374 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003375 return;
3376}
3377
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303378static int adev_dump(const audio_hw_device_t *device __unused,
3379 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003380{
3381 return 0;
3382}
3383
Steve Kondik3abbbc82014-11-29 14:14:43 -08003384/* verifies input and output devices and their capabilities.
3385 *
3386 * This verification is required when enabling extended bit-depth or
3387 * sampling rates, as not all qcom products support it.
3388 *
3389 * Suitable for calling only on initialization such as adev_open().
3390 * It fills the audio_device use_case_table[] array.
3391 *
3392 * Has a side-effect that it needs to configure audio routing / devices
3393 * in order to power up the devices and read the device parameters.
3394 * It does not acquire any hw device lock. Should restore the devices
3395 * back to "normal state" upon completion.
3396 */
3397static int adev_verify_devices(struct audio_device *adev)
3398{
3399 /* enumeration is a bit difficult because one really wants to pull
3400 * the use_case, device id, etc from the hidden pcm_device_table[].
3401 * In this case there are the following use cases and device ids.
3402 *
3403 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
3404 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
3405 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
3406 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
3407 * [USECASE_AUDIO_RECORD] = {0, 0},
3408 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
3409 * [USECASE_VOICE_CALL] = {2, 2},
3410 *
3411 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
3412 * USECASE_VOICE_CALL omitted, but possible for either input or output.
3413 */
3414
3415 /* should be the usecases enabled in adev_open_input_stream() */
3416 static const int test_in_usecases[] = {
3417 USECASE_AUDIO_RECORD,
3418 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
3419 };
3420 /* should be the usecases enabled in adev_open_output_stream()*/
3421 static const int test_out_usecases[] = {
3422 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
3423 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
3424 };
3425 static const usecase_type_t usecase_type_by_dir[] = {
3426 PCM_PLAYBACK,
3427 PCM_CAPTURE,
3428 };
3429 static const unsigned flags_by_dir[] = {
3430 PCM_OUT,
3431 PCM_IN,
3432 };
3433
3434 size_t i;
3435 unsigned dir;
3436 const unsigned card_id = adev->snd_card;
3437 char info[512]; /* for possible debug info */
3438
3439 for (dir = 0; dir < 2; ++dir) {
3440 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
3441 const unsigned flags_dir = flags_by_dir[dir];
3442 const size_t testsize =
3443 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
3444 const int *testcases =
3445 dir ? test_in_usecases : test_out_usecases;
3446 const audio_devices_t audio_device =
3447 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
3448
3449 for (i = 0; i < testsize; ++i) {
3450 const audio_usecase_t audio_usecase = testcases[i];
3451 int device_id;
3452 snd_device_t snd_device;
3453 struct pcm_params **pparams;
3454 struct stream_out out;
3455 struct stream_in in;
3456 struct audio_usecase uc_info;
3457 int retval;
3458
3459 pparams = &adev->use_case_table[audio_usecase];
3460 pcm_params_free(*pparams); /* can accept null input */
3461 *pparams = NULL;
3462
3463 /* find the device ID for the use case (signed, for error) */
3464 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
3465 if (device_id < 0)
3466 continue;
3467
3468 /* prepare structures for device probing */
3469 memset(&uc_info, 0, sizeof(uc_info));
3470 uc_info.id = audio_usecase;
3471 uc_info.type = usecase_type;
3472 if (dir) {
3473 adev->active_input = &in;
3474 memset(&in, 0, sizeof(in));
3475 in.device = audio_device;
3476 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
3477 uc_info.stream.in = &in;
3478 } else {
3479 adev->active_input = NULL;
3480 }
3481 memset(&out, 0, sizeof(out));
3482 out.devices = audio_device; /* only field needed in select_devices */
3483 uc_info.stream.out = &out;
3484 uc_info.devices = audio_device;
3485 uc_info.in_snd_device = SND_DEVICE_NONE;
3486 uc_info.out_snd_device = SND_DEVICE_NONE;
3487 list_add_tail(&adev->usecase_list, &uc_info.list);
3488
3489 /* select device - similar to start_(in/out)put_stream() */
3490 retval = select_devices(adev, audio_usecase);
3491 if (retval >= 0) {
3492 *pparams = pcm_params_get(card_id, device_id, flags_dir);
3493#if LOG_NDEBUG == 0
3494 if (*pparams) {
3495 ALOGV("%s: (%s) card %d device %d", __func__,
3496 dir ? "input" : "output", card_id, device_id);
3497 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
3498 ALOGV(info); /* print parameters */
3499 } else {
3500 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
3501 }
3502#endif
3503 }
3504
3505 /* deselect device - similar to stop_(in/out)put_stream() */
3506 /* 1. Get and set stream specific mixer controls */
3507 retval = disable_audio_route(adev, &uc_info);
3508 /* 2. Disable the rx device */
3509 retval = disable_snd_device(adev,
3510 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
3511 list_remove(&uc_info.list);
3512 }
3513 }
3514 adev->active_input = NULL; /* restore adev state */
3515 return 0;
3516}
3517
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003518static int adev_close(hw_device_t *device)
3519{
Steve Kondik3abbbc82014-11-29 14:14:43 -08003520 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003521 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07003522
3523 if (!adev)
3524 return 0;
3525
3526 pthread_mutex_lock(&adev_init_lock);
3527
3528 if ((--audio_device_ref_count) == 0) {
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303529#ifdef USES_AUDIO_AMPLIFIER
3530 if (amplifier_close() != 0)
3531 ALOGE("Amplifier close failed");
3532#endif
Kiran Kandide144c82013-11-20 15:58:32 -08003533 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07003534 audio_route_free(adev->audio_route);
3535 free(adev->snd_dev_ref_cnt);
3536 platform_deinit(adev->platform);
Eric Laurentda46bfb2014-08-25 22:39:29 -05003537 audio_extn_extspk_deinit(adev->extspk);
Steve Kondik3abbbc82014-11-29 14:14:43 -08003538 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
3539 pcm_params_free(adev->use_case_table[i]);
3540 }
Kiran Kandi910e1862013-10-29 13:29:42 -07003541 free(device);
3542 adev = NULL;
3543 }
3544 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003545 return 0;
3546}
3547
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003548/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
3549 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
3550 * just that it _might_ work.
3551 */
3552static int period_size_is_plausible_for_low_latency(int period_size)
3553{
3554 switch (period_size) {
3555 case 160:
3556 case 240:
3557 case 320:
3558 case 480:
3559 return 1;
3560 default:
3561 return 0;
3562 }
3563}
3564
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003565static int adev_open(const hw_module_t *module, const char *name,
3566 hw_device_t **device)
3567{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003568 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003569
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003570 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003571 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3572
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003573 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07003574 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003575 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07003576 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003577 ALOGD("%s: returning existing instance of adev", __func__);
3578 ALOGD("%s: exit", __func__);
3579 pthread_mutex_unlock(&adev_init_lock);
3580 return 0;
3581 }
3582
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003583 adev = calloc(1, sizeof(struct audio_device));
3584
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003585 if (!adev) {
3586 pthread_mutex_unlock(&adev_init_lock);
3587 return -ENOMEM;
3588 }
3589
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07003590 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3591
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003592 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3593 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3594 adev->device.common.module = (struct hw_module_t *)module;
3595 adev->device.common.close = adev_close;
3596
3597 adev->device.init_check = adev_init_check;
3598 adev->device.set_voice_volume = adev_set_voice_volume;
3599 adev->device.set_master_volume = adev_set_master_volume;
3600 adev->device.get_master_volume = adev_get_master_volume;
3601 adev->device.set_master_mute = adev_set_master_mute;
3602 adev->device.get_master_mute = adev_get_master_mute;
3603 adev->device.set_mode = adev_set_mode;
3604 adev->device.set_mic_mute = adev_set_mic_mute;
3605 adev->device.get_mic_mute = adev_get_mic_mute;
3606 adev->device.set_parameters = adev_set_parameters;
3607 adev->device.get_parameters = adev_get_parameters;
3608 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3609 adev->device.open_output_stream = adev_open_output_stream;
3610 adev->device.close_output_stream = adev_close_output_stream;
3611 adev->device.open_input_stream = adev_open_input_stream;
3612 adev->device.close_input_stream = adev_close_input_stream;
3613 adev->device.dump = adev_dump;
3614
3615 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003616 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003617 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003618 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003619 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003620 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003621 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003622 /* adev->cur_hdmi_channels = 0; by calloc() */
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08003623 adev->cur_codec_backend_samplerate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
3624 adev->cur_codec_backend_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Eric Laurentb23d5282013-05-14 15:27:20 -07003625 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003626 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003627 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly9b7e96b2014-02-14 14:45:49 -08003628 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07003629 adev->offload_usecases_state = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003630
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303631 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
3632 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
3633
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003634 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003635 adev->platform = platform_init(adev);
3636 if (!adev->platform) {
3637 free(adev->snd_dev_ref_cnt);
3638 free(adev);
3639 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3640 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08003641 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003642 return -EINVAL;
3643 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003644
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303645 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
Eric Laurentda46bfb2014-08-25 22:39:29 -05003646 adev->extspk = audio_extn_extspk_init(adev);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303647
Eric Laurentc4aef752013-09-12 17:45:53 -07003648 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
3649 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3650 if (adev->visualizer_lib == NULL) {
3651 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3652 } else {
3653 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3654 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003655 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003656 "visualizer_hal_start_output");
3657 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003658 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003659 "visualizer_hal_stop_output");
3660 }
3661 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003662 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07003663
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003664 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
3665 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3666 if (adev->offload_effects_lib == NULL) {
3667 ALOGE("%s: DLOPEN failed for %s", __func__,
3668 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3669 } else {
3670 ALOGV("%s: DLOPEN successful for %s", __func__,
3671 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3672 adev->offload_effects_start_output =
3673 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3674 "offload_effects_bundle_hal_start_output");
3675 adev->offload_effects_stop_output =
3676 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3677 "offload_effects_bundle_hal_stop_output");
3678 }
3679 }
3680
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003681 *device = &adev->device.common;
Steve Kondik3abbbc82014-11-29 14:14:43 -08003682 if (k_enable_extended_precision)
3683 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003684
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303685#ifdef USES_AUDIO_AMPLIFIER
3686 if (amplifier_open() != 0)
3687 ALOGE("Amplifier initialization failed");
3688#endif
3689
Kiran Kandi910e1862013-10-29 13:29:42 -07003690 audio_device_ref_count++;
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003691
3692 char value[PROPERTY_VALUE_MAX];
3693 int trial;
3694 if (property_get("audio_hal.period_size", value, NULL) > 0) {
3695 trial = atoi(value);
3696 if (period_size_is_plausible_for_low_latency(trial)) {
3697 pcm_config_low_latency.period_size = trial;
3698 pcm_config_low_latency.start_threshold = trial / 4;
3699 pcm_config_low_latency.avail_min = trial / 4;
3700 configured_low_latency_capture_period_size = trial;
3701 }
3702 }
3703 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
3704 trial = atoi(value);
3705 if (period_size_is_plausible_for_low_latency(trial)) {
3706 configured_low_latency_capture_period_size = trial;
3707 }
3708 }
3709
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003710 pthread_mutex_unlock(&adev_init_lock);
3711
Eric Laurent994a6932013-07-17 11:51:42 -07003712 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003713 return 0;
3714}
3715
3716static struct hw_module_methods_t hal_module_methods = {
3717 .open = adev_open,
3718};
3719
3720struct audio_module HAL_MODULE_INFO_SYM = {
3721 .common = {
3722 .tag = HARDWARE_MODULE_TAG,
3723 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3724 .hal_api_version = HARDWARE_HAL_API_VERSION,
3725 .id = AUDIO_HARDWARE_MODULE_ID,
3726 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08003727 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003728 .methods = &hal_module_methods,
3729 },
3730};