blob: 55179e764a9ae970aac03e88e0178cf717893c4c [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
29#include <errno.h>
30#include <pthread.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080034#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070035#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070036#include <sys/resource.h>
37#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038
39#include <cutils/log.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070042#include <cutils/atomic.h>
43#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080044
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <hardware/audio_effect.h>
Steve Kondik3abbbc82014-11-29 14:14:43 -080046#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070047#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070048#include <audio_effects/effect_aec.h>
49#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080050#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070051#include "platform_api.h"
52#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070053#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080054#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080055
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070056#include "sound/compress_params.h"
ApurupaPattapu0c566872014-01-10 14:46:02 -080057#include "sound/asound.h"
58
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +053059#ifdef USES_AUDIO_AMPLIFIER
60#include <audio_amplifier.h>
61#endif
62
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070063#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
64/* ToDo: Check and update a proper value in msec */
65#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
66#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
67
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +053068#define PROXY_OPEN_RETRY_COUNT 100
69#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapu0c566872014-01-10 14:46:02 -080070
Steve Kondikb045f472014-11-12 23:24:07 -080071#ifdef LOW_LATENCY_PRIMARY
72#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_LOW_LATENCY
73#else
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080074#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
Steve Kondikb045f472014-11-12 23:24:07 -080075#endif
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080076
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -070077static unsigned int configured_low_latency_capture_period_size =
78 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
79
Steve Kondik3abbbc82014-11-29 14:14:43 -080080/* This constant enables extended precision handling.
Steve Kondik3abbbc82014-11-29 14:14:43 -080081 */
82static const bool k_enable_extended_precision = true;
83
Eric Laurentb23d5282013-05-14 15:27:20 -070084struct pcm_config pcm_config_deep_buffer = {
85 .channels = 2,
86 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
87 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
88 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
89 .format = PCM_FORMAT_S16_LE,
90 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
91 .stop_threshold = INT_MAX,
92 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
93};
94
95struct pcm_config pcm_config_low_latency = {
96 .channels = 2,
97 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
98 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
99 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
100 .format = PCM_FORMAT_S16_LE,
101 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
102 .stop_threshold = INT_MAX,
103 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
104};
105
106struct pcm_config pcm_config_hdmi_multi = {
107 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
108 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
109 .period_size = HDMI_MULTI_PERIOD_SIZE,
110 .period_count = HDMI_MULTI_PERIOD_COUNT,
111 .format = PCM_FORMAT_S16_LE,
112 .start_threshold = 0,
113 .stop_threshold = INT_MAX,
114 .avail_min = 0,
115};
116
117struct pcm_config pcm_config_audio_capture = {
118 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700119 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
120 .format = PCM_FORMAT_S16_LE,
121};
122
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530123#define AFE_PROXY_CHANNEL_COUNT 2
124#define AFE_PROXY_SAMPLING_RATE 48000
125
126#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
127#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
128
129struct pcm_config pcm_config_afe_proxy_playback = {
130 .channels = AFE_PROXY_CHANNEL_COUNT,
131 .rate = AFE_PROXY_SAMPLING_RATE,
132 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
133 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
134 .format = PCM_FORMAT_S16_LE,
135 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
136 .stop_threshold = INT_MAX,
137 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
138};
139
140#define AFE_PROXY_RECORD_PERIOD_SIZE 768
141#define AFE_PROXY_RECORD_PERIOD_COUNT 4
142
143struct pcm_config pcm_config_afe_proxy_record = {
144 .channels = AFE_PROXY_CHANNEL_COUNT,
145 .rate = AFE_PROXY_SAMPLING_RATE,
146 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
147 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
148 .format = PCM_FORMAT_S16_LE,
149 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
150 .stop_threshold = INT_MAX,
151 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
152};
153
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800154const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700155 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
156 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
157 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700158 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700159#ifdef MULTIPLE_OFFLOAD_ENABLED
160 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
161 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
162 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
163 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
164 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
165 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
166 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
167 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
168#endif
Eric Laurentb23d5282013-05-14 15:27:20 -0700169 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700170 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700171 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700172 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700173 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800174 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800175 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700176 [USECASE_VOICE_CALL] = "voice-call",
Mingming Yinee733602014-04-03 17:47:22 -0700177
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700178 [USECASE_VOICE2_CALL] = "voice2-call",
179 [USECASE_VOLTE_CALL] = "volte-call",
180 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat111aeb32014-02-12 17:58:59 -0800181 [USECASE_VOWLAN_CALL] = "vowlan-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800182 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700183 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
184 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
185 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800186 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
187 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
188 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
189
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700190 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
191 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700192 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
193 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530194
195 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
196 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700197};
198
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700199static const audio_usecase_t offload_usecases[] = {
200 USECASE_AUDIO_PLAYBACK_OFFLOAD,
201#ifdef MULTIPLE_OFFLOAD_ENABLED
202 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
203 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
Steve Kondikac72feb2014-07-20 23:15:34 -0700204#ifndef PLATFORM_MSM8974
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700205 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
206 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
207 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
208 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
Steve Kondikac72feb2014-07-20 23:15:34 -0700209#endif
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700210 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
Steve Kondikac72feb2014-07-20 23:15:34 -0700211#ifndef PLATFORM_MSM8974
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700212 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
213#endif
Steve Kondikac72feb2014-07-20 23:15:34 -0700214#endif
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700215};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800216
217#define STRING_TO_ENUM(string) { #string, string }
218
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800219struct string_to_enum {
220 const char *name;
221 uint32_t value;
222};
223
224static const struct string_to_enum out_channels_name_to_enum_table[] = {
225 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
226 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
227 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
228};
229
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700230static struct audio_device *adev = NULL;
231static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700232static unsigned int audio_device_ref_count;
233
Haynes Mathew George5191a852013-09-11 14:19:36 -0700234static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -0800235
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -0800236static int check_and_set_gapless_mode(struct audio_device *adev) {
237
238
239 char value[PROPERTY_VALUE_MAX] = {0};
240 bool gapless_enabled = false;
241 const char *mixer_ctl_name = "Compress Gapless Playback";
242 struct mixer_ctl *ctl;
243
244 ALOGV("%s:", __func__);
245 property_get("audio.offload.gapless.enabled", value, NULL);
246 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
247
248 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
249 if (!ctl) {
250 ALOGE("%s: Could not get ctl for mixer cmd - %s",
251 __func__, mixer_ctl_name);
252 return -EINVAL;
253 }
254
255 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
256 ALOGE("%s: Could not set gapless mode %d",
257 __func__, gapless_enabled);
258 return -EINVAL;
259 }
260 return 0;
261}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700262
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700263static bool is_supported_format(audio_format_t format)
264{
Steve Kondik3fdf4fc2014-12-10 21:15:43 -0800265 switch (format) {
Steve Kondik3abbbc82014-11-29 14:14:43 -0800266 case AUDIO_FORMAT_MP3:
Steve Kondik3fdf4fc2014-12-10 21:15:43 -0800267 case AUDIO_FORMAT_PCM_16_BIT_OFFLOAD:
268 case AUDIO_FORMAT_PCM_24_BIT_OFFLOAD:
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700269#ifdef FLAC_OFFLOAD_ENABLED
Steve Kondik3abbbc82014-11-29 14:14:43 -0800270 case AUDIO_FORMAT_FLAC:
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700271#endif
Steve Kondik3abbbc82014-11-29 14:14:43 -0800272#ifdef WMA_OFFLOAD_ENABLED
273 case AUDIO_FORMAT_WMA:
274 case AUDIO_FORMAT_WMA_PRO:
275#endif
276#ifdef MP2_OFFLOAD_ENABLED
277 case AUDIO_FORMAT_MP2:
278#endif
279 case AUDIO_FORMAT_AAC_LC:
280 case AUDIO_FORMAT_AAC_HE_V1:
281 case AUDIO_FORMAT_AAC_HE_V2:
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530282 return true;
Steve Kondik3abbbc82014-11-29 14:14:43 -0800283
Steve Kondik3fdf4fc2014-12-10 21:15:43 -0800284 default:
285 return false;
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530286 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700287 return false;
288}
289
290static int get_snd_codec_id(audio_format_t format)
291{
292 int id = 0;
293
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530294 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700295 case AUDIO_FORMAT_MP3:
296 id = SND_AUDIOCODEC_MP3;
297 break;
298 case AUDIO_FORMAT_AAC:
299 id = SND_AUDIOCODEC_AAC;
300 break;
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530301 case AUDIO_FORMAT_PCM_OFFLOAD:
ApurupaPattapu0c566872014-01-10 14:46:02 -0800302 id = SND_AUDIOCODEC_PCM;
303 break;
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700304#ifdef FLAC_OFFLOAD_ENABLED
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800305 case AUDIO_FORMAT_FLAC:
306 id = SND_AUDIOCODEC_FLAC;
307 break;
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700308#endif
Ethan Chenc856d972014-07-25 14:57:15 -0700309#ifdef WMA_OFFLOAD_ENABLED
Steve Kondikba3b35d2014-07-18 01:49:48 -0700310 case AUDIO_FORMAT_WMA:
311 id = SND_AUDIOCODEC_WMA;
312 break;
313 case AUDIO_FORMAT_WMA_PRO:
314 id = SND_AUDIOCODEC_WMA_PRO;
315 break;
Ethan Chenc856d972014-07-25 14:57:15 -0700316#endif
317#ifdef MP2_OFFLOAD_ENABLED
Steve Kondikba3b35d2014-07-18 01:49:48 -0700318 case AUDIO_FORMAT_MP2:
319 id = SND_AUDIOCODEC_MP2;
320 break;
Ethan Chenc856d972014-07-25 14:57:15 -0700321#endif
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700322 default:
Mingming Yin90310102013-11-13 16:57:00 -0800323 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700324 }
325
326 return id;
327}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800328
Avinash Vaish8005dc82014-07-24 15:36:33 +0530329static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
330 struct audio_usecase *uc_info)
331{
332 struct listnode *node;
333 struct audio_usecase *usecase;
334
335 if (uc_info == NULL)
336 return -EINVAL;
337
338 /* Re-route all voice usecases on the shared backend other than the
339 specified usecase to new snd devices */
340 list_for_each(node, &adev->usecase_list) {
341 usecase = node_to_item(node, struct audio_usecase, list);
342 if ((usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) &&
343 (usecase != uc_info))
344 enable_audio_route(adev, usecase);
345 }
346 return 0;
347}
348
Venkata Narendra Kumar Guttac9e9e2c2014-07-09 16:29:28 +0530349int get_snd_card_state(struct audio_device *adev)
Naresh Tannirucef332d2014-06-04 18:17:56 +0530350{
351 int snd_scard_state;
352
353 if (!adev)
354 return SND_CARD_STATE_OFFLINE;
355
356 pthread_mutex_lock(&adev->snd_card_status.lock);
357 snd_scard_state = adev->snd_card_status.state;
358 pthread_mutex_unlock(&adev->snd_card_status.lock);
359
360 return snd_scard_state;
361}
362
363static int set_snd_card_state(struct audio_device *adev, int snd_scard_state)
364{
365 if (!adev)
366 return -ENOSYS;
367
368 pthread_mutex_lock(&adev->snd_card_status.lock);
369 adev->snd_card_status.state = snd_scard_state;
370 pthread_mutex_unlock(&adev->snd_card_status.lock);
371
372 return 0;
373}
374
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -0700375int pcm_ioctl(struct pcm *pcm, int request, ...)
376{
377 va_list ap;
378 void * arg;
379 int pcm_fd = *(int*)pcm;
380
381 va_start(ap, request);
382 arg = va_arg(ap, void *);
383 va_end(ap);
384
385 return ioctl(pcm_fd, request, arg);
386}
387
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700388int enable_audio_route(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700389 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800390{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700391 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700392 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800393
394 if (usecase == NULL)
395 return -EINVAL;
396
397 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
398
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800399 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700400 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800401 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700402 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800403
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -0800404#ifdef DS1_DOLBY_DAP_ENABLED
405 audio_extn_dolby_set_dmid(adev);
406 audio_extn_dolby_set_endpoint(adev);
407#endif
Dhananjay Kumara153c872014-05-29 21:47:27 +0530408 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800409 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700410 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700411 ALOGV("%s: apply mixer and update path: %s", __func__, mixer_path);
412 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800413 ALOGV("%s: exit", __func__);
414 return 0;
415}
416
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700417int disable_audio_route(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700418 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800419{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700420 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700421 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800422
423 if (usecase == NULL)
424 return -EINVAL;
425
426 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700427 if (usecase->type == PCM_CAPTURE)
428 snd_device = usecase->in_snd_device;
429 else
430 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800431 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700432 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700433 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
434 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Dhananjay Kumara153c872014-05-29 21:47:27 +0530435 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800436 ALOGV("%s: exit", __func__);
437 return 0;
438}
439
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700440int enable_snd_device(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700441 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800442{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700443 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
444
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800445 if (snd_device < SND_DEVICE_MIN ||
446 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800447 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800448 return -EINVAL;
449 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700450
451 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700452
453 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
454 ALOGE("%s: Invalid sound device returned", __func__);
455 return -EINVAL;
456 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700457 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700458 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700459 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700460 return 0;
461 }
462
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700463 /* start usb playback thread */
464 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
465 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
466 audio_extn_usb_start_playback(adev);
467
468 /* start usb capture thread */
469 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
470 audio_extn_usb_start_capture(adev);
471
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700472 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
473 audio_extn_spkr_prot_is_enabled()) {
474 if (audio_extn_spkr_prot_start_processing(snd_device)) {
475 ALOGE("%s: spkr_start_processing failed", __func__);
476 return -EINVAL;
477 }
478 } else {
479 ALOGV("%s: snd_device(%d: %s)", __func__,
Dhananjay Kumara153c872014-05-29 21:47:27 +0530480 snd_device, device_name);
481 /* due to the possibility of calibration overwrite between listen
482 and audio, notify listen hal before audio calibration is sent */
483 audio_extn_listen_update_device_status(snd_device,
484 LISTEN_EVENT_SND_DEVICE_BUSY);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700485 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
486 adev->snd_dev_ref_cnt[snd_device]--;
Dhananjay Kumara153c872014-05-29 21:47:27 +0530487 audio_extn_listen_update_device_status(snd_device,
488 LISTEN_EVENT_SND_DEVICE_FREE);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700489 return -EINVAL;
490 }
Kiran Kandide144c82013-11-20 15:58:32 -0800491
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700492 audio_route_apply_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800493 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800494 return 0;
495}
496
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700497int disable_snd_device(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700498 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800499{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700500 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
501
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800502 if (snd_device < SND_DEVICE_MIN ||
503 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800504 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800505 return -EINVAL;
506 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700507 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
508 ALOGE("%s: device ref cnt is already 0", __func__);
509 return -EINVAL;
510 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700511
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700512 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700513
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700514 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
515 ALOGE("%s: Invalid sound device returned", __func__);
516 return -EINVAL;
517 }
518
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700519 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700520 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700521 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800522 /* exit usb play back thread */
523 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
524 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
525 audio_extn_usb_stop_playback();
526
527 /* exit usb capture thread */
528 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
Rajshekar Eashwarappa4dfa0752014-10-02 20:18:20 +0530529 audio_extn_usb_stop_capture();
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700530 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
531 audio_extn_spkr_prot_is_enabled()) {
532 audio_extn_spkr_prot_stop_processing();
533 } else
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700534 audio_route_reset_and_update_path(adev->audio_route, device_name);
Kiran Kandide144c82013-11-20 15:58:32 -0800535
Dhananjay Kumara153c872014-05-29 21:47:27 +0530536 audio_extn_listen_update_device_status(snd_device,
Kiran Kandide144c82013-11-20 15:58:32 -0800537 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700538 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700539
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800540 return 0;
541}
542
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700543static void check_usecases_codec_backend(struct audio_device *adev,
544 struct audio_usecase *uc_info,
545 snd_device_t snd_device)
546{
547 struct listnode *node;
548 struct audio_usecase *usecase;
549 bool switch_device[AUDIO_USECASE_MAX];
550 int i, num_uc_to_switch = 0;
551
552 /*
553 * This function is to make sure that all the usecases that are active on
554 * the hardware codec backend are always routed to any one device that is
555 * handled by the hardware codec.
556 * For example, if low-latency and deep-buffer usecases are currently active
557 * on speaker and out_set_parameters(headset) is received on low-latency
558 * output, then we have to make sure deep-buffer is also switched to headset,
559 * because of the limitation that both the devices cannot be enabled
560 * at the same time as they share the same backend.
561 */
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800562 /*
563 * This call is to check if we need to force routing for a particular stream
564 * If there is a backend configuration change for the device when a
565 * new stream starts, then ADM needs to be closed and re-opened with the new
566 * configuraion. This call check if we need to re-route all the streams
567 * associated with the backend. Touch tone + 24 bit playback.
568 */
569 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info);
570
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700571 /* Disable all the usecases on the shared backend other than the
572 specified usecase */
573 for (i = 0; i < AUDIO_USECASE_MAX; i++)
574 switch_device[i] = false;
575
576 list_for_each(node, &adev->usecase_list) {
577 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800578 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700579 usecase != uc_info &&
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800580 (usecase->out_snd_device != snd_device || force_routing) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700581 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
582 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
583 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700584 platform_get_snd_device_name(usecase->out_snd_device));
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700585 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700586 switch_device[usecase->id] = true;
587 num_uc_to_switch++;
588 }
589 }
590
591 if (num_uc_to_switch) {
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700592 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700593
594 list_for_each(node, &adev->usecase_list) {
595 usecase = node_to_item(node, struct audio_usecase, list);
596 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700597 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700598 }
599 }
600
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700601 list_for_each(node, &adev->usecase_list) {
602 usecase = node_to_item(node, struct audio_usecase, list);
603 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700604 enable_snd_device(adev, snd_device);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700605 }
606 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700607
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700608 /* Re-route all the usecases on the shared backend other than the
609 specified usecase to new snd devices */
610 list_for_each(node, &adev->usecase_list) {
611 usecase = node_to_item(node, struct audio_usecase, list);
612 /* Update the out_snd_device only before enabling the audio route */
613 if (switch_device[usecase->id] ) {
614 usecase->out_snd_device = snd_device;
Avinash Vaish8005dc82014-07-24 15:36:33 +0530615 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
616 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700617 }
618 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700619 }
620}
621
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700622static void check_and_route_capture_usecases(struct audio_device *adev,
623 struct audio_usecase *uc_info,
624 snd_device_t snd_device)
625{
626 struct listnode *node;
627 struct audio_usecase *usecase;
628 bool switch_device[AUDIO_USECASE_MAX];
629 int i, num_uc_to_switch = 0;
630
631 /*
632 * This function is to make sure that all the active capture usecases
633 * are always routed to the same input sound device.
634 * For example, if audio-record and voice-call usecases are currently
635 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
636 * is received for voice call then we have to make sure that audio-record
637 * usecase is also switched to earpiece i.e. voice-dmic-ef,
638 * because of the limitation that two devices cannot be enabled
639 * at the same time if they share the same backend.
640 */
641 for (i = 0; i < AUDIO_USECASE_MAX; i++)
642 switch_device[i] = false;
643
644 list_for_each(node, &adev->usecase_list) {
645 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800646 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700647 usecase != uc_info &&
Avinash Vaish44014792015-03-27 14:46:04 +0530648 usecase->in_snd_device != snd_device &&
649 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700650 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
651 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700652 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700653 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700654 switch_device[usecase->id] = true;
655 num_uc_to_switch++;
656 }
657 }
658
659 if (num_uc_to_switch) {
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700660 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700661
662 list_for_each(node, &adev->usecase_list) {
663 usecase = node_to_item(node, struct audio_usecase, list);
664 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700665 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800666 }
667 }
668
669 list_for_each(node, &adev->usecase_list) {
670 usecase = node_to_item(node, struct audio_usecase, list);
671 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700672 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700673 }
674 }
675
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700676 /* Re-route all the usecases on the shared backend other than the
677 specified usecase to new snd devices */
678 list_for_each(node, &adev->usecase_list) {
679 usecase = node_to_item(node, struct audio_usecase, list);
680 /* Update the in_snd_device only before enabling the audio route */
681 if (switch_device[usecase->id] ) {
682 usecase->in_snd_device = snd_device;
Avinash Vaish8005dc82014-07-24 15:36:33 +0530683 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
684 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700685 }
686 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700687 }
688}
689
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800690/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700691static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800692{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700693 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700694 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800695
696 switch (channels) {
697 /*
698 * Do not handle stereo output in Multi-channel cases
699 * Stereo case is handled in normal playback path
700 */
701 case 6:
702 ALOGV("%s: HDMI supports 5.1", __func__);
703 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
704 break;
705 case 8:
706 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
707 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
708 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
709 break;
710 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700711 ALOGE("HDMI does not support multi channel playback");
712 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800713 break;
714 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700715 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800716}
717
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700718static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
719{
720 struct audio_usecase *usecase;
721 struct listnode *node;
722
723 list_for_each(node, &adev->usecase_list) {
724 usecase = node_to_item(node, struct audio_usecase, list);
725 if (usecase->type == VOICE_CALL) {
726 ALOGV("%s: usecase id %d", __func__, usecase->id);
727 return usecase->id;
728 }
729 }
730 return USECASE_INVALID;
731}
732
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700733struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700734 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700735{
736 struct audio_usecase *usecase;
737 struct listnode *node;
738
739 list_for_each(node, &adev->usecase_list) {
740 usecase = node_to_item(node, struct audio_usecase, list);
741 if (usecase->id == uc_id)
742 return usecase;
743 }
744 return NULL;
745}
746
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700747int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800748{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800749 snd_device_t out_snd_device = SND_DEVICE_NONE;
750 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700751 struct audio_usecase *usecase = NULL;
752 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800753 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800754 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed739e7152014-01-23 15:56:53 -0800755 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800756 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700757 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800758
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700759 usecase = get_usecase_from_list(adev, uc_id);
760 if (usecase == NULL) {
761 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
762 return -EINVAL;
763 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800764
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800765 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800766 (usecase->type == VOIP_CALL) ||
767 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700768 out_snd_device = platform_get_output_snd_device(adev->platform,
769 usecase->stream.out->devices);
770 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700771 usecase->devices = usecase->stream.out->devices;
772 } else {
773 /*
774 * If the voice call is active, use the sound devices of voice call usecase
775 * so that it would not result any device switch. All the usecases will
776 * be switched to new device when select_devices() is called for voice call
777 * usecase. This is to avoid switching devices for voice call when
778 * check_usecases_codec_backend() is called below.
779 */
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -0700780 if (adev->voice.in_call && adev->mode == AUDIO_MODE_IN_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700781 vc_usecase = get_usecase_from_list(adev,
782 get_voice_usecase_id_from_list(adev));
Mingming Yin6c23f382014-08-14 00:00:51 -0700783 if ((vc_usecase) && ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
784 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700785 in_snd_device = vc_usecase->in_snd_device;
786 out_snd_device = vc_usecase->out_snd_device;
787 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800788 } else if (voice_extn_compress_voip_is_active(adev)) {
789 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Mingming Yin6c23f382014-08-14 00:00:51 -0700790 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Avinash Vaish6a4c3372014-06-25 12:20:37 +0530791 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Mingming Yin6c23f382014-08-14 00:00:51 -0700792 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800793 in_snd_device = voip_usecase->in_snd_device;
794 out_snd_device = voip_usecase->out_snd_device;
795 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800796 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed739e7152014-01-23 15:56:53 -0800797 hfp_ucid = audio_extn_hfp_get_usecase();
798 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin6c23f382014-08-14 00:00:51 -0700799 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800800 in_snd_device = hfp_usecase->in_snd_device;
801 out_snd_device = hfp_usecase->out_snd_device;
802 }
Vimal Puthanveed8fa9eab2014-01-07 16:47:47 -0800803 } else if (audio_extn_hfp_is_active(adev)) {
804 hfp_usecase = get_usecase_from_list(adev, USECASE_AUDIO_HFP_SCO);
805 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
806 in_snd_device = hfp_usecase->in_snd_device;
807 out_snd_device = hfp_usecase->out_snd_device;
808 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700809 }
810 if (usecase->type == PCM_PLAYBACK) {
811 usecase->devices = usecase->stream.out->devices;
812 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700813 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700814 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700815 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700816 if (usecase->stream.out == adev->primary_output &&
Shreyas Nagasandra Chandrasekhar23b3af22014-09-03 19:10:01 +0530817 adev->active_input ) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700818 select_devices(adev, adev->active_input->usecase);
819 }
820 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700821 } else if (usecase->type == PCM_CAPTURE) {
822 usecase->devices = usecase->stream.in->device;
823 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700824 if (in_snd_device == SND_DEVICE_NONE) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530825 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Shreyas Nagasandra Chandrasekhar23b3af22014-09-03 19:10:01 +0530826 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 -0700827 adev->primary_output && !adev->primary_output->standby) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530828 out_device = adev->primary_output->devices;
829 } else if(usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
830 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700831 }
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530832 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700833 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700834 }
835 }
836
837 if (out_snd_device == usecase->out_snd_device &&
838 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800839 return 0;
840 }
841
sangwoobc677242013-08-08 16:53:43 +0900842 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700843 out_snd_device, platform_get_snd_device_name(out_snd_device),
844 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800845
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800846 /*
847 * Limitation: While in call, to do a device switch we need to disable
848 * and enable both RX and TX devices though one of them is same as current
849 * device.
850 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800851 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700852 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800853 }
854
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700855 /* Disable current sound devices */
856 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700857 disable_audio_route(adev, usecase);
858 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800859 }
860
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700861 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700862 disable_audio_route(adev, usecase);
863 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800864 }
865
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700866 /* Enable new sound devices */
867 if (out_snd_device != SND_DEVICE_NONE) {
868 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
869 check_usecases_codec_backend(adev, usecase, out_snd_device);
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700870 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800871 }
872
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700873 if (in_snd_device != SND_DEVICE_NONE) {
874 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700875 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700876 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700877
Avinash Vaish8005dc82014-07-24 15:36:33 +0530878 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700879 status = platform_switch_voice_call_device_post(adev->platform,
880 out_snd_device,
881 in_snd_device);
Avinash Vaish8005dc82014-07-24 15:36:33 +0530882 enable_audio_route_for_voice_usecases(adev, usecase);
883 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800884
sangwoo170731f2013-06-08 15:36:36 +0900885 usecase->in_snd_device = in_snd_device;
886 usecase->out_snd_device = out_snd_device;
887
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700888 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900889
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +0530890#ifdef USES_AUDIO_AMPLIFIER
891 /* Rely on amplifier_set_devices to distinguish between in/out devices */
892 amplifier_set_devices(in_snd_device);
893 amplifier_set_devices(out_snd_device);
894#endif
895
896
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800897 /* Applicable only on the targets that has external modem.
898 * Enable device command should be sent to modem only after
899 * enabling voice call mixer controls
900 */
901 if (usecase->type == VOICE_CALL)
902 status = platform_switch_voice_call_usecase_route_post(adev->platform,
903 out_snd_device,
904 in_snd_device);
905
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +0530906 ALOGD("%s: done",__func__);
907
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800908 return status;
909}
910
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800911static int stop_input_stream(struct stream_in *in)
912{
913 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800914 struct audio_usecase *uc_info;
915 struct audio_device *adev = in->dev;
916
Eric Laurentc8400632013-02-14 19:04:54 -0800917 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800918
Eric Laurent994a6932013-07-17 11:51:42 -0700919 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700920 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800921 uc_info = get_usecase_from_list(adev, in->usecase);
922 if (uc_info == NULL) {
923 ALOGE("%s: Could not find the usecase (%d) in the list",
924 __func__, in->usecase);
925 return -EINVAL;
926 }
927
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800928 /* Close in-call recording streams */
929 voice_check_and_stop_incall_rec_usecase(adev, in);
930
Eric Laurent150dbfe2013-02-27 14:31:02 -0800931 /* 1. Disable stream specific mixer controls */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700932 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700933
934 /* 2. Disable the tx device */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700935 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800936
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800937 list_remove(&uc_info->list);
938 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800939
Eric Laurent994a6932013-07-17 11:51:42 -0700940 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800941 return ret;
942}
943
944int start_input_stream(struct stream_in *in)
945{
946 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800947 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800948 struct audio_usecase *uc_info;
949 struct audio_device *adev = in->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +0530950 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800951
Ramjee Singh240ac652015-07-31 16:12:29 +0530952 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
953 if (get_usecase_from_list(adev, usecase) == NULL)
954 in->usecase = usecase;
955
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +0530956 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
957 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700958
Naresh Tannirucef332d2014-06-04 18:17:56 +0530959 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
960 ALOGE("%s: sound card is not active/SSR returning error ", __func__);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530961 ret = -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530962 goto error_config;
963 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530964
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700965 /* Check if source matches incall recording usecase criteria */
966 ret = voice_check_and_set_incall_rec_usecase(adev, in);
967 if (ret)
968 goto error_config;
969 else
970 ALOGV("%s: usecase(%d)", __func__, in->usecase);
971
Ramjee Singh240ac652015-07-31 16:12:29 +0530972 if (get_usecase_from_list(adev, in->usecase) != NULL) {
973 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
974 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
975 goto error_config;
976 }
977
Eric Laurentb23d5282013-05-14 15:27:20 -0700978 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800979 if (in->pcm_device_id < 0) {
980 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
981 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800982 ret = -EINVAL;
983 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800984 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700985
986 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800987 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew George9f0254a2014-06-30 13:56:18 -0700988
989 if (!uc_info) {
990 ret = -ENOMEM;
991 goto error_config;
992 }
993
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800994 uc_info->id = in->usecase;
995 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800996 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700997 uc_info->devices = in->device;
998 uc_info->in_snd_device = SND_DEVICE_NONE;
999 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001000
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001001 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001002 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001003
Eric Laurentc8400632013-02-14 19:04:54 -08001004 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301005 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
1006
1007 unsigned int flags = PCM_IN;
1008 unsigned int pcm_open_retry_entry_count = 0;
1009
1010 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1011 flags |= PCM_MMAP | PCM_NOIRQ;
1012 pcm_open_retry_entry_count = PROXY_OPEN_RETRY_COUNT;
1013 }
1014
1015 while(1) {
1016 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
1017 flags, &in->config);
1018 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
1019 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
1020 if (in->pcm != NULL) {
1021 pcm_close(in->pcm);
1022 in->pcm = NULL;
1023 }
1024 if (pcm_open_retry_entry_count-- == 0) {
1025 ret = -EIO;
1026 goto error_open;
1027 }
1028 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1029 continue;
1030 }
1031 break;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001032 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301033
Eric Laurent994a6932013-07-17 11:51:42 -07001034 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -08001035 return ret;
1036
1037error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001038 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -08001039
1040error_config:
1041 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001042 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -08001043
1044 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001045}
1046
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001047/* must be called with out->lock locked */
1048static int send_offload_cmd_l(struct stream_out* out, int command)
1049{
1050 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
1051
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001052 if (!cmd) {
1053 ALOGE("failed to allocate mem for command 0x%x", command);
1054 return -ENOMEM;
1055 }
1056
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001057 ALOGVV("%s %d", __func__, command);
1058
1059 cmd->cmd = command;
1060 list_add_tail(&out->offload_cmd_list, &cmd->node);
1061 pthread_cond_signal(&out->offload_cond);
1062 return 0;
1063}
1064
1065/* must be called iwth out->lock locked */
1066static void stop_compressed_output_l(struct stream_out *out)
1067{
1068 out->offload_state = OFFLOAD_STATE_IDLE;
1069 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001070 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001071 if (out->compr != NULL) {
1072 compress_stop(out->compr);
1073 while (out->offload_thread_blocked) {
1074 pthread_cond_wait(&out->cond, &out->lock);
1075 }
1076 }
1077}
1078
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001079bool is_offload_usecase(audio_usecase_t uc_id)
1080{
1081 unsigned int i;
1082 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1083 if (uc_id == offload_usecases[i])
1084 return true;
1085 }
1086 return false;
1087}
1088
1089static audio_usecase_t get_offload_usecase(struct audio_device *adev)
1090{
1091 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1092 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
1093 char value[PROPERTY_VALUE_MAX] = {0};
1094
1095 property_get("audio.offload.multiple.enabled", value, NULL);
1096 if (!(atoi(value) || !strncmp("true", value, 4)))
1097 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
1098
1099 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
1100 for (i = 0; i < num_usecase; i++) {
1101 if (!(adev->offload_usecases_state & (0x1<<i))) {
1102 adev->offload_usecases_state |= 0x1 << i;
1103 ret = offload_usecases[i];
1104 break;
1105 }
1106 }
1107 ALOGV("%s: offload usecase is %d", __func__, ret);
1108 return ret;
1109}
1110
1111static void free_offload_usecase(struct audio_device *adev,
1112 audio_usecase_t uc_id)
1113{
1114 unsigned int i;
1115 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1116 if (offload_usecases[i] == uc_id) {
1117 adev->offload_usecases_state &= ~(0x1<<i);
1118 break;
1119 }
1120 }
1121 ALOGV("%s: free offload usecase %d", __func__, uc_id);
1122}
1123
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001124static void *offload_thread_loop(void *context)
1125{
1126 struct stream_out *out = (struct stream_out *) context;
1127 struct listnode *item;
Krishnankutty Kolathappilly0fe78f02014-01-06 18:33:58 -08001128 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001129
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001130 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1131 set_sched_policy(0, SP_FOREGROUND);
1132 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1133
1134 ALOGV("%s", __func__);
1135 pthread_mutex_lock(&out->lock);
1136 for (;;) {
1137 struct offload_cmd *cmd = NULL;
1138 stream_callback_event_t event;
1139 bool send_callback = false;
1140
1141 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1142 __func__, list_empty(&out->offload_cmd_list),
1143 out->offload_state);
1144 if (list_empty(&out->offload_cmd_list)) {
1145 ALOGV("%s SLEEPING", __func__);
1146 pthread_cond_wait(&out->offload_cond, &out->lock);
1147 ALOGV("%s RUNNING", __func__);
1148 continue;
1149 }
1150
1151 item = list_head(&out->offload_cmd_list);
1152 cmd = node_to_item(item, struct offload_cmd, node);
1153 list_remove(item);
1154
1155 ALOGVV("%s STATE %d CMD %d out->compr %p",
1156 __func__, out->offload_state, cmd->cmd, out->compr);
1157
1158 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1159 free(cmd);
1160 break;
1161 }
1162
1163 if (out->compr == NULL) {
1164 ALOGE("%s: Compress handle is NULL", __func__);
1165 pthread_cond_signal(&out->cond);
1166 continue;
1167 }
1168 out->offload_thread_blocked = true;
1169 pthread_mutex_unlock(&out->lock);
1170 send_callback = false;
1171 switch(cmd->cmd) {
1172 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
1173 compress_wait(out->compr, -1);
1174 send_callback = true;
1175 event = STREAM_CBK_EVENT_WRITE_READY;
1176 break;
1177 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappilly0fe78f02014-01-06 18:33:58 -08001178 ret = compress_next_track(out->compr);
1179 if(ret == 0)
1180 compress_partial_drain(out->compr);
1181 else if(ret == -ETIMEDOUT)
1182 compress_drain(out->compr);
1183 else
1184 ALOGE("%s: Next track returned error %d",__func__, ret);
1185
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001186 send_callback = true;
1187 event = STREAM_CBK_EVENT_DRAIN_READY;
Ravi Kumar Alamanda3d316172014-12-02 19:21:51 -08001188 /* Resend the metadata for next iteration */
1189 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001190 break;
1191 case OFFLOAD_CMD_DRAIN:
1192 compress_drain(out->compr);
1193 send_callback = true;
1194 event = STREAM_CBK_EVENT_DRAIN_READY;
1195 break;
1196 default:
1197 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1198 break;
1199 }
1200 pthread_mutex_lock(&out->lock);
1201 out->offload_thread_blocked = false;
1202 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001203 if (send_callback) {
Ravi Kumar Alamanda3d316172014-12-02 19:21:51 -08001204 ALOGVV("%s: sending offload_callback event %d", __func__, event);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001205 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001206 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001207 free(cmd);
1208 }
1209
1210 pthread_cond_signal(&out->cond);
1211 while (!list_empty(&out->offload_cmd_list)) {
1212 item = list_head(&out->offload_cmd_list);
1213 list_remove(item);
1214 free(node_to_item(item, struct offload_cmd, node));
1215 }
1216 pthread_mutex_unlock(&out->lock);
1217
1218 return NULL;
1219}
1220
1221static int create_offload_callback_thread(struct stream_out *out)
1222{
1223 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1224 list_init(&out->offload_cmd_list);
1225 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1226 offload_thread_loop, out);
1227 return 0;
1228}
1229
1230static int destroy_offload_callback_thread(struct stream_out *out)
1231{
1232 pthread_mutex_lock(&out->lock);
1233 stop_compressed_output_l(out);
1234 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1235
1236 pthread_mutex_unlock(&out->lock);
1237 pthread_join(out->offload_thread, (void **) NULL);
1238 pthread_cond_destroy(&out->offload_cond);
1239
1240 return 0;
1241}
1242
Eric Laurent07eeafd2013-10-06 12:52:49 -07001243static bool allow_hdmi_channel_config(struct audio_device *adev)
1244{
1245 struct listnode *node;
1246 struct audio_usecase *usecase;
1247 bool ret = true;
1248
1249 list_for_each(node, &adev->usecase_list) {
1250 usecase = node_to_item(node, struct audio_usecase, list);
1251 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1252 /*
1253 * If voice call is already existing, do not proceed further to avoid
1254 * disabling/enabling both RX and TX devices, CSD calls, etc.
1255 * Once the voice call done, the HDMI channels can be configured to
1256 * max channels of remaining use cases.
1257 */
1258 if (usecase->id == USECASE_VOICE_CALL) {
1259 ALOGD("%s: voice call is active, no change in HDMI channels",
1260 __func__);
1261 ret = false;
1262 break;
1263 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1264 ALOGD("%s: multi channel playback is active, "
1265 "no change in HDMI channels", __func__);
1266 ret = false;
1267 break;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001268 } else if (is_offload_usecase(usecase->id) &&
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05301269 audio_channel_count_from_out_mask(usecase->stream.out->channel_mask) > 2) {
Mingming Yin3ed162b2014-02-24 17:56:01 -08001270 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1271 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1272 ret = false;
1273 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001274 }
1275 }
1276 }
1277 return ret;
1278}
1279
1280static int check_and_set_hdmi_channels(struct audio_device *adev,
1281 unsigned int channels)
1282{
1283 struct listnode *node;
1284 struct audio_usecase *usecase;
1285
1286 /* Check if change in HDMI channel config is allowed */
1287 if (!allow_hdmi_channel_config(adev))
1288 return 0;
1289
1290 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001291 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001292 return 0;
1293 }
1294
1295 platform_set_hdmi_channels(adev->platform, channels);
1296 adev->cur_hdmi_channels = channels;
1297
1298 /*
1299 * Deroute all the playback streams routed to HDMI so that
1300 * the back end is deactivated. Note that backend will not
1301 * be deactivated if any one stream is connected to it.
1302 */
1303 list_for_each(node, &adev->usecase_list) {
1304 usecase = node_to_item(node, struct audio_usecase, list);
1305 if (usecase->type == PCM_PLAYBACK &&
1306 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001307 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001308 }
1309 }
1310
1311 /*
1312 * Enable all the streams disabled above. Now the HDMI backend
1313 * will be activated with new channel configuration
1314 */
1315 list_for_each(node, &adev->usecase_list) {
1316 usecase = node_to_item(node, struct audio_usecase, list);
1317 if (usecase->type == PCM_PLAYBACK &&
1318 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001319 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001320 }
1321 }
1322
1323 return 0;
1324}
1325
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001326static int stop_output_stream(struct stream_out *out)
1327{
1328 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001329 struct audio_usecase *uc_info;
1330 struct audio_device *adev = out->dev;
1331
Eric Laurent994a6932013-07-17 11:51:42 -07001332 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001333 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001334 uc_info = get_usecase_from_list(adev, out->usecase);
1335 if (uc_info == NULL) {
1336 ALOGE("%s: Could not find the usecase (%d) in the list",
1337 __func__, out->usecase);
1338 return -EINVAL;
1339 }
1340
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001341 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001342 if (adev->visualizer_stop_output != NULL)
1343 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1344 if (adev->offload_effects_stop_output != NULL)
1345 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1346 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001347
Eric Laurent150dbfe2013-02-27 14:31:02 -08001348 /* 1. Get and set stream specific mixer controls */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001349 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001350
1351 /* 2. Disable the rx device */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001352 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001353
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001354 list_remove(&uc_info->list);
1355 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001356
Eric Laurent07eeafd2013-10-06 12:52:49 -07001357 /* Must be called after removing the usecase from list */
1358 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1359 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1360
Eric Laurent994a6932013-07-17 11:51:42 -07001361 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001362 return ret;
1363}
1364
1365int start_output_stream(struct stream_out *out)
1366{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001367 int ret = 0;
Mingming Yin6c344432014-05-01 15:37:31 -07001368 int sink_channels = 0;
1369 char prop_value[PROPERTY_VALUE_MAX] = {0};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001370 struct audio_usecase *uc_info;
1371 struct audio_device *adev = out->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +05301372 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001373
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001374 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
1375 ret = -EINVAL;
1376 goto error_config;
1377 }
1378
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301379 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1380 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1381 out->devices);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301382
Naresh Tannirucef332d2014-06-04 18:17:56 +05301383 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301384 ALOGE("%s: sound card is not active/SSR returning error", __func__);
1385 ret = -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301386 goto error_config;
1387 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301388
Eric Laurentb23d5282013-05-14 15:27:20 -07001389 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001390 if (out->pcm_device_id < 0) {
1391 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1392 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001393 ret = -EINVAL;
1394 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001395 }
1396
1397 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001398
1399 if (!uc_info) {
1400 ret = -ENOMEM;
1401 goto error_config;
1402 }
1403
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404 uc_info->id = out->usecase;
1405 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001406 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001407 uc_info->devices = out->devices;
1408 uc_info->in_snd_device = SND_DEVICE_NONE;
1409 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001410
Eric Laurent07eeafd2013-10-06 12:52:49 -07001411 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001412 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Mingming Yin6c344432014-05-01 15:37:31 -07001413 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1414 if (!strncmp("true", prop_value, 4)) {
1415 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1416 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1417 check_and_set_hdmi_channels(adev, sink_channels);
1418 } else {
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001419 if (is_offload_usecase(out->usecase))
Mingming Yin6c344432014-05-01 15:37:31 -07001420 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1421 else
1422 check_and_set_hdmi_channels(adev, out->config.channels);
1423 }
Mingming Yin10fef6a2013-11-26 17:17:01 -08001424 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001425
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001426 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001427
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001428 select_devices(adev, out->usecase);
1429
Steve Kondik3abbbc82014-11-29 14:14:43 -08001430 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
1431 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
1432
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001433 if (!is_offload_usecase(out->usecase)) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301434 unsigned int flags = PCM_OUT;
1435 unsigned int pcm_open_retry_count = 0;
1436 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1437 flags |= PCM_MMAP | PCM_NOIRQ;
1438 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1439 } else
1440 flags |= PCM_MONOTONIC;
1441
1442 while (1) {
1443 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1444 flags, &out->config);
1445 if (out->pcm && !pcm_is_ready(out->pcm)) {
1446 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1447 if (out->pcm != NULL) {
1448 pcm_close(out->pcm);
1449 out->pcm = NULL;
1450 }
1451 if (pcm_open_retry_count-- == 0) {
1452 ret = -EIO;
1453 goto error_open;
1454 }
1455 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1456 continue;
1457 }
1458 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001459 }
1460 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001461 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001462 out->compr = compress_open(adev->snd_card,
1463 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001464 COMPRESS_IN, &out->compr_config);
1465 if (out->compr && !is_compress_ready(out->compr)) {
1466 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1467 compress_close(out->compr);
1468 out->compr = NULL;
1469 ret = -EIO;
1470 goto error_open;
1471 }
1472 if (out->offload_callback)
1473 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001474
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08001475#ifdef DS1_DOLBY_DDP_ENABLED
1476 if (audio_extn_is_dolby_format(out->format))
1477 audio_extn_dolby_send_ddp_endp_params(adev);
1478#endif
1479
Eric Laurentc4aef752013-09-12 17:45:53 -07001480 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001481 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1482 if (adev->offload_effects_start_output != NULL)
1483 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001484 }
Eric Laurent994a6932013-07-17 11:51:42 -07001485 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001486 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001487error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001488 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001489error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001490 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001491}
1492
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001493static int check_input_parameters(uint32_t sample_rate,
1494 audio_format_t format,
1495 int channel_count)
1496{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001497 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001498
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001499 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001500 !voice_extn_compress_voip_is_format_supported(format) &&
1501 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001502
1503 switch (channel_count) {
1504 case 1:
1505 case 2:
1506 case 6:
1507 break;
1508 default:
1509 ret = -EINVAL;
1510 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001511
1512 switch (sample_rate) {
1513 case 8000:
1514 case 11025:
1515 case 12000:
1516 case 16000:
1517 case 22050:
1518 case 24000:
1519 case 32000:
1520 case 44100:
1521 case 48000:
1522 break;
1523 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001524 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001525 }
1526
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001527 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001528}
1529
1530static size_t get_input_buffer_size(uint32_t sample_rate,
1531 audio_format_t format,
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07001532 int channel_count,
1533 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001534{
1535 size_t size = 0;
1536
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001537 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1538 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001539
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001540 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07001541 if (is_low_latency)
1542 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001543 /* ToDo: should use frame_size computed based on the format and
1544 channel_count here. */
1545 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001546
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07001547 /* make sure the size is multiple of 32 bytes
1548 * At 48 kHz mono 16-bit PCM:
1549 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1550 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1551 */
1552 size += 0x1f;
1553 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001554
1555 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001556}
1557
1558static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1559{
1560 struct stream_out *out = (struct stream_out *)stream;
1561
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001562 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001563}
1564
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301565static int out_set_sample_rate(struct audio_stream *stream __unused,
1566 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001567{
1568 return -ENOSYS;
1569}
1570
1571static size_t out_get_buffer_size(const struct audio_stream *stream)
1572{
1573 struct stream_out *out = (struct stream_out *)stream;
1574
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001575 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001576 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001577 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1578 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001579
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05301580 return out->config.period_size *
1581 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001582}
1583
1584static uint32_t out_get_channels(const struct audio_stream *stream)
1585{
1586 struct stream_out *out = (struct stream_out *)stream;
1587
1588 return out->channel_mask;
1589}
1590
1591static audio_format_t out_get_format(const struct audio_stream *stream)
1592{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001593 struct stream_out *out = (struct stream_out *)stream;
1594
1595 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001596}
1597
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301598static int out_set_format(struct audio_stream *stream __unused,
1599 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001600{
1601 return -ENOSYS;
1602}
1603
1604static int out_standby(struct audio_stream *stream)
1605{
1606 struct stream_out *out = (struct stream_out *)stream;
1607 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001608
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301609 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1610 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001611 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1612 /* Ignore standby in case of voip call because the voip output
1613 * stream is closed in adev_close_output_stream()
1614 */
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301615 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001616 return 0;
1617 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001618
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001619 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001620 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001621 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001622 out->standby = true;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001623 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001624 if (out->pcm) {
1625 pcm_close(out->pcm);
1626 out->pcm = NULL;
1627 }
1628 } else {
1629 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001630 out->gapless_mdata.encoder_delay = 0;
1631 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001632 if (out->compr != NULL) {
1633 compress_close(out->compr);
1634 out->compr = NULL;
1635 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001636 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001637 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001638 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001639 }
1640 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001641 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001642 return 0;
1643}
1644
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301645static int out_dump(const struct audio_stream *stream __unused,
1646 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001647{
1648 return 0;
1649}
1650
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001651static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1652{
1653 int ret = 0;
1654 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001655 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001656 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001657 tmp_mdata.encoder_delay = 0;
1658 tmp_mdata.encoder_padding = 0;
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001659
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001660 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001661 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001662 return -EINVAL;
1663 }
1664
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001665 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1666 if (ret >= 0) {
1667 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1668 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1669 ALOGV("ADTS format is set in offload mode");
1670 }
1671 out->send_new_metadata = 1;
1672 }
1673
Steve Kondik6bedcdf2014-07-21 11:48:30 -07001674#ifdef FLAC_OFFLOAD_ENABLED
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08001675 if (out->format == AUDIO_FORMAT_FLAC) {
1676 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_BLK_SIZE, value, sizeof(value));
1677 if (ret >= 0) {
1678 out->compr_config.codec->options.flac_dec.min_blk_size = atoi(value);
1679 out->send_new_metadata = 1;
1680 }
1681 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_BLK_SIZE, value, sizeof(value));
1682 if (ret >= 0) {
1683 out->compr_config.codec->options.flac_dec.max_blk_size = atoi(value);
1684 out->send_new_metadata = 1;
1685 }
1686 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_FRAME_SIZE, value, sizeof(value));
1687 if (ret >= 0) {
1688 out->compr_config.codec->options.flac_dec.min_frame_size = atoi(value);
1689 out->send_new_metadata = 1;
1690 }
1691 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_FRAME_SIZE, value, sizeof(value));
1692 if (ret >= 0) {
1693 out->compr_config.codec->options.flac_dec.max_frame_size = atoi(value);
1694 out->send_new_metadata = 1;
1695 }
1696 }
Steve Kondik6bedcdf2014-07-21 11:48:30 -07001697#endif
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08001698
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001699 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1700 if(ret >= 0)
1701 is_meta_data_params = true;
1702 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1703 if(ret >= 0 )
1704 is_meta_data_params = true;
1705 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1706 if(ret >= 0 )
1707 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001708 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1709 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001710 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001711 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001712 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001713 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1714 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001715 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001716 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001717 }
1718
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001719 if(!is_meta_data_params) {
1720 ALOGV("%s: Not gapless meta data params", __func__);
1721 return 0;
1722 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001723 out->gapless_mdata = tmp_mdata;
1724 out->send_new_metadata = 1;
1725 ALOGV("%s new encoder delay %u and padding %u", __func__,
1726 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1727
Steve Kondikba3b35d2014-07-18 01:49:48 -07001728 if(out->format == AUDIO_FORMAT_WMA || out->format == AUDIO_FORMAT_WMA_PRO) {
1729 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_FORMAT_TAG, value, sizeof(value));
1730 if (ret >= 0) {
1731 out->compr_config.codec->format = atoi(value);
1732 }
1733 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_BLOCK_ALIGN, value, sizeof(value));
1734 if (ret >= 0) {
1735 out->compr_config.codec->options.wma.super_block_align = atoi(value);
1736 }
1737 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_BIT_PER_SAMPLE, value, sizeof(value));
1738 if (ret >= 0) {
1739 out->compr_config.codec->options.wma.bits_per_sample = atoi(value);
1740 }
1741 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_CHANNEL_MASK, value, sizeof(value));
1742 if (ret >= 0) {
1743 out->compr_config.codec->options.wma.channelmask = atoi(value);
1744 }
1745 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_ENCODE_OPTION, value, sizeof(value));
1746 if (ret >= 0) {
1747 out->compr_config.codec->options.wma.encodeopt = atoi(value);
1748 }
1749 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_ENCODE_OPTION1, value, sizeof(value));
1750 if (ret >= 0) {
1751 out->compr_config.codec->options.wma.encodeopt1 = atoi(value);
1752 }
1753 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_ENCODE_OPTION2, value, sizeof(value));
1754 if (ret >= 0) {
1755 out->compr_config.codec->options.wma.encodeopt2 = atoi(value);
1756 }
1757 ALOGV("WMA params: fmt %x, balgn %x, sr %d, chmsk %x, encop %x, op1 %x, op2 %x",
1758 out->compr_config.codec->format,
1759 out->compr_config.codec->options.wma.super_block_align,
1760 out->compr_config.codec->options.wma.bits_per_sample,
1761 out->compr_config.codec->options.wma.channelmask,
1762 out->compr_config.codec->options.wma.encodeopt,
1763 out->compr_config.codec->options.wma.encodeopt1,
1764 out->compr_config.codec->options.wma.encodeopt2);
1765 }
1766
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001767 return 0;
1768}
1769
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301770static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1771{
1772 return out == adev->primary_output || out == adev->voice_tx_output;
1773}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001774
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001775static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1776{
1777 struct stream_out *out = (struct stream_out *)stream;
1778 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001779 struct audio_usecase *usecase;
1780 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001781 struct str_parms *parms;
1782 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001783 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001784 bool select_new_device = false;
Steve Kondik3abbbc82014-11-29 14:14:43 -08001785 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001786
sangwoobc677242013-08-08 16:53:43 +09001787 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001788 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001789 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001790 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1791 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001792 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001793 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001794 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001795
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001796 /*
Dhanalakshmi Siddani523ae412014-04-18 22:26:56 +05301797 * When HDMI cable is unplugged/usb hs is disconnected the
1798 * music playback is paused and the policy manager sends routing=0
1799 * But the audioflingercontinues to write data until standby time
1800 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001801 * Avoid this by routing audio to speaker until standby.
1802 */
Dhanalakshmi Siddani523ae412014-04-18 22:26:56 +05301803 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
Steve Kondikeadb6c82014-09-03 18:40:06 -07001804 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
1805 out->devices == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001806 val == AUDIO_DEVICE_NONE) {
1807 val = AUDIO_DEVICE_OUT_SPEAKER;
1808 }
1809
1810 /*
1811 * select_devices() call below switches all the usecases on the same
1812 * backend to the new device. Refer to check_usecases_codec_backend() in
1813 * the select_devices(). But how do we undo this?
1814 *
1815 * For example, music playback is active on headset (deep-buffer usecase)
1816 * and if we go to ringtones and select a ringtone, low-latency usecase
1817 * will be started on headset+speaker. As we can't enable headset+speaker
1818 * and headset devices at the same time, select_devices() switches the music
1819 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1820 * So when the ringtone playback is completed, how do we undo the same?
1821 *
1822 * We are relying on the out_set_parameters() call on deep-buffer output,
1823 * once the ringtone playback is ended.
1824 * NOTE: We should not check if the current devices are same as new devices.
1825 * Because select_devices() must be called to switch back the music
1826 * playback to headset.
1827 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001828 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001829 out->devices = val;
1830
1831 if (!out->standby)
1832 select_devices(adev, out->usecase);
1833
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001834 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301835 output_drives_call(adev, out)) {
1836 adev->current_call_output = out;
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001837 if (!adev->voice.in_call)
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301838 ret = voice_start_call(adev);
1839 else
1840 voice_update_devices_for_all_voice_usecases(adev);
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001841 }
1842 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001843
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001844 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1845 adev->voice.in_call &&
1846 output_drives_call(adev, out)) {
1847 ret = voice_stop_call(adev);
1848 adev->current_call_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001849 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001850
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001851 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001852 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001853 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001854
1855 if (out == adev->primary_output) {
1856 pthread_mutex_lock(&adev->lock);
1857 audio_extn_set_parameters(adev, parms);
1858 pthread_mutex_unlock(&adev->lock);
1859 }
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001860 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001861 pthread_mutex_lock(&out->lock);
Steve Kondikba3b35d2014-07-18 01:49:48 -07001862 ret = parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001863 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001864 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001865
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001866 str_parms_destroy(parms);
Steve Kondik3abbbc82014-11-29 14:14:43 -08001867 ALOGV("%s: exit: code(%d)", __func__, status);
1868 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001869}
1870
1871static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1872{
1873 struct stream_out *out = (struct stream_out *)stream;
1874 struct str_parms *query = str_parms_create_str(keys);
1875 char *str;
1876 char value[256];
1877 struct str_parms *reply = str_parms_create();
1878 size_t i, j;
1879 int ret;
1880 bool first = true;
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001881
1882 if (!query || !reply) {
1883 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
1884 return NULL;
1885 }
1886
Eric Laurent994a6932013-07-17 11:51:42 -07001887 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001888 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1889 if (ret >= 0) {
1890 value[0] = '\0';
1891 i = 0;
1892 while (out->supported_channel_masks[i] != 0) {
1893 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1894 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1895 if (!first) {
1896 strcat(value, "|");
1897 }
1898 strcat(value, out_channels_name_to_enum_table[j].name);
1899 first = false;
1900 break;
1901 }
1902 }
1903 i++;
1904 }
1905 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1906 str = str_parms_to_str(reply);
1907 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001908 voice_extn_out_get_parameters(out, query, reply);
1909 str = str_parms_to_str(reply);
1910 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella2e032352014-01-29 12:52:19 -08001911 free(str);
1912 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001913 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001914 }
1915 str_parms_destroy(query);
1916 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001917 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001918 return str;
1919}
1920
1921static uint32_t out_get_latency(const struct audio_stream_out *stream)
1922{
1923 struct stream_out *out = (struct stream_out *)stream;
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001924 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001925
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001926 if (is_offload_usecase(out->usecase)) {
1927 if (out->use_small_bufs == true)
1928 latency = ((out->compr_config.fragments *
1929 out->compr_config.fragment_size * 1000) /
1930 (out->sample_rate * out->compr_config.codec->ch_in *
1931 audio_bytes_per_sample(out->format)));
1932 else
1933 latency = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1934 } else {
1935 latency = (out->config.period_count * out->config.period_size * 1000) /
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001936 (out->config.rate);
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001937 }
1938
1939 ALOGV("%s: Latency %d", latency);
1940 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001941}
1942
1943static int out_set_volume(struct audio_stream_out *stream, float left,
1944 float right)
1945{
Eric Laurenta9024de2013-04-04 09:19:12 -07001946 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001947 int volume[2];
1948
Eric Laurenta9024de2013-04-04 09:19:12 -07001949 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1950 /* only take left channel into account: the API is for stereo anyway */
1951 out->muted = (left == 0.0f);
1952 return 0;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001953 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001954 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001955 struct audio_device *adev = out->dev;
1956 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001957 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1958 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001959
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001960 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1961 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001962 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1963 if (!ctl) {
1964 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1965 __func__, mixer_ctl_name);
1966 return -EINVAL;
1967 }
1968 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1969 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1970 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1971 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001972 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001973
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001974 return -ENOSYS;
1975}
1976
1977static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1978 size_t bytes)
1979{
1980 struct stream_out *out = (struct stream_out *)stream;
1981 struct audio_device *adev = out->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +05301982 int snd_scard_state = get_snd_card_state(adev);
Eric Laurent6e895242013-09-05 16:10:57 -07001983 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001984
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001985 pthread_mutex_lock(&out->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301986
Naresh Tannirucef332d2014-06-04 18:17:56 +05301987 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
1988 if (out->pcm) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301989 ALOGD(" %s: sound card is not active/SSR state", __func__);
1990 ret= -ENETRESET;
1991 goto exit;
Naresh Tannirucef332d2014-06-04 18:17:56 +05301992 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1993 //during SSR for compress usecase we should return error to flinger
1994 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
1995 pthread_mutex_unlock(&out->lock);
1996 return -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301997 }
1998 }
1999
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002001 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002002 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002003 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
2004 ret = voice_extn_compress_voip_start_output_stream(out);
2005 else
2006 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002007 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002008 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002009 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002010 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002011 goto exit;
2012 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002013 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002014
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002015 if (is_offload_usecase(out->usecase)) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002016 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
2017 if (out->send_new_metadata) {
2018 ALOGVV("send new gapless metadata");
2019 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
2020 out->send_new_metadata = 0;
2021 }
2022
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002023 ret = compress_write(out->compr, buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302024 if (ret < 0)
2025 ret = -errno;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002026 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07002027 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002028 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302029 } else if (-ENETRESET == ret) {
2030 ALOGE("copl %s: received sound card offline state on compress write", __func__);
2031 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2032 pthread_mutex_unlock(&out->lock);
2033 out_standby(&out->stream.common);
2034 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002035 }
Naresh Tannirucef332d2014-06-04 18:17:56 +05302036 if (!out->playback_started && ret >= 0) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002037 compress_start(out->compr);
2038 out->playback_started = 1;
2039 out->offload_state = OFFLOAD_STATE_PLAYING;
2040 }
2041 pthread_mutex_unlock(&out->lock);
2042 return ret;
2043 } else {
2044 if (out->pcm) {
2045 if (out->muted)
2046 memset((void *)buffer, 0, bytes);
2047 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302048 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY)
2049 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
2050 else
2051 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302052 if (ret < 0)
2053 ret = -errno;
2054 else if (ret == 0)
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002055 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002056 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002057 }
2058
2059exit:
Dhanalakshmi Siddani4fe3e512014-05-26 18:03:42 +05302060 /* ToDo: There may be a corner case when SSR happens back to back during
2061 start/stop. Need to post different error to handle that. */
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302062 if (-ENETRESET == ret) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302063 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302064 }
2065
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002066 pthread_mutex_unlock(&out->lock);
2067
2068 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002069 if (out->pcm)
2070 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302071 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302072 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302073 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302074 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302075 out->standby = true;
2076 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002077 out_standby(&out->stream.common);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002078 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
2079 out_get_sample_rate(&out->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002080 }
2081 return bytes;
2082}
2083
2084static int out_get_render_position(const struct audio_stream_out *stream,
2085 uint32_t *dsp_frames)
2086{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002087 struct stream_out *out = (struct stream_out *)stream;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002088 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
2089 ssize_t ret = 0;
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002090 *dsp_frames = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002091 pthread_mutex_lock(&out->lock);
2092 if (out->compr != NULL) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302093 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002094 &out->sample_rate);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302095 if (ret < 0)
2096 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002097 ALOGVV("%s rendered frames %d sample_rate %d",
2098 __func__, *dsp_frames, out->sample_rate);
2099 }
2100 pthread_mutex_unlock(&out->lock);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302101 if (-ENETRESET == ret) {
2102 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
2103 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2104 return -EINVAL;
2105 } else if(ret < 0) {
Steve Kondik81506242014-07-21 16:12:57 -07002106 if (out->compr == NULL) {
2107 return 0;
2108 }
2109 ALOGE(" ERROR: Unable to get time stamp from compress driver ret=%d", ret);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302110 return -EINVAL;
2111 } else {
2112 return 0;
2113 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002114 } else
2115 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002116}
2117
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302118static int out_add_audio_effect(const struct audio_stream *stream __unused,
2119 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002120{
2121 return 0;
2122}
2123
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302124static int out_remove_audio_effect(const struct audio_stream *stream __unused,
2125 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002126{
2127 return 0;
2128}
2129
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302130static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
2131 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002132{
2133 return -EINVAL;
2134}
2135
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002136static int out_get_presentation_position(const struct audio_stream_out *stream,
2137 uint64_t *frames, struct timespec *timestamp)
2138{
2139 struct stream_out *out = (struct stream_out *)stream;
2140 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07002141 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002142
2143 pthread_mutex_lock(&out->lock);
2144
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002145 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07002146 if (out->compr != NULL) {
2147 compress_get_tstamp(out->compr, &dsp_frames,
2148 &out->sample_rate);
2149 ALOGVV("%s rendered frames %ld sample_rate %d",
2150 __func__, dsp_frames, out->sample_rate);
2151 *frames = dsp_frames;
2152 ret = 0;
2153 /* this is the best we can do */
2154 clock_gettime(CLOCK_MONOTONIC, timestamp);
2155 }
2156 } else {
2157 if (out->pcm) {
2158 size_t avail;
2159 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
2160 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07002161 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07002162 // This adjustment accounts for buffering after app processor.
2163 // It is based on estimated DSP latency per use case, rather than exact.
2164 signed_frames -=
2165 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
2166
Eric Laurent949a0892013-09-20 09:20:13 -07002167 // It would be unusual for this value to be negative, but check just in case ...
2168 if (signed_frames >= 0) {
2169 *frames = signed_frames;
2170 ret = 0;
2171 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002172 }
2173 }
2174 }
2175
2176 pthread_mutex_unlock(&out->lock);
2177
2178 return ret;
2179}
2180
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002181static int out_set_callback(struct audio_stream_out *stream,
2182 stream_callback_t callback, void *cookie)
2183{
2184 struct stream_out *out = (struct stream_out *)stream;
2185
2186 ALOGV("%s", __func__);
2187 pthread_mutex_lock(&out->lock);
2188 out->offload_callback = callback;
2189 out->offload_cookie = cookie;
2190 pthread_mutex_unlock(&out->lock);
2191 return 0;
2192}
2193
2194static int out_pause(struct audio_stream_out* stream)
2195{
2196 struct stream_out *out = (struct stream_out *)stream;
2197 int status = -ENOSYS;
2198 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002199 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002200 pthread_mutex_lock(&out->lock);
2201 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302202 struct audio_device *adev = out->dev;
2203 int snd_scard_state = get_snd_card_state(adev);
2204
2205 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2206 status = compress_pause(out->compr);
2207
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002208 out->offload_state = OFFLOAD_STATE_PAUSED;
2209 }
2210 pthread_mutex_unlock(&out->lock);
2211 }
2212 return status;
2213}
2214
2215static int out_resume(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)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002221 status = 0;
2222 pthread_mutex_lock(&out->lock);
2223 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
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_resume(out->compr);
2229
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002230 out->offload_state = OFFLOAD_STATE_PLAYING;
2231 }
2232 pthread_mutex_unlock(&out->lock);
2233 }
2234 return status;
2235}
2236
2237static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
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)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002243 pthread_mutex_lock(&out->lock);
2244 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2245 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2246 else
2247 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2248 pthread_mutex_unlock(&out->lock);
2249 }
2250 return status;
2251}
2252
2253static int out_flush(struct audio_stream_out* stream)
2254{
2255 struct stream_out *out = (struct stream_out *)stream;
2256 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002257 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002258 pthread_mutex_lock(&out->lock);
2259 stop_compressed_output_l(out);
2260 pthread_mutex_unlock(&out->lock);
2261 return 0;
2262 }
2263 return -ENOSYS;
2264}
2265
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002266/** audio_stream_in implementation **/
2267static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2268{
2269 struct stream_in *in = (struct stream_in *)stream;
2270
2271 return in->config.rate;
2272}
2273
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302274static int in_set_sample_rate(struct audio_stream *stream __unused,
2275 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002276{
2277 return -ENOSYS;
2278}
2279
2280static size_t in_get_buffer_size(const struct audio_stream *stream)
2281{
2282 struct stream_in *in = (struct stream_in *)stream;
2283
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002284 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
2285 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07002286 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
2287 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002288
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302289 return in->config.period_size *
Steve Kondik3abbbc82014-11-29 14:14:43 -08002290 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002291}
2292
2293static uint32_t in_get_channels(const struct audio_stream *stream)
2294{
2295 struct stream_in *in = (struct stream_in *)stream;
2296
2297 return in->channel_mask;
2298}
2299
2300static audio_format_t in_get_format(const struct audio_stream *stream)
2301{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002302 struct stream_in *in = (struct stream_in *)stream;
2303
2304 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002305}
2306
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302307static int in_set_format(struct audio_stream *stream __unused,
2308 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002309{
2310 return -ENOSYS;
2311}
2312
2313static int in_standby(struct audio_stream *stream)
2314{
2315 struct stream_in *in = (struct stream_in *)stream;
2316 struct audio_device *adev = in->dev;
2317 int status = 0;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302318 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2319 stream, in->usecase, use_case_table[in->usecase]);
2320
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002321
2322 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2323 /* Ignore standby in case of voip call because the voip input
2324 * stream is closed in adev_close_input_stream()
2325 */
2326 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2327 return status;
2328 }
2329
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002330 pthread_mutex_lock(&in->lock);
2331 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002332 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002333 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002334 if (in->pcm) {
2335 pcm_close(in->pcm);
2336 in->pcm = NULL;
2337 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002338 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002339 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002340 }
2341 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002342 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002343 return status;
2344}
2345
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302346static int in_dump(const struct audio_stream *stream __unused,
2347 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002348{
2349 return 0;
2350}
2351
2352static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2353{
2354 struct stream_in *in = (struct stream_in *)stream;
2355 struct audio_device *adev = in->dev;
2356 struct str_parms *parms;
2357 char *str;
2358 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002359 int ret = 0, val = 0, err;
Steve Kondik3abbbc82014-11-29 14:14:43 -08002360 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002361
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302362 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002363 parms = str_parms_create_str(kvpairs);
2364
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002365 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002366 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002367
2368 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2369 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002370 val = atoi(value);
2371 /* no audio source uses val == 0 */
2372 if ((in->source != val) && (val != 0)) {
2373 in->source = val;
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002374 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2375 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2376 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2377 (in->config.rate == 8000 || in->config.rate == 16000) &&
2378 (popcount(in->channel_mask) == 1)) {
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08002379 err = voice_extn_compress_voip_open_input_stream(in);
2380 if (err != 0) {
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002381 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08002382 __func__, err);
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002383 }
2384 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002385 }
2386 }
2387
Steve Kondik3abbbc82014-11-29 14:14:43 -08002388 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2389
2390 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002391 val = atoi(value);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302392 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002393 in->device = val;
2394 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002395 if (!in->standby)
Steve Kondik3abbbc82014-11-29 14:14:43 -08002396 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002397 }
2398 }
2399
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002400done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002401 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002402 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002403
2404 str_parms_destroy(parms);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002405 ALOGV("%s: exit: status(%d)", __func__, status);
2406 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002407}
2408
2409static char* in_get_parameters(const struct audio_stream *stream,
2410 const char *keys)
2411{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002412 struct stream_in *in = (struct stream_in *)stream;
2413 struct str_parms *query = str_parms_create_str(keys);
2414 char *str;
2415 char value[256];
2416 struct str_parms *reply = str_parms_create();
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002417
2418 if (!query || !reply) {
2419 ALOGE("in_get_parameters: failed to create query or reply");
2420 return NULL;
2421 }
2422
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002423 ALOGV("%s: enter: keys - %s", __func__, keys);
2424
2425 voice_extn_in_get_parameters(in, query, reply);
2426
2427 str = str_parms_to_str(reply);
2428 str_parms_destroy(query);
2429 str_parms_destroy(reply);
2430
2431 ALOGV("%s: exit: returns - %s", __func__, str);
2432 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002433}
2434
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302435static int in_set_gain(struct audio_stream_in *stream __unused,
2436 float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002437{
2438 return 0;
2439}
2440
2441static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2442 size_t bytes)
2443{
2444 struct stream_in *in = (struct stream_in *)stream;
2445 struct audio_device *adev = in->dev;
2446 int i, ret = -1;
Naresh Tannirucef332d2014-06-04 18:17:56 +05302447 int snd_scard_state = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002448
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002449 pthread_mutex_lock(&in->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302450
2451 if (in->pcm) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302452 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302453 ALOGD(" %s: sound card is not active/SSR state", __func__);
2454 ret= -ENETRESET;
2455 goto exit;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302456 }
2457 }
2458
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002459 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002460 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002461 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2462 ret = voice_extn_compress_voip_start_input_stream(in);
2463 else
2464 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002465 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002466 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002467 goto exit;
2468 }
2469 in->standby = 0;
2470 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002471
2472 if (in->pcm) {
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302473 if (audio_extn_ssr_get_enabled() &&
2474 audio_channel_count_from_in_mask(in->channel_mask) == 6)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002475 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002476 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2477 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302478 else if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY)
2479 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002480 else
2481 ret = pcm_read(in->pcm, buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302482 if (ret < 0)
2483 ret = -errno;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002484 }
2485
2486 /*
2487 * Instead of writing zeroes here, we could trust the hardware
2488 * to always provide zeroes when muted.
2489 */
Pavan Chikkala6c183fd2014-12-04 10:48:28 +05302490 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in) &&
2491 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002492 memset(buffer, 0, bytes);
2493
2494exit:
Dhanalakshmi Siddani4fe3e512014-05-26 18:03:42 +05302495 /* ToDo: There may be a corner case when SSR happens back to back during
2496 start/stop. Need to post different error to handle that. */
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302497 if (-ENETRESET == ret) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302498 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302499 memset(buffer, 0, bytes);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302500 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002501 pthread_mutex_unlock(&in->lock);
2502
2503 if (ret != 0) {
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302504 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302505 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302506 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302507 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302508 in->standby = true;
2509 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002510 in_standby(&in->stream.common);
2511 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002512 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
2513 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002514 }
2515 return bytes;
2516}
2517
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302518static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002519{
2520 return 0;
2521}
2522
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002523static int add_remove_audio_effect(const struct audio_stream *stream,
2524 effect_handle_t effect,
2525 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002526{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002527 struct stream_in *in = (struct stream_in *)stream;
2528 int status = 0;
2529 effect_descriptor_t desc;
2530
2531 status = (*effect)->get_descriptor(effect, &desc);
2532 if (status != 0)
2533 return status;
2534
2535 pthread_mutex_lock(&in->lock);
2536 pthread_mutex_lock(&in->dev->lock);
2537 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2538 in->enable_aec != enable &&
2539 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2540 in->enable_aec = enable;
2541 if (!in->standby)
2542 select_devices(in->dev, in->usecase);
2543 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002544 if (in->enable_ns != enable &&
2545 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2546 in->enable_ns = enable;
2547 if (!in->standby)
2548 select_devices(in->dev, in->usecase);
2549 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002550 pthread_mutex_unlock(&in->dev->lock);
2551 pthread_mutex_unlock(&in->lock);
2552
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002553 return 0;
2554}
2555
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002556static int in_add_audio_effect(const struct audio_stream *stream,
2557 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002558{
Eric Laurent994a6932013-07-17 11:51:42 -07002559 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002560 return add_remove_audio_effect(stream, effect, true);
2561}
2562
2563static int in_remove_audio_effect(const struct audio_stream *stream,
2564 effect_handle_t effect)
2565{
Eric Laurent994a6932013-07-17 11:51:42 -07002566 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002567 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002568}
2569
2570static int adev_open_output_stream(struct audio_hw_device *dev,
2571 audio_io_handle_t handle,
2572 audio_devices_t devices,
2573 audio_output_flags_t flags,
2574 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302575 struct audio_stream_out **stream_out,
2576 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002577{
2578 struct audio_device *adev = (struct audio_device *)dev;
2579 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002580 int i, ret = 0;
Steve Kondik5a447012014-12-02 16:04:20 -08002581 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002582
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002583 *stream_out = NULL;
Naresh Tannirucef332d2014-06-04 18:17:56 +05302584
2585 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2586 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2587 ALOGE(" sound card is not active rejecting compress output open request");
2588 return -EINVAL;
2589 }
2590
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002591 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2592
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302593 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2594 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2595 devices, flags, &out->stream);
2596
2597
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002598 if (!out) {
2599 return -ENOMEM;
2600 }
2601
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002602 if (devices == AUDIO_DEVICE_NONE)
2603 devices = AUDIO_DEVICE_OUT_SPEAKER;
2604
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002605 out->flags = flags;
2606 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002607 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002608 out->format = config->format;
2609 out->sample_rate = config->sample_rate;
2610 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2611 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002612 out->handle = handle;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002613 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Joseph3a2fec32014-12-03 02:46:47 -08002614 out->non_blocking = 0;
2615 out->use_small_bufs = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002616
2617 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002618 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
Mingming Yinee733602014-04-03 17:47:22 -07002619 (
2620#ifdef AFE_PROXY_ENABLED
2621 out->devices & AUDIO_DEVICE_OUT_PROXY ||
2622#endif
2623 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002624
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002625 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002626 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2627 ret = read_hdmi_channel_masks(out);
2628
Mingming Yinee733602014-04-03 17:47:22 -07002629#ifdef AFE_PROXY_ENABLED
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002630 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2631 ret = audio_extn_read_afe_proxy_channel_masks(out);
Mingming Yinee733602014-04-03 17:47:22 -07002632#endif
2633
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002634 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002635 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002636 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002637
2638 if (config->sample_rate == 0)
2639 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2640 if (config->channel_mask == 0)
2641 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2642
2643 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002644 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002645 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2646 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002647 out->config.rate = config->sample_rate;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302648 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002649 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Mingming Yinee733602014-04-03 17:47:22 -07002650#ifdef COMPRESS_VOIP_ENABLED
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002651 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2652 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002653 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002654 ret = voice_extn_compress_voip_open_output_stream(out);
2655 if (ret != 0) {
2656 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2657 __func__, ret);
2658 goto error_open;
2659 }
Mingming Yinee733602014-04-03 17:47:22 -07002660#endif
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002661 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2662 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2663 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2664 ALOGE("%s: Unsupported Offload information", __func__);
2665 ret = -EINVAL;
2666 goto error_open;
2667 }
Mingming Yin90310102013-11-13 16:57:00 -08002668 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002669 !audio_extn_is_dolby_format(config->offload_info.format)) {
Steve Kondik3fdf4fc2014-12-10 21:15:43 -08002670 ALOGE("%s: Unsupported offload audio format %x", __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002671 ret = -EINVAL;
2672 goto error_open;
2673 }
2674
2675 out->compr_config.codec = (struct snd_codec *)
2676 calloc(1, sizeof(struct snd_codec));
2677
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002678 if (!out->compr_config.codec) {
2679 ret = -ENOMEM;
2680 goto error_open;
2681 }
2682
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002683 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002684 if (config->offload_info.channel_mask)
2685 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002686 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002687 out->channel_mask = config->channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002688 config->offload_info.channel_mask = config->channel_mask;
2689 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002690 out->format = config->offload_info.format;
2691 out->sample_rate = config->offload_info.sample_rate;
2692
2693 out->stream.set_callback = out_set_callback;
2694 out->stream.pause = out_pause;
2695 out->stream.resume = out_resume;
2696 out->stream.drain = out_drain;
2697 out->stream.flush = out_flush;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002698 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002699
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002700 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002701 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002702 audio_extn_dolby_get_snd_codec_id(adev, out,
2703 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002704 else
2705 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002706 get_snd_codec_id(config->offload_info.format);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002707
ApurupaPattapu0c566872014-01-10 14:46:02 -08002708 if (audio_is_offload_pcm(config->offload_info.format)) {
2709 out->compr_config.fragment_size =
2710 platform_get_pcm_offload_buffer_size(&config->offload_info);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002711 } else {
ApurupaPattapu0c566872014-01-10 14:46:02 -08002712 out->compr_config.fragment_size =
2713 platform_get_compress_offload_buffer_size(&config->offload_info);
2714 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002715 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2716 out->compr_config.codec->sample_rate =
2717 compress_get_alsa_rate(config->offload_info.sample_rate);
2718 out->compr_config.codec->bit_rate =
2719 config->offload_info.bit_rate;
2720 out->compr_config.codec->ch_in =
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302721 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002722 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002723 out->bit_width = config->offload_info.bit_width;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002724
Steve Kondik3fdf4fc2014-12-10 21:15:43 -08002725 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
Steve Kondik37454012014-11-23 19:54:21 -08002726 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002727 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2728 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002729
Steve Kondik3abbbc82014-11-29 14:14:43 -08002730 if (config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002731 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002732
Steve Kondik3abbbc82014-11-29 14:14:43 -08002733 if (out->bit_width == 24)
2734 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002735
Steve Kondik6bedcdf2014-07-21 11:48:30 -07002736#ifdef FLAC_OFFLOAD_ENABLED
Steve Kondik37454012014-11-23 19:54:21 -08002737 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
2738 out->compr_config.codec->options.flac_dec.sample_size = config->offload_info.bit_width;
Steve Kondik6bedcdf2014-07-21 11:48:30 -07002739#endif
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002740
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002741 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2742 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002743
Alexy Joseph3a2fec32014-12-03 02:46:47 -08002744 if (config->offload_info.use_small_bufs) {
2745 //this flag is set from framework only if its for PCM formats
2746 //no need to check for PCM format again
2747 out->non_blocking = 0;
2748 out->use_small_bufs = true;
2749 ALOGI("Keep write blocking for small buff: non_blockling %d",
2750 out->non_blocking);
2751 }
2752
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002753 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002754 out->offload_state = OFFLOAD_STATE_IDLE;
2755 out->playback_started = 0;
2756
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002757 create_offload_callback_thread(out);
2758 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2759 __func__, config->offload_info.version,
2760 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002761 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -08002762 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002763
Mingming Yinee733602014-04-03 17:47:22 -07002764#ifdef INCALL_MUSIC_ENABLED
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002765 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2766 ret = voice_check_and_set_incall_music_usecase(adev, out);
2767 if (ret != 0) {
2768 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2769 __func__, ret);
2770 goto error_open;
2771 }
Mingming Yinee733602014-04-03 17:47:22 -07002772#endif
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302773 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2774 if (config->sample_rate == 0)
2775 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2776 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2777 config->sample_rate != 8000) {
2778 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2779 ret = -EINVAL;
2780 goto error_open;
2781 }
2782 out->sample_rate = config->sample_rate;
2783 out->config.rate = config->sample_rate;
2784 if (config->format == AUDIO_FORMAT_DEFAULT)
2785 config->format = AUDIO_FORMAT_PCM_16_BIT;
2786 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2787 config->format = AUDIO_FORMAT_PCM_16_BIT;
2788 ret = -EINVAL;
2789 goto error_open;
2790 }
2791 out->format = config->format;
2792 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2793 out->config = pcm_config_afe_proxy_playback;
2794 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002795 } else {
Steve Kondik3abbbc82014-11-29 14:14:43 -08002796#ifndef LOW_LATENCY_PRIMARY
2797 if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
2798 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2799 out->config = pcm_config_low_latency;
Steve Kondikb045f472014-11-12 23:24:07 -08002800#endif
Steve Kondik3abbbc82014-11-29 14:14:43 -08002801#ifdef LOW_LATENCY_PRIMARY
2802 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2803 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2804 out->config = pcm_config_deep_buffer;
2805#endif
2806 } else {
2807 /* primary path is the default path selected if no other outputs are available/suitable */
2808 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
2809#ifdef LOW_LATENCY_PRIMARY
2810 out->config = pcm_config_low_latency;
2811#else
2812 out->config = pcm_config_deep_buffer;
2813#endif
2814 }
2815 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2816 if (k_enable_extended_precision
2817 && pcm_params_format_test(adev->use_case_table[out->usecase],
2818 pcm_format_from_audio_format(config->format))) {
2819 out->config.format = pcm_format_from_audio_format(config->format);
2820 /* out->format already set to config->format */
2821 } else {
2822 /* deny the externally proposed config format
2823 * and use the one specified in audio_hw layer configuration.
2824 * Note: out->format is returned by out->stream.common.get_format()
2825 * and is used to set config->format in the code several lines below.
2826 */
Steve Kondik5a447012014-12-02 16:04:20 -08002827 ALOGW("Audio format %x is not available on this output", out->format);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002828 out->format = audio_format_from_pcm_format(out->config.format);
2829 }
2830 }
2831
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002832 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002833 }
2834
Steve Kondik5a447012014-12-02 16:04:20 -08002835 ALOGV("%s flags %x, format %x, sample_rate %d, out->bit_width %d",
2836 __func__, flags, out->format, out->sample_rate, out->bit_width);
2837
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002838 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
Steve Kondik3abbbc82014-11-29 14:14:43 -08002839 flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002840 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002841 if(adev->primary_output == NULL)
2842 adev->primary_output = out;
2843 else {
2844 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002845 ret = -EEXIST;
2846 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002847 }
2848 }
2849
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002850 /* Check if this usecase is already existing */
2851 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella0d06c8e2014-04-17 20:00:41 -07002852 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2853 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002854 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002855 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002856 ret = -EEXIST;
2857 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002858 }
2859 pthread_mutex_unlock(&adev->lock);
2860
2861 out->stream.common.get_sample_rate = out_get_sample_rate;
2862 out->stream.common.set_sample_rate = out_set_sample_rate;
2863 out->stream.common.get_buffer_size = out_get_buffer_size;
2864 out->stream.common.get_channels = out_get_channels;
2865 out->stream.common.get_format = out_get_format;
2866 out->stream.common.set_format = out_set_format;
2867 out->stream.common.standby = out_standby;
2868 out->stream.common.dump = out_dump;
2869 out->stream.common.set_parameters = out_set_parameters;
2870 out->stream.common.get_parameters = out_get_parameters;
2871 out->stream.common.add_audio_effect = out_add_audio_effect;
2872 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2873 out->stream.get_latency = out_get_latency;
2874 out->stream.set_volume = out_set_volume;
2875 out->stream.write = out_write;
2876 out->stream.get_render_position = out_get_render_position;
2877 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002878 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002879
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002880 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002881 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002882 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002883
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302884 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2885 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2886
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002887 config->format = out->stream.common.get_format(&out->stream.common);
2888 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2889 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2890
2891 *stream_out = &out->stream;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302892 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2893 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002894 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002895 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002896
2897error_open:
2898 free(out);
2899 *stream_out = NULL;
2900 ALOGD("%s: exit: ret %d", __func__, ret);
2901 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002902}
2903
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302904static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002905 struct audio_stream_out *stream)
2906{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002907 struct stream_out *out = (struct stream_out *)stream;
2908 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002909 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002910
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302911 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2912
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002913 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302914 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002915 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302916 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002917 if(ret != 0)
2918 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2919 __func__, ret);
2920 }
2921 else
2922 out_standby(&stream->common);
2923
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002924 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002925 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002926 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002927 if (out->compr_config.codec != NULL)
2928 free(out->compr_config.codec);
2929 }
2930 pthread_cond_destroy(&out->cond);
2931 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002932 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002933 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002934}
2935
2936static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2937{
2938 struct audio_device *adev = (struct audio_device *)dev;
2939 struct str_parms *parms;
2940 char *str;
2941 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002942 int val;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302943 int ret;
2944 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002945
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002946 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002947 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002948
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302949 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
2950 if (ret >= 0) {
2951 char *snd_card_status = value+2;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302952 if (strstr(snd_card_status, "OFFLINE")) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302953 struct listnode *node;
2954 struct audio_usecase *usecase;
2955
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302956 ALOGD("Received sound card OFFLINE status");
Naresh Tannirucef332d2014-06-04 18:17:56 +05302957 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2958
2959 pthread_mutex_lock(&adev->lock);
2960 //close compress session on OFFLINE status
2961 usecase = get_usecase_from_list(adev,USECASE_AUDIO_PLAYBACK_OFFLOAD);
2962 if (usecase && usecase->stream.out) {
2963 ALOGD(" %s closing compress session on OFFLINE state", __func__);
2964
2965 struct stream_out *out = usecase->stream.out;
2966
2967 pthread_mutex_unlock(&adev->lock);
2968 out_standby(&out->stream.common);
2969 } else
2970 pthread_mutex_unlock(&adev->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302971 } else if (strstr(snd_card_status, "ONLINE")) {
2972 ALOGD("Received sound card ONLINE status");
Naresh Tannirucef332d2014-06-04 18:17:56 +05302973 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302974 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302975 }
2976
2977 pthread_mutex_lock(&adev->lock);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302978 status = voice_set_parameters(adev, parms);
2979 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002980 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002981
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302982 status = platform_set_parameters(adev->platform, parms);
2983 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002984 goto done;
2985
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302986 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2987 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002988 /* When set to false, HAL should disable EC and NS
2989 * But it is currently not supported.
2990 */
2991 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2992 adev->bluetooth_nrec = true;
2993 else
2994 adev->bluetooth_nrec = false;
2995 }
2996
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302997 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2998 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002999 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
3000 adev->screen_off = false;
3001 else
3002 adev->screen_off = true;
3003 }
3004
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303005 ret = str_parms_get_int(parms, "rotation", &val);
3006 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003007 bool reverse_speakers = false;
3008 switch(val) {
3009 // FIXME: note that the code below assumes that the speakers are in the correct placement
3010 // relative to the user when the device is rotated 90deg from its default rotation. This
3011 // assumption is device-specific, not platform-specific like this code.
3012 case 270:
3013 reverse_speakers = true;
3014 break;
3015 case 0:
3016 case 90:
3017 case 180:
3018 break;
3019 default:
3020 ALOGE("%s: unexpected rotation of %d", __func__, val);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303021 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003022 }
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303023 if (status == 0) {
3024 if (adev->speaker_lr_swap != reverse_speakers) {
3025 adev->speaker_lr_swap = reverse_speakers;
3026 // only update the selected device if there is active pcm playback
3027 struct audio_usecase *usecase;
3028 struct listnode *node;
3029 list_for_each(node, &adev->usecase_list) {
3030 usecase = node_to_item(node, struct audio_usecase, list);
3031 if (usecase->type == PCM_PLAYBACK) {
Steve Kondik3abbbc82014-11-29 14:14:43 -08003032 status = select_devices(adev, usecase->id);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303033 break;
3034 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003035 }
3036 }
3037 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003038 }
3039
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003040 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003041
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003042done:
3043 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003044 pthread_mutex_unlock(&adev->lock);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303045 ALOGV("%s: exit with code(%d)", __func__, status);
3046 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003047}
3048
3049static char* adev_get_parameters(const struct audio_hw_device *dev,
3050 const char *keys)
3051{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003052 struct audio_device *adev = (struct audio_device *)dev;
3053 struct str_parms *reply = str_parms_create();
3054 struct str_parms *query = str_parms_create_str(keys);
3055 char *str;
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303056 char value[256] = {0};
3057 int ret = 0;
3058
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003059 if (!query || !reply) {
3060 ALOGE("adev_get_parameters: failed to create query or reply");
3061 return NULL;
3062 }
3063
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303064 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_SND_CARD_STATUS, value,
3065 sizeof(value));
3066 if (ret >=0) {
3067 int val = 1;
3068 pthread_mutex_lock(&adev->snd_card_status.lock);
3069 if (SND_CARD_STATE_OFFLINE == adev->snd_card_status.state)
3070 val = 0;
3071 pthread_mutex_unlock(&adev->snd_card_status.lock);
3072 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_SND_CARD_STATUS, val);
3073 goto exit;
3074 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003075
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003076 pthread_mutex_lock(&adev->lock);
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303077
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003078 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08003079 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003080 platform_get_parameters(adev->platform, query, reply);
Naresh Tannirucef332d2014-06-04 18:17:56 +05303081 pthread_mutex_unlock(&adev->lock);
3082
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303083exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003084 str = str_parms_to_str(reply);
3085 str_parms_destroy(query);
3086 str_parms_destroy(reply);
3087
3088 ALOGV("%s: exit: returns - %s", __func__, str);
3089 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003090}
3091
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303092static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003093{
3094 return 0;
3095}
3096
3097static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
3098{
Haynes Mathew George5191a852013-09-11 14:19:36 -07003099 int ret;
3100 struct audio_device *adev = (struct audio_device *)dev;
3101 pthread_mutex_lock(&adev->lock);
3102 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07003103 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07003104 pthread_mutex_unlock(&adev->lock);
3105 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003106}
3107
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303108static int adev_set_master_volume(struct audio_hw_device *dev __unused,
3109 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003110{
3111 return -ENOSYS;
3112}
3113
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303114static int adev_get_master_volume(struct audio_hw_device *dev __unused,
3115 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003116{
3117 return -ENOSYS;
3118}
3119
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303120static int adev_set_master_mute(struct audio_hw_device *dev __unused,
3121 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003122{
3123 return -ENOSYS;
3124}
3125
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303126static int adev_get_master_mute(struct audio_hw_device *dev __unused,
3127 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003128{
3129 return -ENOSYS;
3130}
3131
3132static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
3133{
3134 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003135 pthread_mutex_lock(&adev->lock);
3136 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07003137 ALOGD("%s mode %d\n", __func__, mode);
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303138#ifdef USES_AUDIO_AMPLIFIER
3139 if (amplifier_set_mode(mode) != 0)
3140 ALOGE("Failed setting amplifier mode");
3141#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003142 adev->mode = mode;
3143 }
3144 pthread_mutex_unlock(&adev->lock);
3145 return 0;
3146}
3147
3148static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
3149{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003150 int ret;
3151
3152 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08003153 ALOGD("%s state %d\n", __func__, state);
Shreyas Nagasandra Chandrasekhar9781c6c2014-12-01 05:49:35 -08003154 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003155 pthread_mutex_unlock(&adev->lock);
3156
3157 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003158}
3159
3160static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
3161{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003162 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003163 return 0;
3164}
3165
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303166static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003167 const struct audio_config *config)
3168{
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303169 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003170
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003171 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
3172 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003173}
3174
3175static int adev_open_input_stream(struct audio_hw_device *dev,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303176 audio_io_handle_t handle __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003177 audio_devices_t devices,
3178 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303179 struct audio_stream_in **stream_in,
3180 audio_input_flags_t flags __unused,
3181 const char *address __unused,
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003182 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003183{
3184 struct audio_device *adev = (struct audio_device *)dev;
3185 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003186 int ret = 0, buffer_size, frame_size;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303187 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003188 bool is_low_latency = false;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303189
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003190 *stream_in = NULL;
3191 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
3192 return -EINVAL;
3193
3194 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003195
3196 if (!in) {
3197 ALOGE("failed to allocate input stream");
3198 return -ENOMEM;
3199 }
3200
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303201 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003202 stream_handle(%p) io_handle(%d) source(%d)",__func__, config->sample_rate, config->channel_mask,
3203 devices, &in->stream, handle, source);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003204
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07003205 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
3206
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003207 in->stream.common.get_sample_rate = in_get_sample_rate;
3208 in->stream.common.set_sample_rate = in_set_sample_rate;
3209 in->stream.common.get_buffer_size = in_get_buffer_size;
3210 in->stream.common.get_channels = in_get_channels;
3211 in->stream.common.get_format = in_get_format;
3212 in->stream.common.set_format = in_set_format;
3213 in->stream.common.standby = in_standby;
3214 in->stream.common.dump = in_dump;
3215 in->stream.common.set_parameters = in_set_parameters;
3216 in->stream.common.get_parameters = in_get_parameters;
3217 in->stream.common.add_audio_effect = in_add_audio_effect;
3218 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3219 in->stream.set_gain = in_set_gain;
3220 in->stream.read = in_read;
3221 in->stream.get_input_frames_lost = in_get_input_frames_lost;
3222
3223 in->device = devices;
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003224 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003225 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003226 in->standby = 1;
3227 in->channel_mask = config->channel_mask;
3228
3229 /* Update config params with the requested sample rate and channels */
3230 in->usecase = USECASE_AUDIO_RECORD;
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003231 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
3232 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
3233 is_low_latency = true;
3234#if LOW_LATENCY_CAPTURE_USE_CASE
3235 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
3236#endif
3237 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003238 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003239 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003240 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003241
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05303242 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
Pavan Chikkala3c0036d2014-12-17 16:45:10 +05303243 if (adev->mode != AUDIO_MODE_IN_CALL) {
3244 ret = -EINVAL;
3245 goto err_open;
Pavan Chikkala3ec42ef2014-11-21 20:57:48 +05303246 }
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05303247 if (config->sample_rate == 0)
3248 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3249 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
3250 config->sample_rate != 8000) {
3251 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3252 ret = -EINVAL;
3253 goto err_open;
3254 }
3255 if (config->format == AUDIO_FORMAT_DEFAULT)
3256 config->format = AUDIO_FORMAT_PCM_16_BIT;
3257 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
3258 config->format = AUDIO_FORMAT_PCM_16_BIT;
3259 ret = -EINVAL;
3260 goto err_open;
3261 }
3262 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
3263 in->config = pcm_config_afe_proxy_record;
3264 in->config.channels = channel_count;
3265 in->config.rate = config->sample_rate;
3266 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003267 if(audio_extn_ssr_get_enabled()) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303268 if(audio_extn_ssr_init(in)) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003269 ALOGE("%s: audio_extn_ssr_init failed", __func__);
3270 ret = -EINVAL;
3271 goto err_open;
3272 }
3273 } else {
Mingming Yindaf9c542014-09-16 17:41:33 -07003274 ALOGW("%s: surround sound recording is not supported", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003275 }
Mingming Yine62d7842013-10-25 16:26:03 -07003276 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08003277 audio_extn_compr_cap_format_supported(config->format) &&
3278 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303279 audio_extn_compr_cap_init(in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003280 } else {
3281 in->config.channels = channel_count;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303282 frame_size = audio_stream_in_frame_size(&in->stream);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003283 buffer_size = get_input_buffer_size(config->sample_rate,
3284 config->format,
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003285 channel_count,
3286 is_low_latency);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003287 in->config.period_size = buffer_size / frame_size;
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003288 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
3289 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
3290 (voice_extn_compress_voip_is_format_supported(in->format)) &&
3291 (in->config.rate == 8000 || in->config.rate == 16000) &&
3292 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
3293 voice_extn_compress_voip_open_input_stream(in);
3294 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003295 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003296
3297 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07003298 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003299 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003300
3301err_open:
3302 free(in);
3303 *stream_in = NULL;
3304 return ret;
3305}
3306
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303307static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003308 struct audio_stream_in *stream)
3309{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003310 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003311 struct stream_in *in = (struct stream_in *)stream;
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303312 struct audio_device *adev = in->dev;
3313
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303314 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003315
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003316 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303317 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003318 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303319 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003320 if (ret != 0)
3321 ALOGE("%s: Compress voip input cannot be closed, error:%d",
3322 __func__, ret);
3323 } else
3324 in_standby(&stream->common);
3325
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303326 if (audio_extn_ssr_get_enabled() &&
3327 (audio_channel_count_from_in_mask(in->channel_mask) == 6)) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003328 audio_extn_ssr_deinit();
3329 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003330 free(stream);
3331
Mingming Yine62d7842013-10-25 16:26:03 -07003332 if(audio_extn_compr_cap_enabled() &&
3333 audio_extn_compr_cap_format_supported(in->config.format))
3334 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003335 return;
3336}
3337
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303338static int adev_dump(const audio_hw_device_t *device __unused,
3339 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003340{
3341 return 0;
3342}
3343
Steve Kondik3abbbc82014-11-29 14:14:43 -08003344/* verifies input and output devices and their capabilities.
3345 *
3346 * This verification is required when enabling extended bit-depth or
3347 * sampling rates, as not all qcom products support it.
3348 *
3349 * Suitable for calling only on initialization such as adev_open().
3350 * It fills the audio_device use_case_table[] array.
3351 *
3352 * Has a side-effect that it needs to configure audio routing / devices
3353 * in order to power up the devices and read the device parameters.
3354 * It does not acquire any hw device lock. Should restore the devices
3355 * back to "normal state" upon completion.
3356 */
3357static int adev_verify_devices(struct audio_device *adev)
3358{
3359 /* enumeration is a bit difficult because one really wants to pull
3360 * the use_case, device id, etc from the hidden pcm_device_table[].
3361 * In this case there are the following use cases and device ids.
3362 *
3363 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
3364 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
3365 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
3366 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
3367 * [USECASE_AUDIO_RECORD] = {0, 0},
3368 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
3369 * [USECASE_VOICE_CALL] = {2, 2},
3370 *
3371 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
3372 * USECASE_VOICE_CALL omitted, but possible for either input or output.
3373 */
3374
3375 /* should be the usecases enabled in adev_open_input_stream() */
3376 static const int test_in_usecases[] = {
3377 USECASE_AUDIO_RECORD,
3378 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
3379 };
3380 /* should be the usecases enabled in adev_open_output_stream()*/
3381 static const int test_out_usecases[] = {
3382 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
3383 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
3384 };
3385 static const usecase_type_t usecase_type_by_dir[] = {
3386 PCM_PLAYBACK,
3387 PCM_CAPTURE,
3388 };
3389 static const unsigned flags_by_dir[] = {
3390 PCM_OUT,
3391 PCM_IN,
3392 };
3393
3394 size_t i;
3395 unsigned dir;
3396 const unsigned card_id = adev->snd_card;
3397 char info[512]; /* for possible debug info */
3398
3399 for (dir = 0; dir < 2; ++dir) {
3400 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
3401 const unsigned flags_dir = flags_by_dir[dir];
3402 const size_t testsize =
3403 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
3404 const int *testcases =
3405 dir ? test_in_usecases : test_out_usecases;
3406 const audio_devices_t audio_device =
3407 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
3408
3409 for (i = 0; i < testsize; ++i) {
3410 const audio_usecase_t audio_usecase = testcases[i];
3411 int device_id;
3412 snd_device_t snd_device;
3413 struct pcm_params **pparams;
3414 struct stream_out out;
3415 struct stream_in in;
3416 struct audio_usecase uc_info;
3417 int retval;
3418
3419 pparams = &adev->use_case_table[audio_usecase];
3420 pcm_params_free(*pparams); /* can accept null input */
3421 *pparams = NULL;
3422
3423 /* find the device ID for the use case (signed, for error) */
3424 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
3425 if (device_id < 0)
3426 continue;
3427
3428 /* prepare structures for device probing */
3429 memset(&uc_info, 0, sizeof(uc_info));
3430 uc_info.id = audio_usecase;
3431 uc_info.type = usecase_type;
3432 if (dir) {
3433 adev->active_input = &in;
3434 memset(&in, 0, sizeof(in));
3435 in.device = audio_device;
3436 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
3437 uc_info.stream.in = &in;
3438 } else {
3439 adev->active_input = NULL;
3440 }
3441 memset(&out, 0, sizeof(out));
3442 out.devices = audio_device; /* only field needed in select_devices */
3443 uc_info.stream.out = &out;
3444 uc_info.devices = audio_device;
3445 uc_info.in_snd_device = SND_DEVICE_NONE;
3446 uc_info.out_snd_device = SND_DEVICE_NONE;
3447 list_add_tail(&adev->usecase_list, &uc_info.list);
3448
3449 /* select device - similar to start_(in/out)put_stream() */
3450 retval = select_devices(adev, audio_usecase);
3451 if (retval >= 0) {
3452 *pparams = pcm_params_get(card_id, device_id, flags_dir);
3453#if LOG_NDEBUG == 0
3454 if (*pparams) {
3455 ALOGV("%s: (%s) card %d device %d", __func__,
3456 dir ? "input" : "output", card_id, device_id);
3457 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
3458 ALOGV(info); /* print parameters */
3459 } else {
3460 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
3461 }
3462#endif
3463 }
3464
3465 /* deselect device - similar to stop_(in/out)put_stream() */
3466 /* 1. Get and set stream specific mixer controls */
3467 retval = disable_audio_route(adev, &uc_info);
3468 /* 2. Disable the rx device */
3469 retval = disable_snd_device(adev,
3470 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
3471 list_remove(&uc_info.list);
3472 }
3473 }
3474 adev->active_input = NULL; /* restore adev state */
3475 return 0;
3476}
3477
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003478static int adev_close(hw_device_t *device)
3479{
Steve Kondik3abbbc82014-11-29 14:14:43 -08003480 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003481 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07003482
3483 if (!adev)
3484 return 0;
3485
3486 pthread_mutex_lock(&adev_init_lock);
3487
3488 if ((--audio_device_ref_count) == 0) {
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303489#ifdef USES_AUDIO_AMPLIFIER
3490 if (amplifier_close() != 0)
3491 ALOGE("Amplifier close failed");
3492#endif
Kiran Kandide144c82013-11-20 15:58:32 -08003493 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07003494 audio_route_free(adev->audio_route);
3495 free(adev->snd_dev_ref_cnt);
3496 platform_deinit(adev->platform);
Steve Kondik3abbbc82014-11-29 14:14:43 -08003497 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
3498 pcm_params_free(adev->use_case_table[i]);
3499 }
Kiran Kandi910e1862013-10-29 13:29:42 -07003500 free(device);
3501 adev = NULL;
3502 }
3503 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003504 return 0;
3505}
3506
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003507/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
3508 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
3509 * just that it _might_ work.
3510 */
3511static int period_size_is_plausible_for_low_latency(int period_size)
3512{
3513 switch (period_size) {
3514 case 160:
3515 case 240:
3516 case 320:
3517 case 480:
3518 return 1;
3519 default:
3520 return 0;
3521 }
3522}
3523
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003524static int adev_open(const hw_module_t *module, const char *name,
3525 hw_device_t **device)
3526{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003527 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003528
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003529 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003530 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3531
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003532 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07003533 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003534 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07003535 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003536 ALOGD("%s: returning existing instance of adev", __func__);
3537 ALOGD("%s: exit", __func__);
3538 pthread_mutex_unlock(&adev_init_lock);
3539 return 0;
3540 }
3541
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003542 adev = calloc(1, sizeof(struct audio_device));
3543
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003544 if (!adev) {
3545 pthread_mutex_unlock(&adev_init_lock);
3546 return -ENOMEM;
3547 }
3548
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07003549 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3550
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003551 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3552 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3553 adev->device.common.module = (struct hw_module_t *)module;
3554 adev->device.common.close = adev_close;
3555
3556 adev->device.init_check = adev_init_check;
3557 adev->device.set_voice_volume = adev_set_voice_volume;
3558 adev->device.set_master_volume = adev_set_master_volume;
3559 adev->device.get_master_volume = adev_get_master_volume;
3560 adev->device.set_master_mute = adev_set_master_mute;
3561 adev->device.get_master_mute = adev_get_master_mute;
3562 adev->device.set_mode = adev_set_mode;
3563 adev->device.set_mic_mute = adev_set_mic_mute;
3564 adev->device.get_mic_mute = adev_get_mic_mute;
3565 adev->device.set_parameters = adev_set_parameters;
3566 adev->device.get_parameters = adev_get_parameters;
3567 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3568 adev->device.open_output_stream = adev_open_output_stream;
3569 adev->device.close_output_stream = adev_close_output_stream;
3570 adev->device.open_input_stream = adev_open_input_stream;
3571 adev->device.close_input_stream = adev_close_input_stream;
3572 adev->device.dump = adev_dump;
3573
3574 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003575 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003576 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003577 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003578 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003579 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003580 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003581 /* adev->cur_hdmi_channels = 0; by calloc() */
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08003582 adev->cur_codec_backend_samplerate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
3583 adev->cur_codec_backend_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Eric Laurentb23d5282013-05-14 15:27:20 -07003584 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003585 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003586 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly9b7e96b2014-02-14 14:45:49 -08003587 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07003588 adev->offload_usecases_state = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003589
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303590 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
3591 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
3592
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003593 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003594 adev->platform = platform_init(adev);
3595 if (!adev->platform) {
3596 free(adev->snd_dev_ref_cnt);
3597 free(adev);
3598 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3599 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08003600 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003601 return -EINVAL;
3602 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003603
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303604 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
3605
Eric Laurentc4aef752013-09-12 17:45:53 -07003606 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
3607 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3608 if (adev->visualizer_lib == NULL) {
3609 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3610 } else {
3611 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3612 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003613 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003614 "visualizer_hal_start_output");
3615 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003616 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003617 "visualizer_hal_stop_output");
3618 }
3619 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003620 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07003621
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003622 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
3623 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3624 if (adev->offload_effects_lib == NULL) {
3625 ALOGE("%s: DLOPEN failed for %s", __func__,
3626 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3627 } else {
3628 ALOGV("%s: DLOPEN successful for %s", __func__,
3629 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3630 adev->offload_effects_start_output =
3631 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3632 "offload_effects_bundle_hal_start_output");
3633 adev->offload_effects_stop_output =
3634 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3635 "offload_effects_bundle_hal_stop_output");
3636 }
3637 }
3638
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003639 *device = &adev->device.common;
Steve Kondik3abbbc82014-11-29 14:14:43 -08003640 if (k_enable_extended_precision)
3641 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003642
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303643#ifdef USES_AUDIO_AMPLIFIER
3644 if (amplifier_open() != 0)
3645 ALOGE("Amplifier initialization failed");
3646#endif
3647
Kiran Kandi910e1862013-10-29 13:29:42 -07003648 audio_device_ref_count++;
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003649
3650 char value[PROPERTY_VALUE_MAX];
3651 int trial;
3652 if (property_get("audio_hal.period_size", value, NULL) > 0) {
3653 trial = atoi(value);
3654 if (period_size_is_plausible_for_low_latency(trial)) {
3655 pcm_config_low_latency.period_size = trial;
3656 pcm_config_low_latency.start_threshold = trial / 4;
3657 pcm_config_low_latency.avail_min = trial / 4;
3658 configured_low_latency_capture_period_size = trial;
3659 }
3660 }
3661 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
3662 trial = atoi(value);
3663 if (period_size_is_plausible_for_low_latency(trial)) {
3664 configured_low_latency_capture_period_size = trial;
3665 }
3666 }
3667
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003668 pthread_mutex_unlock(&adev_init_lock);
3669
Eric Laurent994a6932013-07-17 11:51:42 -07003670 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003671 return 0;
3672}
3673
3674static struct hw_module_methods_t hal_module_methods = {
3675 .open = adev_open,
3676};
3677
3678struct audio_module HAL_MODULE_INFO_SYM = {
3679 .common = {
3680 .tag = HARDWARE_MODULE_TAG,
3681 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3682 .hal_api_version = HARDWARE_HAL_API_VERSION,
3683 .id = AUDIO_HARDWARE_MODULE_ID,
3684 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08003685 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003686 .methods = &hal_module_methods,
3687 },
3688};