blob: 34ebe911492f798a52c5f0abb725ca4d24994572 [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
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800408 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700409 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700410 ALOGV("%s: apply mixer and update path: %s", __func__, mixer_path);
411 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800412 ALOGV("%s: exit", __func__);
413 return 0;
414}
415
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700416int disable_audio_route(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700417 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800418{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700419 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700420 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800421
422 if (usecase == NULL)
423 return -EINVAL;
424
425 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700426 if (usecase->type == PCM_CAPTURE)
427 snd_device = usecase->in_snd_device;
428 else
429 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800430 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700431 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700432 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
433 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800434 ALOGV("%s: exit", __func__);
435 return 0;
436}
437
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700438int enable_snd_device(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700439 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800440{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700441 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
442
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800443 if (snd_device < SND_DEVICE_MIN ||
444 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800445 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800446 return -EINVAL;
447 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700448
449 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700450
451 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
452 ALOGE("%s: Invalid sound device returned", __func__);
453 return -EINVAL;
454 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700455 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700456 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700457 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700458 return 0;
459 }
460
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700461 /* start usb playback thread */
462 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
463 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
464 audio_extn_usb_start_playback(adev);
465
466 /* start usb capture thread */
467 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
468 audio_extn_usb_start_capture(adev);
469
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700470 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
471 audio_extn_spkr_prot_is_enabled()) {
472 if (audio_extn_spkr_prot_start_processing(snd_device)) {
473 ALOGE("%s: spkr_start_processing failed", __func__);
474 return -EINVAL;
475 }
476 } else {
477 ALOGV("%s: snd_device(%d: %s)", __func__,
Steve Kondik6288f9e2014-12-27 13:36:30 -0800478 snd_device, device_name);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700479 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
480 adev->snd_dev_ref_cnt[snd_device]--;
481 return -EINVAL;
482 }
Steve Kondik6288f9e2014-12-27 13:36:30 -0800483 audio_extn_listen_update_status(snd_device,
484 LISTEN_EVENT_SND_DEVICE_BUSY);
Kiran Kandide144c82013-11-20 15:58:32 -0800485
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700486 audio_route_apply_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800487 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800488 return 0;
489}
490
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700491int disable_snd_device(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700492 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800493{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700494 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
495
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800496 if (snd_device < SND_DEVICE_MIN ||
497 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800498 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800499 return -EINVAL;
500 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700501 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
502 ALOGE("%s: device ref cnt is already 0", __func__);
503 return -EINVAL;
504 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700505
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700506 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700507
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700508 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
509 ALOGE("%s: Invalid sound device returned", __func__);
510 return -EINVAL;
511 }
512
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700513 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700514 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700515 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800516 /* exit usb play back thread */
517 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
518 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
519 audio_extn_usb_stop_playback();
520
521 /* exit usb capture thread */
522 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
Rajshekar Eashwarappa4dfa0752014-10-02 20:18:20 +0530523 audio_extn_usb_stop_capture();
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700524 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
525 audio_extn_spkr_prot_is_enabled()) {
526 audio_extn_spkr_prot_stop_processing();
527 } else
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700528 audio_route_reset_and_update_path(adev->audio_route, device_name);
Kiran Kandide144c82013-11-20 15:58:32 -0800529
Steve Kondik6288f9e2014-12-27 13:36:30 -0800530 audio_extn_listen_update_status(snd_device,
Kiran Kandide144c82013-11-20 15:58:32 -0800531 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700532 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700533
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800534 return 0;
535}
536
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700537static void check_usecases_codec_backend(struct audio_device *adev,
538 struct audio_usecase *uc_info,
539 snd_device_t snd_device)
540{
541 struct listnode *node;
542 struct audio_usecase *usecase;
543 bool switch_device[AUDIO_USECASE_MAX];
544 int i, num_uc_to_switch = 0;
545
546 /*
547 * This function is to make sure that all the usecases that are active on
548 * the hardware codec backend are always routed to any one device that is
549 * handled by the hardware codec.
550 * For example, if low-latency and deep-buffer usecases are currently active
551 * on speaker and out_set_parameters(headset) is received on low-latency
552 * output, then we have to make sure deep-buffer is also switched to headset,
553 * because of the limitation that both the devices cannot be enabled
554 * at the same time as they share the same backend.
555 */
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800556 /*
557 * This call is to check if we need to force routing for a particular stream
558 * If there is a backend configuration change for the device when a
559 * new stream starts, then ADM needs to be closed and re-opened with the new
560 * configuraion. This call check if we need to re-route all the streams
561 * associated with the backend. Touch tone + 24 bit playback.
562 */
563 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info);
564
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700565 /* Disable all the usecases on the shared backend other than the
566 specified usecase */
567 for (i = 0; i < AUDIO_USECASE_MAX; i++)
568 switch_device[i] = false;
569
570 list_for_each(node, &adev->usecase_list) {
571 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800572 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700573 usecase != uc_info &&
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800574 (usecase->out_snd_device != snd_device || force_routing) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700575 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
576 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
577 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700578 platform_get_snd_device_name(usecase->out_snd_device));
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700579 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700580 switch_device[usecase->id] = true;
581 num_uc_to_switch++;
582 }
583 }
584
585 if (num_uc_to_switch) {
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700586 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700587
588 list_for_each(node, &adev->usecase_list) {
589 usecase = node_to_item(node, struct audio_usecase, list);
590 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700591 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700592 }
593 }
594
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700595 list_for_each(node, &adev->usecase_list) {
596 usecase = node_to_item(node, struct audio_usecase, list);
597 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700598 enable_snd_device(adev, snd_device);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700599 }
600 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700601
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700602 /* Re-route all the usecases on the shared backend other than the
603 specified usecase to new snd devices */
604 list_for_each(node, &adev->usecase_list) {
605 usecase = node_to_item(node, struct audio_usecase, list);
606 /* Update the out_snd_device only before enabling the audio route */
607 if (switch_device[usecase->id] ) {
608 usecase->out_snd_device = snd_device;
Avinash Vaish8005dc82014-07-24 15:36:33 +0530609 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
610 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700611 }
612 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700613 }
614}
615
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700616static void check_and_route_capture_usecases(struct audio_device *adev,
617 struct audio_usecase *uc_info,
618 snd_device_t snd_device)
619{
620 struct listnode *node;
621 struct audio_usecase *usecase;
622 bool switch_device[AUDIO_USECASE_MAX];
623 int i, num_uc_to_switch = 0;
624
625 /*
626 * This function is to make sure that all the active capture usecases
627 * are always routed to the same input sound device.
628 * For example, if audio-record and voice-call usecases are currently
629 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
630 * is received for voice call then we have to make sure that audio-record
631 * usecase is also switched to earpiece i.e. voice-dmic-ef,
632 * because of the limitation that two devices cannot be enabled
633 * at the same time if they share the same backend.
634 */
635 for (i = 0; i < AUDIO_USECASE_MAX; i++)
636 switch_device[i] = false;
637
638 list_for_each(node, &adev->usecase_list) {
639 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800640 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700641 usecase != uc_info &&
Avinash Vaish44014792015-03-27 14:46:04 +0530642 usecase->in_snd_device != snd_device &&
643 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700644 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
645 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700646 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700647 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700648 switch_device[usecase->id] = true;
649 num_uc_to_switch++;
650 }
651 }
652
653 if (num_uc_to_switch) {
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700654 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700655
656 list_for_each(node, &adev->usecase_list) {
657 usecase = node_to_item(node, struct audio_usecase, list);
658 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700659 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800660 }
661 }
662
663 list_for_each(node, &adev->usecase_list) {
664 usecase = node_to_item(node, struct audio_usecase, list);
665 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700666 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700667 }
668 }
669
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700670 /* Re-route all the usecases on the shared backend other than the
671 specified usecase to new snd devices */
672 list_for_each(node, &adev->usecase_list) {
673 usecase = node_to_item(node, struct audio_usecase, list);
674 /* Update the in_snd_device only before enabling the audio route */
675 if (switch_device[usecase->id] ) {
676 usecase->in_snd_device = snd_device;
Avinash Vaish8005dc82014-07-24 15:36:33 +0530677 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
678 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700679 }
680 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700681 }
682}
683
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800684/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700685static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800686{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700687 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700688 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800689
690 switch (channels) {
691 /*
692 * Do not handle stereo output in Multi-channel cases
693 * Stereo case is handled in normal playback path
694 */
695 case 6:
696 ALOGV("%s: HDMI supports 5.1", __func__);
697 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
698 break;
699 case 8:
700 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
701 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
702 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
703 break;
704 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700705 ALOGE("HDMI does not support multi channel playback");
706 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800707 break;
708 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700709 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800710}
711
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700712static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
713{
714 struct audio_usecase *usecase;
715 struct listnode *node;
716
717 list_for_each(node, &adev->usecase_list) {
718 usecase = node_to_item(node, struct audio_usecase, list);
719 if (usecase->type == VOICE_CALL) {
720 ALOGV("%s: usecase id %d", __func__, usecase->id);
721 return usecase->id;
722 }
723 }
724 return USECASE_INVALID;
725}
726
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700727struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700728 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700729{
730 struct audio_usecase *usecase;
731 struct listnode *node;
732
733 list_for_each(node, &adev->usecase_list) {
734 usecase = node_to_item(node, struct audio_usecase, list);
735 if (usecase->id == uc_id)
736 return usecase;
737 }
738 return NULL;
739}
740
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700741int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800742{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800743 snd_device_t out_snd_device = SND_DEVICE_NONE;
744 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700745 struct audio_usecase *usecase = NULL;
746 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800747 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800748 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed739e7152014-01-23 15:56:53 -0800749 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800750 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700751 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800752
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700753 usecase = get_usecase_from_list(adev, uc_id);
754 if (usecase == NULL) {
755 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
756 return -EINVAL;
757 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800758
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800759 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800760 (usecase->type == VOIP_CALL) ||
761 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700762 out_snd_device = platform_get_output_snd_device(adev->platform,
763 usecase->stream.out->devices);
764 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700765 usecase->devices = usecase->stream.out->devices;
766 } else {
767 /*
768 * If the voice call is active, use the sound devices of voice call usecase
769 * so that it would not result any device switch. All the usecases will
770 * be switched to new device when select_devices() is called for voice call
771 * usecase. This is to avoid switching devices for voice call when
772 * check_usecases_codec_backend() is called below.
773 */
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -0700774 if (adev->voice.in_call && adev->mode == AUDIO_MODE_IN_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700775 vc_usecase = get_usecase_from_list(adev,
776 get_voice_usecase_id_from_list(adev));
Mingming Yin6c23f382014-08-14 00:00:51 -0700777 if ((vc_usecase) && ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
778 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700779 in_snd_device = vc_usecase->in_snd_device;
780 out_snd_device = vc_usecase->out_snd_device;
781 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800782 } else if (voice_extn_compress_voip_is_active(adev)) {
783 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Mingming Yin6c23f382014-08-14 00:00:51 -0700784 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Avinash Vaish6a4c3372014-06-25 12:20:37 +0530785 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Mingming Yin6c23f382014-08-14 00:00:51 -0700786 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800787 in_snd_device = voip_usecase->in_snd_device;
788 out_snd_device = voip_usecase->out_snd_device;
789 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800790 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed739e7152014-01-23 15:56:53 -0800791 hfp_ucid = audio_extn_hfp_get_usecase();
792 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin6c23f382014-08-14 00:00:51 -0700793 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800794 in_snd_device = hfp_usecase->in_snd_device;
795 out_snd_device = hfp_usecase->out_snd_device;
796 }
Vimal Puthanveed8fa9eab2014-01-07 16:47:47 -0800797 } else if (audio_extn_hfp_is_active(adev)) {
798 hfp_usecase = get_usecase_from_list(adev, USECASE_AUDIO_HFP_SCO);
799 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
800 in_snd_device = hfp_usecase->in_snd_device;
801 out_snd_device = hfp_usecase->out_snd_device;
802 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700803 }
804 if (usecase->type == PCM_PLAYBACK) {
805 usecase->devices = usecase->stream.out->devices;
806 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700807 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700808 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700809 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700810 if (usecase->stream.out == adev->primary_output &&
Shreyas Nagasandra Chandrasekhar23b3af22014-09-03 19:10:01 +0530811 adev->active_input ) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700812 select_devices(adev, adev->active_input->usecase);
813 }
814 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700815 } else if (usecase->type == PCM_CAPTURE) {
816 usecase->devices = usecase->stream.in->device;
817 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700818 if (in_snd_device == SND_DEVICE_NONE) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530819 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Shreyas Nagasandra Chandrasekhar23b3af22014-09-03 19:10:01 +0530820 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 -0700821 adev->primary_output && !adev->primary_output->standby) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530822 out_device = adev->primary_output->devices;
823 } else if(usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
824 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700825 }
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530826 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700827 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700828 }
829 }
830
831 if (out_snd_device == usecase->out_snd_device &&
832 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800833 return 0;
834 }
835
sangwoobc677242013-08-08 16:53:43 +0900836 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700837 out_snd_device, platform_get_snd_device_name(out_snd_device),
838 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800839
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800840 /*
841 * Limitation: While in call, to do a device switch we need to disable
842 * and enable both RX and TX devices though one of them is same as current
843 * device.
844 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800845 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700846 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800847 }
848
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700849 /* Disable current sound devices */
850 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700851 disable_audio_route(adev, usecase);
852 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800853 }
854
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700855 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700856 disable_audio_route(adev, usecase);
857 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800858 }
859
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700860 /* Enable new sound devices */
861 if (out_snd_device != SND_DEVICE_NONE) {
862 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
863 check_usecases_codec_backend(adev, usecase, out_snd_device);
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700864 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800865 }
866
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700867 if (in_snd_device != SND_DEVICE_NONE) {
868 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700869 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700870 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700871
Avinash Vaish8005dc82014-07-24 15:36:33 +0530872 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700873 status = platform_switch_voice_call_device_post(adev->platform,
874 out_snd_device,
875 in_snd_device);
Avinash Vaish8005dc82014-07-24 15:36:33 +0530876 enable_audio_route_for_voice_usecases(adev, usecase);
877 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800878
sangwoo170731f2013-06-08 15:36:36 +0900879 usecase->in_snd_device = in_snd_device;
880 usecase->out_snd_device = out_snd_device;
881
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700882 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900883
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +0530884#ifdef USES_AUDIO_AMPLIFIER
885 /* Rely on amplifier_set_devices to distinguish between in/out devices */
886 amplifier_set_devices(in_snd_device);
887 amplifier_set_devices(out_snd_device);
888#endif
889
890
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800891 /* Applicable only on the targets that has external modem.
892 * Enable device command should be sent to modem only after
893 * enabling voice call mixer controls
894 */
895 if (usecase->type == VOICE_CALL)
896 status = platform_switch_voice_call_usecase_route_post(adev->platform,
897 out_snd_device,
898 in_snd_device);
899
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +0530900 ALOGD("%s: done",__func__);
901
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800902 return status;
903}
904
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800905static int stop_input_stream(struct stream_in *in)
906{
907 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800908 struct audio_usecase *uc_info;
909 struct audio_device *adev = in->dev;
910
Eric Laurentc8400632013-02-14 19:04:54 -0800911 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800912
Eric Laurent994a6932013-07-17 11:51:42 -0700913 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700914 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800915 uc_info = get_usecase_from_list(adev, in->usecase);
916 if (uc_info == NULL) {
917 ALOGE("%s: Could not find the usecase (%d) in the list",
918 __func__, in->usecase);
919 return -EINVAL;
920 }
921
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800922 /* Close in-call recording streams */
923 voice_check_and_stop_incall_rec_usecase(adev, in);
924
Eric Laurent150dbfe2013-02-27 14:31:02 -0800925 /* 1. Disable stream specific mixer controls */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700926 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700927
928 /* 2. Disable the tx device */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700929 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800930
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800931 list_remove(&uc_info->list);
932 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800933
Eric Laurent994a6932013-07-17 11:51:42 -0700934 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800935 return ret;
936}
937
938int start_input_stream(struct stream_in *in)
939{
940 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800941 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800942 struct audio_usecase *uc_info;
943 struct audio_device *adev = in->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +0530944 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800945
Ramjee Singh240ac652015-07-31 16:12:29 +0530946 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
947 if (get_usecase_from_list(adev, usecase) == NULL)
948 in->usecase = usecase;
949
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +0530950 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
951 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700952
Naresh Tannirucef332d2014-06-04 18:17:56 +0530953 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
954 ALOGE("%s: sound card is not active/SSR returning error ", __func__);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530955 ret = -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530956 goto error_config;
957 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530958
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700959 /* Check if source matches incall recording usecase criteria */
960 ret = voice_check_and_set_incall_rec_usecase(adev, in);
961 if (ret)
962 goto error_config;
963 else
964 ALOGV("%s: usecase(%d)", __func__, in->usecase);
965
Ramjee Singh240ac652015-07-31 16:12:29 +0530966 if (get_usecase_from_list(adev, in->usecase) != NULL) {
967 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
968 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
969 goto error_config;
970 }
971
Eric Laurentb23d5282013-05-14 15:27:20 -0700972 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800973 if (in->pcm_device_id < 0) {
974 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
975 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800976 ret = -EINVAL;
977 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800978 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700979
980 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800981 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew George9f0254a2014-06-30 13:56:18 -0700982
983 if (!uc_info) {
984 ret = -ENOMEM;
985 goto error_config;
986 }
987
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800988 uc_info->id = in->usecase;
989 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800990 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700991 uc_info->devices = in->device;
992 uc_info->in_snd_device = SND_DEVICE_NONE;
993 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800994
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800995 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700996 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800997
Eric Laurentc8400632013-02-14 19:04:54 -0800998 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530999 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
1000
1001 unsigned int flags = PCM_IN;
1002 unsigned int pcm_open_retry_entry_count = 0;
1003
1004 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1005 flags |= PCM_MMAP | PCM_NOIRQ;
1006 pcm_open_retry_entry_count = PROXY_OPEN_RETRY_COUNT;
1007 }
1008
1009 while(1) {
1010 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
1011 flags, &in->config);
1012 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
1013 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
1014 if (in->pcm != NULL) {
1015 pcm_close(in->pcm);
1016 in->pcm = NULL;
1017 }
1018 if (pcm_open_retry_entry_count-- == 0) {
1019 ret = -EIO;
1020 goto error_open;
1021 }
1022 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1023 continue;
1024 }
1025 break;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301027
Eric Laurent994a6932013-07-17 11:51:42 -07001028 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -08001029 return ret;
1030
1031error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001032 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -08001033
1034error_config:
1035 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001036 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -08001037
1038 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001039}
1040
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001041/* must be called with out->lock locked */
1042static int send_offload_cmd_l(struct stream_out* out, int command)
1043{
1044 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
1045
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001046 if (!cmd) {
1047 ALOGE("failed to allocate mem for command 0x%x", command);
1048 return -ENOMEM;
1049 }
1050
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001051 ALOGVV("%s %d", __func__, command);
1052
1053 cmd->cmd = command;
1054 list_add_tail(&out->offload_cmd_list, &cmd->node);
1055 pthread_cond_signal(&out->offload_cond);
1056 return 0;
1057}
1058
1059/* must be called iwth out->lock locked */
1060static void stop_compressed_output_l(struct stream_out *out)
1061{
1062 out->offload_state = OFFLOAD_STATE_IDLE;
1063 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001064 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001065 if (out->compr != NULL) {
1066 compress_stop(out->compr);
1067 while (out->offload_thread_blocked) {
1068 pthread_cond_wait(&out->cond, &out->lock);
1069 }
1070 }
1071}
1072
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001073bool is_offload_usecase(audio_usecase_t uc_id)
1074{
1075 unsigned int i;
1076 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1077 if (uc_id == offload_usecases[i])
1078 return true;
1079 }
1080 return false;
1081}
1082
1083static audio_usecase_t get_offload_usecase(struct audio_device *adev)
1084{
1085 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1086 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
1087 char value[PROPERTY_VALUE_MAX] = {0};
1088
1089 property_get("audio.offload.multiple.enabled", value, NULL);
1090 if (!(atoi(value) || !strncmp("true", value, 4)))
1091 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
1092
1093 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
1094 for (i = 0; i < num_usecase; i++) {
1095 if (!(adev->offload_usecases_state & (0x1<<i))) {
1096 adev->offload_usecases_state |= 0x1 << i;
1097 ret = offload_usecases[i];
1098 break;
1099 }
1100 }
1101 ALOGV("%s: offload usecase is %d", __func__, ret);
1102 return ret;
1103}
1104
1105static void free_offload_usecase(struct audio_device *adev,
1106 audio_usecase_t uc_id)
1107{
1108 unsigned int i;
1109 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1110 if (offload_usecases[i] == uc_id) {
1111 adev->offload_usecases_state &= ~(0x1<<i);
1112 break;
1113 }
1114 }
1115 ALOGV("%s: free offload usecase %d", __func__, uc_id);
1116}
1117
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001118static void *offload_thread_loop(void *context)
1119{
1120 struct stream_out *out = (struct stream_out *) context;
1121 struct listnode *item;
Krishnankutty Kolathappilly0fe78f02014-01-06 18:33:58 -08001122 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001123
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001124 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1125 set_sched_policy(0, SP_FOREGROUND);
1126 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1127
1128 ALOGV("%s", __func__);
1129 pthread_mutex_lock(&out->lock);
1130 for (;;) {
1131 struct offload_cmd *cmd = NULL;
1132 stream_callback_event_t event;
1133 bool send_callback = false;
1134
1135 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1136 __func__, list_empty(&out->offload_cmd_list),
1137 out->offload_state);
1138 if (list_empty(&out->offload_cmd_list)) {
1139 ALOGV("%s SLEEPING", __func__);
1140 pthread_cond_wait(&out->offload_cond, &out->lock);
1141 ALOGV("%s RUNNING", __func__);
1142 continue;
1143 }
1144
1145 item = list_head(&out->offload_cmd_list);
1146 cmd = node_to_item(item, struct offload_cmd, node);
1147 list_remove(item);
1148
1149 ALOGVV("%s STATE %d CMD %d out->compr %p",
1150 __func__, out->offload_state, cmd->cmd, out->compr);
1151
1152 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1153 free(cmd);
1154 break;
1155 }
1156
1157 if (out->compr == NULL) {
1158 ALOGE("%s: Compress handle is NULL", __func__);
1159 pthread_cond_signal(&out->cond);
1160 continue;
1161 }
1162 out->offload_thread_blocked = true;
1163 pthread_mutex_unlock(&out->lock);
1164 send_callback = false;
1165 switch(cmd->cmd) {
1166 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
1167 compress_wait(out->compr, -1);
1168 send_callback = true;
1169 event = STREAM_CBK_EVENT_WRITE_READY;
1170 break;
1171 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappilly0fe78f02014-01-06 18:33:58 -08001172 ret = compress_next_track(out->compr);
1173 if(ret == 0)
1174 compress_partial_drain(out->compr);
1175 else if(ret == -ETIMEDOUT)
1176 compress_drain(out->compr);
1177 else
1178 ALOGE("%s: Next track returned error %d",__func__, ret);
1179
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001180 send_callback = true;
1181 event = STREAM_CBK_EVENT_DRAIN_READY;
Ravi Kumar Alamanda3d316172014-12-02 19:21:51 -08001182 /* Resend the metadata for next iteration */
1183 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001184 break;
1185 case OFFLOAD_CMD_DRAIN:
1186 compress_drain(out->compr);
1187 send_callback = true;
1188 event = STREAM_CBK_EVENT_DRAIN_READY;
1189 break;
1190 default:
1191 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1192 break;
1193 }
1194 pthread_mutex_lock(&out->lock);
1195 out->offload_thread_blocked = false;
1196 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001197 if (send_callback) {
Ravi Kumar Alamanda3d316172014-12-02 19:21:51 -08001198 ALOGVV("%s: sending offload_callback event %d", __func__, event);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001199 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001200 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001201 free(cmd);
1202 }
1203
1204 pthread_cond_signal(&out->cond);
1205 while (!list_empty(&out->offload_cmd_list)) {
1206 item = list_head(&out->offload_cmd_list);
1207 list_remove(item);
1208 free(node_to_item(item, struct offload_cmd, node));
1209 }
1210 pthread_mutex_unlock(&out->lock);
1211
1212 return NULL;
1213}
1214
1215static int create_offload_callback_thread(struct stream_out *out)
1216{
1217 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1218 list_init(&out->offload_cmd_list);
1219 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1220 offload_thread_loop, out);
1221 return 0;
1222}
1223
1224static int destroy_offload_callback_thread(struct stream_out *out)
1225{
1226 pthread_mutex_lock(&out->lock);
1227 stop_compressed_output_l(out);
1228 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1229
1230 pthread_mutex_unlock(&out->lock);
1231 pthread_join(out->offload_thread, (void **) NULL);
1232 pthread_cond_destroy(&out->offload_cond);
1233
1234 return 0;
1235}
1236
Eric Laurent07eeafd2013-10-06 12:52:49 -07001237static bool allow_hdmi_channel_config(struct audio_device *adev)
1238{
1239 struct listnode *node;
1240 struct audio_usecase *usecase;
1241 bool ret = true;
1242
1243 list_for_each(node, &adev->usecase_list) {
1244 usecase = node_to_item(node, struct audio_usecase, list);
1245 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1246 /*
1247 * If voice call is already existing, do not proceed further to avoid
1248 * disabling/enabling both RX and TX devices, CSD calls, etc.
1249 * Once the voice call done, the HDMI channels can be configured to
1250 * max channels of remaining use cases.
1251 */
1252 if (usecase->id == USECASE_VOICE_CALL) {
1253 ALOGD("%s: voice call is active, no change in HDMI channels",
1254 __func__);
1255 ret = false;
1256 break;
1257 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1258 ALOGD("%s: multi channel playback is active, "
1259 "no change in HDMI channels", __func__);
1260 ret = false;
1261 break;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001262 } else if (is_offload_usecase(usecase->id) &&
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05301263 audio_channel_count_from_out_mask(usecase->stream.out->channel_mask) > 2) {
Mingming Yin3ed162b2014-02-24 17:56:01 -08001264 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1265 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1266 ret = false;
1267 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001268 }
1269 }
1270 }
1271 return ret;
1272}
1273
1274static int check_and_set_hdmi_channels(struct audio_device *adev,
1275 unsigned int channels)
1276{
1277 struct listnode *node;
1278 struct audio_usecase *usecase;
1279
1280 /* Check if change in HDMI channel config is allowed */
1281 if (!allow_hdmi_channel_config(adev))
1282 return 0;
1283
1284 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001285 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001286 return 0;
1287 }
1288
1289 platform_set_hdmi_channels(adev->platform, channels);
1290 adev->cur_hdmi_channels = channels;
1291
1292 /*
1293 * Deroute all the playback streams routed to HDMI so that
1294 * the back end is deactivated. Note that backend will not
1295 * be deactivated if any one stream is connected to it.
1296 */
1297 list_for_each(node, &adev->usecase_list) {
1298 usecase = node_to_item(node, struct audio_usecase, list);
1299 if (usecase->type == PCM_PLAYBACK &&
1300 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001301 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001302 }
1303 }
1304
1305 /*
1306 * Enable all the streams disabled above. Now the HDMI backend
1307 * will be activated with new channel configuration
1308 */
1309 list_for_each(node, &adev->usecase_list) {
1310 usecase = node_to_item(node, struct audio_usecase, list);
1311 if (usecase->type == PCM_PLAYBACK &&
1312 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001313 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001314 }
1315 }
1316
1317 return 0;
1318}
1319
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001320static int stop_output_stream(struct stream_out *out)
1321{
1322 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323 struct audio_usecase *uc_info;
1324 struct audio_device *adev = out->dev;
1325
Eric Laurent994a6932013-07-17 11:51:42 -07001326 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001327 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001328 uc_info = get_usecase_from_list(adev, out->usecase);
1329 if (uc_info == NULL) {
1330 ALOGE("%s: Could not find the usecase (%d) in the list",
1331 __func__, out->usecase);
1332 return -EINVAL;
1333 }
1334
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001335 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001336 if (adev->visualizer_stop_output != NULL)
1337 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1338 if (adev->offload_effects_stop_output != NULL)
1339 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1340 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001341
Eric Laurent150dbfe2013-02-27 14:31:02 -08001342 /* 1. Get and set stream specific mixer controls */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001343 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001344
1345 /* 2. Disable the rx device */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001346 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001347
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001348 list_remove(&uc_info->list);
1349 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001350
Eric Laurentda46bfb2014-08-25 22:39:29 -05001351 audio_extn_extspk_update(adev->extspk);
1352
Eric Laurent07eeafd2013-10-06 12:52:49 -07001353 /* Must be called after removing the usecase from list */
1354 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1355 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1356
Eric Laurent994a6932013-07-17 11:51:42 -07001357 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001358 return ret;
1359}
1360
1361int start_output_stream(struct stream_out *out)
1362{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001363 int ret = 0;
Mingming Yin6c344432014-05-01 15:37:31 -07001364 int sink_channels = 0;
1365 char prop_value[PROPERTY_VALUE_MAX] = {0};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001366 struct audio_usecase *uc_info;
1367 struct audio_device *adev = out->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +05301368 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001369
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001370 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
1371 ret = -EINVAL;
1372 goto error_config;
1373 }
1374
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301375 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1376 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1377 out->devices);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301378
Naresh Tannirucef332d2014-06-04 18:17:56 +05301379 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301380 ALOGE("%s: sound card is not active/SSR returning error", __func__);
1381 ret = -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301382 goto error_config;
1383 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301384
Eric Laurentb23d5282013-05-14 15:27:20 -07001385 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001386 if (out->pcm_device_id < 0) {
1387 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1388 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001389 ret = -EINVAL;
1390 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001391 }
1392
1393 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001394
1395 if (!uc_info) {
1396 ret = -ENOMEM;
1397 goto error_config;
1398 }
1399
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001400 uc_info->id = out->usecase;
1401 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001402 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001403 uc_info->devices = out->devices;
1404 uc_info->in_snd_device = SND_DEVICE_NONE;
1405 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001406
Eric Laurent07eeafd2013-10-06 12:52:49 -07001407 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001408 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Mingming Yin6c344432014-05-01 15:37:31 -07001409 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1410 if (!strncmp("true", prop_value, 4)) {
1411 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1412 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1413 check_and_set_hdmi_channels(adev, sink_channels);
1414 } else {
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001415 if (is_offload_usecase(out->usecase))
Mingming Yin6c344432014-05-01 15:37:31 -07001416 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1417 else
1418 check_and_set_hdmi_channels(adev, out->config.channels);
1419 }
Mingming Yin10fef6a2013-11-26 17:17:01 -08001420 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001421
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001422 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001423
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001424 select_devices(adev, out->usecase);
1425
Eric Laurentda46bfb2014-08-25 22:39:29 -05001426 audio_extn_extspk_update(adev->extspk);
1427
Steve Kondik3abbbc82014-11-29 14:14:43 -08001428 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
1429 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
1430
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001431 if (!is_offload_usecase(out->usecase)) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301432 unsigned int flags = PCM_OUT;
1433 unsigned int pcm_open_retry_count = 0;
1434 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1435 flags |= PCM_MMAP | PCM_NOIRQ;
1436 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1437 } else
1438 flags |= PCM_MONOTONIC;
1439
1440 while (1) {
1441 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1442 flags, &out->config);
1443 if (out->pcm && !pcm_is_ready(out->pcm)) {
1444 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1445 if (out->pcm != NULL) {
1446 pcm_close(out->pcm);
1447 out->pcm = NULL;
1448 }
1449 if (pcm_open_retry_count-- == 0) {
1450 ret = -EIO;
1451 goto error_open;
1452 }
1453 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1454 continue;
1455 }
1456 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001457 }
1458 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001459 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001460 out->compr = compress_open(adev->snd_card,
1461 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001462 COMPRESS_IN, &out->compr_config);
1463 if (out->compr && !is_compress_ready(out->compr)) {
1464 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1465 compress_close(out->compr);
1466 out->compr = NULL;
1467 ret = -EIO;
1468 goto error_open;
1469 }
1470 if (out->offload_callback)
1471 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001472
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08001473#ifdef DS1_DOLBY_DDP_ENABLED
1474 if (audio_extn_is_dolby_format(out->format))
1475 audio_extn_dolby_send_ddp_endp_params(adev);
1476#endif
1477
Eric Laurentc4aef752013-09-12 17:45:53 -07001478 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001479 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1480 if (adev->offload_effects_start_output != NULL)
1481 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001482 }
Eric Laurent994a6932013-07-17 11:51:42 -07001483 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001484 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001485error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001486 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001487error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001488 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001489}
1490
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001491static int check_input_parameters(uint32_t sample_rate,
1492 audio_format_t format,
1493 int channel_count)
1494{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001495 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001496
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001497 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001498 !voice_extn_compress_voip_is_format_supported(format) &&
1499 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001500
1501 switch (channel_count) {
1502 case 1:
1503 case 2:
1504 case 6:
1505 break;
1506 default:
1507 ret = -EINVAL;
1508 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001509
1510 switch (sample_rate) {
1511 case 8000:
1512 case 11025:
1513 case 12000:
1514 case 16000:
1515 case 22050:
1516 case 24000:
1517 case 32000:
1518 case 44100:
1519 case 48000:
1520 break;
1521 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001522 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001523 }
1524
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001525 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001526}
1527
1528static size_t get_input_buffer_size(uint32_t sample_rate,
1529 audio_format_t format,
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07001530 int channel_count,
1531 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001532{
1533 size_t size = 0;
1534
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001535 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1536 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001537
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001538 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07001539 if (is_low_latency)
1540 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001541 /* ToDo: should use frame_size computed based on the format and
1542 channel_count here. */
1543 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001544
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07001545 /* make sure the size is multiple of 32 bytes
1546 * At 48 kHz mono 16-bit PCM:
1547 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1548 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1549 */
1550 size += 0x1f;
1551 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001552
1553 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001554}
1555
1556static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1557{
1558 struct stream_out *out = (struct stream_out *)stream;
1559
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001560 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001561}
1562
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301563static int out_set_sample_rate(struct audio_stream *stream __unused,
1564 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001565{
1566 return -ENOSYS;
1567}
1568
1569static size_t out_get_buffer_size(const struct audio_stream *stream)
1570{
1571 struct stream_out *out = (struct stream_out *)stream;
1572
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001573 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001574 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001575 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1576 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001577
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05301578 return out->config.period_size *
1579 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001580}
1581
1582static uint32_t out_get_channels(const struct audio_stream *stream)
1583{
1584 struct stream_out *out = (struct stream_out *)stream;
1585
1586 return out->channel_mask;
1587}
1588
1589static audio_format_t out_get_format(const struct audio_stream *stream)
1590{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001591 struct stream_out *out = (struct stream_out *)stream;
1592
1593 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001594}
1595
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301596static int out_set_format(struct audio_stream *stream __unused,
1597 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001598{
1599 return -ENOSYS;
1600}
1601
1602static int out_standby(struct audio_stream *stream)
1603{
1604 struct stream_out *out = (struct stream_out *)stream;
1605 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001606
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301607 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1608 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001609 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1610 /* Ignore standby in case of voip call because the voip output
1611 * stream is closed in adev_close_output_stream()
1612 */
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301613 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001614 return 0;
1615 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001616
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001617 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001618 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001619 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001620 out->standby = true;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001621 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001622 if (out->pcm) {
1623 pcm_close(out->pcm);
1624 out->pcm = NULL;
1625 }
1626 } else {
1627 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001628 out->gapless_mdata.encoder_delay = 0;
1629 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001630 if (out->compr != NULL) {
1631 compress_close(out->compr);
1632 out->compr = NULL;
1633 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001634 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001635 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001636 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001637 }
1638 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001639 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001640 return 0;
1641}
1642
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301643static int out_dump(const struct audio_stream *stream __unused,
1644 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001645{
1646 return 0;
1647}
1648
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001649static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1650{
1651 int ret = 0;
1652 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001653 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001654 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001655 tmp_mdata.encoder_delay = 0;
1656 tmp_mdata.encoder_padding = 0;
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001657
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001658 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001659 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001660 return -EINVAL;
1661 }
1662
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001663 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1664 if (ret >= 0) {
1665 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1666 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1667 ALOGV("ADTS format is set in offload mode");
1668 }
1669 out->send_new_metadata = 1;
1670 }
1671
Steve Kondik6bedcdf2014-07-21 11:48:30 -07001672#ifdef FLAC_OFFLOAD_ENABLED
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08001673 if (out->format == AUDIO_FORMAT_FLAC) {
1674 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_BLK_SIZE, value, sizeof(value));
1675 if (ret >= 0) {
1676 out->compr_config.codec->options.flac_dec.min_blk_size = atoi(value);
1677 out->send_new_metadata = 1;
1678 }
1679 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_BLK_SIZE, value, sizeof(value));
1680 if (ret >= 0) {
1681 out->compr_config.codec->options.flac_dec.max_blk_size = atoi(value);
1682 out->send_new_metadata = 1;
1683 }
1684 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_FRAME_SIZE, value, sizeof(value));
1685 if (ret >= 0) {
1686 out->compr_config.codec->options.flac_dec.min_frame_size = atoi(value);
1687 out->send_new_metadata = 1;
1688 }
1689 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_FRAME_SIZE, value, sizeof(value));
1690 if (ret >= 0) {
1691 out->compr_config.codec->options.flac_dec.max_frame_size = atoi(value);
1692 out->send_new_metadata = 1;
1693 }
1694 }
Steve Kondik6bedcdf2014-07-21 11:48:30 -07001695#endif
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08001696
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001697 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1698 if(ret >= 0)
1699 is_meta_data_params = true;
1700 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1701 if(ret >= 0 )
1702 is_meta_data_params = true;
1703 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1704 if(ret >= 0 )
1705 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001706 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1707 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001708 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001709 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001710 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001711 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1712 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001713 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001714 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001715 }
1716
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001717 if(!is_meta_data_params) {
1718 ALOGV("%s: Not gapless meta data params", __func__);
1719 return 0;
1720 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001721 out->gapless_mdata = tmp_mdata;
1722 out->send_new_metadata = 1;
1723 ALOGV("%s new encoder delay %u and padding %u", __func__,
1724 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1725
Steve Kondikba3b35d2014-07-18 01:49:48 -07001726 if(out->format == AUDIO_FORMAT_WMA || out->format == AUDIO_FORMAT_WMA_PRO) {
1727 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_FORMAT_TAG, value, sizeof(value));
1728 if (ret >= 0) {
1729 out->compr_config.codec->format = atoi(value);
1730 }
1731 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_BLOCK_ALIGN, value, sizeof(value));
1732 if (ret >= 0) {
1733 out->compr_config.codec->options.wma.super_block_align = atoi(value);
1734 }
1735 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_BIT_PER_SAMPLE, value, sizeof(value));
1736 if (ret >= 0) {
1737 out->compr_config.codec->options.wma.bits_per_sample = atoi(value);
1738 }
1739 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_CHANNEL_MASK, value, sizeof(value));
1740 if (ret >= 0) {
1741 out->compr_config.codec->options.wma.channelmask = atoi(value);
1742 }
1743 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_ENCODE_OPTION, value, sizeof(value));
1744 if (ret >= 0) {
1745 out->compr_config.codec->options.wma.encodeopt = atoi(value);
1746 }
1747 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_ENCODE_OPTION1, value, sizeof(value));
1748 if (ret >= 0) {
1749 out->compr_config.codec->options.wma.encodeopt1 = atoi(value);
1750 }
1751 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_WMA_ENCODE_OPTION2, value, sizeof(value));
1752 if (ret >= 0) {
1753 out->compr_config.codec->options.wma.encodeopt2 = atoi(value);
1754 }
1755 ALOGV("WMA params: fmt %x, balgn %x, sr %d, chmsk %x, encop %x, op1 %x, op2 %x",
1756 out->compr_config.codec->format,
1757 out->compr_config.codec->options.wma.super_block_align,
1758 out->compr_config.codec->options.wma.bits_per_sample,
1759 out->compr_config.codec->options.wma.channelmask,
1760 out->compr_config.codec->options.wma.encodeopt,
1761 out->compr_config.codec->options.wma.encodeopt1,
1762 out->compr_config.codec->options.wma.encodeopt2);
1763 }
1764
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001765 return 0;
1766}
1767
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301768static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1769{
1770 return out == adev->primary_output || out == adev->voice_tx_output;
1771}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001772
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001773static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1774{
1775 struct stream_out *out = (struct stream_out *)stream;
1776 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001777 struct audio_usecase *usecase;
1778 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001779 struct str_parms *parms;
1780 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001781 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001782 bool select_new_device = false;
Steve Kondik3abbbc82014-11-29 14:14:43 -08001783 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001784
sangwoobc677242013-08-08 16:53:43 +09001785 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001786 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001787 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001788 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1789 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001791 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001792 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001793
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001794 /*
Dhanalakshmi Siddani523ae412014-04-18 22:26:56 +05301795 * When HDMI cable is unplugged/usb hs is disconnected the
1796 * music playback is paused and the policy manager sends routing=0
1797 * But the audioflingercontinues to write data until standby time
1798 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001799 * Avoid this by routing audio to speaker until standby.
1800 */
Dhanalakshmi Siddani523ae412014-04-18 22:26:56 +05301801 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
Steve Kondikeadb6c82014-09-03 18:40:06 -07001802 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
1803 out->devices == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001804 val == AUDIO_DEVICE_NONE) {
1805 val = AUDIO_DEVICE_OUT_SPEAKER;
1806 }
1807
1808 /*
1809 * select_devices() call below switches all the usecases on the same
1810 * backend to the new device. Refer to check_usecases_codec_backend() in
1811 * the select_devices(). But how do we undo this?
1812 *
1813 * For example, music playback is active on headset (deep-buffer usecase)
1814 * and if we go to ringtones and select a ringtone, low-latency usecase
1815 * will be started on headset+speaker. As we can't enable headset+speaker
1816 * and headset devices at the same time, select_devices() switches the music
1817 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1818 * So when the ringtone playback is completed, how do we undo the same?
1819 *
1820 * We are relying on the out_set_parameters() call on deep-buffer output,
1821 * once the ringtone playback is ended.
1822 * NOTE: We should not check if the current devices are same as new devices.
1823 * Because select_devices() must be called to switch back the music
1824 * playback to headset.
1825 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001826 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001827 out->devices = val;
1828
1829 if (!out->standby)
1830 select_devices(adev, out->usecase);
1831
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001832 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301833 output_drives_call(adev, out)) {
1834 adev->current_call_output = out;
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001835 if (!adev->voice.in_call)
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301836 ret = voice_start_call(adev);
1837 else
1838 voice_update_devices_for_all_voice_usecases(adev);
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001839 }
1840 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001841
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001842 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1843 adev->voice.in_call &&
1844 output_drives_call(adev, out)) {
1845 ret = voice_stop_call(adev);
1846 adev->current_call_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001847 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001848
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001849 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001850 pthread_mutex_unlock(&out->lock);
Eric Laurentda46bfb2014-08-25 22:39:29 -05001851
1852 /*handles device and call state changes*/
1853 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001854 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001855
1856 if (out == adev->primary_output) {
1857 pthread_mutex_lock(&adev->lock);
1858 audio_extn_set_parameters(adev, parms);
1859 pthread_mutex_unlock(&adev->lock);
1860 }
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001861 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001862 pthread_mutex_lock(&out->lock);
Steve Kondikba3b35d2014-07-18 01:49:48 -07001863 ret = parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001864 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001865 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001866
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001867 str_parms_destroy(parms);
Steve Kondik3abbbc82014-11-29 14:14:43 -08001868 ALOGV("%s: exit: code(%d)", __func__, status);
1869 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001870}
1871
1872static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1873{
1874 struct stream_out *out = (struct stream_out *)stream;
1875 struct str_parms *query = str_parms_create_str(keys);
1876 char *str;
1877 char value[256];
1878 struct str_parms *reply = str_parms_create();
1879 size_t i, j;
1880 int ret;
1881 bool first = true;
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07001882
1883 if (!query || !reply) {
1884 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
1885 return NULL;
1886 }
1887
Eric Laurent994a6932013-07-17 11:51:42 -07001888 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001889 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1890 if (ret >= 0) {
1891 value[0] = '\0';
1892 i = 0;
1893 while (out->supported_channel_masks[i] != 0) {
1894 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1895 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1896 if (!first) {
1897 strcat(value, "|");
1898 }
1899 strcat(value, out_channels_name_to_enum_table[j].name);
1900 first = false;
1901 break;
1902 }
1903 }
1904 i++;
1905 }
1906 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1907 str = str_parms_to_str(reply);
1908 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001909 voice_extn_out_get_parameters(out, query, reply);
1910 str = str_parms_to_str(reply);
1911 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella2e032352014-01-29 12:52:19 -08001912 free(str);
1913 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001914 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001915 }
1916 str_parms_destroy(query);
1917 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001918 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001919 return str;
1920}
1921
1922static uint32_t out_get_latency(const struct audio_stream_out *stream)
1923{
1924 struct stream_out *out = (struct stream_out *)stream;
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001925 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001926
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001927 if (is_offload_usecase(out->usecase)) {
1928 if (out->use_small_bufs == true)
1929 latency = ((out->compr_config.fragments *
1930 out->compr_config.fragment_size * 1000) /
1931 (out->sample_rate * out->compr_config.codec->ch_in *
1932 audio_bytes_per_sample(out->format)));
1933 else
1934 latency = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1935 } else {
1936 latency = (out->config.period_count * out->config.period_size * 1000) /
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001937 (out->config.rate);
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001938 }
1939
Anish Kumar1a0594f2014-12-09 04:01:39 +05301940 ALOGV("%s: Latency %d", __func__, latency);
Alexy Joseph3a2fec32014-12-03 02:46:47 -08001941 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001942}
1943
1944static int out_set_volume(struct audio_stream_out *stream, float left,
1945 float right)
1946{
Eric Laurenta9024de2013-04-04 09:19:12 -07001947 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001948 int volume[2];
1949
Eric Laurenta9024de2013-04-04 09:19:12 -07001950 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1951 /* only take left channel into account: the API is for stereo anyway */
1952 out->muted = (left == 0.0f);
1953 return 0;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001954 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001955 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001956 struct audio_device *adev = out->dev;
1957 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001958 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1959 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001960
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001961 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1962 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001963 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1964 if (!ctl) {
1965 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1966 __func__, mixer_ctl_name);
1967 return -EINVAL;
1968 }
1969 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1970 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1971 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1972 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001973 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001974
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001975 return -ENOSYS;
1976}
1977
1978static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1979 size_t bytes)
1980{
1981 struct stream_out *out = (struct stream_out *)stream;
1982 struct audio_device *adev = out->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +05301983 int snd_scard_state = get_snd_card_state(adev);
Eric Laurent6e895242013-09-05 16:10:57 -07001984 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001985
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001986 pthread_mutex_lock(&out->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301987
Naresh Tannirucef332d2014-06-04 18:17:56 +05301988 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
1989 if (out->pcm) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301990 ALOGD(" %s: sound card is not active/SSR state", __func__);
1991 ret= -ENETRESET;
1992 goto exit;
Alexy Joseph55204352014-10-06 12:15:01 -07001993 } else if (is_offload_usecase(out->usecase)) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05301994 //during SSR for compress usecase we should return error to flinger
1995 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
1996 pthread_mutex_unlock(&out->lock);
1997 return -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301998 }
1999 }
2000
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002001 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002002 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002003 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002004 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
2005 ret = voice_extn_compress_voip_start_output_stream(out);
2006 else
2007 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002008 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002009 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002010 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002011 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002012 goto exit;
2013 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002014 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002015
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002016 if (is_offload_usecase(out->usecase)) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002017 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
2018 if (out->send_new_metadata) {
2019 ALOGVV("send new gapless metadata");
2020 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
2021 out->send_new_metadata = 0;
2022 }
2023
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002024 ret = compress_write(out->compr, buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302025 if (ret < 0)
2026 ret = -errno;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002027 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07002028 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002029 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302030 } else if (-ENETRESET == ret) {
2031 ALOGE("copl %s: received sound card offline state on compress write", __func__);
2032 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2033 pthread_mutex_unlock(&out->lock);
2034 out_standby(&out->stream.common);
2035 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002036 }
Naresh Tannirucef332d2014-06-04 18:17:56 +05302037 if (!out->playback_started && ret >= 0) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002038 compress_start(out->compr);
2039 out->playback_started = 1;
2040 out->offload_state = OFFLOAD_STATE_PLAYING;
2041 }
2042 pthread_mutex_unlock(&out->lock);
2043 return ret;
2044 } else {
2045 if (out->pcm) {
2046 if (out->muted)
2047 memset((void *)buffer, 0, bytes);
2048 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302049 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY)
2050 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
2051 else
2052 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302053 if (ret < 0)
2054 ret = -errno;
2055 else if (ret == 0)
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002056 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002057 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002058 }
2059
2060exit:
Dhanalakshmi Siddani4fe3e512014-05-26 18:03:42 +05302061 /* ToDo: There may be a corner case when SSR happens back to back during
2062 start/stop. Need to post different error to handle that. */
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302063 if (-ENETRESET == ret) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302064 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302065 }
2066
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002067 pthread_mutex_unlock(&out->lock);
2068
2069 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002070 if (out->pcm)
2071 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302072 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302073 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302074 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302075 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302076 out->standby = true;
2077 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002078 out_standby(&out->stream.common);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002079 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
2080 out_get_sample_rate(&out->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002081 }
2082 return bytes;
2083}
2084
2085static int out_get_render_position(const struct audio_stream_out *stream,
2086 uint32_t *dsp_frames)
2087{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002088 struct stream_out *out = (struct stream_out *)stream;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002089 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
2090 ssize_t ret = 0;
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002091 *dsp_frames = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002092 pthread_mutex_lock(&out->lock);
2093 if (out->compr != NULL) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302094 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002095 &out->sample_rate);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302096 if (ret < 0)
2097 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002098 ALOGVV("%s rendered frames %d sample_rate %d",
2099 __func__, *dsp_frames, out->sample_rate);
2100 }
2101 pthread_mutex_unlock(&out->lock);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302102 if (-ENETRESET == ret) {
2103 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
2104 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2105 return -EINVAL;
2106 } else if(ret < 0) {
Steve Kondik81506242014-07-21 16:12:57 -07002107 if (out->compr == NULL) {
2108 return 0;
2109 }
2110 ALOGE(" ERROR: Unable to get time stamp from compress driver ret=%d", ret);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302111 return -EINVAL;
2112 } else {
2113 return 0;
2114 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002115 } else
2116 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002117}
2118
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302119static int out_add_audio_effect(const struct audio_stream *stream __unused,
2120 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002121{
2122 return 0;
2123}
2124
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302125static int out_remove_audio_effect(const struct audio_stream *stream __unused,
2126 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002127{
2128 return 0;
2129}
2130
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302131static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
2132 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002133{
2134 return -EINVAL;
2135}
2136
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002137static int out_get_presentation_position(const struct audio_stream_out *stream,
2138 uint64_t *frames, struct timespec *timestamp)
2139{
2140 struct stream_out *out = (struct stream_out *)stream;
2141 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07002142 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002143
2144 pthread_mutex_lock(&out->lock);
2145
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002146 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07002147 if (out->compr != NULL) {
2148 compress_get_tstamp(out->compr, &dsp_frames,
2149 &out->sample_rate);
2150 ALOGVV("%s rendered frames %ld sample_rate %d",
2151 __func__, dsp_frames, out->sample_rate);
2152 *frames = dsp_frames;
2153 ret = 0;
2154 /* this is the best we can do */
2155 clock_gettime(CLOCK_MONOTONIC, timestamp);
2156 }
2157 } else {
2158 if (out->pcm) {
2159 size_t avail;
2160 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
2161 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07002162 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07002163 // This adjustment accounts for buffering after app processor.
2164 // It is based on estimated DSP latency per use case, rather than exact.
2165 signed_frames -=
2166 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
2167
Eric Laurent949a0892013-09-20 09:20:13 -07002168 // It would be unusual for this value to be negative, but check just in case ...
2169 if (signed_frames >= 0) {
2170 *frames = signed_frames;
2171 ret = 0;
2172 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002173 }
2174 }
2175 }
2176
2177 pthread_mutex_unlock(&out->lock);
2178
2179 return ret;
2180}
2181
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002182static int out_set_callback(struct audio_stream_out *stream,
2183 stream_callback_t callback, void *cookie)
2184{
2185 struct stream_out *out = (struct stream_out *)stream;
2186
2187 ALOGV("%s", __func__);
2188 pthread_mutex_lock(&out->lock);
2189 out->offload_callback = callback;
2190 out->offload_cookie = cookie;
2191 pthread_mutex_unlock(&out->lock);
2192 return 0;
2193}
2194
2195static int out_pause(struct audio_stream_out* stream)
2196{
2197 struct stream_out *out = (struct stream_out *)stream;
2198 int status = -ENOSYS;
2199 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002200 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002201 pthread_mutex_lock(&out->lock);
2202 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302203 struct audio_device *adev = out->dev;
2204 int snd_scard_state = get_snd_card_state(adev);
2205
2206 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2207 status = compress_pause(out->compr);
2208
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002209 out->offload_state = OFFLOAD_STATE_PAUSED;
2210 }
2211 pthread_mutex_unlock(&out->lock);
2212 }
2213 return status;
2214}
2215
2216static int out_resume(struct audio_stream_out* stream)
2217{
2218 struct stream_out *out = (struct stream_out *)stream;
2219 int status = -ENOSYS;
2220 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002221 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002222 status = 0;
2223 pthread_mutex_lock(&out->lock);
2224 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302225 struct audio_device *adev = out->dev;
2226 int snd_scard_state = get_snd_card_state(adev);
2227
2228 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2229 status = compress_resume(out->compr);
2230
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002231 out->offload_state = OFFLOAD_STATE_PLAYING;
2232 }
2233 pthread_mutex_unlock(&out->lock);
2234 }
2235 return status;
2236}
2237
2238static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2239{
2240 struct stream_out *out = (struct stream_out *)stream;
2241 int status = -ENOSYS;
2242 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002243 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002244 pthread_mutex_lock(&out->lock);
2245 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2246 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2247 else
2248 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2249 pthread_mutex_unlock(&out->lock);
2250 }
2251 return status;
2252}
2253
2254static int out_flush(struct audio_stream_out* stream)
2255{
2256 struct stream_out *out = (struct stream_out *)stream;
2257 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002258 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002259 pthread_mutex_lock(&out->lock);
2260 stop_compressed_output_l(out);
2261 pthread_mutex_unlock(&out->lock);
2262 return 0;
2263 }
2264 return -ENOSYS;
2265}
2266
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002267/** audio_stream_in implementation **/
2268static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2269{
2270 struct stream_in *in = (struct stream_in *)stream;
2271
2272 return in->config.rate;
2273}
2274
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302275static int in_set_sample_rate(struct audio_stream *stream __unused,
2276 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002277{
2278 return -ENOSYS;
2279}
2280
2281static size_t in_get_buffer_size(const struct audio_stream *stream)
2282{
2283 struct stream_in *in = (struct stream_in *)stream;
2284
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002285 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
2286 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07002287 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
2288 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002289
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302290 return in->config.period_size *
Steve Kondik3abbbc82014-11-29 14:14:43 -08002291 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002292}
2293
2294static uint32_t in_get_channels(const struct audio_stream *stream)
2295{
2296 struct stream_in *in = (struct stream_in *)stream;
2297
2298 return in->channel_mask;
2299}
2300
2301static audio_format_t in_get_format(const struct audio_stream *stream)
2302{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002303 struct stream_in *in = (struct stream_in *)stream;
2304
2305 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002306}
2307
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302308static int in_set_format(struct audio_stream *stream __unused,
2309 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002310{
2311 return -ENOSYS;
2312}
2313
2314static int in_standby(struct audio_stream *stream)
2315{
2316 struct stream_in *in = (struct stream_in *)stream;
2317 struct audio_device *adev = in->dev;
2318 int status = 0;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302319 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2320 stream, in->usecase, use_case_table[in->usecase]);
2321
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002322
2323 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2324 /* Ignore standby in case of voip call because the voip input
2325 * stream is closed in adev_close_input_stream()
2326 */
2327 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2328 return status;
2329 }
2330
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002331 pthread_mutex_lock(&in->lock);
2332 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002333 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002334 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002335 if (in->pcm) {
2336 pcm_close(in->pcm);
2337 in->pcm = NULL;
2338 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002339 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002340 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002341 }
2342 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002343 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002344 return status;
2345}
2346
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302347static int in_dump(const struct audio_stream *stream __unused,
2348 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002349{
2350 return 0;
2351}
2352
2353static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2354{
2355 struct stream_in *in = (struct stream_in *)stream;
2356 struct audio_device *adev = in->dev;
2357 struct str_parms *parms;
2358 char *str;
2359 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002360 int ret = 0, val = 0, err;
Steve Kondik3abbbc82014-11-29 14:14:43 -08002361 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002362
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302363 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002364 parms = str_parms_create_str(kvpairs);
2365
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002366 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002367 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002368
2369 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2370 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002371 val = atoi(value);
2372 /* no audio source uses val == 0 */
2373 if ((in->source != val) && (val != 0)) {
2374 in->source = val;
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002375 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2376 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2377 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2378 (in->config.rate == 8000 || in->config.rate == 16000) &&
2379 (popcount(in->channel_mask) == 1)) {
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08002380 err = voice_extn_compress_voip_open_input_stream(in);
2381 if (err != 0) {
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002382 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08002383 __func__, err);
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002384 }
2385 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002386 }
2387 }
2388
Steve Kondik3abbbc82014-11-29 14:14:43 -08002389 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2390
2391 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002392 val = atoi(value);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302393 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002394 in->device = val;
2395 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002396 if (!in->standby)
Steve Kondik3abbbc82014-11-29 14:14:43 -08002397 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002398 }
2399 }
2400
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002401done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002402 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002403 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002404
2405 str_parms_destroy(parms);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002406 ALOGV("%s: exit: status(%d)", __func__, status);
2407 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002408}
2409
2410static char* in_get_parameters(const struct audio_stream *stream,
2411 const char *keys)
2412{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002413 struct stream_in *in = (struct stream_in *)stream;
2414 struct str_parms *query = str_parms_create_str(keys);
2415 char *str;
2416 char value[256];
2417 struct str_parms *reply = str_parms_create();
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002418
2419 if (!query || !reply) {
2420 ALOGE("in_get_parameters: failed to create query or reply");
2421 return NULL;
2422 }
2423
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002424 ALOGV("%s: enter: keys - %s", __func__, keys);
2425
2426 voice_extn_in_get_parameters(in, query, reply);
2427
2428 str = str_parms_to_str(reply);
2429 str_parms_destroy(query);
2430 str_parms_destroy(reply);
2431
2432 ALOGV("%s: exit: returns - %s", __func__, str);
2433 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002434}
2435
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302436static int in_set_gain(struct audio_stream_in *stream __unused,
2437 float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002438{
2439 return 0;
2440}
2441
2442static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2443 size_t bytes)
2444{
2445 struct stream_in *in = (struct stream_in *)stream;
2446 struct audio_device *adev = in->dev;
2447 int i, ret = -1;
Naresh Tannirucef332d2014-06-04 18:17:56 +05302448 int snd_scard_state = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002449
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002450 pthread_mutex_lock(&in->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302451
2452 if (in->pcm) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302453 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302454 ALOGD(" %s: sound card is not active/SSR state", __func__);
2455 ret= -ENETRESET;
2456 goto exit;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302457 }
2458 }
2459
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002460 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002461 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002462 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2463 ret = voice_extn_compress_voip_start_input_stream(in);
2464 else
2465 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002466 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002467 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002468 goto exit;
2469 }
2470 in->standby = 0;
2471 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002472
2473 if (in->pcm) {
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302474 if (audio_extn_ssr_get_enabled() &&
2475 audio_channel_count_from_in_mask(in->channel_mask) == 6)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002476 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002477 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2478 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302479 else if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY)
2480 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002481 else
2482 ret = pcm_read(in->pcm, buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302483 if (ret < 0)
2484 ret = -errno;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002485 }
2486
2487 /*
2488 * Instead of writing zeroes here, we could trust the hardware
2489 * to always provide zeroes when muted.
2490 */
Pavan Chikkala6c183fd2014-12-04 10:48:28 +05302491 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in) &&
2492 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002493 memset(buffer, 0, bytes);
2494
2495exit:
Dhanalakshmi Siddani4fe3e512014-05-26 18:03:42 +05302496 /* ToDo: There may be a corner case when SSR happens back to back during
2497 start/stop. Need to post different error to handle that. */
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302498 if (-ENETRESET == ret) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302499 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302500 memset(buffer, 0, bytes);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302501 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002502 pthread_mutex_unlock(&in->lock);
2503
2504 if (ret != 0) {
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302505 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302506 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302507 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302508 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302509 in->standby = true;
2510 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002511 in_standby(&in->stream.common);
2512 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002513 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
2514 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002515 }
2516 return bytes;
2517}
2518
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302519static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002520{
2521 return 0;
2522}
2523
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002524static int add_remove_audio_effect(const struct audio_stream *stream,
2525 effect_handle_t effect,
2526 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002527{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002528 struct stream_in *in = (struct stream_in *)stream;
2529 int status = 0;
2530 effect_descriptor_t desc;
2531
2532 status = (*effect)->get_descriptor(effect, &desc);
2533 if (status != 0)
2534 return status;
2535
2536 pthread_mutex_lock(&in->lock);
2537 pthread_mutex_lock(&in->dev->lock);
2538 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2539 in->enable_aec != enable &&
2540 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2541 in->enable_aec = enable;
2542 if (!in->standby)
2543 select_devices(in->dev, in->usecase);
2544 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002545 if (in->enable_ns != enable &&
2546 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2547 in->enable_ns = enable;
2548 if (!in->standby)
2549 select_devices(in->dev, in->usecase);
2550 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002551 pthread_mutex_unlock(&in->dev->lock);
2552 pthread_mutex_unlock(&in->lock);
2553
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002554 return 0;
2555}
2556
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002557static int in_add_audio_effect(const struct audio_stream *stream,
2558 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002559{
Eric Laurent994a6932013-07-17 11:51:42 -07002560 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002561 return add_remove_audio_effect(stream, effect, true);
2562}
2563
2564static int in_remove_audio_effect(const struct audio_stream *stream,
2565 effect_handle_t effect)
2566{
Eric Laurent994a6932013-07-17 11:51:42 -07002567 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002568 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002569}
2570
2571static int adev_open_output_stream(struct audio_hw_device *dev,
2572 audio_io_handle_t handle,
2573 audio_devices_t devices,
2574 audio_output_flags_t flags,
2575 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302576 struct audio_stream_out **stream_out,
2577 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002578{
2579 struct audio_device *adev = (struct audio_device *)dev;
2580 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002581 int i, ret = 0;
Steve Kondik5a447012014-12-02 16:04:20 -08002582 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002583
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002584 *stream_out = NULL;
Naresh Tannirucef332d2014-06-04 18:17:56 +05302585
2586 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2587 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2588 ALOGE(" sound card is not active rejecting compress output open request");
2589 return -EINVAL;
2590 }
2591
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002592 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2593
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302594 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2595 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2596 devices, flags, &out->stream);
2597
2598
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002599 if (!out) {
2600 return -ENOMEM;
2601 }
2602
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002603 if (devices == AUDIO_DEVICE_NONE)
2604 devices = AUDIO_DEVICE_OUT_SPEAKER;
2605
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002606 out->flags = flags;
2607 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002608 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002609 out->format = config->format;
2610 out->sample_rate = config->sample_rate;
2611 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2612 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002613 out->handle = handle;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002614 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Joseph3a2fec32014-12-03 02:46:47 -08002615 out->non_blocking = 0;
2616 out->use_small_bufs = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002617
2618 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002619 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
Mingming Yinee733602014-04-03 17:47:22 -07002620 (
2621#ifdef AFE_PROXY_ENABLED
2622 out->devices & AUDIO_DEVICE_OUT_PROXY ||
2623#endif
2624 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002625
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002626 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002627 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2628 ret = read_hdmi_channel_masks(out);
2629
Mingming Yinee733602014-04-03 17:47:22 -07002630#ifdef AFE_PROXY_ENABLED
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002631 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2632 ret = audio_extn_read_afe_proxy_channel_masks(out);
Mingming Yinee733602014-04-03 17:47:22 -07002633#endif
2634
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002635 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002636 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002637 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002638
2639 if (config->sample_rate == 0)
2640 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2641 if (config->channel_mask == 0)
2642 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2643
2644 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002645 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002646 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2647 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002648 out->config.rate = config->sample_rate;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302649 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002650 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Mingming Yinee733602014-04-03 17:47:22 -07002651#ifdef COMPRESS_VOIP_ENABLED
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002652 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2653 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002654 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002655 ret = voice_extn_compress_voip_open_output_stream(out);
2656 if (ret != 0) {
2657 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2658 __func__, ret);
2659 goto error_open;
2660 }
Mingming Yinee733602014-04-03 17:47:22 -07002661#endif
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002662 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2663 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2664 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2665 ALOGE("%s: Unsupported Offload information", __func__);
2666 ret = -EINVAL;
2667 goto error_open;
2668 }
Mingming Yin90310102013-11-13 16:57:00 -08002669 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002670 !audio_extn_is_dolby_format(config->offload_info.format)) {
Steve Kondik3fdf4fc2014-12-10 21:15:43 -08002671 ALOGE("%s: Unsupported offload audio format %x", __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002672 ret = -EINVAL;
2673 goto error_open;
2674 }
2675
2676 out->compr_config.codec = (struct snd_codec *)
2677 calloc(1, sizeof(struct snd_codec));
2678
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07002679 if (!out->compr_config.codec) {
2680 ret = -ENOMEM;
2681 goto error_open;
2682 }
2683
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002684 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002685 if (config->offload_info.channel_mask)
2686 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002687 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002688 out->channel_mask = config->channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002689 config->offload_info.channel_mask = config->channel_mask;
2690 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002691 out->format = config->offload_info.format;
2692 out->sample_rate = config->offload_info.sample_rate;
2693
2694 out->stream.set_callback = out_set_callback;
2695 out->stream.pause = out_pause;
2696 out->stream.resume = out_resume;
2697 out->stream.drain = out_drain;
2698 out->stream.flush = out_flush;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002699 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002700
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002701 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002702 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002703 audio_extn_dolby_get_snd_codec_id(adev, out,
2704 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002705 else
2706 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002707 get_snd_codec_id(config->offload_info.format);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002708
ApurupaPattapu0c566872014-01-10 14:46:02 -08002709 if (audio_is_offload_pcm(config->offload_info.format)) {
2710 out->compr_config.fragment_size =
2711 platform_get_pcm_offload_buffer_size(&config->offload_info);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002712 } else {
ApurupaPattapu0c566872014-01-10 14:46:02 -08002713 out->compr_config.fragment_size =
2714 platform_get_compress_offload_buffer_size(&config->offload_info);
2715 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002716 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2717 out->compr_config.codec->sample_rate =
2718 compress_get_alsa_rate(config->offload_info.sample_rate);
2719 out->compr_config.codec->bit_rate =
2720 config->offload_info.bit_rate;
2721 out->compr_config.codec->ch_in =
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302722 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002723 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002724 out->bit_width = config->offload_info.bit_width;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002725
Steve Kondik3fdf4fc2014-12-10 21:15:43 -08002726 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
Steve Kondik37454012014-11-23 19:54:21 -08002727 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002728 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2729 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002730
Steve Kondik3abbbc82014-11-29 14:14:43 -08002731 if (config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002732 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002733
Steve Kondik3abbbc82014-11-29 14:14:43 -08002734 if (out->bit_width == 24)
2735 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002736
Steve Kondik6bedcdf2014-07-21 11:48:30 -07002737#ifdef FLAC_OFFLOAD_ENABLED
Steve Kondik37454012014-11-23 19:54:21 -08002738 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
2739 out->compr_config.codec->options.flac_dec.sample_size = config->offload_info.bit_width;
Steve Kondik6bedcdf2014-07-21 11:48:30 -07002740#endif
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002741
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002742 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2743 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002744
Alexy Joseph3a2fec32014-12-03 02:46:47 -08002745 if (config->offload_info.use_small_bufs) {
2746 //this flag is set from framework only if its for PCM formats
2747 //no need to check for PCM format again
2748 out->non_blocking = 0;
2749 out->use_small_bufs = true;
2750 ALOGI("Keep write blocking for small buff: non_blockling %d",
2751 out->non_blocking);
2752 }
2753
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002754 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002755 out->offload_state = OFFLOAD_STATE_IDLE;
2756 out->playback_started = 0;
2757
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002758 create_offload_callback_thread(out);
2759 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2760 __func__, config->offload_info.version,
2761 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002762 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -08002763 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002764
Mingming Yinee733602014-04-03 17:47:22 -07002765#ifdef INCALL_MUSIC_ENABLED
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002766 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2767 ret = voice_check_and_set_incall_music_usecase(adev, out);
2768 if (ret != 0) {
2769 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2770 __func__, ret);
2771 goto error_open;
2772 }
Mingming Yinee733602014-04-03 17:47:22 -07002773#endif
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302774 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2775 if (config->sample_rate == 0)
2776 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2777 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2778 config->sample_rate != 8000) {
2779 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2780 ret = -EINVAL;
2781 goto error_open;
2782 }
2783 out->sample_rate = config->sample_rate;
2784 out->config.rate = config->sample_rate;
2785 if (config->format == AUDIO_FORMAT_DEFAULT)
2786 config->format = AUDIO_FORMAT_PCM_16_BIT;
2787 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2788 config->format = AUDIO_FORMAT_PCM_16_BIT;
2789 ret = -EINVAL;
2790 goto error_open;
2791 }
2792 out->format = config->format;
2793 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2794 out->config = pcm_config_afe_proxy_playback;
2795 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002796 } else {
Steve Kondik3abbbc82014-11-29 14:14:43 -08002797#ifndef LOW_LATENCY_PRIMARY
2798 if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
2799 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2800 out->config = pcm_config_low_latency;
Steve Kondikb045f472014-11-12 23:24:07 -08002801#endif
Steve Kondik3abbbc82014-11-29 14:14:43 -08002802#ifdef LOW_LATENCY_PRIMARY
2803 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2804 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2805 out->config = pcm_config_deep_buffer;
2806#endif
2807 } else {
2808 /* primary path is the default path selected if no other outputs are available/suitable */
2809 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
2810#ifdef LOW_LATENCY_PRIMARY
2811 out->config = pcm_config_low_latency;
2812#else
2813 out->config = pcm_config_deep_buffer;
2814#endif
2815 }
2816 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2817 if (k_enable_extended_precision
2818 && pcm_params_format_test(adev->use_case_table[out->usecase],
2819 pcm_format_from_audio_format(config->format))) {
2820 out->config.format = pcm_format_from_audio_format(config->format);
2821 /* out->format already set to config->format */
2822 } else {
2823 /* deny the externally proposed config format
2824 * and use the one specified in audio_hw layer configuration.
2825 * Note: out->format is returned by out->stream.common.get_format()
2826 * and is used to set config->format in the code several lines below.
2827 */
Steve Kondik5a447012014-12-02 16:04:20 -08002828 ALOGW("Audio format %x is not available on this output", out->format);
Steve Kondik3abbbc82014-11-29 14:14:43 -08002829 out->format = audio_format_from_pcm_format(out->config.format);
2830 }
2831 }
2832
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002833 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002834 }
2835
Steve Kondik5a447012014-12-02 16:04:20 -08002836 ALOGV("%s flags %x, format %x, sample_rate %d, out->bit_width %d",
2837 __func__, flags, out->format, out->sample_rate, out->bit_width);
2838
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002839 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
Steve Kondik3abbbc82014-11-29 14:14:43 -08002840 flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002841 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002842 if(adev->primary_output == NULL)
2843 adev->primary_output = out;
2844 else {
2845 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002846 ret = -EEXIST;
2847 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002848 }
2849 }
2850
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002851 /* Check if this usecase is already existing */
2852 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella0d06c8e2014-04-17 20:00:41 -07002853 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2854 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002855 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002856 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002857 ret = -EEXIST;
2858 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002859 }
2860 pthread_mutex_unlock(&adev->lock);
2861
2862 out->stream.common.get_sample_rate = out_get_sample_rate;
2863 out->stream.common.set_sample_rate = out_set_sample_rate;
2864 out->stream.common.get_buffer_size = out_get_buffer_size;
2865 out->stream.common.get_channels = out_get_channels;
2866 out->stream.common.get_format = out_get_format;
2867 out->stream.common.set_format = out_set_format;
2868 out->stream.common.standby = out_standby;
2869 out->stream.common.dump = out_dump;
2870 out->stream.common.set_parameters = out_set_parameters;
2871 out->stream.common.get_parameters = out_get_parameters;
2872 out->stream.common.add_audio_effect = out_add_audio_effect;
2873 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2874 out->stream.get_latency = out_get_latency;
2875 out->stream.set_volume = out_set_volume;
2876 out->stream.write = out_write;
2877 out->stream.get_render_position = out_get_render_position;
2878 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002879 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002880
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002881 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002882 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002883 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002884
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302885 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2886 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2887
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002888 config->format = out->stream.common.get_format(&out->stream.common);
2889 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2890 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2891
2892 *stream_out = &out->stream;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302893 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2894 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002895 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002896 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002897
2898error_open:
2899 free(out);
2900 *stream_out = NULL;
2901 ALOGD("%s: exit: ret %d", __func__, ret);
2902 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002903}
2904
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302905static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002906 struct audio_stream_out *stream)
2907{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002908 struct stream_out *out = (struct stream_out *)stream;
2909 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002910 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002911
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302912 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2913
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002914 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302915 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002916 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302917 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002918 if(ret != 0)
2919 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2920 __func__, ret);
2921 }
2922 else
2923 out_standby(&stream->common);
2924
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002925 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002926 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002927 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002928 if (out->compr_config.codec != NULL)
2929 free(out->compr_config.codec);
2930 }
2931 pthread_cond_destroy(&out->cond);
2932 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002933 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002934 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002935}
2936
Alexy Joseph55204352014-10-06 12:15:01 -07002937static void close_compress_sessions(struct audio_device *adev)
2938{
2939 struct stream_out *out = NULL;
2940 struct listnode *node = NULL;
2941 struct listnode *tmp = NULL;
2942 struct audio_usecase *usecase = NULL;
2943 pthread_mutex_lock(&adev->lock);
2944 list_for_each_safe(node, tmp, &adev->usecase_list) {
2945 usecase = node_to_item(node, struct audio_usecase, list);
2946 if (is_offload_usecase(usecase->id)) {
2947 if (usecase && usecase->stream.out) {
2948 ALOGI(" %s closing compress session %d on OFFLINE state", __func__, usecase->id);
2949 out = usecase->stream.out;
2950 pthread_mutex_unlock(&adev->lock);
2951 out_standby(&out->stream.common);
2952 pthread_mutex_lock(&adev->lock);
2953 }
2954 }
2955 }
2956 pthread_mutex_unlock(&adev->lock);
2957}
2958
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002959static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2960{
2961 struct audio_device *adev = (struct audio_device *)dev;
2962 struct str_parms *parms;
2963 char *str;
2964 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002965 int val;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302966 int ret;
2967 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002968
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002969 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002970 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002971
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302972 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
2973 if (ret >= 0) {
2974 char *snd_card_status = value+2;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302975 if (strstr(snd_card_status, "OFFLINE")) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302976 struct listnode *node;
2977 struct audio_usecase *usecase;
2978
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302979 ALOGD("Received sound card OFFLINE status");
Naresh Tannirucef332d2014-06-04 18:17:56 +05302980 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2981
Alexy Joseph55204352014-10-06 12:15:01 -07002982 //close compress sessions on OFFLINE status
2983 close_compress_sessions(adev);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302984 } else if (strstr(snd_card_status, "ONLINE")) {
2985 ALOGD("Received sound card ONLINE status");
Naresh Tannirucef332d2014-06-04 18:17:56 +05302986 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302987 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302988 }
2989
2990 pthread_mutex_lock(&adev->lock);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302991 status = voice_set_parameters(adev, parms);
2992 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002993 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002994
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302995 status = platform_set_parameters(adev->platform, parms);
2996 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002997 goto done;
2998
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302999 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
3000 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003001 /* When set to false, HAL should disable EC and NS
3002 * But it is currently not supported.
3003 */
3004 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
3005 adev->bluetooth_nrec = true;
3006 else
3007 adev->bluetooth_nrec = false;
3008 }
3009
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303010 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
3011 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003012 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
3013 adev->screen_off = false;
3014 else
3015 adev->screen_off = true;
3016 }
3017
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303018 ret = str_parms_get_int(parms, "rotation", &val);
3019 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003020 bool reverse_speakers = false;
3021 switch(val) {
3022 // FIXME: note that the code below assumes that the speakers are in the correct placement
3023 // relative to the user when the device is rotated 90deg from its default rotation. This
3024 // assumption is device-specific, not platform-specific like this code.
3025 case 270:
3026 reverse_speakers = true;
3027 break;
3028 case 0:
3029 case 90:
3030 case 180:
3031 break;
3032 default:
3033 ALOGE("%s: unexpected rotation of %d", __func__, val);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303034 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003035 }
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303036 if (status == 0) {
3037 if (adev->speaker_lr_swap != reverse_speakers) {
3038 adev->speaker_lr_swap = reverse_speakers;
3039 // only update the selected device if there is active pcm playback
3040 struct audio_usecase *usecase;
3041 struct listnode *node;
3042 list_for_each(node, &adev->usecase_list) {
3043 usecase = node_to_item(node, struct audio_usecase, list);
3044 if (usecase->type == PCM_PLAYBACK) {
Steve Kondik3abbbc82014-11-29 14:14:43 -08003045 status = select_devices(adev, usecase->id);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303046 break;
3047 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003048 }
3049 }
3050 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07003051 }
3052
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003053 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003054
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003055done:
3056 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003057 pthread_mutex_unlock(&adev->lock);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303058 ALOGV("%s: exit with code(%d)", __func__, status);
3059 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003060}
3061
3062static char* adev_get_parameters(const struct audio_hw_device *dev,
3063 const char *keys)
3064{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003065 struct audio_device *adev = (struct audio_device *)dev;
3066 struct str_parms *reply = str_parms_create();
3067 struct str_parms *query = str_parms_create_str(keys);
3068 char *str;
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303069 char value[256] = {0};
3070 int ret = 0;
3071
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003072 if (!query || !reply) {
3073 ALOGE("adev_get_parameters: failed to create query or reply");
3074 return NULL;
3075 }
3076
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303077 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_SND_CARD_STATUS, value,
3078 sizeof(value));
3079 if (ret >=0) {
3080 int val = 1;
3081 pthread_mutex_lock(&adev->snd_card_status.lock);
3082 if (SND_CARD_STATE_OFFLINE == adev->snd_card_status.state)
3083 val = 0;
3084 pthread_mutex_unlock(&adev->snd_card_status.lock);
3085 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_SND_CARD_STATUS, val);
3086 goto exit;
3087 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003088
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003089 pthread_mutex_lock(&adev->lock);
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303090
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003091 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08003092 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003093 platform_get_parameters(adev->platform, query, reply);
Naresh Tannirucef332d2014-06-04 18:17:56 +05303094 pthread_mutex_unlock(&adev->lock);
3095
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05303096exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003097 str = str_parms_to_str(reply);
3098 str_parms_destroy(query);
3099 str_parms_destroy(reply);
3100
3101 ALOGV("%s: exit: returns - %s", __func__, str);
3102 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003103}
3104
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303105static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003106{
3107 return 0;
3108}
3109
3110static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
3111{
Haynes Mathew George5191a852013-09-11 14:19:36 -07003112 int ret;
3113 struct audio_device *adev = (struct audio_device *)dev;
Eric Laurent321b96d2014-09-10 13:21:01 -05003114
3115 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
3116
Haynes Mathew George5191a852013-09-11 14:19:36 -07003117 pthread_mutex_lock(&adev->lock);
3118 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07003119 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07003120 pthread_mutex_unlock(&adev->lock);
3121 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003122}
3123
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303124static int adev_set_master_volume(struct audio_hw_device *dev __unused,
3125 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003126{
3127 return -ENOSYS;
3128}
3129
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303130static int adev_get_master_volume(struct audio_hw_device *dev __unused,
3131 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003132{
3133 return -ENOSYS;
3134}
3135
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303136static int adev_set_master_mute(struct audio_hw_device *dev __unused,
3137 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003138{
3139 return -ENOSYS;
3140}
3141
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303142static int adev_get_master_mute(struct audio_hw_device *dev __unused,
3143 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003144{
3145 return -ENOSYS;
3146}
3147
3148static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
3149{
3150 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003151 pthread_mutex_lock(&adev->lock);
3152 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07003153 ALOGD("%s mode %d\n", __func__, mode);
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303154#ifdef USES_AUDIO_AMPLIFIER
3155 if (amplifier_set_mode(mode) != 0)
3156 ALOGE("Failed setting amplifier mode");
3157#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003158 adev->mode = mode;
3159 }
3160 pthread_mutex_unlock(&adev->lock);
Eric Laurentda46bfb2014-08-25 22:39:29 -05003161
3162 audio_extn_extspk_set_mode(adev->extspk, mode);
3163
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003164 return 0;
3165}
3166
3167static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
3168{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003169 int ret;
3170
3171 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08003172 ALOGD("%s state %d\n", __func__, state);
Shreyas Nagasandra Chandrasekhar9781c6c2014-12-01 05:49:35 -08003173 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08003174 pthread_mutex_unlock(&adev->lock);
3175
3176 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003177}
3178
3179static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
3180{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003181 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003182 return 0;
3183}
3184
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303185static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003186 const struct audio_config *config)
3187{
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303188 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003189
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003190 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
3191 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003192}
3193
3194static int adev_open_input_stream(struct audio_hw_device *dev,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303195 audio_io_handle_t handle __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003196 audio_devices_t devices,
3197 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303198 struct audio_stream_in **stream_in,
3199 audio_input_flags_t flags __unused,
3200 const char *address __unused,
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003201 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003202{
3203 struct audio_device *adev = (struct audio_device *)dev;
3204 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003205 int ret = 0, buffer_size, frame_size;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303206 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003207 bool is_low_latency = false;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303208
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003209 *stream_in = NULL;
3210 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
3211 return -EINVAL;
3212
3213 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003214
3215 if (!in) {
3216 ALOGE("failed to allocate input stream");
3217 return -ENOMEM;
3218 }
3219
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303220 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003221 stream_handle(%p) io_handle(%d) source(%d)",__func__, config->sample_rate, config->channel_mask,
3222 devices, &in->stream, handle, source);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003223
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07003224 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
3225
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003226 in->stream.common.get_sample_rate = in_get_sample_rate;
3227 in->stream.common.set_sample_rate = in_set_sample_rate;
3228 in->stream.common.get_buffer_size = in_get_buffer_size;
3229 in->stream.common.get_channels = in_get_channels;
3230 in->stream.common.get_format = in_get_format;
3231 in->stream.common.set_format = in_set_format;
3232 in->stream.common.standby = in_standby;
3233 in->stream.common.dump = in_dump;
3234 in->stream.common.set_parameters = in_set_parameters;
3235 in->stream.common.get_parameters = in_get_parameters;
3236 in->stream.common.add_audio_effect = in_add_audio_effect;
3237 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3238 in->stream.set_gain = in_set_gain;
3239 in->stream.read = in_read;
3240 in->stream.get_input_frames_lost = in_get_input_frames_lost;
3241
3242 in->device = devices;
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003243 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003244 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003245 in->standby = 1;
3246 in->channel_mask = config->channel_mask;
3247
3248 /* Update config params with the requested sample rate and channels */
3249 in->usecase = USECASE_AUDIO_RECORD;
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003250 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
3251 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
3252 is_low_latency = true;
3253#if LOW_LATENCY_CAPTURE_USE_CASE
3254 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
3255#endif
3256 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003257 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003258 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003259 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003260
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05303261 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
Pavan Chikkala3c0036d2014-12-17 16:45:10 +05303262 if (adev->mode != AUDIO_MODE_IN_CALL) {
3263 ret = -EINVAL;
3264 goto err_open;
Pavan Chikkala3ec42ef2014-11-21 20:57:48 +05303265 }
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05303266 if (config->sample_rate == 0)
3267 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3268 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
3269 config->sample_rate != 8000) {
3270 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3271 ret = -EINVAL;
3272 goto err_open;
3273 }
3274 if (config->format == AUDIO_FORMAT_DEFAULT)
3275 config->format = AUDIO_FORMAT_PCM_16_BIT;
3276 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
3277 config->format = AUDIO_FORMAT_PCM_16_BIT;
3278 ret = -EINVAL;
3279 goto err_open;
3280 }
3281 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
3282 in->config = pcm_config_afe_proxy_record;
3283 in->config.channels = channel_count;
3284 in->config.rate = config->sample_rate;
3285 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003286 if(audio_extn_ssr_get_enabled()) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303287 if(audio_extn_ssr_init(in)) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003288 ALOGE("%s: audio_extn_ssr_init failed", __func__);
3289 ret = -EINVAL;
3290 goto err_open;
3291 }
3292 } else {
Mingming Yindaf9c542014-09-16 17:41:33 -07003293 ALOGW("%s: surround sound recording is not supported", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003294 }
Mingming Yine62d7842013-10-25 16:26:03 -07003295 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08003296 audio_extn_compr_cap_format_supported(config->format) &&
3297 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303298 audio_extn_compr_cap_init(in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003299 } else {
3300 in->config.channels = channel_count;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303301 frame_size = audio_stream_in_frame_size(&in->stream);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003302 buffer_size = get_input_buffer_size(config->sample_rate,
3303 config->format,
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003304 channel_count,
3305 is_low_latency);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003306 in->config.period_size = buffer_size / frame_size;
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003307 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
3308 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
3309 (voice_extn_compress_voip_is_format_supported(in->format)) &&
3310 (in->config.rate == 8000 || in->config.rate == 16000) &&
3311 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
3312 voice_extn_compress_voip_open_input_stream(in);
3313 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003314 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003315
3316 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07003317 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003318 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003319
3320err_open:
3321 free(in);
3322 *stream_in = NULL;
3323 return ret;
3324}
3325
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303326static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003327 struct audio_stream_in *stream)
3328{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003329 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003330 struct stream_in *in = (struct stream_in *)stream;
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303331 struct audio_device *adev = in->dev;
3332
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303333 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003334
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003335 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303336 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003337 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303338 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003339 if (ret != 0)
3340 ALOGE("%s: Compress voip input cannot be closed, error:%d",
3341 __func__, ret);
3342 } else
3343 in_standby(&stream->common);
3344
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303345 if (audio_extn_ssr_get_enabled() &&
3346 (audio_channel_count_from_in_mask(in->channel_mask) == 6)) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003347 audio_extn_ssr_deinit();
3348 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003349 free(stream);
3350
Mingming Yine62d7842013-10-25 16:26:03 -07003351 if(audio_extn_compr_cap_enabled() &&
3352 audio_extn_compr_cap_format_supported(in->config.format))
3353 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003354 return;
3355}
3356
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303357static int adev_dump(const audio_hw_device_t *device __unused,
3358 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003359{
3360 return 0;
3361}
3362
Steve Kondik3abbbc82014-11-29 14:14:43 -08003363/* verifies input and output devices and their capabilities.
3364 *
3365 * This verification is required when enabling extended bit-depth or
3366 * sampling rates, as not all qcom products support it.
3367 *
3368 * Suitable for calling only on initialization such as adev_open().
3369 * It fills the audio_device use_case_table[] array.
3370 *
3371 * Has a side-effect that it needs to configure audio routing / devices
3372 * in order to power up the devices and read the device parameters.
3373 * It does not acquire any hw device lock. Should restore the devices
3374 * back to "normal state" upon completion.
3375 */
3376static int adev_verify_devices(struct audio_device *adev)
3377{
3378 /* enumeration is a bit difficult because one really wants to pull
3379 * the use_case, device id, etc from the hidden pcm_device_table[].
3380 * In this case there are the following use cases and device ids.
3381 *
3382 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
3383 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
3384 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
3385 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
3386 * [USECASE_AUDIO_RECORD] = {0, 0},
3387 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
3388 * [USECASE_VOICE_CALL] = {2, 2},
3389 *
3390 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
3391 * USECASE_VOICE_CALL omitted, but possible for either input or output.
3392 */
3393
3394 /* should be the usecases enabled in adev_open_input_stream() */
3395 static const int test_in_usecases[] = {
3396 USECASE_AUDIO_RECORD,
3397 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
3398 };
3399 /* should be the usecases enabled in adev_open_output_stream()*/
3400 static const int test_out_usecases[] = {
3401 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
3402 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
3403 };
3404 static const usecase_type_t usecase_type_by_dir[] = {
3405 PCM_PLAYBACK,
3406 PCM_CAPTURE,
3407 };
3408 static const unsigned flags_by_dir[] = {
3409 PCM_OUT,
3410 PCM_IN,
3411 };
3412
3413 size_t i;
3414 unsigned dir;
3415 const unsigned card_id = adev->snd_card;
3416 char info[512]; /* for possible debug info */
3417
3418 for (dir = 0; dir < 2; ++dir) {
3419 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
3420 const unsigned flags_dir = flags_by_dir[dir];
3421 const size_t testsize =
3422 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
3423 const int *testcases =
3424 dir ? test_in_usecases : test_out_usecases;
3425 const audio_devices_t audio_device =
3426 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
3427
3428 for (i = 0; i < testsize; ++i) {
3429 const audio_usecase_t audio_usecase = testcases[i];
3430 int device_id;
3431 snd_device_t snd_device;
3432 struct pcm_params **pparams;
3433 struct stream_out out;
3434 struct stream_in in;
3435 struct audio_usecase uc_info;
3436 int retval;
3437
3438 pparams = &adev->use_case_table[audio_usecase];
3439 pcm_params_free(*pparams); /* can accept null input */
3440 *pparams = NULL;
3441
3442 /* find the device ID for the use case (signed, for error) */
3443 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
3444 if (device_id < 0)
3445 continue;
3446
3447 /* prepare structures for device probing */
3448 memset(&uc_info, 0, sizeof(uc_info));
3449 uc_info.id = audio_usecase;
3450 uc_info.type = usecase_type;
3451 if (dir) {
3452 adev->active_input = &in;
3453 memset(&in, 0, sizeof(in));
3454 in.device = audio_device;
3455 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
3456 uc_info.stream.in = &in;
3457 } else {
3458 adev->active_input = NULL;
3459 }
3460 memset(&out, 0, sizeof(out));
3461 out.devices = audio_device; /* only field needed in select_devices */
3462 uc_info.stream.out = &out;
3463 uc_info.devices = audio_device;
3464 uc_info.in_snd_device = SND_DEVICE_NONE;
3465 uc_info.out_snd_device = SND_DEVICE_NONE;
3466 list_add_tail(&adev->usecase_list, &uc_info.list);
3467
3468 /* select device - similar to start_(in/out)put_stream() */
3469 retval = select_devices(adev, audio_usecase);
3470 if (retval >= 0) {
3471 *pparams = pcm_params_get(card_id, device_id, flags_dir);
3472#if LOG_NDEBUG == 0
3473 if (*pparams) {
3474 ALOGV("%s: (%s) card %d device %d", __func__,
3475 dir ? "input" : "output", card_id, device_id);
3476 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
3477 ALOGV(info); /* print parameters */
3478 } else {
3479 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
3480 }
3481#endif
3482 }
3483
3484 /* deselect device - similar to stop_(in/out)put_stream() */
3485 /* 1. Get and set stream specific mixer controls */
3486 retval = disable_audio_route(adev, &uc_info);
3487 /* 2. Disable the rx device */
3488 retval = disable_snd_device(adev,
3489 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
3490 list_remove(&uc_info.list);
3491 }
3492 }
3493 adev->active_input = NULL; /* restore adev state */
3494 return 0;
3495}
3496
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003497static int adev_close(hw_device_t *device)
3498{
Steve Kondik3abbbc82014-11-29 14:14:43 -08003499 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003500 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07003501
3502 if (!adev)
3503 return 0;
3504
3505 pthread_mutex_lock(&adev_init_lock);
3506
3507 if ((--audio_device_ref_count) == 0) {
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303508#ifdef USES_AUDIO_AMPLIFIER
3509 if (amplifier_close() != 0)
3510 ALOGE("Amplifier close failed");
3511#endif
Kiran Kandide144c82013-11-20 15:58:32 -08003512 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07003513 audio_route_free(adev->audio_route);
3514 free(adev->snd_dev_ref_cnt);
3515 platform_deinit(adev->platform);
Eric Laurentda46bfb2014-08-25 22:39:29 -05003516 audio_extn_extspk_deinit(adev->extspk);
Steve Kondik3abbbc82014-11-29 14:14:43 -08003517 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
3518 pcm_params_free(adev->use_case_table[i]);
3519 }
Kiran Kandi910e1862013-10-29 13:29:42 -07003520 free(device);
3521 adev = NULL;
3522 }
3523 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003524 return 0;
3525}
3526
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003527/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
3528 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
3529 * just that it _might_ work.
3530 */
3531static int period_size_is_plausible_for_low_latency(int period_size)
3532{
3533 switch (period_size) {
3534 case 160:
3535 case 240:
3536 case 320:
3537 case 480:
3538 return 1;
3539 default:
3540 return 0;
3541 }
3542}
3543
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003544static int adev_open(const hw_module_t *module, const char *name,
3545 hw_device_t **device)
3546{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003547 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003548
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003549 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003550 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3551
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003552 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07003553 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003554 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07003555 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003556 ALOGD("%s: returning existing instance of adev", __func__);
3557 ALOGD("%s: exit", __func__);
3558 pthread_mutex_unlock(&adev_init_lock);
3559 return 0;
3560 }
3561
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003562 adev = calloc(1, sizeof(struct audio_device));
3563
Haynes Mathew George9f0254a2014-06-30 13:56:18 -07003564 if (!adev) {
3565 pthread_mutex_unlock(&adev_init_lock);
3566 return -ENOMEM;
3567 }
3568
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07003569 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3570
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003571 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3572 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3573 adev->device.common.module = (struct hw_module_t *)module;
3574 adev->device.common.close = adev_close;
3575
3576 adev->device.init_check = adev_init_check;
3577 adev->device.set_voice_volume = adev_set_voice_volume;
3578 adev->device.set_master_volume = adev_set_master_volume;
3579 adev->device.get_master_volume = adev_get_master_volume;
3580 adev->device.set_master_mute = adev_set_master_mute;
3581 adev->device.get_master_mute = adev_get_master_mute;
3582 adev->device.set_mode = adev_set_mode;
3583 adev->device.set_mic_mute = adev_set_mic_mute;
3584 adev->device.get_mic_mute = adev_get_mic_mute;
3585 adev->device.set_parameters = adev_set_parameters;
3586 adev->device.get_parameters = adev_get_parameters;
3587 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3588 adev->device.open_output_stream = adev_open_output_stream;
3589 adev->device.close_output_stream = adev_close_output_stream;
3590 adev->device.open_input_stream = adev_open_input_stream;
3591 adev->device.close_input_stream = adev_close_input_stream;
3592 adev->device.dump = adev_dump;
3593
3594 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003595 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003596 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003597 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003598 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003599 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003600 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003601 /* adev->cur_hdmi_channels = 0; by calloc() */
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08003602 adev->cur_codec_backend_samplerate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
3603 adev->cur_codec_backend_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Eric Laurentb23d5282013-05-14 15:27:20 -07003604 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003605 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003606 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly9b7e96b2014-02-14 14:45:49 -08003607 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07003608 adev->offload_usecases_state = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003609
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303610 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
3611 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
3612
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003613 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003614 adev->platform = platform_init(adev);
3615 if (!adev->platform) {
3616 free(adev->snd_dev_ref_cnt);
3617 free(adev);
3618 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3619 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08003620 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003621 return -EINVAL;
3622 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003623
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303624 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
Eric Laurentda46bfb2014-08-25 22:39:29 -05003625 adev->extspk = audio_extn_extspk_init(adev);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303626
Eric Laurentc4aef752013-09-12 17:45:53 -07003627 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
3628 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3629 if (adev->visualizer_lib == NULL) {
3630 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3631 } else {
3632 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3633 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003634 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003635 "visualizer_hal_start_output");
3636 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003637 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003638 "visualizer_hal_stop_output");
3639 }
3640 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003641 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07003642
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003643 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
3644 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3645 if (adev->offload_effects_lib == NULL) {
3646 ALOGE("%s: DLOPEN failed for %s", __func__,
3647 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3648 } else {
3649 ALOGV("%s: DLOPEN successful for %s", __func__,
3650 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3651 adev->offload_effects_start_output =
3652 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3653 "offload_effects_bundle_hal_start_output");
3654 adev->offload_effects_stop_output =
3655 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3656 "offload_effects_bundle_hal_stop_output");
3657 }
3658 }
3659
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003660 *device = &adev->device.common;
Steve Kondik3abbbc82014-11-29 14:14:43 -08003661 if (k_enable_extended_precision)
3662 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003663
Daniel Hillenbrand8373bc62013-05-23 10:10:00 +05303664#ifdef USES_AUDIO_AMPLIFIER
3665 if (amplifier_open() != 0)
3666 ALOGE("Amplifier initialization failed");
3667#endif
3668
Kiran Kandi910e1862013-10-29 13:29:42 -07003669 audio_device_ref_count++;
Ravi Kumar Alamanda1c3de5e2014-09-08 15:59:58 -07003670
3671 char value[PROPERTY_VALUE_MAX];
3672 int trial;
3673 if (property_get("audio_hal.period_size", value, NULL) > 0) {
3674 trial = atoi(value);
3675 if (period_size_is_plausible_for_low_latency(trial)) {
3676 pcm_config_low_latency.period_size = trial;
3677 pcm_config_low_latency.start_threshold = trial / 4;
3678 pcm_config_low_latency.avail_min = trial / 4;
3679 configured_low_latency_capture_period_size = trial;
3680 }
3681 }
3682 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
3683 trial = atoi(value);
3684 if (period_size_is_plausible_for_low_latency(trial)) {
3685 configured_low_latency_capture_period_size = trial;
3686 }
3687 }
3688
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003689 pthread_mutex_unlock(&adev_init_lock);
3690
Eric Laurent994a6932013-07-17 11:51:42 -07003691 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003692 return 0;
3693}
3694
3695static struct hw_module_methods_t hal_module_methods = {
3696 .open = adev_open,
3697};
3698
3699struct audio_module HAL_MODULE_INFO_SYM = {
3700 .common = {
3701 .tag = HARDWARE_MODULE_TAG,
3702 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3703 .hal_api_version = HARDWARE_HAL_API_VERSION,
3704 .id = AUDIO_HARDWARE_MODULE_ID,
3705 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08003706 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003707 .methods = &hal_module_methods,
3708 },
3709};