blob: 51e8188677cacc2a7c6c37538d9f7202e5f790cb [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"
21/*#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>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070050#include "platform_api.h"
51#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070052#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080053#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include "sound/compress_params.h"
ApurupaPattapu0c566872014-01-10 14:46:02 -080056#include "sound/asound.h"
57
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070058#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
59/* ToDo: Check and update a proper value in msec */
60#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
61#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
62
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +053063#define PROXY_OPEN_RETRY_COUNT 100
64#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapu0c566872014-01-10 14:46:02 -080065
Steve Kondikb045f472014-11-12 23:24:07 -080066#ifdef LOW_LATENCY_PRIMARY
67#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_LOW_LATENCY
68#else
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080069#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
Steve Kondikb045f472014-11-12 23:24:07 -080070#endif
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080071
Eric Laurentb23d5282013-05-14 15:27:20 -070072struct pcm_config pcm_config_deep_buffer = {
73 .channels = 2,
74 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
75 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
76 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
77 .format = PCM_FORMAT_S16_LE,
78 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
79 .stop_threshold = INT_MAX,
80 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
81};
82
83struct pcm_config pcm_config_low_latency = {
84 .channels = 2,
85 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
86 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
87 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
88 .format = PCM_FORMAT_S16_LE,
89 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
90 .stop_threshold = INT_MAX,
91 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
92};
93
94struct pcm_config pcm_config_hdmi_multi = {
95 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
96 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
97 .period_size = HDMI_MULTI_PERIOD_SIZE,
98 .period_count = HDMI_MULTI_PERIOD_COUNT,
99 .format = PCM_FORMAT_S16_LE,
100 .start_threshold = 0,
101 .stop_threshold = INT_MAX,
102 .avail_min = 0,
103};
104
105struct pcm_config pcm_config_audio_capture = {
106 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700107 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
108 .format = PCM_FORMAT_S16_LE,
109};
110
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530111#define AFE_PROXY_CHANNEL_COUNT 2
112#define AFE_PROXY_SAMPLING_RATE 48000
113
114#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
115#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
116
117struct pcm_config pcm_config_afe_proxy_playback = {
118 .channels = AFE_PROXY_CHANNEL_COUNT,
119 .rate = AFE_PROXY_SAMPLING_RATE,
120 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
121 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
122 .format = PCM_FORMAT_S16_LE,
123 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
124 .stop_threshold = INT_MAX,
125 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
126};
127
128#define AFE_PROXY_RECORD_PERIOD_SIZE 768
129#define AFE_PROXY_RECORD_PERIOD_COUNT 4
130
131struct pcm_config pcm_config_afe_proxy_record = {
132 .channels = AFE_PROXY_CHANNEL_COUNT,
133 .rate = AFE_PROXY_SAMPLING_RATE,
134 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
135 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
136 .format = PCM_FORMAT_S16_LE,
137 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
138 .stop_threshold = INT_MAX,
139 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
140};
141
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800142const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700143 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
144 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
145 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700146 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700147#ifdef MULTIPLE_OFFLOAD_ENABLED
148 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
149 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
150 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
151 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
152 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
153 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
154 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
155 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
156#endif
Eric Laurentb23d5282013-05-14 15:27:20 -0700157 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700158 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700159 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700160 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700161 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800162 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800163 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700164 [USECASE_VOICE_CALL] = "voice-call",
Mingming Yinee733602014-04-03 17:47:22 -0700165
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700166 [USECASE_VOICE2_CALL] = "voice2-call",
167 [USECASE_VOLTE_CALL] = "volte-call",
168 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat111aeb32014-02-12 17:58:59 -0800169 [USECASE_VOWLAN_CALL] = "vowlan-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800170 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700171 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
172 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
173 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800174 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
175 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
176 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
177
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700178 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
179 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700180 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
181 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530182
183 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
184 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700185};
186
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700187static const audio_usecase_t offload_usecases[] = {
188 USECASE_AUDIO_PLAYBACK_OFFLOAD,
189#ifdef MULTIPLE_OFFLOAD_ENABLED
190 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
191 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
Steve Kondikac72feb2014-07-20 23:15:34 -0700192#ifndef PLATFORM_MSM8974
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700193 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
194 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
195 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
196 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
Steve Kondikac72feb2014-07-20 23:15:34 -0700197#endif
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700198 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
Steve Kondikac72feb2014-07-20 23:15:34 -0700199#ifndef PLATFORM_MSM8974
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700200 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
201#endif
Steve Kondikac72feb2014-07-20 23:15:34 -0700202#endif
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -0700203};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800204
205#define STRING_TO_ENUM(string) { #string, string }
206
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800207struct string_to_enum {
208 const char *name;
209 uint32_t value;
210};
211
212static const struct string_to_enum out_channels_name_to_enum_table[] = {
213 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
214 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
215 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
216};
217
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700218static struct audio_device *adev = NULL;
219static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700220static unsigned int audio_device_ref_count;
221
Haynes Mathew George5191a852013-09-11 14:19:36 -0700222static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -0800223
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -0800224static int check_and_set_gapless_mode(struct audio_device *adev) {
225
226
227 char value[PROPERTY_VALUE_MAX] = {0};
228 bool gapless_enabled = false;
229 const char *mixer_ctl_name = "Compress Gapless Playback";
230 struct mixer_ctl *ctl;
231
232 ALOGV("%s:", __func__);
233 property_get("audio.offload.gapless.enabled", value, NULL);
234 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
235
236 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
237 if (!ctl) {
238 ALOGE("%s: Could not get ctl for mixer cmd - %s",
239 __func__, mixer_ctl_name);
240 return -EINVAL;
241 }
242
243 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
244 ALOGE("%s: Could not set gapless mode %d",
245 __func__, gapless_enabled);
246 return -EINVAL;
247 }
248 return 0;
249}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700250
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700251static bool is_supported_format(audio_format_t format)
252{
Eric Laurent86e17132013-09-12 17:49:30 -0700253 if (format == AUDIO_FORMAT_MP3 ||
Mingming Yinee733602014-04-03 17:47:22 -0700254#ifdef EXTN_OFFLOAD_ENABLED
ApurupaPattapu0c566872014-01-10 14:46:02 -0800255 format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD ||
Mingming Yinee733602014-04-03 17:47:22 -0700256 format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD ||
257#endif
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700258#ifdef FLAC_OFFLOAD_ENABLED
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800259 format == AUDIO_FORMAT_FLAC ||
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700260#endif
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530261 format == AUDIO_FORMAT_AAC_LC ||
262 format == AUDIO_FORMAT_AAC_HE_V1 ||
263 format == AUDIO_FORMAT_AAC_HE_V2 ){
264 return true;
265 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700266 return false;
267}
268
269static int get_snd_codec_id(audio_format_t format)
270{
271 int id = 0;
272
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530273 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700274 case AUDIO_FORMAT_MP3:
275 id = SND_AUDIOCODEC_MP3;
276 break;
277 case AUDIO_FORMAT_AAC:
278 id = SND_AUDIOCODEC_AAC;
279 break;
Mingming Yinee733602014-04-03 17:47:22 -0700280#ifdef EXTN_OFFLOAD_ENABLED
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530281 case AUDIO_FORMAT_PCM_OFFLOAD:
ApurupaPattapu0c566872014-01-10 14:46:02 -0800282 id = SND_AUDIOCODEC_PCM;
283 break;
Mingming Yinee733602014-04-03 17:47:22 -0700284#endif
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700285#ifdef FLAC_OFFLOAD_ENABLED
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800286 case AUDIO_FORMAT_FLAC:
287 id = SND_AUDIOCODEC_FLAC;
288 break;
Steve Kondik6bedcdf2014-07-21 11:48:30 -0700289#endif
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700290 default:
Mingming Yin90310102013-11-13 16:57:00 -0800291 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700292 }
293
294 return id;
295}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800296
Avinash Vaish8005dc82014-07-24 15:36:33 +0530297static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
298 struct audio_usecase *uc_info)
299{
300 struct listnode *node;
301 struct audio_usecase *usecase;
302
303 if (uc_info == NULL)
304 return -EINVAL;
305
306 /* Re-route all voice usecases on the shared backend other than the
307 specified usecase to new snd devices */
308 list_for_each(node, &adev->usecase_list) {
309 usecase = node_to_item(node, struct audio_usecase, list);
310 if ((usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) &&
311 (usecase != uc_info))
312 enable_audio_route(adev, usecase);
313 }
314 return 0;
315}
316
Venkata Narendra Kumar Guttac9e9e2c2014-07-09 16:29:28 +0530317int get_snd_card_state(struct audio_device *adev)
Naresh Tannirucef332d2014-06-04 18:17:56 +0530318{
319 int snd_scard_state;
320
321 if (!adev)
322 return SND_CARD_STATE_OFFLINE;
323
324 pthread_mutex_lock(&adev->snd_card_status.lock);
325 snd_scard_state = adev->snd_card_status.state;
326 pthread_mutex_unlock(&adev->snd_card_status.lock);
327
328 return snd_scard_state;
329}
330
331static int set_snd_card_state(struct audio_device *adev, int snd_scard_state)
332{
333 if (!adev)
334 return -ENOSYS;
335
336 pthread_mutex_lock(&adev->snd_card_status.lock);
337 adev->snd_card_status.state = snd_scard_state;
338 pthread_mutex_unlock(&adev->snd_card_status.lock);
339
340 return 0;
341}
342
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700343int enable_audio_route(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700344 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800345{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700346 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700347 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800348
349 if (usecase == NULL)
350 return -EINVAL;
351
352 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
353
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800354 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700355 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800356 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700357 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800358
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -0800359#ifdef DS1_DOLBY_DAP_ENABLED
360 audio_extn_dolby_set_dmid(adev);
361 audio_extn_dolby_set_endpoint(adev);
362#endif
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800363 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700364 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700365 ALOGV("%s: apply mixer and update path: %s", __func__, mixer_path);
366 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800367 ALOGV("%s: exit", __func__);
368 return 0;
369}
370
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700371int disable_audio_route(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700372 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800373{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700374 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700375 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800376
377 if (usecase == NULL)
378 return -EINVAL;
379
380 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700381 if (usecase->type == PCM_CAPTURE)
382 snd_device = usecase->in_snd_device;
383 else
384 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800385 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700386 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700387 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
388 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800389 ALOGV("%s: exit", __func__);
390 return 0;
391}
392
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700393int enable_snd_device(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700394 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800395{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700396 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
397
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800398 if (snd_device < SND_DEVICE_MIN ||
399 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800400 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800401 return -EINVAL;
402 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700403
404 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700405
406 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
407 ALOGE("%s: Invalid sound device returned", __func__);
408 return -EINVAL;
409 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700410 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700411 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700412 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700413 return 0;
414 }
415
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700416 /* start usb playback thread */
417 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
418 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
419 audio_extn_usb_start_playback(adev);
420
421 /* start usb capture thread */
422 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
423 audio_extn_usb_start_capture(adev);
424
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700425 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
426 audio_extn_spkr_prot_is_enabled()) {
427 if (audio_extn_spkr_prot_start_processing(snd_device)) {
428 ALOGE("%s: spkr_start_processing failed", __func__);
429 return -EINVAL;
430 }
431 } else {
432 ALOGV("%s: snd_device(%d: %s)", __func__,
433 snd_device, device_name);
434 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
435 adev->snd_dev_ref_cnt[snd_device]--;
436 return -EINVAL;
437 }
Kiran Kandide144c82013-11-20 15:58:32 -0800438 audio_extn_listen_update_status(snd_device,
439 LISTEN_EVENT_SND_DEVICE_BUSY);
440
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700441 audio_route_apply_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800442 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800443 return 0;
444}
445
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700446int disable_snd_device(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700447 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800448{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700449 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
450
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800451 if (snd_device < SND_DEVICE_MIN ||
452 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800453 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800454 return -EINVAL;
455 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700456 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
457 ALOGE("%s: device ref cnt is already 0", __func__);
458 return -EINVAL;
459 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700460
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700461 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700462
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700463 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
464 ALOGE("%s: Invalid sound device returned", __func__);
465 return -EINVAL;
466 }
467
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700468 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700469 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700470 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800471 /* exit usb play back thread */
472 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
473 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
474 audio_extn_usb_stop_playback();
475
476 /* exit usb capture thread */
477 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
Rajshekar Eashwarappa4dfa0752014-10-02 20:18:20 +0530478 audio_extn_usb_stop_capture();
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700479 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
480 audio_extn_spkr_prot_is_enabled()) {
481 audio_extn_spkr_prot_stop_processing();
482 } else
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700483 audio_route_reset_and_update_path(adev->audio_route, device_name);
Kiran Kandide144c82013-11-20 15:58:32 -0800484
485 audio_extn_listen_update_status(snd_device,
486 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700487 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700488
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800489 return 0;
490}
491
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700492static void check_usecases_codec_backend(struct audio_device *adev,
493 struct audio_usecase *uc_info,
494 snd_device_t snd_device)
495{
496 struct listnode *node;
497 struct audio_usecase *usecase;
498 bool switch_device[AUDIO_USECASE_MAX];
499 int i, num_uc_to_switch = 0;
500
501 /*
502 * This function is to make sure that all the usecases that are active on
503 * the hardware codec backend are always routed to any one device that is
504 * handled by the hardware codec.
505 * For example, if low-latency and deep-buffer usecases are currently active
506 * on speaker and out_set_parameters(headset) is received on low-latency
507 * output, then we have to make sure deep-buffer is also switched to headset,
508 * because of the limitation that both the devices cannot be enabled
509 * at the same time as they share the same backend.
510 */
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800511 /*
512 * This call is to check if we need to force routing for a particular stream
513 * If there is a backend configuration change for the device when a
514 * new stream starts, then ADM needs to be closed and re-opened with the new
515 * configuraion. This call check if we need to re-route all the streams
516 * associated with the backend. Touch tone + 24 bit playback.
517 */
518 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info);
519
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700520 /* Disable all the usecases on the shared backend other than the
521 specified usecase */
522 for (i = 0; i < AUDIO_USECASE_MAX; i++)
523 switch_device[i] = false;
524
525 list_for_each(node, &adev->usecase_list) {
526 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800527 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700528 usecase != uc_info &&
ApurupaPattapu9d6b4362014-02-27 10:24:11 -0800529 (usecase->out_snd_device != snd_device || force_routing) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700530 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
531 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
532 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700533 platform_get_snd_device_name(usecase->out_snd_device));
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700534 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700535 switch_device[usecase->id] = true;
536 num_uc_to_switch++;
537 }
538 }
539
540 if (num_uc_to_switch) {
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700541 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700542
543 list_for_each(node, &adev->usecase_list) {
544 usecase = node_to_item(node, struct audio_usecase, list);
545 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700546 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700547 }
548 }
549
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700550 list_for_each(node, &adev->usecase_list) {
551 usecase = node_to_item(node, struct audio_usecase, list);
552 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700553 enable_snd_device(adev, snd_device);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700554 }
555 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700556
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700557 /* Re-route all the usecases on the shared backend other than the
558 specified usecase to new snd devices */
559 list_for_each(node, &adev->usecase_list) {
560 usecase = node_to_item(node, struct audio_usecase, list);
561 /* Update the out_snd_device only before enabling the audio route */
562 if (switch_device[usecase->id] ) {
563 usecase->out_snd_device = snd_device;
Avinash Vaish8005dc82014-07-24 15:36:33 +0530564 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
565 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700566 }
567 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700568 }
569}
570
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700571static void check_and_route_capture_usecases(struct audio_device *adev,
572 struct audio_usecase *uc_info,
573 snd_device_t snd_device)
574{
575 struct listnode *node;
576 struct audio_usecase *usecase;
577 bool switch_device[AUDIO_USECASE_MAX];
578 int i, num_uc_to_switch = 0;
579
580 /*
581 * This function is to make sure that all the active capture usecases
582 * are always routed to the same input sound device.
583 * For example, if audio-record and voice-call usecases are currently
584 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
585 * is received for voice call then we have to make sure that audio-record
586 * usecase is also switched to earpiece i.e. voice-dmic-ef,
587 * because of the limitation that two devices cannot be enabled
588 * at the same time if they share the same backend.
589 */
590 for (i = 0; i < AUDIO_USECASE_MAX; i++)
591 switch_device[i] = false;
592
593 list_for_each(node, &adev->usecase_list) {
594 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800595 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700596 usecase != uc_info &&
Avinash Vaish44014792015-03-27 14:46:04 +0530597 usecase->in_snd_device != snd_device &&
598 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700599 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
600 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700601 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700602 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700603 switch_device[usecase->id] = true;
604 num_uc_to_switch++;
605 }
606 }
607
608 if (num_uc_to_switch) {
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700609 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700610
611 list_for_each(node, &adev->usecase_list) {
612 usecase = node_to_item(node, struct audio_usecase, list);
613 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700614 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800615 }
616 }
617
618 list_for_each(node, &adev->usecase_list) {
619 usecase = node_to_item(node, struct audio_usecase, list);
620 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700621 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700622 }
623 }
624
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700625 /* Re-route all the usecases on the shared backend other than the
626 specified usecase to new snd devices */
627 list_for_each(node, &adev->usecase_list) {
628 usecase = node_to_item(node, struct audio_usecase, list);
629 /* Update the in_snd_device only before enabling the audio route */
630 if (switch_device[usecase->id] ) {
631 usecase->in_snd_device = snd_device;
Avinash Vaish8005dc82014-07-24 15:36:33 +0530632 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
633 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700634 }
635 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700636 }
637}
638
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800639/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700640static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800641{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700642 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700643 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800644
645 switch (channels) {
646 /*
647 * Do not handle stereo output in Multi-channel cases
648 * Stereo case is handled in normal playback path
649 */
650 case 6:
651 ALOGV("%s: HDMI supports 5.1", __func__);
652 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
653 break;
654 case 8:
655 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
656 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
657 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
658 break;
659 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700660 ALOGE("HDMI does not support multi channel playback");
661 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800662 break;
663 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700664 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800665}
666
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700667static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
668{
669 struct audio_usecase *usecase;
670 struct listnode *node;
671
672 list_for_each(node, &adev->usecase_list) {
673 usecase = node_to_item(node, struct audio_usecase, list);
674 if (usecase->type == VOICE_CALL) {
675 ALOGV("%s: usecase id %d", __func__, usecase->id);
676 return usecase->id;
677 }
678 }
679 return USECASE_INVALID;
680}
681
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700682struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700683 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700684{
685 struct audio_usecase *usecase;
686 struct listnode *node;
687
688 list_for_each(node, &adev->usecase_list) {
689 usecase = node_to_item(node, struct audio_usecase, list);
690 if (usecase->id == uc_id)
691 return usecase;
692 }
693 return NULL;
694}
695
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700696int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800697{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800698 snd_device_t out_snd_device = SND_DEVICE_NONE;
699 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700700 struct audio_usecase *usecase = NULL;
701 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800702 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800703 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed739e7152014-01-23 15:56:53 -0800704 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800705 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700706 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800707
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700708 usecase = get_usecase_from_list(adev, uc_id);
709 if (usecase == NULL) {
710 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
711 return -EINVAL;
712 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800713
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800714 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800715 (usecase->type == VOIP_CALL) ||
716 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700717 out_snd_device = platform_get_output_snd_device(adev->platform,
718 usecase->stream.out->devices);
719 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700720 usecase->devices = usecase->stream.out->devices;
721 } else {
722 /*
723 * If the voice call is active, use the sound devices of voice call usecase
724 * so that it would not result any device switch. All the usecases will
725 * be switched to new device when select_devices() is called for voice call
726 * usecase. This is to avoid switching devices for voice call when
727 * check_usecases_codec_backend() is called below.
728 */
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -0700729 if (adev->voice.in_call && adev->mode == AUDIO_MODE_IN_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700730 vc_usecase = get_usecase_from_list(adev,
731 get_voice_usecase_id_from_list(adev));
Mingming Yin6c23f382014-08-14 00:00:51 -0700732 if ((vc_usecase) && ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
733 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700734 in_snd_device = vc_usecase->in_snd_device;
735 out_snd_device = vc_usecase->out_snd_device;
736 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800737 } else if (voice_extn_compress_voip_is_active(adev)) {
738 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Mingming Yin6c23f382014-08-14 00:00:51 -0700739 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Avinash Vaish6a4c3372014-06-25 12:20:37 +0530740 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Mingming Yin6c23f382014-08-14 00:00:51 -0700741 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800742 in_snd_device = voip_usecase->in_snd_device;
743 out_snd_device = voip_usecase->out_snd_device;
744 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800745 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed739e7152014-01-23 15:56:53 -0800746 hfp_ucid = audio_extn_hfp_get_usecase();
747 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin6c23f382014-08-14 00:00:51 -0700748 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800749 in_snd_device = hfp_usecase->in_snd_device;
750 out_snd_device = hfp_usecase->out_snd_device;
751 }
Vimal Puthanveed8fa9eab2014-01-07 16:47:47 -0800752 } else if (audio_extn_hfp_is_active(adev)) {
753 hfp_usecase = get_usecase_from_list(adev, USECASE_AUDIO_HFP_SCO);
754 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
755 in_snd_device = hfp_usecase->in_snd_device;
756 out_snd_device = hfp_usecase->out_snd_device;
757 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700758 }
759 if (usecase->type == PCM_PLAYBACK) {
760 usecase->devices = usecase->stream.out->devices;
761 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700762 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700763 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700764 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700765 if (usecase->stream.out == adev->primary_output &&
Shreyas Nagasandra Chandrasekhar23b3af22014-09-03 19:10:01 +0530766 adev->active_input ) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700767 select_devices(adev, adev->active_input->usecase);
768 }
769 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700770 } else if (usecase->type == PCM_CAPTURE) {
771 usecase->devices = usecase->stream.in->device;
772 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700773 if (in_snd_device == SND_DEVICE_NONE) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530774 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Shreyas Nagasandra Chandrasekhar23b3af22014-09-03 19:10:01 +0530775 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 -0700776 adev->primary_output && !adev->primary_output->standby) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530777 out_device = adev->primary_output->devices;
778 } else if(usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
779 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700780 }
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530781 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700782 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700783 }
784 }
785
786 if (out_snd_device == usecase->out_snd_device &&
787 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800788 return 0;
789 }
790
sangwoobc677242013-08-08 16:53:43 +0900791 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700792 out_snd_device, platform_get_snd_device_name(out_snd_device),
793 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800794
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800795 /*
796 * Limitation: While in call, to do a device switch we need to disable
797 * and enable both RX and TX devices though one of them is same as current
798 * device.
799 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800800 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700801 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800802 }
803
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700804 /* Disable current sound devices */
805 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700806 disable_audio_route(adev, usecase);
807 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800808 }
809
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700810 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700811 disable_audio_route(adev, usecase);
812 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800813 }
814
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700815 /* Enable new sound devices */
816 if (out_snd_device != SND_DEVICE_NONE) {
817 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
818 check_usecases_codec_backend(adev, usecase, out_snd_device);
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700819 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800820 }
821
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700822 if (in_snd_device != SND_DEVICE_NONE) {
823 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700824 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700825 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700826
Avinash Vaish8005dc82014-07-24 15:36:33 +0530827 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700828 status = platform_switch_voice_call_device_post(adev->platform,
829 out_snd_device,
830 in_snd_device);
Avinash Vaish8005dc82014-07-24 15:36:33 +0530831 enable_audio_route_for_voice_usecases(adev, usecase);
832 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800833
sangwoo170731f2013-06-08 15:36:36 +0900834 usecase->in_snd_device = in_snd_device;
835 usecase->out_snd_device = out_snd_device;
836
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700837 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900838
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800839 /* Applicable only on the targets that has external modem.
840 * Enable device command should be sent to modem only after
841 * enabling voice call mixer controls
842 */
843 if (usecase->type == VOICE_CALL)
844 status = platform_switch_voice_call_usecase_route_post(adev->platform,
845 out_snd_device,
846 in_snd_device);
847
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +0530848 ALOGD("%s: done",__func__);
849
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800850 return status;
851}
852
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800853static int stop_input_stream(struct stream_in *in)
854{
855 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800856 struct audio_usecase *uc_info;
857 struct audio_device *adev = in->dev;
858
Eric Laurentc8400632013-02-14 19:04:54 -0800859 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800860
Eric Laurent994a6932013-07-17 11:51:42 -0700861 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700862 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800863 uc_info = get_usecase_from_list(adev, in->usecase);
864 if (uc_info == NULL) {
865 ALOGE("%s: Could not find the usecase (%d) in the list",
866 __func__, in->usecase);
867 return -EINVAL;
868 }
869
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800870 /* Close in-call recording streams */
871 voice_check_and_stop_incall_rec_usecase(adev, in);
872
Eric Laurent150dbfe2013-02-27 14:31:02 -0800873 /* 1. Disable stream specific mixer controls */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700874 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700875
876 /* 2. Disable the tx device */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700877 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800878
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800879 list_remove(&uc_info->list);
880 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800881
Eric Laurent994a6932013-07-17 11:51:42 -0700882 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800883 return ret;
884}
885
886int start_input_stream(struct stream_in *in)
887{
888 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800889 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800890 struct audio_usecase *uc_info;
891 struct audio_device *adev = in->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +0530892 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800893
Ramjee Singh240ac652015-07-31 16:12:29 +0530894 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
895 if (get_usecase_from_list(adev, usecase) == NULL)
896 in->usecase = usecase;
897
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +0530898 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
899 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700900
Naresh Tannirucef332d2014-06-04 18:17:56 +0530901 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
902 ALOGE("%s: sound card is not active/SSR returning error ", __func__);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530903 ret = -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530904 goto error_config;
905 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530906
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700907 /* Check if source matches incall recording usecase criteria */
908 ret = voice_check_and_set_incall_rec_usecase(adev, in);
909 if (ret)
910 goto error_config;
911 else
912 ALOGV("%s: usecase(%d)", __func__, in->usecase);
913
Ramjee Singh240ac652015-07-31 16:12:29 +0530914 if (get_usecase_from_list(adev, in->usecase) != NULL) {
915 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
916 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
917 goto error_config;
918 }
919
Eric Laurentb23d5282013-05-14 15:27:20 -0700920 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800921 if (in->pcm_device_id < 0) {
922 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
923 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800924 ret = -EINVAL;
925 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800926 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700927
928 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800929 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
930 uc_info->id = in->usecase;
931 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800932 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700933 uc_info->devices = in->device;
934 uc_info->in_snd_device = SND_DEVICE_NONE;
935 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800936
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800937 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700938 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800939
Eric Laurentc8400632013-02-14 19:04:54 -0800940 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530941 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
942
943 unsigned int flags = PCM_IN;
944 unsigned int pcm_open_retry_entry_count = 0;
945
946 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
947 flags |= PCM_MMAP | PCM_NOIRQ;
948 pcm_open_retry_entry_count = PROXY_OPEN_RETRY_COUNT;
949 }
950
951 while(1) {
952 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
953 flags, &in->config);
954 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
955 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
956 if (in->pcm != NULL) {
957 pcm_close(in->pcm);
958 in->pcm = NULL;
959 }
960 if (pcm_open_retry_entry_count-- == 0) {
961 ret = -EIO;
962 goto error_open;
963 }
964 usleep(PROXY_OPEN_WAIT_TIME * 1000);
965 continue;
966 }
967 break;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800968 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +0530969
Eric Laurent994a6932013-07-17 11:51:42 -0700970 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800971 return ret;
972
973error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800974 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800975
976error_config:
977 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700978 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800979
980 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800981}
982
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700983/* must be called with out->lock locked */
984static int send_offload_cmd_l(struct stream_out* out, int command)
985{
986 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
987
988 ALOGVV("%s %d", __func__, command);
989
990 cmd->cmd = command;
991 list_add_tail(&out->offload_cmd_list, &cmd->node);
992 pthread_cond_signal(&out->offload_cond);
993 return 0;
994}
995
996/* must be called iwth out->lock locked */
997static void stop_compressed_output_l(struct stream_out *out)
998{
999 out->offload_state = OFFLOAD_STATE_IDLE;
1000 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001001 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001002 if (out->compr != NULL) {
1003 compress_stop(out->compr);
1004 while (out->offload_thread_blocked) {
1005 pthread_cond_wait(&out->cond, &out->lock);
1006 }
1007 }
1008}
1009
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001010bool is_offload_usecase(audio_usecase_t uc_id)
1011{
1012 unsigned int i;
1013 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1014 if (uc_id == offload_usecases[i])
1015 return true;
1016 }
1017 return false;
1018}
1019
1020static audio_usecase_t get_offload_usecase(struct audio_device *adev)
1021{
1022 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1023 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
1024 char value[PROPERTY_VALUE_MAX] = {0};
1025
1026 property_get("audio.offload.multiple.enabled", value, NULL);
1027 if (!(atoi(value) || !strncmp("true", value, 4)))
1028 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
1029
1030 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
1031 for (i = 0; i < num_usecase; i++) {
1032 if (!(adev->offload_usecases_state & (0x1<<i))) {
1033 adev->offload_usecases_state |= 0x1 << i;
1034 ret = offload_usecases[i];
1035 break;
1036 }
1037 }
1038 ALOGV("%s: offload usecase is %d", __func__, ret);
1039 return ret;
1040}
1041
1042static void free_offload_usecase(struct audio_device *adev,
1043 audio_usecase_t uc_id)
1044{
1045 unsigned int i;
1046 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1047 if (offload_usecases[i] == uc_id) {
1048 adev->offload_usecases_state &= ~(0x1<<i);
1049 break;
1050 }
1051 }
1052 ALOGV("%s: free offload usecase %d", __func__, uc_id);
1053}
1054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001055static void *offload_thread_loop(void *context)
1056{
1057 struct stream_out *out = (struct stream_out *) context;
1058 struct listnode *item;
Krishnankutty Kolathappilly0fe78f02014-01-06 18:33:58 -08001059 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001060
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001061 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1062 set_sched_policy(0, SP_FOREGROUND);
1063 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1064
1065 ALOGV("%s", __func__);
1066 pthread_mutex_lock(&out->lock);
1067 for (;;) {
1068 struct offload_cmd *cmd = NULL;
1069 stream_callback_event_t event;
1070 bool send_callback = false;
1071
1072 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1073 __func__, list_empty(&out->offload_cmd_list),
1074 out->offload_state);
1075 if (list_empty(&out->offload_cmd_list)) {
1076 ALOGV("%s SLEEPING", __func__);
1077 pthread_cond_wait(&out->offload_cond, &out->lock);
1078 ALOGV("%s RUNNING", __func__);
1079 continue;
1080 }
1081
1082 item = list_head(&out->offload_cmd_list);
1083 cmd = node_to_item(item, struct offload_cmd, node);
1084 list_remove(item);
1085
1086 ALOGVV("%s STATE %d CMD %d out->compr %p",
1087 __func__, out->offload_state, cmd->cmd, out->compr);
1088
1089 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1090 free(cmd);
1091 break;
1092 }
1093
1094 if (out->compr == NULL) {
1095 ALOGE("%s: Compress handle is NULL", __func__);
1096 pthread_cond_signal(&out->cond);
1097 continue;
1098 }
1099 out->offload_thread_blocked = true;
1100 pthread_mutex_unlock(&out->lock);
1101 send_callback = false;
1102 switch(cmd->cmd) {
1103 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
1104 compress_wait(out->compr, -1);
1105 send_callback = true;
1106 event = STREAM_CBK_EVENT_WRITE_READY;
1107 break;
1108 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappilly0fe78f02014-01-06 18:33:58 -08001109 ret = compress_next_track(out->compr);
1110 if(ret == 0)
1111 compress_partial_drain(out->compr);
1112 else if(ret == -ETIMEDOUT)
1113 compress_drain(out->compr);
1114 else
1115 ALOGE("%s: Next track returned error %d",__func__, ret);
1116
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001117 send_callback = true;
1118 event = STREAM_CBK_EVENT_DRAIN_READY;
1119 break;
1120 case OFFLOAD_CMD_DRAIN:
1121 compress_drain(out->compr);
1122 send_callback = true;
1123 event = STREAM_CBK_EVENT_DRAIN_READY;
1124 break;
1125 default:
1126 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1127 break;
1128 }
1129 pthread_mutex_lock(&out->lock);
1130 out->offload_thread_blocked = false;
1131 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001132 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001133 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001134 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001135 free(cmd);
1136 }
1137
1138 pthread_cond_signal(&out->cond);
1139 while (!list_empty(&out->offload_cmd_list)) {
1140 item = list_head(&out->offload_cmd_list);
1141 list_remove(item);
1142 free(node_to_item(item, struct offload_cmd, node));
1143 }
1144 pthread_mutex_unlock(&out->lock);
1145
1146 return NULL;
1147}
1148
1149static int create_offload_callback_thread(struct stream_out *out)
1150{
1151 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1152 list_init(&out->offload_cmd_list);
1153 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1154 offload_thread_loop, out);
1155 return 0;
1156}
1157
1158static int destroy_offload_callback_thread(struct stream_out *out)
1159{
1160 pthread_mutex_lock(&out->lock);
1161 stop_compressed_output_l(out);
1162 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1163
1164 pthread_mutex_unlock(&out->lock);
1165 pthread_join(out->offload_thread, (void **) NULL);
1166 pthread_cond_destroy(&out->offload_cond);
1167
1168 return 0;
1169}
1170
Eric Laurent07eeafd2013-10-06 12:52:49 -07001171static bool allow_hdmi_channel_config(struct audio_device *adev)
1172{
1173 struct listnode *node;
1174 struct audio_usecase *usecase;
1175 bool ret = true;
1176
1177 list_for_each(node, &adev->usecase_list) {
1178 usecase = node_to_item(node, struct audio_usecase, list);
1179 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1180 /*
1181 * If voice call is already existing, do not proceed further to avoid
1182 * disabling/enabling both RX and TX devices, CSD calls, etc.
1183 * Once the voice call done, the HDMI channels can be configured to
1184 * max channels of remaining use cases.
1185 */
1186 if (usecase->id == USECASE_VOICE_CALL) {
1187 ALOGD("%s: voice call is active, no change in HDMI channels",
1188 __func__);
1189 ret = false;
1190 break;
1191 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1192 ALOGD("%s: multi channel playback is active, "
1193 "no change in HDMI channels", __func__);
1194 ret = false;
1195 break;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001196 } else if (is_offload_usecase(usecase->id) &&
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05301197 audio_channel_count_from_out_mask(usecase->stream.out->channel_mask) > 2) {
Mingming Yin3ed162b2014-02-24 17:56:01 -08001198 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1199 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1200 ret = false;
1201 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001202 }
1203 }
1204 }
1205 return ret;
1206}
1207
1208static int check_and_set_hdmi_channels(struct audio_device *adev,
1209 unsigned int channels)
1210{
1211 struct listnode *node;
1212 struct audio_usecase *usecase;
1213
1214 /* Check if change in HDMI channel config is allowed */
1215 if (!allow_hdmi_channel_config(adev))
1216 return 0;
1217
1218 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001219 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001220 return 0;
1221 }
1222
1223 platform_set_hdmi_channels(adev->platform, channels);
1224 adev->cur_hdmi_channels = channels;
1225
1226 /*
1227 * Deroute all the playback streams routed to HDMI so that
1228 * the back end is deactivated. Note that backend will not
1229 * be deactivated if any one stream is connected to it.
1230 */
1231 list_for_each(node, &adev->usecase_list) {
1232 usecase = node_to_item(node, struct audio_usecase, list);
1233 if (usecase->type == PCM_PLAYBACK &&
1234 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001235 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001236 }
1237 }
1238
1239 /*
1240 * Enable all the streams disabled above. Now the HDMI backend
1241 * will be activated with new channel configuration
1242 */
1243 list_for_each(node, &adev->usecase_list) {
1244 usecase = node_to_item(node, struct audio_usecase, list);
1245 if (usecase->type == PCM_PLAYBACK &&
1246 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001247 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001248 }
1249 }
1250
1251 return 0;
1252}
1253
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001254static int stop_output_stream(struct stream_out *out)
1255{
1256 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001257 struct audio_usecase *uc_info;
1258 struct audio_device *adev = out->dev;
1259
Eric Laurent994a6932013-07-17 11:51:42 -07001260 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001261 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262 uc_info = get_usecase_from_list(adev, out->usecase);
1263 if (uc_info == NULL) {
1264 ALOGE("%s: Could not find the usecase (%d) in the list",
1265 __func__, out->usecase);
1266 return -EINVAL;
1267 }
1268
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001269 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001270 if (adev->visualizer_stop_output != NULL)
1271 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1272 if (adev->offload_effects_stop_output != NULL)
1273 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1274 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001275
Eric Laurent150dbfe2013-02-27 14:31:02 -08001276 /* 1. Get and set stream specific mixer controls */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001277 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001278
1279 /* 2. Disable the rx device */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001280 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001281
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001282 list_remove(&uc_info->list);
1283 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001284
Eric Laurent07eeafd2013-10-06 12:52:49 -07001285 /* Must be called after removing the usecase from list */
1286 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1287 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1288
Eric Laurent994a6932013-07-17 11:51:42 -07001289 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001290 return ret;
1291}
1292
1293int start_output_stream(struct stream_out *out)
1294{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001295 int ret = 0;
Mingming Yin6c344432014-05-01 15:37:31 -07001296 int sink_channels = 0;
1297 char prop_value[PROPERTY_VALUE_MAX] = {0};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001298 struct audio_usecase *uc_info;
1299 struct audio_device *adev = out->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +05301300 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001301
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301302 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1303 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1304 out->devices);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301305
Naresh Tannirucef332d2014-06-04 18:17:56 +05301306 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301307 ALOGE("%s: sound card is not active/SSR returning error", __func__);
1308 ret = -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301309 goto error_config;
1310 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301311
Eric Laurentb23d5282013-05-14 15:27:20 -07001312 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001313 if (out->pcm_device_id < 0) {
1314 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1315 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001316 ret = -EINVAL;
1317 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001318 }
1319
1320 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1321 uc_info->id = out->usecase;
1322 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001323 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001324 uc_info->devices = out->devices;
1325 uc_info->in_snd_device = SND_DEVICE_NONE;
1326 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001327
Eric Laurent07eeafd2013-10-06 12:52:49 -07001328 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001329 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Mingming Yin6c344432014-05-01 15:37:31 -07001330 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1331 if (!strncmp("true", prop_value, 4)) {
1332 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1333 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1334 check_and_set_hdmi_channels(adev, sink_channels);
1335 } else {
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001336 if (is_offload_usecase(out->usecase))
Mingming Yin6c344432014-05-01 15:37:31 -07001337 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1338 else
1339 check_and_set_hdmi_channels(adev, out->config.channels);
1340 }
Mingming Yin10fef6a2013-11-26 17:17:01 -08001341 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001342
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001343 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001344
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001345 select_devices(adev, out->usecase);
1346
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001347 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1348 __func__, 0, out->pcm_device_id);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001349 if (!is_offload_usecase(out->usecase)) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301350 unsigned int flags = PCM_OUT;
1351 unsigned int pcm_open_retry_count = 0;
1352 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1353 flags |= PCM_MMAP | PCM_NOIRQ;
1354 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1355 } else
1356 flags |= PCM_MONOTONIC;
1357
1358 while (1) {
1359 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1360 flags, &out->config);
1361 if (out->pcm && !pcm_is_ready(out->pcm)) {
1362 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1363 if (out->pcm != NULL) {
1364 pcm_close(out->pcm);
1365 out->pcm = NULL;
1366 }
1367 if (pcm_open_retry_count-- == 0) {
1368 ret = -EIO;
1369 goto error_open;
1370 }
1371 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1372 continue;
1373 }
1374 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001375 }
1376 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001377 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001378 out->compr = compress_open(adev->snd_card,
1379 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001380 COMPRESS_IN, &out->compr_config);
1381 if (out->compr && !is_compress_ready(out->compr)) {
1382 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1383 compress_close(out->compr);
1384 out->compr = NULL;
1385 ret = -EIO;
1386 goto error_open;
1387 }
1388 if (out->offload_callback)
1389 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001390
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08001391#ifdef DS1_DOLBY_DDP_ENABLED
1392 if (audio_extn_is_dolby_format(out->format))
1393 audio_extn_dolby_send_ddp_endp_params(adev);
1394#endif
1395
Eric Laurentc4aef752013-09-12 17:45:53 -07001396 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001397 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1398 if (adev->offload_effects_start_output != NULL)
1399 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001400 }
Eric Laurent994a6932013-07-17 11:51:42 -07001401 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001402 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001403error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001405error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001406 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001407}
1408
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409static int check_input_parameters(uint32_t sample_rate,
1410 audio_format_t format,
1411 int channel_count)
1412{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001413 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001414
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001415 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001416 !voice_extn_compress_voip_is_format_supported(format) &&
1417 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001418
1419 switch (channel_count) {
1420 case 1:
1421 case 2:
1422 case 6:
1423 break;
1424 default:
1425 ret = -EINVAL;
1426 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001427
1428 switch (sample_rate) {
1429 case 8000:
1430 case 11025:
1431 case 12000:
1432 case 16000:
1433 case 22050:
1434 case 24000:
1435 case 32000:
1436 case 44100:
1437 case 48000:
1438 break;
1439 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001440 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001441 }
1442
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001443 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001444}
1445
1446static size_t get_input_buffer_size(uint32_t sample_rate,
1447 audio_format_t format,
1448 int channel_count)
1449{
1450 size_t size = 0;
1451
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001452 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1453 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001454
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001455 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1456 /* ToDo: should use frame_size computed based on the format and
1457 channel_count here. */
1458 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001459
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001460 /* make sure the size is multiple of 64 */
1461 size += 0x3f;
1462 size &= ~0x3f;
1463
1464 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001465}
1466
1467static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1468{
1469 struct stream_out *out = (struct stream_out *)stream;
1470
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001471 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001472}
1473
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301474static int out_set_sample_rate(struct audio_stream *stream __unused,
1475 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001476{
1477 return -ENOSYS;
1478}
1479
1480static size_t out_get_buffer_size(const struct audio_stream *stream)
1481{
1482 struct stream_out *out = (struct stream_out *)stream;
1483
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001484 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001485 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001486 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1487 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001488
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05301489 return out->config.period_size *
1490 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001491}
1492
1493static uint32_t out_get_channels(const struct audio_stream *stream)
1494{
1495 struct stream_out *out = (struct stream_out *)stream;
1496
1497 return out->channel_mask;
1498}
1499
1500static audio_format_t out_get_format(const struct audio_stream *stream)
1501{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001502 struct stream_out *out = (struct stream_out *)stream;
1503
1504 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001505}
1506
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301507static int out_set_format(struct audio_stream *stream __unused,
1508 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001509{
1510 return -ENOSYS;
1511}
1512
1513static int out_standby(struct audio_stream *stream)
1514{
1515 struct stream_out *out = (struct stream_out *)stream;
1516 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001517
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301518 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1519 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001520 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1521 /* Ignore standby in case of voip call because the voip output
1522 * stream is closed in adev_close_output_stream()
1523 */
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301524 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001525 return 0;
1526 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001527
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001528 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001529 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001530 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001531 out->standby = true;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001532 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001533 if (out->pcm) {
1534 pcm_close(out->pcm);
1535 out->pcm = NULL;
1536 }
1537 } else {
1538 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001539 out->gapless_mdata.encoder_delay = 0;
1540 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001541 if (out->compr != NULL) {
1542 compress_close(out->compr);
1543 out->compr = NULL;
1544 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001545 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001546 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001547 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001548 }
1549 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001550 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001551 return 0;
1552}
1553
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301554static int out_dump(const struct audio_stream *stream __unused,
1555 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001556{
1557 return 0;
1558}
1559
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001560static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1561{
1562 int ret = 0;
1563 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001564 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001565 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001566 tmp_mdata.encoder_delay = 0;
1567 tmp_mdata.encoder_padding = 0;
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001568
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001569 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001570 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001571 return -EINVAL;
1572 }
1573
Mingming Yinee733602014-04-03 17:47:22 -07001574#ifdef EXTN_OFFLOAD_ENABLED
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001575 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1576 if (ret >= 0) {
1577 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1578 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1579 ALOGV("ADTS format is set in offload mode");
1580 }
1581 out->send_new_metadata = 1;
1582 }
Mingming Yinee733602014-04-03 17:47:22 -07001583#endif
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001584
Steve Kondik6bedcdf2014-07-21 11:48:30 -07001585#ifdef FLAC_OFFLOAD_ENABLED
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08001586 if (out->format == AUDIO_FORMAT_FLAC) {
1587 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_BLK_SIZE, value, sizeof(value));
1588 if (ret >= 0) {
1589 out->compr_config.codec->options.flac_dec.min_blk_size = atoi(value);
1590 out->send_new_metadata = 1;
1591 }
1592 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_BLK_SIZE, value, sizeof(value));
1593 if (ret >= 0) {
1594 out->compr_config.codec->options.flac_dec.max_blk_size = atoi(value);
1595 out->send_new_metadata = 1;
1596 }
1597 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_FRAME_SIZE, value, sizeof(value));
1598 if (ret >= 0) {
1599 out->compr_config.codec->options.flac_dec.min_frame_size = atoi(value);
1600 out->send_new_metadata = 1;
1601 }
1602 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_FRAME_SIZE, value, sizeof(value));
1603 if (ret >= 0) {
1604 out->compr_config.codec->options.flac_dec.max_frame_size = atoi(value);
1605 out->send_new_metadata = 1;
1606 }
1607 }
Steve Kondik6bedcdf2014-07-21 11:48:30 -07001608#endif
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08001609
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001610 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1611 if(ret >= 0)
1612 is_meta_data_params = true;
1613 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1614 if(ret >= 0 )
1615 is_meta_data_params = true;
1616 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1617 if(ret >= 0 )
1618 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001619 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1620 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001621 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001622 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001623 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001624 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1625 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001626 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001627 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001628 }
1629
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001630 if(!is_meta_data_params) {
1631 ALOGV("%s: Not gapless meta data params", __func__);
1632 return 0;
1633 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001634 out->gapless_mdata = tmp_mdata;
1635 out->send_new_metadata = 1;
1636 ALOGV("%s new encoder delay %u and padding %u", __func__,
1637 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1638
1639 return 0;
1640}
1641
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301642static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1643{
1644 return out == adev->primary_output || out == adev->voice_tx_output;
1645}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001646
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001647static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1648{
1649 struct stream_out *out = (struct stream_out *)stream;
1650 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001651 struct audio_usecase *usecase;
1652 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001653 struct str_parms *parms;
1654 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001655 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001656 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001657
sangwoobc677242013-08-08 16:53:43 +09001658 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001659 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001660 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001661 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1662 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001663 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001664 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001665 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001666
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001667 /*
Dhanalakshmi Siddani523ae412014-04-18 22:26:56 +05301668 * When HDMI cable is unplugged/usb hs is disconnected the
1669 * music playback is paused and the policy manager sends routing=0
1670 * But the audioflingercontinues to write data until standby time
1671 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001672 * Avoid this by routing audio to speaker until standby.
1673 */
Dhanalakshmi Siddani523ae412014-04-18 22:26:56 +05301674 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
Steve Kondikeadb6c82014-09-03 18:40:06 -07001675 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET ||
1676 out->devices == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001677 val == AUDIO_DEVICE_NONE) {
1678 val = AUDIO_DEVICE_OUT_SPEAKER;
1679 }
1680
1681 /*
1682 * select_devices() call below switches all the usecases on the same
1683 * backend to the new device. Refer to check_usecases_codec_backend() in
1684 * the select_devices(). But how do we undo this?
1685 *
1686 * For example, music playback is active on headset (deep-buffer usecase)
1687 * and if we go to ringtones and select a ringtone, low-latency usecase
1688 * will be started on headset+speaker. As we can't enable headset+speaker
1689 * and headset devices at the same time, select_devices() switches the music
1690 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1691 * So when the ringtone playback is completed, how do we undo the same?
1692 *
1693 * We are relying on the out_set_parameters() call on deep-buffer output,
1694 * once the ringtone playback is ended.
1695 * NOTE: We should not check if the current devices are same as new devices.
1696 * Because select_devices() must be called to switch back the music
1697 * playback to headset.
1698 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001699 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001700 out->devices = val;
1701
1702 if (!out->standby)
1703 select_devices(adev, out->usecase);
1704
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001705 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301706 output_drives_call(adev, out)) {
1707 adev->current_call_output = out;
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001708 if (!adev->voice.in_call)
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301709 ret = voice_start_call(adev);
1710 else
1711 voice_update_devices_for_all_voice_usecases(adev);
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001712 }
1713 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001714
Vidyakumar Athotaf85f6a22014-08-05 18:20:42 -07001715 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1716 adev->voice.in_call &&
1717 output_drives_call(adev, out)) {
1718 ret = voice_stop_call(adev);
1719 adev->current_call_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001720 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001721
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001722 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001723 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001724 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001725
1726 if (out == adev->primary_output) {
1727 pthread_mutex_lock(&adev->lock);
1728 audio_extn_set_parameters(adev, parms);
1729 pthread_mutex_unlock(&adev->lock);
1730 }
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001731 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001732 pthread_mutex_lock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001733 parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001734 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001735 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001736
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001737 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001738 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001739 return ret;
1740}
1741
1742static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1743{
1744 struct stream_out *out = (struct stream_out *)stream;
1745 struct str_parms *query = str_parms_create_str(keys);
1746 char *str;
1747 char value[256];
1748 struct str_parms *reply = str_parms_create();
1749 size_t i, j;
1750 int ret;
1751 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001752 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001753 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1754 if (ret >= 0) {
1755 value[0] = '\0';
1756 i = 0;
1757 while (out->supported_channel_masks[i] != 0) {
1758 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1759 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1760 if (!first) {
1761 strcat(value, "|");
1762 }
1763 strcat(value, out_channels_name_to_enum_table[j].name);
1764 first = false;
1765 break;
1766 }
1767 }
1768 i++;
1769 }
1770 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1771 str = str_parms_to_str(reply);
1772 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001773 voice_extn_out_get_parameters(out, query, reply);
1774 str = str_parms_to_str(reply);
1775 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella2e032352014-01-29 12:52:19 -08001776 free(str);
1777 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001778 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001779 }
1780 str_parms_destroy(query);
1781 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001782 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001783 return str;
1784}
1785
1786static uint32_t out_get_latency(const struct audio_stream_out *stream)
1787{
1788 struct stream_out *out = (struct stream_out *)stream;
1789
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001790 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001791 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1792
1793 return (out->config.period_count * out->config.period_size * 1000) /
1794 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001795}
1796
1797static int out_set_volume(struct audio_stream_out *stream, float left,
1798 float right)
1799{
Eric Laurenta9024de2013-04-04 09:19:12 -07001800 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001801 int volume[2];
1802
Eric Laurenta9024de2013-04-04 09:19:12 -07001803 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1804 /* only take left channel into account: the API is for stereo anyway */
1805 out->muted = (left == 0.0f);
1806 return 0;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001807 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001808 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001809 struct audio_device *adev = out->dev;
1810 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001811 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1812 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001813
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001814 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1815 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001816 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1817 if (!ctl) {
1818 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1819 __func__, mixer_ctl_name);
1820 return -EINVAL;
1821 }
1822 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1823 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1824 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1825 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001826 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001827
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001828 return -ENOSYS;
1829}
1830
1831static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1832 size_t bytes)
1833{
1834 struct stream_out *out = (struct stream_out *)stream;
1835 struct audio_device *adev = out->dev;
Naresh Tannirucef332d2014-06-04 18:17:56 +05301836 int snd_scard_state = get_snd_card_state(adev);
Eric Laurent6e895242013-09-05 16:10:57 -07001837 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001838
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001839 pthread_mutex_lock(&out->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301840
Naresh Tannirucef332d2014-06-04 18:17:56 +05301841 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
1842 if (out->pcm) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301843 ALOGD(" %s: sound card is not active/SSR state", __func__);
1844 ret= -ENETRESET;
1845 goto exit;
Naresh Tannirucef332d2014-06-04 18:17:56 +05301846 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1847 //during SSR for compress usecase we should return error to flinger
1848 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
1849 pthread_mutex_unlock(&out->lock);
1850 return -ENETRESET;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301851 }
1852 }
1853
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001854 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001855 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001856 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001857 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1858 ret = voice_extn_compress_voip_start_output_stream(out);
1859 else
1860 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001861 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001862 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001863 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001864 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001865 goto exit;
1866 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001867 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001868
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001869 if (is_offload_usecase(out->usecase)) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001870 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1871 if (out->send_new_metadata) {
1872 ALOGVV("send new gapless metadata");
1873 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1874 out->send_new_metadata = 0;
1875 }
1876
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001877 ret = compress_write(out->compr, buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05301878 if (ret < 0)
1879 ret = -errno;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001880 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001881 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001882 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tannirucef332d2014-06-04 18:17:56 +05301883 } else if (-ENETRESET == ret) {
1884 ALOGE("copl %s: received sound card offline state on compress write", __func__);
1885 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1886 pthread_mutex_unlock(&out->lock);
1887 out_standby(&out->stream.common);
1888 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001889 }
Naresh Tannirucef332d2014-06-04 18:17:56 +05301890 if (!out->playback_started && ret >= 0) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001891 compress_start(out->compr);
1892 out->playback_started = 1;
1893 out->offload_state = OFFLOAD_STATE_PLAYING;
1894 }
1895 pthread_mutex_unlock(&out->lock);
1896 return ret;
1897 } else {
1898 if (out->pcm) {
1899 if (out->muted)
1900 memset((void *)buffer, 0, bytes);
1901 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301902 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY)
1903 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
1904 else
1905 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05301906 if (ret < 0)
1907 ret = -errno;
1908 else if (ret == 0)
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001909 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001910 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001911 }
1912
1913exit:
Dhanalakshmi Siddani4fe3e512014-05-26 18:03:42 +05301914 /* ToDo: There may be a corner case when SSR happens back to back during
1915 start/stop. Need to post different error to handle that. */
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301916 if (-ENETRESET == ret) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05301917 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301918 }
1919
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001920 pthread_mutex_unlock(&out->lock);
1921
1922 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001923 if (out->pcm)
1924 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05301925 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05301926 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05301927 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05301928 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05301929 out->standby = true;
1930 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001931 out_standby(&out->stream.common);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05301932 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1933 out_get_sample_rate(&out->stream.common));
1934
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001935 }
1936 return bytes;
1937}
1938
1939static int out_get_render_position(const struct audio_stream_out *stream,
1940 uint32_t *dsp_frames)
1941{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001942 struct stream_out *out = (struct stream_out *)stream;
1943 *dsp_frames = 0;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001944 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
1945 ssize_t ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001946 pthread_mutex_lock(&out->lock);
1947 if (out->compr != NULL) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05301948 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001949 &out->sample_rate);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05301950 if (ret < 0)
1951 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001952 ALOGVV("%s rendered frames %d sample_rate %d",
1953 __func__, *dsp_frames, out->sample_rate);
1954 }
1955 pthread_mutex_unlock(&out->lock);
Naresh Tannirucef332d2014-06-04 18:17:56 +05301956 if (-ENETRESET == ret) {
1957 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
1958 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1959 return -EINVAL;
1960 } else if(ret < 0) {
1961 ALOGE(" ERROR: Unable to get time stamp from compress driver");
1962 return -EINVAL;
1963 } else {
1964 return 0;
1965 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001966 } else
1967 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001968}
1969
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301970static int out_add_audio_effect(const struct audio_stream *stream __unused,
1971 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001972{
1973 return 0;
1974}
1975
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301976static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1977 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001978{
1979 return 0;
1980}
1981
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301982static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1983 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001984{
1985 return -EINVAL;
1986}
1987
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001988static int out_get_presentation_position(const struct audio_stream_out *stream,
1989 uint64_t *frames, struct timespec *timestamp)
1990{
1991 struct stream_out *out = (struct stream_out *)stream;
1992 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001993 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001994
1995 pthread_mutex_lock(&out->lock);
1996
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07001997 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07001998 if (out->compr != NULL) {
1999 compress_get_tstamp(out->compr, &dsp_frames,
2000 &out->sample_rate);
2001 ALOGVV("%s rendered frames %ld sample_rate %d",
2002 __func__, dsp_frames, out->sample_rate);
2003 *frames = dsp_frames;
2004 ret = 0;
2005 /* this is the best we can do */
2006 clock_gettime(CLOCK_MONOTONIC, timestamp);
2007 }
2008 } else {
2009 if (out->pcm) {
2010 size_t avail;
2011 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
2012 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07002013 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07002014 // This adjustment accounts for buffering after app processor.
2015 // It is based on estimated DSP latency per use case, rather than exact.
2016 signed_frames -=
2017 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
2018
Eric Laurent949a0892013-09-20 09:20:13 -07002019 // It would be unusual for this value to be negative, but check just in case ...
2020 if (signed_frames >= 0) {
2021 *frames = signed_frames;
2022 ret = 0;
2023 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002024 }
2025 }
2026 }
2027
2028 pthread_mutex_unlock(&out->lock);
2029
2030 return ret;
2031}
2032
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002033static int out_set_callback(struct audio_stream_out *stream,
2034 stream_callback_t callback, void *cookie)
2035{
2036 struct stream_out *out = (struct stream_out *)stream;
2037
2038 ALOGV("%s", __func__);
2039 pthread_mutex_lock(&out->lock);
2040 out->offload_callback = callback;
2041 out->offload_cookie = cookie;
2042 pthread_mutex_unlock(&out->lock);
2043 return 0;
2044}
2045
2046static int out_pause(struct audio_stream_out* stream)
2047{
2048 struct stream_out *out = (struct stream_out *)stream;
2049 int status = -ENOSYS;
2050 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002051 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002052 pthread_mutex_lock(&out->lock);
2053 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302054 struct audio_device *adev = out->dev;
2055 int snd_scard_state = get_snd_card_state(adev);
2056
2057 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2058 status = compress_pause(out->compr);
2059
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002060 out->offload_state = OFFLOAD_STATE_PAUSED;
2061 }
2062 pthread_mutex_unlock(&out->lock);
2063 }
2064 return status;
2065}
2066
2067static int out_resume(struct audio_stream_out* stream)
2068{
2069 struct stream_out *out = (struct stream_out *)stream;
2070 int status = -ENOSYS;
2071 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002072 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002073 status = 0;
2074 pthread_mutex_lock(&out->lock);
2075 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302076 struct audio_device *adev = out->dev;
2077 int snd_scard_state = get_snd_card_state(adev);
2078
2079 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2080 status = compress_resume(out->compr);
2081
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002082 out->offload_state = OFFLOAD_STATE_PLAYING;
2083 }
2084 pthread_mutex_unlock(&out->lock);
2085 }
2086 return status;
2087}
2088
2089static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2090{
2091 struct stream_out *out = (struct stream_out *)stream;
2092 int status = -ENOSYS;
2093 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002094 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002095 pthread_mutex_lock(&out->lock);
2096 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2097 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2098 else
2099 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2100 pthread_mutex_unlock(&out->lock);
2101 }
2102 return status;
2103}
2104
2105static int out_flush(struct audio_stream_out* stream)
2106{
2107 struct stream_out *out = (struct stream_out *)stream;
2108 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002109 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002110 pthread_mutex_lock(&out->lock);
2111 stop_compressed_output_l(out);
2112 pthread_mutex_unlock(&out->lock);
2113 return 0;
2114 }
2115 return -ENOSYS;
2116}
2117
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002118/** audio_stream_in implementation **/
2119static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2120{
2121 struct stream_in *in = (struct stream_in *)stream;
2122
2123 return in->config.rate;
2124}
2125
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302126static int in_set_sample_rate(struct audio_stream *stream __unused,
2127 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002128{
2129 return -ENOSYS;
2130}
2131
2132static size_t in_get_buffer_size(const struct audio_stream *stream)
2133{
2134 struct stream_in *in = (struct stream_in *)stream;
2135
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002136 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
2137 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07002138 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
2139 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002140
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302141 return in->config.period_size *
2142 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002143}
2144
2145static uint32_t in_get_channels(const struct audio_stream *stream)
2146{
2147 struct stream_in *in = (struct stream_in *)stream;
2148
2149 return in->channel_mask;
2150}
2151
2152static audio_format_t in_get_format(const struct audio_stream *stream)
2153{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002154 struct stream_in *in = (struct stream_in *)stream;
2155
2156 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002157}
2158
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302159static int in_set_format(struct audio_stream *stream __unused,
2160 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002161{
2162 return -ENOSYS;
2163}
2164
2165static int in_standby(struct audio_stream *stream)
2166{
2167 struct stream_in *in = (struct stream_in *)stream;
2168 struct audio_device *adev = in->dev;
2169 int status = 0;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302170 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2171 stream, in->usecase, use_case_table[in->usecase]);
2172
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002173
2174 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2175 /* Ignore standby in case of voip call because the voip input
2176 * stream is closed in adev_close_input_stream()
2177 */
2178 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2179 return status;
2180 }
2181
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002182 pthread_mutex_lock(&in->lock);
2183 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002184 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002185 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002186 if (in->pcm) {
2187 pcm_close(in->pcm);
2188 in->pcm = NULL;
2189 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002190 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002191 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002192 }
2193 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002194 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002195 return status;
2196}
2197
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302198static int in_dump(const struct audio_stream *stream __unused,
2199 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002200{
2201 return 0;
2202}
2203
2204static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2205{
2206 struct stream_in *in = (struct stream_in *)stream;
2207 struct audio_device *adev = in->dev;
2208 struct str_parms *parms;
2209 char *str;
2210 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002211 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002212
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302213 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002214 parms = str_parms_create_str(kvpairs);
2215
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002216 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002217 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002218
2219 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2220 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002221 val = atoi(value);
2222 /* no audio source uses val == 0 */
2223 if ((in->source != val) && (val != 0)) {
2224 in->source = val;
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002225 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2226 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2227 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2228 (in->config.rate == 8000 || in->config.rate == 16000) &&
2229 (popcount(in->channel_mask) == 1)) {
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08002230 err = voice_extn_compress_voip_open_input_stream(in);
2231 if (err != 0) {
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002232 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08002233 __func__, err);
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002234 }
2235 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002236 }
2237 }
2238
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002239 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2240 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002241 val = atoi(value);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302242 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002243 in->device = val;
2244 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002245 if (!in->standby)
2246 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002247 }
2248 }
2249
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002250done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002251 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002252 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002253
2254 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002255 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002256 return ret;
2257}
2258
2259static char* in_get_parameters(const struct audio_stream *stream,
2260 const char *keys)
2261{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002262 struct stream_in *in = (struct stream_in *)stream;
2263 struct str_parms *query = str_parms_create_str(keys);
2264 char *str;
2265 char value[256];
2266 struct str_parms *reply = str_parms_create();
2267 ALOGV("%s: enter: keys - %s", __func__, keys);
2268
2269 voice_extn_in_get_parameters(in, query, reply);
2270
2271 str = str_parms_to_str(reply);
2272 str_parms_destroy(query);
2273 str_parms_destroy(reply);
2274
2275 ALOGV("%s: exit: returns - %s", __func__, str);
2276 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002277}
2278
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302279static int in_set_gain(struct audio_stream_in *stream __unused,
2280 float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002281{
2282 return 0;
2283}
2284
2285static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2286 size_t bytes)
2287{
2288 struct stream_in *in = (struct stream_in *)stream;
2289 struct audio_device *adev = in->dev;
2290 int i, ret = -1;
Naresh Tannirucef332d2014-06-04 18:17:56 +05302291 int snd_scard_state = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002292
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002293 pthread_mutex_lock(&in->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302294
2295 if (in->pcm) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302296 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302297 ALOGD(" %s: sound card is not active/SSR state", __func__);
2298 ret= -ENETRESET;
2299 goto exit;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302300 }
2301 }
2302
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002303 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002304 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002305 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2306 ret = voice_extn_compress_voip_start_input_stream(in);
2307 else
2308 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002309 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002310 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002311 goto exit;
2312 }
2313 in->standby = 0;
2314 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002315
2316 if (in->pcm) {
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302317 if (audio_extn_ssr_get_enabled() &&
2318 audio_channel_count_from_in_mask(in->channel_mask) == 6)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002319 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002320 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2321 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302322 else if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY)
2323 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002324 else
2325 ret = pcm_read(in->pcm, buffer, bytes);
Dhanalakshmi Siddani834b22f2014-08-20 12:28:34 +05302326 if (ret < 0)
2327 ret = -errno;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002328 }
2329
2330 /*
2331 * Instead of writing zeroes here, we could trust the hardware
2332 * to always provide zeroes when muted.
2333 */
Pavan Chikkala6c183fd2014-12-04 10:48:28 +05302334 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in) &&
2335 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002336 memset(buffer, 0, bytes);
2337
2338exit:
Dhanalakshmi Siddani4fe3e512014-05-26 18:03:42 +05302339 /* ToDo: There may be a corner case when SSR happens back to back during
2340 start/stop. Need to post different error to handle that. */
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302341 if (-ENETRESET == ret) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302342 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302343 memset(buffer, 0, bytes);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302344 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002345 pthread_mutex_unlock(&in->lock);
2346
2347 if (ret != 0) {
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302348 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302349 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302350 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302351 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Gutta369dd682014-06-25 20:38:03 +05302352 in->standby = true;
2353 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002354 in_standby(&in->stream.common);
2355 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302356 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
2357 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002358 }
2359 return bytes;
2360}
2361
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302362static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002363{
2364 return 0;
2365}
2366
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002367static int add_remove_audio_effect(const struct audio_stream *stream,
2368 effect_handle_t effect,
2369 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002370{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002371 struct stream_in *in = (struct stream_in *)stream;
2372 int status = 0;
2373 effect_descriptor_t desc;
2374
2375 status = (*effect)->get_descriptor(effect, &desc);
2376 if (status != 0)
2377 return status;
2378
2379 pthread_mutex_lock(&in->lock);
2380 pthread_mutex_lock(&in->dev->lock);
2381 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2382 in->enable_aec != enable &&
2383 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2384 in->enable_aec = enable;
2385 if (!in->standby)
2386 select_devices(in->dev, in->usecase);
2387 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002388 if (in->enable_ns != enable &&
2389 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2390 in->enable_ns = enable;
2391 if (!in->standby)
2392 select_devices(in->dev, in->usecase);
2393 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002394 pthread_mutex_unlock(&in->dev->lock);
2395 pthread_mutex_unlock(&in->lock);
2396
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002397 return 0;
2398}
2399
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002400static int in_add_audio_effect(const struct audio_stream *stream,
2401 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002402{
Eric Laurent994a6932013-07-17 11:51:42 -07002403 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002404 return add_remove_audio_effect(stream, effect, true);
2405}
2406
2407static int in_remove_audio_effect(const struct audio_stream *stream,
2408 effect_handle_t effect)
2409{
Eric Laurent994a6932013-07-17 11:51:42 -07002410 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002411 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002412}
2413
2414static int adev_open_output_stream(struct audio_hw_device *dev,
2415 audio_io_handle_t handle,
2416 audio_devices_t devices,
2417 audio_output_flags_t flags,
2418 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302419 struct audio_stream_out **stream_out,
2420 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002421{
2422 struct audio_device *adev = (struct audio_device *)dev;
2423 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002424 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002425
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002426 *stream_out = NULL;
Naresh Tannirucef332d2014-06-04 18:17:56 +05302427
2428 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2429 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2430 ALOGE(" sound card is not active rejecting compress output open request");
2431 return -EINVAL;
2432 }
2433
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002434 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2435
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302436 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2437 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2438 devices, flags, &out->stream);
2439
2440
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002441 if (!out) {
2442 return -ENOMEM;
2443 }
2444
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002445 if (devices == AUDIO_DEVICE_NONE)
2446 devices = AUDIO_DEVICE_OUT_SPEAKER;
2447
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002448 out->flags = flags;
2449 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002450 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002451 out->format = config->format;
2452 out->sample_rate = config->sample_rate;
2453 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2454 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002455 out->handle = handle;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002456 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002457
2458 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002459 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
Mingming Yinee733602014-04-03 17:47:22 -07002460 (
2461#ifdef AFE_PROXY_ENABLED
2462 out->devices & AUDIO_DEVICE_OUT_PROXY ||
2463#endif
2464 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002465
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002466 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002467 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2468 ret = read_hdmi_channel_masks(out);
2469
Mingming Yinee733602014-04-03 17:47:22 -07002470#ifdef AFE_PROXY_ENABLED
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002471 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2472 ret = audio_extn_read_afe_proxy_channel_masks(out);
Mingming Yinee733602014-04-03 17:47:22 -07002473#endif
2474
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002475 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002476 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002477 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002478
2479 if (config->sample_rate == 0)
2480 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2481 if (config->channel_mask == 0)
2482 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2483
2484 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002485 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002486 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2487 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002488 out->config.rate = config->sample_rate;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302489 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002490 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Mingming Yinee733602014-04-03 17:47:22 -07002491#ifdef COMPRESS_VOIP_ENABLED
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002492 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2493 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002494 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002495 ret = voice_extn_compress_voip_open_output_stream(out);
2496 if (ret != 0) {
2497 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2498 __func__, ret);
2499 goto error_open;
2500 }
Mingming Yinee733602014-04-03 17:47:22 -07002501#endif
Steve Kondikb045f472014-11-12 23:24:07 -08002502#ifdef LOW_LATENCY_PRIMARY
2503 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2504 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2505 out->config = pcm_config_deep_buffer;
2506 out->sample_rate = out->config.rate;
2507#endif
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002508 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2509 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2510 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2511 ALOGE("%s: Unsupported Offload information", __func__);
2512 ret = -EINVAL;
2513 goto error_open;
2514 }
Mingming Yin90310102013-11-13 16:57:00 -08002515 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002516 !audio_extn_is_dolby_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002517 ALOGE("%s: Unsupported audio format", __func__);
2518 ret = -EINVAL;
2519 goto error_open;
2520 }
2521
2522 out->compr_config.codec = (struct snd_codec *)
2523 calloc(1, sizeof(struct snd_codec));
2524
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002525 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002526 if (config->offload_info.channel_mask)
2527 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002528 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002529 out->channel_mask = config->channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002530 config->offload_info.channel_mask = config->channel_mask;
2531 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002532 out->format = config->offload_info.format;
2533 out->sample_rate = config->offload_info.sample_rate;
2534
2535 out->stream.set_callback = out_set_callback;
2536 out->stream.pause = out_pause;
2537 out->stream.resume = out_resume;
2538 out->stream.drain = out_drain;
2539 out->stream.flush = out_flush;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002540 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002541
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002542 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002543 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002544 audio_extn_dolby_get_snd_codec_id(adev, out,
2545 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002546 else
2547 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002548 get_snd_codec_id(config->offload_info.format);
Mingming Yinee733602014-04-03 17:47:22 -07002549#ifdef EXTN_OFFLOAD_ENABLED
ApurupaPattapu0c566872014-01-10 14:46:02 -08002550 if (audio_is_offload_pcm(config->offload_info.format)) {
2551 out->compr_config.fragment_size =
2552 platform_get_pcm_offload_buffer_size(&config->offload_info);
Mingming Yinee733602014-04-03 17:47:22 -07002553 } else
2554#endif
2555 {
ApurupaPattapu0c566872014-01-10 14:46:02 -08002556 out->compr_config.fragment_size =
2557 platform_get_compress_offload_buffer_size(&config->offload_info);
2558 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002559 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2560 out->compr_config.codec->sample_rate =
2561 compress_get_alsa_rate(config->offload_info.sample_rate);
2562 out->compr_config.codec->bit_rate =
2563 config->offload_info.bit_rate;
2564 out->compr_config.codec->ch_in =
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302565 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002566 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002567 out->bit_width = config->offload_info.bit_width;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002568
Mingming Yinee733602014-04-03 17:47:22 -07002569#ifdef EXTN_OFFLOAD_ENABLED
ApurupaPattapu0c566872014-01-10 14:46:02 -08002570 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2571 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002572 if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
ApurupaPattapu0c566872014-01-10 14:46:02 -08002573 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
Mingming Yinee733602014-04-03 17:47:22 -07002574#endif
ApurupaPattapu0c566872014-01-10 14:46:02 -08002575
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002576 if (config->offload_info.bit_width == 24) {
2577 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2578 }
2579
2580 if (out->bit_width == 24 && !platform_check_24_bit_support()) {
2581 ALOGW("24 bit support is not enabled, using 16 bit backend");
2582 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
2583 }
2584
Steve Kondik6bedcdf2014-07-21 11:48:30 -07002585#ifdef FLAC_OFFLOAD_ENABLED
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002586 out->compr_config.codec->options.flac_dec.sample_size = config->offload_info.bit_width;
Steve Kondik6bedcdf2014-07-21 11:48:30 -07002587#endif
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08002588
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002589 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2590 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002591
2592 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002593 out->offload_state = OFFLOAD_STATE_IDLE;
2594 out->playback_started = 0;
2595
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002596 create_offload_callback_thread(out);
2597 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2598 __func__, config->offload_info.version,
2599 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002600 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -08002601 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002602
Mingming Yinee733602014-04-03 17:47:22 -07002603#ifdef INCALL_MUSIC_ENABLED
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002604 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2605 ret = voice_check_and_set_incall_music_usecase(adev, out);
2606 if (ret != 0) {
2607 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2608 __func__, ret);
2609 goto error_open;
2610 }
Mingming Yinee733602014-04-03 17:47:22 -07002611#endif
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302612 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2613 if (config->sample_rate == 0)
2614 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2615 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2616 config->sample_rate != 8000) {
2617 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2618 ret = -EINVAL;
2619 goto error_open;
2620 }
2621 out->sample_rate = config->sample_rate;
2622 out->config.rate = config->sample_rate;
2623 if (config->format == AUDIO_FORMAT_DEFAULT)
2624 config->format = AUDIO_FORMAT_PCM_16_BIT;
2625 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2626 config->format = AUDIO_FORMAT_PCM_16_BIT;
2627 ret = -EINVAL;
2628 goto error_open;
2629 }
2630 out->format = config->format;
2631 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2632 out->config = pcm_config_afe_proxy_playback;
2633 adev->voice_tx_output = out;
Steve Kondikb045f472014-11-12 23:24:07 -08002634#ifndef LOW_LATENCY_PRIMARY
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002635 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002636 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2637 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002638 out->sample_rate = out->config.rate;
Steve Kondikb045f472014-11-12 23:24:07 -08002639#endif
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002640 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002641 /* primary path is the default path selected if no other outputs are available/suitable */
2642 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Steve Kondikb045f472014-11-12 23:24:07 -08002643#ifdef LOW_LATENCY_PRIMARY
2644 out->config = pcm_config_low_latency;
2645#else
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002646 out->config = pcm_config_deep_buffer;
Steve Kondikb045f472014-11-12 23:24:07 -08002647#endif
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002648 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002649 }
2650
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002651 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2652 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2653 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002654 if(adev->primary_output == NULL)
2655 adev->primary_output = out;
2656 else {
2657 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002658 ret = -EEXIST;
2659 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002660 }
2661 }
2662
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002663 /* Check if this usecase is already existing */
2664 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella0d06c8e2014-04-17 20:00:41 -07002665 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2666 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002667 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002668 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002669 ret = -EEXIST;
2670 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002671 }
2672 pthread_mutex_unlock(&adev->lock);
2673
2674 out->stream.common.get_sample_rate = out_get_sample_rate;
2675 out->stream.common.set_sample_rate = out_set_sample_rate;
2676 out->stream.common.get_buffer_size = out_get_buffer_size;
2677 out->stream.common.get_channels = out_get_channels;
2678 out->stream.common.get_format = out_get_format;
2679 out->stream.common.set_format = out_set_format;
2680 out->stream.common.standby = out_standby;
2681 out->stream.common.dump = out_dump;
2682 out->stream.common.set_parameters = out_set_parameters;
2683 out->stream.common.get_parameters = out_get_parameters;
2684 out->stream.common.add_audio_effect = out_add_audio_effect;
2685 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2686 out->stream.get_latency = out_get_latency;
2687 out->stream.set_volume = out_set_volume;
2688 out->stream.write = out_write;
2689 out->stream.get_render_position = out_get_render_position;
2690 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002691 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002692
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002693 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002694 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002695 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002696
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302697 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2698 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2699
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002700 config->format = out->stream.common.get_format(&out->stream.common);
2701 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2702 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2703
2704 *stream_out = &out->stream;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302705 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2706 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002707 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002708 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002709
2710error_open:
2711 free(out);
2712 *stream_out = NULL;
2713 ALOGD("%s: exit: ret %d", __func__, ret);
2714 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002715}
2716
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302717static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002718 struct audio_stream_out *stream)
2719{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002720 struct stream_out *out = (struct stream_out *)stream;
2721 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002722 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002723
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302724 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2725
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002726 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302727 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002728 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05302729 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002730 if(ret != 0)
2731 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2732 __func__, ret);
2733 }
2734 else
2735 out_standby(&stream->common);
2736
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002737 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002738 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07002739 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002740 if (out->compr_config.codec != NULL)
2741 free(out->compr_config.codec);
2742 }
2743 pthread_cond_destroy(&out->cond);
2744 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002745 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002746 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002747}
2748
2749static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2750{
2751 struct audio_device *adev = (struct audio_device *)dev;
2752 struct str_parms *parms;
2753 char *str;
2754 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002755 int val;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302756 int ret;
2757 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002758
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002759 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002760 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002761
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302762 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
2763 if (ret >= 0) {
2764 char *snd_card_status = value+2;
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302765 if (strstr(snd_card_status, "OFFLINE")) {
Naresh Tannirucef332d2014-06-04 18:17:56 +05302766 struct listnode *node;
2767 struct audio_usecase *usecase;
2768
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302769 ALOGD("Received sound card OFFLINE status");
Naresh Tannirucef332d2014-06-04 18:17:56 +05302770 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2771
2772 pthread_mutex_lock(&adev->lock);
2773 //close compress session on OFFLINE status
2774 usecase = get_usecase_from_list(adev,USECASE_AUDIO_PLAYBACK_OFFLOAD);
2775 if (usecase && usecase->stream.out) {
2776 ALOGD(" %s closing compress session on OFFLINE state", __func__);
2777
2778 struct stream_out *out = usecase->stream.out;
2779
2780 pthread_mutex_unlock(&adev->lock);
2781 out_standby(&out->stream.common);
2782 } else
2783 pthread_mutex_unlock(&adev->lock);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302784 } else if (strstr(snd_card_status, "ONLINE")) {
2785 ALOGD("Received sound card ONLINE status");
Naresh Tannirucef332d2014-06-04 18:17:56 +05302786 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302787 }
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05302788 }
2789
2790 pthread_mutex_lock(&adev->lock);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302791 status = voice_set_parameters(adev, parms);
2792 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002793 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002794
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302795 status = platform_set_parameters(adev->platform, parms);
2796 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002797 goto done;
2798
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302799 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2800 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002801 /* When set to false, HAL should disable EC and NS
2802 * But it is currently not supported.
2803 */
2804 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2805 adev->bluetooth_nrec = true;
2806 else
2807 adev->bluetooth_nrec = false;
2808 }
2809
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302810 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2811 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002812 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2813 adev->screen_off = false;
2814 else
2815 adev->screen_off = true;
2816 }
2817
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302818 ret = str_parms_get_int(parms, "rotation", &val);
2819 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002820 bool reverse_speakers = false;
2821 switch(val) {
2822 // FIXME: note that the code below assumes that the speakers are in the correct placement
2823 // relative to the user when the device is rotated 90deg from its default rotation. This
2824 // assumption is device-specific, not platform-specific like this code.
2825 case 270:
2826 reverse_speakers = true;
2827 break;
2828 case 0:
2829 case 90:
2830 case 180:
2831 break;
2832 default:
2833 ALOGE("%s: unexpected rotation of %d", __func__, val);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302834 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002835 }
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302836 if (status == 0) {
2837 if (adev->speaker_lr_swap != reverse_speakers) {
2838 adev->speaker_lr_swap = reverse_speakers;
2839 // only update the selected device if there is active pcm playback
2840 struct audio_usecase *usecase;
2841 struct listnode *node;
2842 list_for_each(node, &adev->usecase_list) {
2843 usecase = node_to_item(node, struct audio_usecase, list);
2844 if (usecase->type == PCM_PLAYBACK) {
2845 select_devices(adev, usecase->id);
2846 break;
2847 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002848 }
2849 }
2850 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002851 }
2852
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002853 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002854
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002855done:
2856 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002857 pthread_mutex_unlock(&adev->lock);
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302858 ALOGV("%s: exit with code(%d)", __func__, status);
2859 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002860}
2861
2862static char* adev_get_parameters(const struct audio_hw_device *dev,
2863 const char *keys)
2864{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002865 struct audio_device *adev = (struct audio_device *)dev;
2866 struct str_parms *reply = str_parms_create();
2867 struct str_parms *query = str_parms_create_str(keys);
2868 char *str;
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05302869 char value[256] = {0};
2870 int ret = 0;
2871
2872 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_SND_CARD_STATUS, value,
2873 sizeof(value));
2874 if (ret >=0) {
2875 int val = 1;
2876 pthread_mutex_lock(&adev->snd_card_status.lock);
2877 if (SND_CARD_STATE_OFFLINE == adev->snd_card_status.state)
2878 val = 0;
2879 pthread_mutex_unlock(&adev->snd_card_status.lock);
2880 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_SND_CARD_STATUS, val);
2881 goto exit;
2882 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002883
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002884 pthread_mutex_lock(&adev->lock);
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05302885
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002886 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002887 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002888 platform_get_parameters(adev->platform, query, reply);
Naresh Tannirucef332d2014-06-04 18:17:56 +05302889 pthread_mutex_unlock(&adev->lock);
2890
Naresh Tanniruc6ca6352014-06-20 02:54:48 +05302891exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002892 str = str_parms_to_str(reply);
2893 str_parms_destroy(query);
2894 str_parms_destroy(reply);
2895
2896 ALOGV("%s: exit: returns - %s", __func__, str);
2897 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002898}
2899
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302900static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002901{
2902 return 0;
2903}
2904
2905static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2906{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002907 int ret;
2908 struct audio_device *adev = (struct audio_device *)dev;
2909 pthread_mutex_lock(&adev->lock);
2910 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002911 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002912 pthread_mutex_unlock(&adev->lock);
2913 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002914}
2915
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302916static int adev_set_master_volume(struct audio_hw_device *dev __unused,
2917 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002918{
2919 return -ENOSYS;
2920}
2921
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302922static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2923 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002924{
2925 return -ENOSYS;
2926}
2927
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302928static int adev_set_master_mute(struct audio_hw_device *dev __unused,
2929 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002930{
2931 return -ENOSYS;
2932}
2933
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302934static int adev_get_master_mute(struct audio_hw_device *dev __unused,
2935 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002936{
2937 return -ENOSYS;
2938}
2939
2940static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2941{
2942 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002943 pthread_mutex_lock(&adev->lock);
2944 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002945 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002946 adev->mode = mode;
2947 }
2948 pthread_mutex_unlock(&adev->lock);
2949 return 0;
2950}
2951
2952static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2953{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002954 int ret;
2955
2956 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002957 ALOGD("%s state %d\n", __func__, state);
Shreyas Nagasandra Chandrasekhar9781c6c2014-12-01 05:49:35 -08002958 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002959 pthread_mutex_unlock(&adev->lock);
2960
2961 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002962}
2963
2964static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2965{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002966 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002967 return 0;
2968}
2969
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302970static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002971 const struct audio_config *config)
2972{
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302973 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002974
2975 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2976}
2977
2978static int adev_open_input_stream(struct audio_hw_device *dev,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302979 audio_io_handle_t handle __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002980 audio_devices_t devices,
2981 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302982 struct audio_stream_in **stream_in,
2983 audio_input_flags_t flags __unused,
2984 const char *address __unused,
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07002985 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002986{
2987 struct audio_device *adev = (struct audio_device *)dev;
2988 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002989 int ret = 0, buffer_size, frame_size;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05302990 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002991
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302992
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002993 *stream_in = NULL;
2994 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2995 return -EINVAL;
2996
2997 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302998 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07002999 stream_handle(%p) io_handle(%d) source(%d)",__func__, config->sample_rate, config->channel_mask,
3000 devices, &in->stream, handle, source);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003001
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07003002 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
3003
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003004 in->stream.common.get_sample_rate = in_get_sample_rate;
3005 in->stream.common.set_sample_rate = in_set_sample_rate;
3006 in->stream.common.get_buffer_size = in_get_buffer_size;
3007 in->stream.common.get_channels = in_get_channels;
3008 in->stream.common.get_format = in_get_format;
3009 in->stream.common.set_format = in_set_format;
3010 in->stream.common.standby = in_standby;
3011 in->stream.common.dump = in_dump;
3012 in->stream.common.set_parameters = in_set_parameters;
3013 in->stream.common.get_parameters = in_get_parameters;
3014 in->stream.common.add_audio_effect = in_add_audio_effect;
3015 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3016 in->stream.set_gain = in_set_gain;
3017 in->stream.read = in_read;
3018 in->stream.get_input_frames_lost = in_get_input_frames_lost;
3019
3020 in->device = devices;
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003021 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003022 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003023 in->standby = 1;
3024 in->channel_mask = config->channel_mask;
3025
3026 /* Update config params with the requested sample rate and channels */
3027 in->usecase = USECASE_AUDIO_RECORD;
3028 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003029 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003030 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003031
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05303032 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
Pavan Chikkala3c0036d2014-12-17 16:45:10 +05303033 if (adev->mode != AUDIO_MODE_IN_CALL) {
3034 ret = -EINVAL;
3035 goto err_open;
Pavan Chikkala3ec42ef2014-11-21 20:57:48 +05303036 }
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05303037 if (config->sample_rate == 0)
3038 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3039 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
3040 config->sample_rate != 8000) {
3041 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3042 ret = -EINVAL;
3043 goto err_open;
3044 }
3045 if (config->format == AUDIO_FORMAT_DEFAULT)
3046 config->format = AUDIO_FORMAT_PCM_16_BIT;
3047 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
3048 config->format = AUDIO_FORMAT_PCM_16_BIT;
3049 ret = -EINVAL;
3050 goto err_open;
3051 }
3052 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
3053 in->config = pcm_config_afe_proxy_record;
3054 in->config.channels = channel_count;
3055 in->config.rate = config->sample_rate;
3056 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003057 if(audio_extn_ssr_get_enabled()) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303058 if(audio_extn_ssr_init(in)) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003059 ALOGE("%s: audio_extn_ssr_init failed", __func__);
3060 ret = -EINVAL;
3061 goto err_open;
3062 }
3063 } else {
Mingming Yindaf9c542014-09-16 17:41:33 -07003064 ALOGW("%s: surround sound recording is not supported", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003065 }
Mingming Yine62d7842013-10-25 16:26:03 -07003066 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08003067 audio_extn_compr_cap_format_supported(config->format) &&
3068 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303069 audio_extn_compr_cap_init(in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003070 } else {
3071 in->config.channels = channel_count;
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303072 frame_size = audio_stream_in_frame_size(&in->stream);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003073 buffer_size = get_input_buffer_size(config->sample_rate,
3074 config->format,
3075 channel_count);
3076 in->config.period_size = buffer_size / frame_size;
Vidyakumar Athota7e6ba2e2015-03-31 21:53:21 -07003077 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
3078 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
3079 (voice_extn_compress_voip_is_format_supported(in->format)) &&
3080 (in->config.rate == 8000 || in->config.rate == 16000) &&
3081 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
3082 voice_extn_compress_voip_open_input_stream(in);
3083 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003084 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003085
3086 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07003087 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003088 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003089
3090err_open:
3091 free(in);
3092 *stream_in = NULL;
3093 return ret;
3094}
3095
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303096static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003097 struct audio_stream_in *stream)
3098{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003099 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003100 struct stream_in *in = (struct stream_in *)stream;
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303101 struct audio_device *adev = in->dev;
3102
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05303103 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003104
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003105 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303106 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003107 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta593b7c12014-08-11 18:20:49 +05303108 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003109 if (ret != 0)
3110 ALOGE("%s: Compress voip input cannot be closed, error:%d",
3111 __func__, ret);
3112 } else
3113 in_standby(&stream->common);
3114
Divya Narayanan Poojary69236ba2014-09-18 11:57:57 +05303115 if (audio_extn_ssr_get_enabled() &&
3116 (audio_channel_count_from_in_mask(in->channel_mask) == 6)) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003117 audio_extn_ssr_deinit();
3118 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003119 free(stream);
3120
Mingming Yine62d7842013-10-25 16:26:03 -07003121 if(audio_extn_compr_cap_enabled() &&
3122 audio_extn_compr_cap_format_supported(in->config.format))
3123 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003124 return;
3125}
3126
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05303127static int adev_dump(const audio_hw_device_t *device __unused,
3128 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003129{
3130 return 0;
3131}
3132
3133static int adev_close(hw_device_t *device)
3134{
3135 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07003136
3137 if (!adev)
3138 return 0;
3139
3140 pthread_mutex_lock(&adev_init_lock);
3141
3142 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08003143 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07003144 audio_route_free(adev->audio_route);
3145 free(adev->snd_dev_ref_cnt);
3146 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07003147 free(device);
3148 adev = NULL;
3149 }
3150 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003151 return 0;
3152}
3153
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003154static int adev_open(const hw_module_t *module, const char *name,
3155 hw_device_t **device)
3156{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003157 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003158
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003159 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003160 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3161
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003162 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07003163 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003164 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07003165 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003166 ALOGD("%s: returning existing instance of adev", __func__);
3167 ALOGD("%s: exit", __func__);
3168 pthread_mutex_unlock(&adev_init_lock);
3169 return 0;
3170 }
3171
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003172 adev = calloc(1, sizeof(struct audio_device));
3173
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07003174 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3175
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003176 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3177 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3178 adev->device.common.module = (struct hw_module_t *)module;
3179 adev->device.common.close = adev_close;
3180
3181 adev->device.init_check = adev_init_check;
3182 adev->device.set_voice_volume = adev_set_voice_volume;
3183 adev->device.set_master_volume = adev_set_master_volume;
3184 adev->device.get_master_volume = adev_get_master_volume;
3185 adev->device.set_master_mute = adev_set_master_mute;
3186 adev->device.get_master_mute = adev_get_master_mute;
3187 adev->device.set_mode = adev_set_mode;
3188 adev->device.set_mic_mute = adev_set_mic_mute;
3189 adev->device.get_mic_mute = adev_get_mic_mute;
3190 adev->device.set_parameters = adev_set_parameters;
3191 adev->device.get_parameters = adev_get_parameters;
3192 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3193 adev->device.open_output_stream = adev_open_output_stream;
3194 adev->device.close_output_stream = adev_close_output_stream;
3195 adev->device.open_input_stream = adev_open_input_stream;
3196 adev->device.close_input_stream = adev_close_input_stream;
3197 adev->device.dump = adev_dump;
3198
3199 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003200 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003201 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003202 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003203 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003204 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003205 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003206 /* adev->cur_hdmi_channels = 0; by calloc() */
ApurupaPattapu9d6b4362014-02-27 10:24:11 -08003207 adev->cur_codec_backend_samplerate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
3208 adev->cur_codec_backend_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Eric Laurentb23d5282013-05-14 15:27:20 -07003209 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003210 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003211 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly9b7e96b2014-02-14 14:45:49 -08003212 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy148144e2014-04-01 21:03:10 -07003213 adev->offload_usecases_state = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003214
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303215 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
3216 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
3217
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003218 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003219 adev->platform = platform_init(adev);
3220 if (!adev->platform) {
3221 free(adev->snd_dev_ref_cnt);
3222 free(adev);
3223 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3224 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08003225 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003226 return -EINVAL;
3227 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003228
Naresh Tanniru2d19ab42014-05-11 19:56:25 +05303229 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
3230
Eric Laurentc4aef752013-09-12 17:45:53 -07003231 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
3232 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3233 if (adev->visualizer_lib == NULL) {
3234 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3235 } else {
3236 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3237 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003238 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003239 "visualizer_hal_start_output");
3240 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003241 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003242 "visualizer_hal_stop_output");
3243 }
3244 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003245 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07003246
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003247 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
3248 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3249 if (adev->offload_effects_lib == NULL) {
3250 ALOGE("%s: DLOPEN failed for %s", __func__,
3251 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3252 } else {
3253 ALOGV("%s: DLOPEN successful for %s", __func__,
3254 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3255 adev->offload_effects_start_output =
3256 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3257 "offload_effects_bundle_hal_start_output");
3258 adev->offload_effects_stop_output =
3259 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3260 "offload_effects_bundle_hal_stop_output");
3261 }
3262 }
3263
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003264 *device = &adev->device.common;
3265
Kiran Kandi910e1862013-10-29 13:29:42 -07003266 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003267 pthread_mutex_unlock(&adev_init_lock);
3268
Eric Laurent994a6932013-07-17 11:51:42 -07003269 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003270 return 0;
3271}
3272
Mingming Yin90e2cd82014-06-06 17:11:23 -07003273int pcm_ioctl(struct pcm *pcm, int request, ...)
3274{
3275 va_list ap;
3276 void * arg;
3277 int pcm_fd = *(int*)pcm;
3278
3279 va_start(ap, request);
3280 arg = va_arg(ap, void *);
3281 va_end(ap);
3282
3283 return ioctl(pcm_fd, request, arg);
3284}
3285
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003286static struct hw_module_methods_t hal_module_methods = {
3287 .open = adev_open,
3288};
3289
3290struct audio_module HAL_MODULE_INFO_SYM = {
3291 .common = {
3292 .tag = HARDWARE_MODULE_TAG,
3293 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3294 .hal_api_version = HARDWARE_HAL_API_VERSION,
3295 .id = AUDIO_HARDWARE_MODULE_ID,
3296 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08003297 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003298 .methods = &hal_module_methods,
3299 },
3300};