blob: 26979de4dcc96062fc586077945accddbde21e58 [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 */
64#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
65#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)) {
1947 if (out->use_small_bufs == true)
1948 latency = ((out->compr_config.fragments *
1949 out->compr_config.fragment_size * 1000) /
1950 (out->sample_rate * out->compr_config.codec->ch_in *
1951 audio_bytes_per_sample(out->format)));
1952 else
1953 latency = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1954 } else {
1955 latency = (out->config.period_count * out->config.period_size * 1000) /
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001956 (out->config.rate);
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001957 }
1958
Anish Kumar1a0594f2014-12-09 04:01:39 +05301959 ALOGV("%s: Latency %d", __func__, latency);
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001960 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001961}
1962
1963static int out_set_volume(struct audio_stream_out *stream, float left,
1964 float right)
1965{
Eric Laurenta9024de2013-04-04 09:19:12 -07001966 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001967 int volume[2];
1968
Eric Laurenta9024de2013-04-04 09:19:12 -07001969 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1970 /* only take left channel into account: the API is for stereo anyway */
1971 out->muted = (left == 0.0f);
1972 return 0;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001973 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001974 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001975 struct audio_device *adev = out->dev;
1976 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001977 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1978 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001979
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001980 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1981 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001982 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1983 if (!ctl) {
1984 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1985 __func__, mixer_ctl_name);
1986 return -EINVAL;
1987 }
1988 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1989 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1990 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1991 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001992 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001993
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001994 return -ENOSYS;
1995}
1996
1997static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1998 size_t bytes)
1999{
2000 struct stream_out *out = (struct stream_out *)stream;
2001 struct audio_device *adev = out->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +05302002 int snd_scard_state = get_snd_card_state(adev);
Eric Laurent6e895242013-09-05 16:10:57 -07002003 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002004
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002005 pthread_mutex_lock(&out->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302006
Naresh Tannirucef332d2014-06-04 18:17:56 +05302007 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
2008 if (out->pcm) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302009 ALOGD(" %s: sound card is not active/SSR state", __func__);
2010 ret= -ENETRESET;
2011 goto exit;
Alexy Joseph55204352014-10-06 12:15:01 -07002012 } else if (is_offload_usecase(out->usecase)) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302013 //during SSR for compress usecase we should return error to flinger
2014 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
2015 pthread_mutex_unlock(&out->lock);
2016 return -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302017 }
2018 }
2019
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002020 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002021 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002022 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002023 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
2024 ret = voice_extn_compress_voip_start_output_stream(out);
2025 else
2026 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002027 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002028 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002029 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002030 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002031 goto exit;
2032 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002033 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002034
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002035 if (is_offload_usecase(out->usecase)) {
Steve Kondik05c17962015-02-25 00:55:50 -08002036 ALOGV("copl(%x): writing buffer (%d bytes) to compress device", (unsigned int)out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002037 if (out->send_new_metadata) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302038 ALOGD("copl(%x):send new gapless metadata", (unsigned int)out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002039 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
2040 out->send_new_metadata = 0;
2041 }
2042
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002043 ret = compress_write(out->compr, buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302044 if (ret < 0)
2045 ret = -errno;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002046 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07002047 if (ret >= 0 && ret < (ssize_t)bytes) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302048 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002049 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302050 } else if (-ENETRESET == ret) {
2051 ALOGE("copl %s: received sound card offline state on compress write", __func__);
2052 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2053 pthread_mutex_unlock(&out->lock);
2054 out_standby(&out->stream.common);
2055 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002056 }
Naresh Tannirucef332d2014-06-04 18:17:56 +05302057 if (!out->playback_started && ret >= 0) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002058 compress_start(out->compr);
2059 out->playback_started = 1;
2060 out->offload_state = OFFLOAD_STATE_PLAYING;
2061 }
2062 pthread_mutex_unlock(&out->lock);
2063 return ret;
2064 } else {
2065 if (out->pcm) {
2066 if (out->muted)
2067 memset((void *)buffer, 0, bytes);
2068 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302069 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY)
2070 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
2071 else
2072 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302073 if (ret < 0)
2074 ret = -errno;
2075 else if (ret == 0)
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002076 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002077 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002078 }
2079
2080exit:
Dhanalakshmi Siddani4fe3e512014-05-26 18:03:42 +05302081 /* ToDo: There may be a corner case when SSR happens back to back during
2082 start/stop. Need to post different error to handle that. */
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302083 if (-ENETRESET == ret) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302084 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302085 }
2086
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002087 pthread_mutex_unlock(&out->lock);
2088
2089 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002090 if (out->pcm)
2091 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302092 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302093 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302094 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302095 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302096 out->standby = true;
2097 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002098 out_standby(&out->stream.common);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002099 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
2100 out_get_sample_rate(&out->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002101 }
2102 return bytes;
2103}
2104
2105static int out_get_render_position(const struct audio_stream_out *stream,
2106 uint32_t *dsp_frames)
2107{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002108 struct stream_out *out = (struct stream_out *)stream;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002109 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
2110 ssize_t ret = 0;
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002111 *dsp_frames = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002112 pthread_mutex_lock(&out->lock);
2113 if (out->compr != NULL) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302114 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002115 &out->sample_rate);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302116 if (ret < 0)
2117 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002118 ALOGVV("%s rendered frames %d sample_rate %d",
2119 __func__, *dsp_frames, out->sample_rate);
2120 }
2121 pthread_mutex_unlock(&out->lock);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302122 if (-ENETRESET == ret) {
2123 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
2124 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2125 return -EINVAL;
2126 } else if(ret < 0) {
Steve Kondik81506242014-07-21 16:12:57 -07002127 if (out->compr == NULL) {
2128 return 0;
2129 }
2130 ALOGE(" ERROR: Unable to get time stamp from compress driver ret=%d", ret);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302131 return -EINVAL;
2132 } else {
2133 return 0;
2134 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002135 } else
2136 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002137}
2138
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302139static int out_add_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_remove_audio_effect(const struct audio_stream *stream __unused,
2146 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002147{
2148 return 0;
2149}
2150
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302151static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
2152 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002153{
2154 return -EINVAL;
2155}
2156
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002157static int out_get_presentation_position(const struct audio_stream_out *stream,
2158 uint64_t *frames, struct timespec *timestamp)
2159{
2160 struct stream_out *out = (struct stream_out *)stream;
2161 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07002162 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002163
2164 pthread_mutex_lock(&out->lock);
2165
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002166 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07002167 if (out->compr != NULL) {
2168 compress_get_tstamp(out->compr, &dsp_frames,
2169 &out->sample_rate);
2170 ALOGVV("%s rendered frames %ld sample_rate %d",
2171 __func__, dsp_frames, out->sample_rate);
2172 *frames = dsp_frames;
2173 ret = 0;
2174 /* this is the best we can do */
2175 clock_gettime(CLOCK_MONOTONIC, timestamp);
2176 }
2177 } else {
2178 if (out->pcm) {
2179 size_t avail;
2180 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
2181 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07002182 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07002183 // This adjustment accounts for buffering after app processor.
2184 // It is based on estimated DSP latency per use case, rather than exact.
2185 signed_frames -=
2186 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
2187
Eric Laurent949a0892013-09-20 09:20:13 -07002188 // It would be unusual for this value to be negative, but check just in case ...
2189 if (signed_frames >= 0) {
2190 *frames = signed_frames;
2191 ret = 0;
2192 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002193 }
2194 }
2195 }
2196
2197 pthread_mutex_unlock(&out->lock);
2198
2199 return ret;
2200}
2201
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002202static int out_set_callback(struct audio_stream_out *stream,
2203 stream_callback_t callback, void *cookie)
2204{
2205 struct stream_out *out = (struct stream_out *)stream;
2206
2207 ALOGV("%s", __func__);
2208 pthread_mutex_lock(&out->lock);
2209 out->offload_callback = callback;
2210 out->offload_cookie = cookie;
2211 pthread_mutex_unlock(&out->lock);
2212 return 0;
2213}
2214
2215static int out_pause(struct audio_stream_out* stream)
2216{
2217 struct stream_out *out = (struct stream_out *)stream;
2218 int status = -ENOSYS;
2219 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002220 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302221 ALOGD("copl(%x):pause compress driver", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002222 pthread_mutex_lock(&out->lock);
2223 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302224 struct audio_device *adev = out->dev;
2225 int snd_scard_state = get_snd_card_state(adev);
2226
2227 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2228 status = compress_pause(out->compr);
2229
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002230 out->offload_state = OFFLOAD_STATE_PAUSED;
2231 }
2232 pthread_mutex_unlock(&out->lock);
2233 }
2234 return status;
2235}
2236
2237static int out_resume(struct audio_stream_out* stream)
2238{
2239 struct stream_out *out = (struct stream_out *)stream;
2240 int status = -ENOSYS;
2241 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002242 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302243 ALOGD("copl(%x):resume compress driver", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002244 status = 0;
2245 pthread_mutex_lock(&out->lock);
2246 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302247 struct audio_device *adev = out->dev;
2248 int snd_scard_state = get_snd_card_state(adev);
2249
2250 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2251 status = compress_resume(out->compr);
2252
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002253 out->offload_state = OFFLOAD_STATE_PLAYING;
2254 }
2255 pthread_mutex_unlock(&out->lock);
2256 }
2257 return status;
2258}
2259
2260static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2261{
2262 struct stream_out *out = (struct stream_out *)stream;
2263 int status = -ENOSYS;
2264 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002265 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002266 pthread_mutex_lock(&out->lock);
2267 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2268 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2269 else
2270 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2271 pthread_mutex_unlock(&out->lock);
2272 }
2273 return status;
2274}
2275
2276static int out_flush(struct audio_stream_out* stream)
2277{
2278 struct stream_out *out = (struct stream_out *)stream;
2279 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002280 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302281 ALOGD("copl(%x):calling compress flush", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002282 pthread_mutex_lock(&out->lock);
2283 stop_compressed_output_l(out);
2284 pthread_mutex_unlock(&out->lock);
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302285 ALOGD("copl(%x):out of compress flush", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002286 return 0;
2287 }
2288 return -ENOSYS;
2289}
2290
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002291/** audio_stream_in implementation **/
2292static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2293{
2294 struct stream_in *in = (struct stream_in *)stream;
2295
2296 return in->config.rate;
2297}
2298
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302299static int in_set_sample_rate(struct audio_stream *stream __unused,
2300 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002301{
2302 return -ENOSYS;
2303}
2304
2305static size_t in_get_buffer_size(const struct audio_stream *stream)
2306{
2307 struct stream_in *in = (struct stream_in *)stream;
2308
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002309 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
2310 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07002311 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
2312 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002313
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302314 return in->config.period_size *
Steve Kondik3abbbc82014-11-29 14:14:43 -08002315 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002316}
2317
2318static uint32_t in_get_channels(const struct audio_stream *stream)
2319{
2320 struct stream_in *in = (struct stream_in *)stream;
2321
2322 return in->channel_mask;
2323}
2324
2325static audio_format_t in_get_format(const struct audio_stream *stream)
2326{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002327 struct stream_in *in = (struct stream_in *)stream;
2328
2329 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002330}
2331
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302332static int in_set_format(struct audio_stream *stream __unused,
2333 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002334{
2335 return -ENOSYS;
2336}
2337
2338static int in_standby(struct audio_stream *stream)
2339{
2340 struct stream_in *in = (struct stream_in *)stream;
2341 struct audio_device *adev = in->dev;
2342 int status = 0;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302343 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2344 stream, in->usecase, use_case_table[in->usecase]);
2345
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002346
2347 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2348 /* Ignore standby in case of voip call because the voip input
2349 * stream is closed in adev_close_input_stream()
2350 */
2351 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2352 return status;
2353 }
2354
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002355 pthread_mutex_lock(&in->lock);
2356 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002357 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002358 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002359 if (in->pcm) {
2360 pcm_close(in->pcm);
2361 in->pcm = NULL;
2362 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002363 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002364 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002365 }
2366 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002367 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002368 return status;
2369}
2370
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302371static int in_dump(const struct audio_stream *stream __unused,
2372 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002373{
2374 return 0;
2375}
2376
2377static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2378{
2379 struct stream_in *in = (struct stream_in *)stream;
2380 struct audio_device *adev = in->dev;
2381 struct str_parms *parms;
2382 char *str;
2383 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002384 int ret = 0, val = 0, err;
Steve Kondik3abbbc82014-11-29 14:14:43 -08002385 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002386
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302387 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002388 parms = str_parms_create_str(kvpairs);
2389
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002390 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002391 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002392
2393 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2394 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002395 val = atoi(value);
2396 /* no audio source uses val == 0 */
2397 if ((in->source != val) && (val != 0)) {
2398 in->source = val;
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002399 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2400 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2401 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2402 (in->config.rate == 8000 || in->config.rate == 16000) &&
Steve Kondikb6ab1fa2015-02-06 16:55:26 -08002403 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08002404 err = voice_extn_compress_voip_open_input_stream(in);
2405 if (err != 0) {
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002406 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08002407 __func__, err);
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002408 }
2409 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002410 }
2411 }
2412
Steve Kondik3abbbc82014-11-29 14:14:43 -08002413 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2414
2415 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002416 val = atoi(value);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302417 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002418 in->device = val;
2419 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002420 if (!in->standby)
Steve Kondik3abbbc82014-11-29 14:14:43 -08002421 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002422 }
2423 }
2424
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002425done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002426 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002427 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002428
2429 str_parms_destroy(parms);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002430 ALOGV("%s: exit: status(%d)", __func__, status);
2431 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002432}
2433
2434static char* in_get_parameters(const struct audio_stream *stream,
2435 const char *keys)
2436{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002437 struct stream_in *in = (struct stream_in *)stream;
2438 struct str_parms *query = str_parms_create_str(keys);
2439 char *str;
2440 char value[256];
2441 struct str_parms *reply = str_parms_create();
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002442
2443 if (!query || !reply) {
2444 ALOGE("in_get_parameters: failed to create query or reply");
2445 return NULL;
2446 }
2447
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002448 ALOGV("%s: enter: keys - %s", __func__, keys);
2449
2450 voice_extn_in_get_parameters(in, query, reply);
2451
2452 str = str_parms_to_str(reply);
2453 str_parms_destroy(query);
2454 str_parms_destroy(reply);
2455
2456 ALOGV("%s: exit: returns - %s", __func__, str);
2457 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002458}
2459
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302460static int in_set_gain(struct audio_stream_in *stream __unused,
2461 float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002462{
2463 return 0;
2464}
2465
2466static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2467 size_t bytes)
2468{
2469 struct stream_in *in = (struct stream_in *)stream;
2470 struct audio_device *adev = in->dev;
2471 int i, ret = -1;
Naresh Tannirucef332d2014-06-04 18:17:56 +05302472 int snd_scard_state = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002473
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002474 pthread_mutex_lock(&in->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302475
2476 if (in->pcm) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302477 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302478 ALOGD(" %s: sound card is not active/SSR state", __func__);
2479 ret= -ENETRESET;
2480 goto exit;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302481 }
2482 }
2483
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002484 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002485 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002486 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2487 ret = voice_extn_compress_voip_start_input_stream(in);
2488 else
2489 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002490 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002491 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002492 goto exit;
2493 }
2494 in->standby = 0;
2495 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002496
2497 if (in->pcm) {
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302498 if (audio_extn_ssr_get_enabled() &&
2499 audio_channel_count_from_in_mask(in->channel_mask) == 6)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002500 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002501 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2502 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302503 else if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY)
2504 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002505 else
2506 ret = pcm_read(in->pcm, buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302507 if (ret < 0)
2508 ret = -errno;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002509 }
2510
2511 /*
2512 * Instead of writing zeroes here, we could trust the hardware
2513 * to always provide zeroes when muted.
2514 */
Pavan Chikkala6c183fd2014-12-04 10:48:28 +05302515 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in) &&
2516 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002517 memset(buffer, 0, bytes);
2518
2519exit:
Dhanalakshmi Siddani4fe3e512014-05-26 18:03:42 +05302520 /* ToDo: There may be a corner case when SSR happens back to back during
2521 start/stop. Need to post different error to handle that. */
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302522 if (-ENETRESET == ret) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302523 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302524 memset(buffer, 0, bytes);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302525 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002526 pthread_mutex_unlock(&in->lock);
2527
2528 if (ret != 0) {
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302529 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302530 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302531 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302532 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302533 in->standby = true;
2534 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002535 in_standby(&in->stream.common);
2536 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002537 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
2538 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002539 }
2540 return bytes;
2541}
2542
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302543static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002544{
2545 return 0;
2546}
2547
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002548static int add_remove_audio_effect(const struct audio_stream *stream,
2549 effect_handle_t effect,
2550 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002551{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002552 struct stream_in *in = (struct stream_in *)stream;
2553 int status = 0;
2554 effect_descriptor_t desc;
2555
2556 status = (*effect)->get_descriptor(effect, &desc);
2557 if (status != 0)
2558 return status;
2559
2560 pthread_mutex_lock(&in->lock);
2561 pthread_mutex_lock(&in->dev->lock);
2562 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2563 in->enable_aec != enable &&
2564 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2565 in->enable_aec = enable;
2566 if (!in->standby)
2567 select_devices(in->dev, in->usecase);
2568 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002569 if (in->enable_ns != enable &&
2570 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2571 in->enable_ns = enable;
2572 if (!in->standby)
2573 select_devices(in->dev, in->usecase);
2574 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002575 pthread_mutex_unlock(&in->dev->lock);
2576 pthread_mutex_unlock(&in->lock);
2577
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002578 return 0;
2579}
2580
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002581static int in_add_audio_effect(const struct audio_stream *stream,
2582 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002583{
Eric Laurent994a6932013-07-17 11:51:42 -07002584 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002585 return add_remove_audio_effect(stream, effect, true);
2586}
2587
2588static int in_remove_audio_effect(const struct audio_stream *stream,
2589 effect_handle_t effect)
2590{
Eric Laurent994a6932013-07-17 11:51:42 -07002591 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002592 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002593}
2594
2595static int adev_open_output_stream(struct audio_hw_device *dev,
2596 audio_io_handle_t handle,
2597 audio_devices_t devices,
2598 audio_output_flags_t flags,
2599 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302600 struct audio_stream_out **stream_out,
2601 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002602{
2603 struct audio_device *adev = (struct audio_device *)dev;
2604 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002605 int i, ret = 0;
Steve Kondik5a447012014-12-02 16:04:20 -08002606 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002607
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002608 *stream_out = NULL;
Naresh Tannirucef332d2014-06-04 18:17:56 +05302609
2610 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2611 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2612 ALOGE(" sound card is not active rejecting compress output open request");
2613 return -EINVAL;
2614 }
2615
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002616 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2617
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302618 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2619 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2620 devices, flags, &out->stream);
2621
2622
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002623 if (!out) {
2624 return -ENOMEM;
2625 }
2626
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002627 if (devices == AUDIO_DEVICE_NONE)
2628 devices = AUDIO_DEVICE_OUT_SPEAKER;
2629
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002630 out->flags = flags;
2631 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002632 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002633 out->format = config->format;
2634 out->sample_rate = config->sample_rate;
2635 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2636 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002637 out->handle = handle;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002638 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Joseph3a2fec32014-12-03 02:46:47 -08002639 out->non_blocking = 0;
2640 out->use_small_bufs = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002641
2642 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002643 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
Mingming Yinee733602014-04-03 17:47:22 -07002644 (
2645#ifdef AFE_PROXY_ENABLED
2646 out->devices & AUDIO_DEVICE_OUT_PROXY ||
2647#endif
2648 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002649
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002650 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002651 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2652 ret = read_hdmi_channel_masks(out);
2653
Mingming Yinee733602014-04-03 17:47:22 -07002654#ifdef AFE_PROXY_ENABLED
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002655 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2656 ret = audio_extn_read_afe_proxy_channel_masks(out);
Mingming Yinee733602014-04-03 17:47:22 -07002657#endif
2658
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002659 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002660 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002661 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002662
2663 if (config->sample_rate == 0)
2664 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2665 if (config->channel_mask == 0)
2666 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2667
2668 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002669 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002670 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2671 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002672 out->config.rate = config->sample_rate;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302673 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002674 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Mingming Yinee733602014-04-03 17:47:22 -07002675#ifdef COMPRESS_VOIP_ENABLED
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002676 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2677 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002678 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002679 ret = voice_extn_compress_voip_open_output_stream(out);
2680 if (ret != 0) {
2681 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2682 __func__, ret);
2683 goto error_open;
2684 }
Mingming Yinee733602014-04-03 17:47:22 -07002685#endif
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002686 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Sidipotu Ashok5bfacf72014-02-10 16:16:38 +05302687 ALOGD("%s: copl(%x): sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
2688 __func__, (unsigned int)out, config->sample_rate, config->channel_mask, devices, flags);
2689
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002690 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2691 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2692 ALOGE("%s: Unsupported Offload information", __func__);
2693 ret = -EINVAL;
2694 goto error_open;
2695 }
Mingming Yin90310102013-11-13 16:57:00 -08002696 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002697 !audio_extn_is_dolby_format(config->offload_info.format)) {
Steve Kondik3fdf4fc2014-12-10 21:15:43 -08002698 ALOGE("%s: Unsupported offload audio format %x", __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002699 ret = -EINVAL;
2700 goto error_open;
2701 }
2702
2703 out->compr_config.codec = (struct snd_codec *)
2704 calloc(1, sizeof(struct snd_codec));
2705
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002706 if (!out->compr_config.codec) {
2707 ret = -ENOMEM;
2708 goto error_open;
2709 }
2710
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002711 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002712 if (config->offload_info.channel_mask)
2713 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002714 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002715 out->channel_mask = config->channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002716 config->offload_info.channel_mask = config->channel_mask;
2717 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002718 out->format = config->offload_info.format;
2719 out->sample_rate = config->offload_info.sample_rate;
2720
2721 out->stream.set_callback = out_set_callback;
2722 out->stream.pause = out_pause;
2723 out->stream.resume = out_resume;
2724 out->stream.drain = out_drain;
2725 out->stream.flush = out_flush;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002726 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002727
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002728 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002729 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002730 audio_extn_dolby_get_snd_codec_id(adev, out,
2731 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002732 else
2733 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002734 get_snd_codec_id(config->offload_info.format);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002735
ApurupaPattapu0c566872014-01-10 14:46:02 -08002736 if (audio_is_offload_pcm(config->offload_info.format)) {
2737 out->compr_config.fragment_size =
2738 platform_get_pcm_offload_buffer_size(&config->offload_info);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002739 } else {
ApurupaPattapu0c566872014-01-10 14:46:02 -08002740 out->compr_config.fragment_size =
2741 platform_get_compress_offload_buffer_size(&config->offload_info);
2742 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002743 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2744 out->compr_config.codec->sample_rate =
2745 compress_get_alsa_rate(config->offload_info.sample_rate);
2746 out->compr_config.codec->bit_rate =
2747 config->offload_info.bit_rate;
2748 out->compr_config.codec->ch_in =
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302749 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002750 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002751 out->bit_width = config->offload_info.bit_width;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002752
Steve Kondik3fdf4fc2014-12-10 21:15:43 -08002753 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
Steve Kondik37454012014-11-23 19:54:21 -08002754 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002755 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2756 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002757
Steve Kondik3abbbc82014-11-29 14:14:43 -08002758 if (config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002759 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002760
Steve Kondik3abbbc82014-11-29 14:14:43 -08002761 if (out->bit_width == 24)
2762 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002763
Steve Kondik6bedcdf2014-07-21 11:48:30 -07002764#ifdef FLAC_OFFLOAD_ENABLED
Steve Kondik37454012014-11-23 19:54:21 -08002765 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
2766 out->compr_config.codec->options.flac_dec.sample_size = config->offload_info.bit_width;
Steve Kondik6bedcdf2014-07-21 11:48:30 -07002767#endif
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002768
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002769 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2770 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002771
Alexy Joseph3a2fec32014-12-03 02:46:47 -08002772 if (config->offload_info.use_small_bufs) {
2773 //this flag is set from framework only if its for PCM formats
2774 //no need to check for PCM format again
2775 out->non_blocking = 0;
2776 out->use_small_bufs = true;
2777 ALOGI("Keep write blocking for small buff: non_blockling %d",
2778 out->non_blocking);
2779 }
2780
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002781 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002782 out->offload_state = OFFLOAD_STATE_IDLE;
2783 out->playback_started = 0;
2784
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002785 create_offload_callback_thread(out);
2786 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2787 __func__, config->offload_info.version,
2788 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002789 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -08002790 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002791
Mingming Yinee733602014-04-03 17:47:22 -07002792#ifdef INCALL_MUSIC_ENABLED
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002793 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2794 ret = voice_check_and_set_incall_music_usecase(adev, out);
2795 if (ret != 0) {
2796 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2797 __func__, ret);
2798 goto error_open;
2799 }
Mingming Yinee733602014-04-03 17:47:22 -07002800#endif
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302801 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2802 if (config->sample_rate == 0)
2803 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2804 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2805 config->sample_rate != 8000) {
2806 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2807 ret = -EINVAL;
2808 goto error_open;
2809 }
2810 out->sample_rate = config->sample_rate;
2811 out->config.rate = config->sample_rate;
2812 if (config->format == AUDIO_FORMAT_DEFAULT)
2813 config->format = AUDIO_FORMAT_PCM_16_BIT;
2814 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2815 config->format = AUDIO_FORMAT_PCM_16_BIT;
2816 ret = -EINVAL;
2817 goto error_open;
2818 }
2819 out->format = config->format;
2820 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2821 out->config = pcm_config_afe_proxy_playback;
2822 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002823 } else {
Steve Kondik3abbbc82014-11-29 14:14:43 -08002824#ifndef LOW_LATENCY_PRIMARY
2825 if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
2826 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2827 out->config = pcm_config_low_latency;
Steve Kondikb045f472014-11-12 23:24:07 -08002828#endif
Steve Kondik3abbbc82014-11-29 14:14:43 -08002829#ifdef LOW_LATENCY_PRIMARY
2830 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2831 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2832 out->config = pcm_config_deep_buffer;
2833#endif
2834 } else {
2835 /* primary path is the default path selected if no other outputs are available/suitable */
2836 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
2837#ifdef LOW_LATENCY_PRIMARY
2838 out->config = pcm_config_low_latency;
2839#else
2840 out->config = pcm_config_deep_buffer;
2841#endif
2842 }
2843 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2844 if (k_enable_extended_precision
2845 && pcm_params_format_test(adev->use_case_table[out->usecase],
2846 pcm_format_from_audio_format(config->format))) {
2847 out->config.format = pcm_format_from_audio_format(config->format);
2848 /* out->format already set to config->format */
2849 } else {
2850 /* deny the externally proposed config format
2851 * and use the one specified in audio_hw layer configuration.
2852 * Note: out->format is returned by out->stream.common.get_format()
2853 * and is used to set config->format in the code several lines below.
2854 */
Steve Kondik5a447012014-12-02 16:04:20 -08002855 ALOGW("Audio format %x is not available on this output", out->format);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002856 out->format = audio_format_from_pcm_format(out->config.format);
2857 }
2858 }
2859
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002860 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002861 }
2862
Steve Kondik5a447012014-12-02 16:04:20 -08002863 ALOGV("%s flags %x, format %x, sample_rate %d, out->bit_width %d",
2864 __func__, flags, out->format, out->sample_rate, out->bit_width);
2865
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002866 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
Steve Kondik3abbbc82014-11-29 14:14:43 -08002867 flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002868 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002869 if(adev->primary_output == NULL)
2870 adev->primary_output = out;
2871 else {
2872 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002873 ret = -EEXIST;
2874 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002875 }
2876 }
2877
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002878 /* Check if this usecase is already existing */
2879 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella0d06c8e2014-04-17 20:00:41 -07002880 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2881 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002882 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002883 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002884 ret = -EEXIST;
2885 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002886 }
2887 pthread_mutex_unlock(&adev->lock);
2888
2889 out->stream.common.get_sample_rate = out_get_sample_rate;
2890 out->stream.common.set_sample_rate = out_set_sample_rate;
2891 out->stream.common.get_buffer_size = out_get_buffer_size;
2892 out->stream.common.get_channels = out_get_channels;
2893 out->stream.common.get_format = out_get_format;
2894 out->stream.common.set_format = out_set_format;
2895 out->stream.common.standby = out_standby;
2896 out->stream.common.dump = out_dump;
2897 out->stream.common.set_parameters = out_set_parameters;
2898 out->stream.common.get_parameters = out_get_parameters;
2899 out->stream.common.add_audio_effect = out_add_audio_effect;
2900 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2901 out->stream.get_latency = out_get_latency;
2902 out->stream.set_volume = out_set_volume;
2903 out->stream.write = out_write;
2904 out->stream.get_render_position = out_get_render_position;
2905 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002906 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002907
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002908 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002909 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002910 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002911
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302912 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2913 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2914
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002915 config->format = out->stream.common.get_format(&out->stream.common);
2916 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2917 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2918
2919 *stream_out = &out->stream;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302920 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2921 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002922 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002923 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002924
2925error_open:
2926 free(out);
2927 *stream_out = NULL;
2928 ALOGD("%s: exit: ret %d", __func__, ret);
2929 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002930}
2931
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302932static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002933 struct audio_stream_out *stream)
2934{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002935 struct stream_out *out = (struct stream_out *)stream;
2936 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002937 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002938
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302939 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2940
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002941 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302942 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002943 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302944 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002945 if(ret != 0)
2946 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2947 __func__, ret);
2948 }
2949 else
2950 out_standby(&stream->common);
2951
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002952 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002953 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002954 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002955 if (out->compr_config.codec != NULL)
2956 free(out->compr_config.codec);
2957 }
2958 pthread_cond_destroy(&out->cond);
2959 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002960 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002961 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002962}
2963
Alexy Joseph55204352014-10-06 12:15:01 -07002964static void close_compress_sessions(struct audio_device *adev)
2965{
2966 struct stream_out *out = NULL;
2967 struct listnode *node = NULL;
2968 struct listnode *tmp = NULL;
2969 struct audio_usecase *usecase = NULL;
2970 pthread_mutex_lock(&adev->lock);
2971 list_for_each_safe(node, tmp, &adev->usecase_list) {
2972 usecase = node_to_item(node, struct audio_usecase, list);
2973 if (is_offload_usecase(usecase->id)) {
2974 if (usecase && usecase->stream.out) {
2975 ALOGI(" %s closing compress session %d on OFFLINE state", __func__, usecase->id);
2976 out = usecase->stream.out;
2977 pthread_mutex_unlock(&adev->lock);
2978 out_standby(&out->stream.common);
2979 pthread_mutex_lock(&adev->lock);
2980 }
2981 }
2982 }
2983 pthread_mutex_unlock(&adev->lock);
2984}
2985
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002986static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2987{
2988 struct audio_device *adev = (struct audio_device *)dev;
2989 struct str_parms *parms;
2990 char *str;
2991 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002992 int val;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302993 int ret;
2994 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002995
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002996 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002997 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002998
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302999 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
3000 if (ret >= 0) {
3001 char *snd_card_status = value+2;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303002 if (strstr(snd_card_status, "OFFLINE")) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05303003 struct listnode *node;
3004 struct audio_usecase *usecase;
3005
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303006 ALOGD("Received sound card OFFLINE status");
Naresh Tannirucef332d2014-06-04 18:17:56 +05303007 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
3008
Alexy Joseph55204352014-10-06 12:15:01 -07003009 //close compress sessions on OFFLINE status
3010 close_compress_sessions(adev);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303011 } else if (strstr(snd_card_status, "ONLINE")) {
3012 ALOGD("Received sound card ONLINE status");
Naresh Tannirucef332d2014-06-04 18:17:56 +05303013 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303014 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303015 }
3016
3017 pthread_mutex_lock(&adev->lock);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303018 status = voice_set_parameters(adev, parms);
3019 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003020 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003021
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303022 status = platform_set_parameters(adev->platform, parms);
3023 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003024 goto done;
3025
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303026 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
3027 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003028 /* When set to false, HAL should disable EC and NS
3029 * But it is currently not supported.
3030 */
3031 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
3032 adev->bluetooth_nrec = true;
3033 else
3034 adev->bluetooth_nrec = false;
3035 }
3036
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303037 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
3038 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003039 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
3040 adev->screen_off = false;
3041 else
3042 adev->screen_off = true;
3043 }
3044
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303045 ret = str_parms_get_int(parms, "rotation", &val);
3046 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003047 bool reverse_speakers = false;
3048 switch(val) {
3049 // FIXME: note that the code below assumes that the speakers are in the correct placement
3050 // relative to the user when the device is rotated 90deg from its default rotation. This
3051 // assumption is device-specific, not platform-specific like this code.
3052 case 270:
3053 reverse_speakers = true;
3054 break;
3055 case 0:
3056 case 90:
3057 case 180:
3058 break;
3059 default:
3060 ALOGE("%s: unexpected rotation of %d", __func__, val);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303061 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003062 }
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303063 if (status == 0) {
3064 if (adev->speaker_lr_swap != reverse_speakers) {
3065 adev->speaker_lr_swap = reverse_speakers;
3066 // only update the selected device if there is active pcm playback
3067 struct audio_usecase *usecase;
3068 struct listnode *node;
3069 list_for_each(node, &adev->usecase_list) {
3070 usecase = node_to_item(node, struct audio_usecase, list);
3071 if (usecase->type == PCM_PLAYBACK) {
Steve Kondik3abbbc82014-11-29 14:14:43 -08003072 status = select_devices(adev, usecase->id);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303073 break;
3074 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003075 }
3076 }
3077 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003078 }
3079
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003080 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003081
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003082done:
3083 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003084 pthread_mutex_unlock(&adev->lock);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303085 ALOGV("%s: exit with code(%d)", __func__, status);
3086 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003087}
3088
3089static char* adev_get_parameters(const struct audio_hw_device *dev,
3090 const char *keys)
3091{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003092 struct audio_device *adev = (struct audio_device *)dev;
3093 struct str_parms *reply = str_parms_create();
3094 struct str_parms *query = str_parms_create_str(keys);
3095 char *str;
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303096 char value[256] = {0};
3097 int ret = 0;
3098
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003099 if (!query || !reply) {
3100 ALOGE("adev_get_parameters: failed to create query or reply");
3101 return NULL;
3102 }
3103
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303104 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_SND_CARD_STATUS, value,
3105 sizeof(value));
3106 if (ret >=0) {
3107 int val = 1;
3108 pthread_mutex_lock(&adev->snd_card_status.lock);
3109 if (SND_CARD_STATE_OFFLINE == adev->snd_card_status.state)
3110 val = 0;
3111 pthread_mutex_unlock(&adev->snd_card_status.lock);
3112 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_SND_CARD_STATUS, val);
3113 goto exit;
3114 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003115
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003116 pthread_mutex_lock(&adev->lock);
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303117
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003118 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08003119 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003120 platform_get_parameters(adev->platform, query, reply);
Naresh Tannirucef332d2014-06-04 18:17:56 +05303121 pthread_mutex_unlock(&adev->lock);
3122
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303123exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003124 str = str_parms_to_str(reply);
3125 str_parms_destroy(query);
3126 str_parms_destroy(reply);
3127
3128 ALOGV("%s: exit: returns - %s", __func__, str);
3129 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003130}
3131
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303132static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003133{
3134 return 0;
3135}
3136
3137static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
3138{
Haynes Mathew George5191a852013-09-11 14:19:36 -07003139 int ret;
3140 struct audio_device *adev = (struct audio_device *)dev;
Eric Laurent321b96d2014-09-10 13:21:01 -05003141
3142 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
3143
Haynes Mathew George5191a852013-09-11 14:19:36 -07003144 pthread_mutex_lock(&adev->lock);
3145 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07003146 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07003147 pthread_mutex_unlock(&adev->lock);
3148 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003149}
3150
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303151static int adev_set_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_get_master_volume(struct audio_hw_device *dev __unused,
3158 float *volume __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_set_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
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303169static int adev_get_master_mute(struct audio_hw_device *dev __unused,
3170 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003171{
3172 return -ENOSYS;
3173}
3174
3175static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
3176{
3177 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003178 pthread_mutex_lock(&adev->lock);
3179 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07003180 ALOGD("%s mode %d\n", __func__, mode);
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303181#ifdef USES_AUDIO_AMPLIFIER
3182 if (amplifier_set_mode(mode) != 0)
3183 ALOGE("Failed setting amplifier mode");
3184#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003185 adev->mode = mode;
3186 }
3187 pthread_mutex_unlock(&adev->lock);
Eric Laurentda46bfb2014-08-25 22:39:29 -05003188
3189 audio_extn_extspk_set_mode(adev->extspk, mode);
3190
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003191 return 0;
3192}
3193
3194static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
3195{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003196 int ret;
3197
3198 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08003199 ALOGD("%s state %d\n", __func__, state);
Shreyas Nagasandra Chandrasekhar9781c6c2014-12-01 05:49:35 -08003200 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003201 pthread_mutex_unlock(&adev->lock);
3202
3203 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003204}
3205
3206static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
3207{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003208 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003209 return 0;
3210}
3211
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303212static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003213 const struct audio_config *config)
3214{
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303215 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003216
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003217 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
3218 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003219}
3220
3221static int adev_open_input_stream(struct audio_hw_device *dev,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303222 audio_io_handle_t handle __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003223 audio_devices_t devices,
3224 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303225 struct audio_stream_in **stream_in,
3226 audio_input_flags_t flags __unused,
3227 const char *address __unused,
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003228 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003229{
3230 struct audio_device *adev = (struct audio_device *)dev;
3231 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003232 int ret = 0, buffer_size, frame_size;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303233 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003234 bool is_low_latency = false;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303235
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003236 *stream_in = NULL;
3237 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
3238 return -EINVAL;
3239
3240 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003241
3242 if (!in) {
3243 ALOGE("failed to allocate input stream");
3244 return -ENOMEM;
3245 }
3246
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303247 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003248 stream_handle(%p) io_handle(%d) source(%d)",__func__, config->sample_rate, config->channel_mask,
3249 devices, &in->stream, handle, source);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003250
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07003251 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
3252
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003253 in->stream.common.get_sample_rate = in_get_sample_rate;
3254 in->stream.common.set_sample_rate = in_set_sample_rate;
3255 in->stream.common.get_buffer_size = in_get_buffer_size;
3256 in->stream.common.get_channels = in_get_channels;
3257 in->stream.common.get_format = in_get_format;
3258 in->stream.common.set_format = in_set_format;
3259 in->stream.common.standby = in_standby;
3260 in->stream.common.dump = in_dump;
3261 in->stream.common.set_parameters = in_set_parameters;
3262 in->stream.common.get_parameters = in_get_parameters;
3263 in->stream.common.add_audio_effect = in_add_audio_effect;
3264 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3265 in->stream.set_gain = in_set_gain;
3266 in->stream.read = in_read;
3267 in->stream.get_input_frames_lost = in_get_input_frames_lost;
3268
3269 in->device = devices;
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003270 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003271 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003272 in->standby = 1;
3273 in->channel_mask = config->channel_mask;
3274
3275 /* Update config params with the requested sample rate and channels */
3276 in->usecase = USECASE_AUDIO_RECORD;
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003277 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
3278 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
3279 is_low_latency = true;
3280#if LOW_LATENCY_CAPTURE_USE_CASE
3281 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
3282#endif
3283 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003284 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003285 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003286 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003287
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05303288 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
Pavan Chikkala3c0036d2014-12-17 16:45:10 +05303289 if (adev->mode != AUDIO_MODE_IN_CALL) {
3290 ret = -EINVAL;
3291 goto err_open;
Pavan Chikkala3ec42ef2014-11-21 20:57:48 +05303292 }
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05303293 if (config->sample_rate == 0)
3294 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3295 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
3296 config->sample_rate != 8000) {
3297 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3298 ret = -EINVAL;
3299 goto err_open;
3300 }
3301 if (config->format == AUDIO_FORMAT_DEFAULT)
3302 config->format = AUDIO_FORMAT_PCM_16_BIT;
3303 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
3304 config->format = AUDIO_FORMAT_PCM_16_BIT;
3305 ret = -EINVAL;
3306 goto err_open;
3307 }
3308 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
3309 in->config = pcm_config_afe_proxy_record;
3310 in->config.channels = channel_count;
3311 in->config.rate = config->sample_rate;
3312 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003313 if(audio_extn_ssr_get_enabled()) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303314 if(audio_extn_ssr_init(in)) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003315 ALOGE("%s: audio_extn_ssr_init failed", __func__);
3316 ret = -EINVAL;
3317 goto err_open;
3318 }
3319 } else {
Mingming Yindaf9c542014-09-16 17:41:33 -07003320 ALOGW("%s: surround sound recording is not supported", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003321 }
Mingming Yine62d7842013-10-25 16:26:03 -07003322 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08003323 audio_extn_compr_cap_format_supported(config->format) &&
3324 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303325 audio_extn_compr_cap_init(in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003326 } else {
3327 in->config.channels = channel_count;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303328 frame_size = audio_stream_in_frame_size(&in->stream);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003329 buffer_size = get_input_buffer_size(config->sample_rate,
3330 config->format,
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003331 channel_count,
3332 is_low_latency);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003333 in->config.period_size = buffer_size / frame_size;
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003334 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
3335 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
3336 (voice_extn_compress_voip_is_format_supported(in->format)) &&
3337 (in->config.rate == 8000 || in->config.rate == 16000) &&
3338 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
3339 voice_extn_compress_voip_open_input_stream(in);
3340 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003341 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003342
3343 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07003344 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003345 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003346
3347err_open:
3348 free(in);
3349 *stream_in = NULL;
3350 return ret;
3351}
3352
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303353static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003354 struct audio_stream_in *stream)
3355{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003356 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003357 struct stream_in *in = (struct stream_in *)stream;
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303358 struct audio_device *adev = in->dev;
3359
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303360 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003361
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003362 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303363 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003364 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303365 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003366 if (ret != 0)
3367 ALOGE("%s: Compress voip input cannot be closed, error:%d",
3368 __func__, ret);
3369 } else
3370 in_standby(&stream->common);
3371
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303372 if (audio_extn_ssr_get_enabled() &&
3373 (audio_channel_count_from_in_mask(in->channel_mask) == 6)) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003374 audio_extn_ssr_deinit();
3375 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003376 free(stream);
3377
Mingming Yine62d7842013-10-25 16:26:03 -07003378 if(audio_extn_compr_cap_enabled() &&
3379 audio_extn_compr_cap_format_supported(in->config.format))
3380 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003381 return;
3382}
3383
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303384static int adev_dump(const audio_hw_device_t *device __unused,
3385 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003386{
3387 return 0;
3388}
3389
Steve Kondik3abbbc82014-11-29 14:14:43 -08003390/* verifies input and output devices and their capabilities.
3391 *
3392 * This verification is required when enabling extended bit-depth or
3393 * sampling rates, as not all qcom products support it.
3394 *
3395 * Suitable for calling only on initialization such as adev_open().
3396 * It fills the audio_device use_case_table[] array.
3397 *
3398 * Has a side-effect that it needs to configure audio routing / devices
3399 * in order to power up the devices and read the device parameters.
3400 * It does not acquire any hw device lock. Should restore the devices
3401 * back to "normal state" upon completion.
3402 */
3403static int adev_verify_devices(struct audio_device *adev)
3404{
3405 /* enumeration is a bit difficult because one really wants to pull
3406 * the use_case, device id, etc from the hidden pcm_device_table[].
3407 * In this case there are the following use cases and device ids.
3408 *
3409 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
3410 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
3411 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
3412 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
3413 * [USECASE_AUDIO_RECORD] = {0, 0},
3414 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
3415 * [USECASE_VOICE_CALL] = {2, 2},
3416 *
3417 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
3418 * USECASE_VOICE_CALL omitted, but possible for either input or output.
3419 */
3420
3421 /* should be the usecases enabled in adev_open_input_stream() */
3422 static const int test_in_usecases[] = {
3423 USECASE_AUDIO_RECORD,
3424 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
3425 };
3426 /* should be the usecases enabled in adev_open_output_stream()*/
3427 static const int test_out_usecases[] = {
3428 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
3429 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
3430 };
3431 static const usecase_type_t usecase_type_by_dir[] = {
3432 PCM_PLAYBACK,
3433 PCM_CAPTURE,
3434 };
3435 static const unsigned flags_by_dir[] = {
3436 PCM_OUT,
3437 PCM_IN,
3438 };
3439
3440 size_t i;
3441 unsigned dir;
3442 const unsigned card_id = adev->snd_card;
3443 char info[512]; /* for possible debug info */
3444
3445 for (dir = 0; dir < 2; ++dir) {
3446 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
3447 const unsigned flags_dir = flags_by_dir[dir];
3448 const size_t testsize =
3449 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
3450 const int *testcases =
3451 dir ? test_in_usecases : test_out_usecases;
3452 const audio_devices_t audio_device =
3453 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
3454
3455 for (i = 0; i < testsize; ++i) {
3456 const audio_usecase_t audio_usecase = testcases[i];
3457 int device_id;
3458 snd_device_t snd_device;
3459 struct pcm_params **pparams;
3460 struct stream_out out;
3461 struct stream_in in;
3462 struct audio_usecase uc_info;
3463 int retval;
3464
3465 pparams = &adev->use_case_table[audio_usecase];
3466 pcm_params_free(*pparams); /* can accept null input */
3467 *pparams = NULL;
3468
3469 /* find the device ID for the use case (signed, for error) */
3470 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
3471 if (device_id < 0)
3472 continue;
3473
3474 /* prepare structures for device probing */
3475 memset(&uc_info, 0, sizeof(uc_info));
3476 uc_info.id = audio_usecase;
3477 uc_info.type = usecase_type;
3478 if (dir) {
3479 adev->active_input = &in;
3480 memset(&in, 0, sizeof(in));
3481 in.device = audio_device;
3482 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
3483 uc_info.stream.in = &in;
3484 } else {
3485 adev->active_input = NULL;
3486 }
3487 memset(&out, 0, sizeof(out));
3488 out.devices = audio_device; /* only field needed in select_devices */
3489 uc_info.stream.out = &out;
3490 uc_info.devices = audio_device;
3491 uc_info.in_snd_device = SND_DEVICE_NONE;
3492 uc_info.out_snd_device = SND_DEVICE_NONE;
3493 list_add_tail(&adev->usecase_list, &uc_info.list);
3494
3495 /* select device - similar to start_(in/out)put_stream() */
3496 retval = select_devices(adev, audio_usecase);
3497 if (retval >= 0) {
3498 *pparams = pcm_params_get(card_id, device_id, flags_dir);
3499#if LOG_NDEBUG == 0
3500 if (*pparams) {
3501 ALOGV("%s: (%s) card %d device %d", __func__,
3502 dir ? "input" : "output", card_id, device_id);
3503 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
3504 ALOGV(info); /* print parameters */
3505 } else {
3506 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
3507 }
3508#endif
3509 }
3510
3511 /* deselect device - similar to stop_(in/out)put_stream() */
3512 /* 1. Get and set stream specific mixer controls */
3513 retval = disable_audio_route(adev, &uc_info);
3514 /* 2. Disable the rx device */
3515 retval = disable_snd_device(adev,
3516 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
3517 list_remove(&uc_info.list);
3518 }
3519 }
3520 adev->active_input = NULL; /* restore adev state */
3521 return 0;
3522}
3523
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003524static int adev_close(hw_device_t *device)
3525{
Steve Kondik3abbbc82014-11-29 14:14:43 -08003526 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003527 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07003528
3529 if (!adev)
3530 return 0;
3531
3532 pthread_mutex_lock(&adev_init_lock);
3533
3534 if ((--audio_device_ref_count) == 0) {
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303535#ifdef USES_AUDIO_AMPLIFIER
3536 if (amplifier_close() != 0)
3537 ALOGE("Amplifier close failed");
3538#endif
Kiran Kandide144c82013-11-20 15:58:32 -08003539 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07003540 audio_route_free(adev->audio_route);
3541 free(adev->snd_dev_ref_cnt);
3542 platform_deinit(adev->platform);
Eric Laurentda46bfb2014-08-25 22:39:29 -05003543 audio_extn_extspk_deinit(adev->extspk);
Steve Kondik3abbbc82014-11-29 14:14:43 -08003544 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
3545 pcm_params_free(adev->use_case_table[i]);
3546 }
Kiran Kandi910e1862013-10-29 13:29:42 -07003547 free(device);
3548 adev = NULL;
3549 }
3550 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003551 return 0;
3552}
3553
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003554/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
3555 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
3556 * just that it _might_ work.
3557 */
3558static int period_size_is_plausible_for_low_latency(int period_size)
3559{
3560 switch (period_size) {
3561 case 160:
3562 case 240:
3563 case 320:
3564 case 480:
3565 return 1;
3566 default:
3567 return 0;
3568 }
3569}
3570
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003571static int adev_open(const hw_module_t *module, const char *name,
3572 hw_device_t **device)
3573{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003574 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003575
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003576 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003577 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3578
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003579 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07003580 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003581 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07003582 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003583 ALOGD("%s: returning existing instance of adev", __func__);
3584 ALOGD("%s: exit", __func__);
3585 pthread_mutex_unlock(&adev_init_lock);
3586 return 0;
3587 }
3588
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003589 adev = calloc(1, sizeof(struct audio_device));
3590
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003591 if (!adev) {
3592 pthread_mutex_unlock(&adev_init_lock);
3593 return -ENOMEM;
3594 }
3595
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07003596 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3597
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003598 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3599 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3600 adev->device.common.module = (struct hw_module_t *)module;
3601 adev->device.common.close = adev_close;
3602
3603 adev->device.init_check = adev_init_check;
3604 adev->device.set_voice_volume = adev_set_voice_volume;
3605 adev->device.set_master_volume = adev_set_master_volume;
3606 adev->device.get_master_volume = adev_get_master_volume;
3607 adev->device.set_master_mute = adev_set_master_mute;
3608 adev->device.get_master_mute = adev_get_master_mute;
3609 adev->device.set_mode = adev_set_mode;
3610 adev->device.set_mic_mute = adev_set_mic_mute;
3611 adev->device.get_mic_mute = adev_get_mic_mute;
3612 adev->device.set_parameters = adev_set_parameters;
3613 adev->device.get_parameters = adev_get_parameters;
3614 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3615 adev->device.open_output_stream = adev_open_output_stream;
3616 adev->device.close_output_stream = adev_close_output_stream;
3617 adev->device.open_input_stream = adev_open_input_stream;
3618 adev->device.close_input_stream = adev_close_input_stream;
3619 adev->device.dump = adev_dump;
3620
3621 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003622 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003623 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003624 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003625 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003626 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003627 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003628 /* adev->cur_hdmi_channels = 0; by calloc() */
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08003629 adev->cur_codec_backend_samplerate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
3630 adev->cur_codec_backend_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Eric Laurentb23d5282013-05-14 15:27:20 -07003631 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003632 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003633 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly9b7e96b2014-02-14 14:45:49 -08003634 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07003635 adev->offload_usecases_state = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003636
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303637 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
3638 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
3639
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003640 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003641 adev->platform = platform_init(adev);
3642 if (!adev->platform) {
3643 free(adev->snd_dev_ref_cnt);
3644 free(adev);
3645 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3646 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08003647 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003648 return -EINVAL;
3649 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003650
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303651 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
Eric Laurentda46bfb2014-08-25 22:39:29 -05003652 adev->extspk = audio_extn_extspk_init(adev);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303653
Eric Laurentc4aef752013-09-12 17:45:53 -07003654 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
3655 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3656 if (adev->visualizer_lib == NULL) {
3657 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3658 } else {
3659 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3660 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003661 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003662 "visualizer_hal_start_output");
3663 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003664 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003665 "visualizer_hal_stop_output");
3666 }
3667 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003668 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07003669
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003670 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
3671 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3672 if (adev->offload_effects_lib == NULL) {
3673 ALOGE("%s: DLOPEN failed for %s", __func__,
3674 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3675 } else {
3676 ALOGV("%s: DLOPEN successful for %s", __func__,
3677 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3678 adev->offload_effects_start_output =
3679 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3680 "offload_effects_bundle_hal_start_output");
3681 adev->offload_effects_stop_output =
3682 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3683 "offload_effects_bundle_hal_stop_output");
3684 }
3685 }
3686
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003687 *device = &adev->device.common;
Steve Kondik3abbbc82014-11-29 14:14:43 -08003688 if (k_enable_extended_precision)
3689 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003690
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303691#ifdef USES_AUDIO_AMPLIFIER
3692 if (amplifier_open() != 0)
3693 ALOGE("Amplifier initialization failed");
3694#endif
3695
Kiran Kandi910e1862013-10-29 13:29:42 -07003696 audio_device_ref_count++;
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003697
3698 char value[PROPERTY_VALUE_MAX];
3699 int trial;
3700 if (property_get("audio_hal.period_size", value, NULL) > 0) {
3701 trial = atoi(value);
3702 if (period_size_is_plausible_for_low_latency(trial)) {
3703 pcm_config_low_latency.period_size = trial;
3704 pcm_config_low_latency.start_threshold = trial / 4;
3705 pcm_config_low_latency.avail_min = trial / 4;
3706 configured_low_latency_capture_period_size = trial;
3707 }
3708 }
3709 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
3710 trial = atoi(value);
3711 if (period_size_is_plausible_for_low_latency(trial)) {
3712 configured_low_latency_capture_period_size = trial;
3713 }
3714 }
3715
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003716 pthread_mutex_unlock(&adev_init_lock);
3717
Eric Laurent994a6932013-07-17 11:51:42 -07003718 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003719 return 0;
3720}
3721
3722static struct hw_module_methods_t hal_module_methods = {
3723 .open = adev_open,
3724};
3725
3726struct audio_module HAL_MODULE_INFO_SYM = {
3727 .common = {
3728 .tag = HARDWARE_MODULE_TAG,
3729 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3730 .hal_api_version = HARDWARE_HAL_API_VERSION,
3731 .id = AUDIO_HARDWARE_MODULE_ID,
3732 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08003733 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003734 .methods = &hal_module_methods,
3735 },
3736};