blob: ca3098bd57b8455fb9cfebea98bb82ba273dc5f4 [file] [log] [blame]
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001/*
Alexy Josephb1379942016-01-29 15:49:38 -08002 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003 * Not a contribution.
4 *
Shiv Maliyappanahalli8911f282014-01-10 15:56:19 -08005 * Copyright (C) 2013 The Android Open Source Project
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006 *
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 "voice"
21/*#define LOG_NDEBUG 0*/
22#define LOG_NDDEBUG 0
23
24#include <errno.h>
Sharad Sangleb27354b2015-06-18 15:58:55 +053025#include <stdlib.h>
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070026#include <math.h>
27#include <cutils/log.h>
28#include <cutils/str_parms.h>
29
30#include "audio_hw.h"
31#include "voice.h"
32#include "voice_extn/voice_extn.h"
33#include "platform.h"
34#include "platform_api.h"
Kiran Kandi910e1862013-10-29 13:29:42 -070035#include "audio_extn.h"
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070036
37struct pcm_config pcm_config_voice_call = {
38 .channels = 1,
39 .rate = 8000,
40 .period_size = 160,
41 .period_count = 2,
42 .format = PCM_FORMAT_S16_LE,
43};
44
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070045static struct voice_session *voice_get_session_from_use_case(struct audio_device *adev,
46 audio_usecase_t usecase_id)
47{
48 struct voice_session *session = NULL;
49 int ret = 0;
50
51 ret = voice_extn_get_session_from_use_case(adev, usecase_id, &session);
52 if (ret == -ENOSYS) {
53 session = &adev->voice.session[VOICE_SESS_IDX];
54 }
55
56 return session;
57}
58
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070059static bool voice_is_sidetone_device(snd_device_t out_device,
60 char *mixer_path)
61{
62 bool is_sidetone_dev;
63
64 switch (out_device) {
65 case SND_DEVICE_OUT_VOICE_HANDSET:
66 is_sidetone_dev = true;
67 strlcpy(mixer_path, "sidetone-handset", MIXER_PATH_MAX_LENGTH);
68 break;
69 case SND_DEVICE_OUT_VOICE_HEADPHONES:
70 case SND_DEVICE_OUT_VOICE_ANC_HEADSET:
71 case SND_DEVICE_OUT_VOICE_ANC_FB_HEADSET:
72 is_sidetone_dev = true;
73 strlcpy(mixer_path, "sidetone-headphones", MIXER_PATH_MAX_LENGTH);
74 break;
Kuirong Wang1cad7142016-05-24 15:21:56 -070075 case SND_DEVICE_OUT_USB_HEADSET:
76 is_sidetone_dev = true;
77 break;
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070078 default:
79 is_sidetone_dev = false;
80 break;
81 }
82
83 return is_sidetone_dev;
84}
85
86void voice_set_sidetone(struct audio_device *adev,
87 snd_device_t out_snd_device, bool enable)
88{
89 char mixer_path[MIXER_PATH_MAX_LENGTH];
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070090 ALOGD("%s: %s, out_snd_device: %d\n",
91 __func__, (enable ? "enable" : "disable"),
92 out_snd_device);
Kuirong Wang1cad7142016-05-24 15:21:56 -070093 if (voice_is_sidetone_device(out_snd_device, mixer_path))
94 platform_set_sidetone(adev, out_snd_device, enable, mixer_path);
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070095 return;
96}
97
Vidyakumar Athotaea269c62016-10-31 09:05:59 -070098static bool voice_is_aanc_device(snd_device_t out_device,
99 char *mixer_path)
100{
101 bool is_aanc_dev;
102
103 switch (out_device) {
104 case SND_DEVICE_OUT_ANC_HANDSET:
105 is_aanc_dev = true;
106 strlcpy(mixer_path, "aanc-path", MIXER_PATH_MAX_LENGTH);
107 break;
108 default:
109 is_aanc_dev = false;
110 break;
111 }
112
113 return is_aanc_dev;
114}
115
116void voice_check_and_update_aanc_path(struct audio_device *adev,
117 snd_device_t out_snd_device,
118 bool enable)
119{
120 char mixer_path[MIXER_PATH_MAX_LENGTH];
121
122 ALOGV("%s: %s, out_snd_device: %d\n",
123 __func__, (enable ? "enable" : "disable"), out_snd_device);
124
125 if (voice_is_aanc_device(out_snd_device, mixer_path))
126 platform_update_aanc_path(adev, out_snd_device, enable, mixer_path);
127
128 return;
129}
130
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700131int voice_stop_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700132{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530133 int ret = 0;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700134 struct audio_usecase *uc_info;
135 struct voice_session *session = NULL;
136
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800137 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700138
139 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700140 if (!session) {
141 ALOGE("stop_call: couldn't find voice session");
142 return -EINVAL;
143 }
144
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700145 uc_info = get_usecase_from_list(adev, usecase_id);
146 if (uc_info == NULL) {
147 ALOGE("%s: Could not find the usecase (%d) in the list",
148 __func__, usecase_id);
149 return -EINVAL;
150 }
151
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700152 session->state.current = CALL_INACTIVE;
153
kunleiz0f7d0a92016-04-19 10:28:42 +0800154 if (!voice_is_call_state_active(adev))
155 adev->voice.in_call = false;
156
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700157 /* Disable sidetone only when no calls are active */
158 if (!voice_is_call_state_active(adev))
159 voice_set_sidetone(adev, uc_info->out_snd_device, false);
160
Vidyakumar Athotaea269c62016-10-31 09:05:59 -0700161 /* Disable aanc only when no calls are active */
162 if (!voice_is_call_state_active(adev))
163 voice_check_and_update_aanc_path(adev, uc_info->out_snd_device, false);
164
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800165 ret = platform_stop_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700166
167 /* 1. Close the PCM devices */
168 if (session->pcm_rx) {
169 pcm_close(session->pcm_rx);
170 session->pcm_rx = NULL;
171 }
172 if (session->pcm_tx) {
173 pcm_close(session->pcm_tx);
174 session->pcm_tx = NULL;
175 }
176
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700177 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700178 disable_audio_route(adev, uc_info);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700179
180 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700181 disable_snd_device(adev, uc_info->out_snd_device);
182 disable_snd_device(adev, uc_info->in_snd_device);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700183
184 list_remove(&uc_info->list);
185 free(uc_info);
186
187 ALOGD("%s: exit: status(%d)", __func__, ret);
188 return ret;
189}
190
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700191int voice_start_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700192{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530193 int ret = 0;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700194 struct audio_usecase *uc_info;
195 int pcm_dev_rx_id, pcm_dev_tx_id;
Helen Zeng6a16ad72014-02-23 22:04:44 -0800196 uint32_t sample_rate = 8000;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700197 struct voice_session *session = NULL;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700198 struct pcm_config voice_config = pcm_config_voice_call;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800199
200 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700201
202 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700203 if (!session) {
204 ALOGE("start_call: couldn't find voice session");
205 return -EINVAL;
206 }
207
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700208 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700209 if (!uc_info) {
210 ALOGE("start_call: couldn't allocate mem for audio_usecase");
211 return -ENOMEM;
212 }
213
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700214 uc_info->id = usecase_id;
215 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700216 uc_info->stream.out = adev->current_call_output ;
217 uc_info->devices = adev->current_call_output ->devices;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700218 uc_info->in_snd_device = SND_DEVICE_NONE;
219 uc_info->out_snd_device = SND_DEVICE_NONE;
220
221 list_add_tail(&adev->usecase_list, &uc_info->list);
222
223 select_devices(adev, usecase_id);
224
225 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
226 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
227
228 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
229 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
230 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
231 ret = -EIO;
232 goto error_start_voice;
233 }
Helen Zeng6a16ad72014-02-23 22:04:44 -0800234 ret = platform_get_sample_rate(adev->platform, &sample_rate);
235 if (ret < 0) {
236 ALOGE("platform_get_sample_rate error %d\n", ret);
237 } else {
238 voice_config.rate = sample_rate;
239 }
240 ALOGD("voice_config.rate %d\n", voice_config.rate);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700241
Vidyakumar Athotab000e0b2015-04-09 17:45:20 -0700242 voice_set_mic_mute(adev, adev->voice.mic_mute);
243
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700244 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
245 __func__, adev->snd_card, pcm_dev_tx_id);
246 session->pcm_tx = pcm_open(adev->snd_card,
247 pcm_dev_tx_id,
248 PCM_IN, &voice_config);
249 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
250 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
251 ret = -EIO;
252 goto error_start_voice;
253 }
254
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700255 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800256 __func__, adev->snd_card, pcm_dev_rx_id);
257 session->pcm_rx = pcm_open(adev->snd_card,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700258 pcm_dev_rx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700259 PCM_OUT, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700260 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
261 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
262 ret = -EIO;
263 goto error_start_voice;
264 }
265
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700266 pcm_start(session->pcm_tx);
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700267 pcm_start(session->pcm_rx);
268
Vidyakumar Athotaea269c62016-10-31 09:05:59 -0700269 /* Enable aanc only when no calls are active */
270 if (!voice_is_call_state_active(adev))
271 voice_check_and_update_aanc_path(adev, uc_info->out_snd_device, true);
272
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700273 /* Enable sidetone only when no calls are already active */
274 if (!voice_is_call_state_active(adev))
275 voice_set_sidetone(adev, uc_info->out_snd_device, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700276
Shruthi Krishnaace10852013-10-25 14:32:12 -0700277 voice_set_volume(adev, adev->voice.volume);
278
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800279 ret = platform_start_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700280 if (ret < 0) {
281 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
282 goto error_start_voice;
283 }
284
285 session->state.current = CALL_ACTIVE;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700286 goto done;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700287
288error_start_voice:
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700289 voice_stop_usecase(adev, usecase_id);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700290
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700291done:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700292 ALOGD("%s: exit: status(%d)", __func__, ret);
293 return ret;
294}
295
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700296bool voice_is_call_state_active(struct audio_device *adev)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700297{
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700298 bool call_state = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700299 int ret = 0;
300
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700301 ret = voice_extn_is_call_state_active(adev, &call_state);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700302 if (ret == -ENOSYS) {
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700303 call_state = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700304 }
305
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700306 return call_state;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700307}
308
Alexy Josephb1379942016-01-29 15:49:38 -0800309bool voice_is_in_call(const struct audio_device *adev)
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700310{
311 return adev->voice.in_call;
312}
313
Alexy Josephb1379942016-01-29 15:49:38 -0800314bool voice_is_in_call_rec_stream(const struct stream_in *in)
kunleizc5a639b2014-04-24 18:46:22 +0800315{
316 bool in_call_rec = false;
kunleizc5a639b2014-04-24 18:46:22 +0800317
Anish Kumar50ebcbf2014-12-09 04:01:39 +0530318 if (!in) {
319 ALOGE("%s: input stream is NULL", __func__);
320 return in_call_rec;
321 }
322
Narsinga Rao Chellad06b0982014-11-20 16:59:57 -0800323 if(in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
324 in->source == AUDIO_SOURCE_VOICE_UPLINK ||
325 in->source == AUDIO_SOURCE_VOICE_CALL) {
326 in_call_rec = true;
kunleizc5a639b2014-04-24 18:46:22 +0800327 }
328
329 return in_call_rec;
330}
331
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700332uint32_t voice_get_active_session_id(struct audio_device *adev)
333{
334 int ret = 0;
335 uint32_t session_id;
336
337 ret = voice_extn_get_active_session_id(adev, &session_id);
338 if (ret == -ENOSYS) {
339 session_id = VOICE_VSID;
340 }
341 return session_id;
342}
343
344int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800345 struct stream_in *in)
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700346{
347 int ret = 0;
348 uint32_t session_id;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800349 int rec_mode = INCALL_REC_NONE;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700350
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700351 if (voice_is_call_state_active(adev)) {
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700352 switch (in->source) {
353 case AUDIO_SOURCE_VOICE_UPLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800354 if (audio_extn_compr_cap_enabled() &&
355 audio_extn_compr_cap_format_supported(in->config.format)) {
356 in->usecase = USECASE_INCALL_REC_UPLINK_COMPRESS;
357 } else
358 in->usecase = USECASE_INCALL_REC_UPLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800359 rec_mode = INCALL_REC_UPLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700360 break;
361 case AUDIO_SOURCE_VOICE_DOWNLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800362 if (audio_extn_compr_cap_enabled() &&
363 audio_extn_compr_cap_format_supported(in->config.format)) {
364 in->usecase = USECASE_INCALL_REC_DOWNLINK_COMPRESS;
365 } else
366 in->usecase = USECASE_INCALL_REC_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800367 rec_mode = INCALL_REC_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700368 break;
369 case AUDIO_SOURCE_VOICE_CALL:
Helen Zenge56b4852013-12-03 16:54:40 -0800370 if (audio_extn_compr_cap_enabled() &&
371 audio_extn_compr_cap_format_supported(in->config.format)) {
372 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS;
373 } else
374 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800375 rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700376 break;
377 default:
378 ALOGV("%s: Source type %d doesnt match incall recording criteria",
379 __func__, in->source);
380 return ret;
381 }
382
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700383 session_id = voice_get_active_session_id(adev);
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800384 ret = platform_set_incall_recording_session_id(adev->platform,
385 session_id, rec_mode);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700386 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
387 } else {
Venkata Narendra Kumar Gutta76440ba2015-03-30 19:16:14 +0530388 /*
389 * Reject the recording instances, where the recording is started
390 * with In-call voice recording source types but voice call is not
391 * active by the time input is started
392 */
393 if ((in->source == AUDIO_SOURCE_VOICE_UPLINK) ||
394 (in->source == AUDIO_SOURCE_VOICE_DOWNLINK) ||
395 (in->source == AUDIO_SOURCE_VOICE_CALL)) {
396 ret = -EINVAL;
397 ALOGE("%s: As voice call is not active, Incall rec usecase can't be \
398 selected for requested source:%d",__func__, in->source);
399 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700400 ALOGV("%s: voice call not active", __func__);
401 }
402
403 return ret;
404}
405
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800406int voice_check_and_stop_incall_rec_usecase(struct audio_device *adev,
407 struct stream_in *in)
408{
409 int ret = 0;
410
411 if (in->source == AUDIO_SOURCE_VOICE_UPLINK ||
412 in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
413 in->source == AUDIO_SOURCE_VOICE_CALL) {
414 ret = platform_stop_incall_recording_usecase(adev->platform);
415 ALOGV("%s: Stop In-call recording", __func__);
416 }
417
418 return ret;
419}
420
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800421snd_device_t voice_get_incall_rec_snd_device(snd_device_t in_snd_device)
422{
423 snd_device_t incall_record_device = in_snd_device;
424
425 /*
426 * For incall recording stream, AUDIO_COPP topology will be picked up
427 * from the calibration data of the input sound device which is nothing
428 * but the voice call's input device. But there are requirements to use
429 * AUDIO_COPP_MONO topology even if the voice call's input device is
430 * different. Hence override the input device with the one which uses
431 * the AUDIO_COPP_MONO topology.
432 */
433 switch(in_snd_device) {
434 case SND_DEVICE_IN_HANDSET_MIC:
435 case SND_DEVICE_IN_VOICE_DMIC:
436 case SND_DEVICE_IN_AANC_HANDSET_MIC:
437 incall_record_device = SND_DEVICE_IN_HANDSET_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800438 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800439 case SND_DEVICE_IN_VOICE_SPEAKER_MIC:
440 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC:
441 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE:
442 case SND_DEVICE_IN_VOICE_SPEAKER_QMIC:
443 incall_record_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800444 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800445 default:
446 incall_record_device = in_snd_device;
447 }
448
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800449 ALOGD("%s: in_snd_device(%d: %s) incall_record_device(%d: %s)", __func__,
450 in_snd_device, platform_get_snd_device_name(in_snd_device),
451 incall_record_device, platform_get_snd_device_name(incall_record_device));
452
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800453 return incall_record_device;
454}
455
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700456int voice_set_mic_mute(struct audio_device *adev, bool state)
457{
458 int err = 0;
459
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800460 adev->voice.mic_mute = state;
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530461 if (audio_extn_hfp_is_active(adev)) {
462 err = hfp_set_mic_mute(adev, state);
463 } else if (adev->mode == AUDIO_MODE_IN_CALL) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800464 err = platform_set_mic_mute(adev->platform, state);
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530465 } else if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800466 err = voice_extn_compress_voip_set_mic_mute(adev, state);
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530467 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700468 return err;
469}
470
471bool voice_get_mic_mute(struct audio_device *adev)
472{
473 return adev->voice.mic_mute;
474}
475
476int voice_set_volume(struct audio_device *adev, float volume)
477{
478 int vol, err = 0;
479
Shruthi Krishnaace10852013-10-25 14:32:12 -0700480 adev->voice.volume = volume;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700481 if (adev->mode == AUDIO_MODE_IN_CALL) {
482 if (volume < 0.0) {
483 volume = 0.0;
484 } else if (volume > 1.0) {
485 volume = 1.0;
486 }
487
488 vol = lrint(volume * 100.0);
489
490 // Voice volume levels from android are mapped to driver volume levels as follows.
491 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
492 // So adjust the volume to get the correct volume index in driver
493 vol = 100 - vol;
494
495 err = platform_set_voice_volume(adev->platform, vol);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700496 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800497 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
498 err = voice_extn_compress_voip_set_volume(adev, volume);
499
Shruthi Krishnaace10852013-10-25 14:32:12 -0700500
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700501 return err;
502}
503
504int voice_start_call(struct audio_device *adev)
505{
506 int ret = 0;
507
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -0700508 adev->voice.in_call = true;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800509 ret = voice_extn_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700510 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700511 ret = voice_start_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700512 }
513
514 return ret;
515}
516
517int voice_stop_call(struct audio_device *adev)
518{
519 int ret = 0;
520
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700521 adev->voice.in_call = false;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800522 ret = voice_extn_stop_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700523 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700524 ret = voice_stop_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700525 }
526
527 return ret;
528}
529
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -0800530void voice_get_parameters(struct audio_device *adev,
531 struct str_parms *query,
532 struct str_parms *reply)
533{
534 voice_extn_get_parameters(adev, query, reply);
535}
536
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700537int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
538{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700539 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800540 int ret = 0, err;
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800541 char *kv_pairs = str_parms_to_str(parms);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700542
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800543 ALOGV_IF(kv_pairs != NULL, "%s: enter: %s", __func__, kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700544
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800545 ret = voice_extn_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700546 if (ret != 0) {
547 if (ret == -ENOSYS)
548 ret = 0;
549 else
550 goto done;
551 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700552
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800553 ret = voice_extn_compress_voip_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700554 if (ret != 0) {
555 if (ret == -ENOSYS)
556 ret = 0;
557 else
558 goto done;
559 }
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800560
561 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
562 if (err >= 0) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700563 int tty_mode;
564 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
565 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
566 tty_mode = TTY_MODE_OFF;
567 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
568 tty_mode = TTY_MODE_VCO;
569 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
570 tty_mode = TTY_MODE_HCO;
571 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
572 tty_mode = TTY_MODE_FULL;
573 else {
574 ret = -EINVAL;
575 goto done;
576 }
577
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700578 if (tty_mode != adev->voice.tty_mode) {
579 adev->voice.tty_mode = tty_mode;
580 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700581 if (voice_is_call_state_active(adev))
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800582 voice_update_devices_for_all_voice_usecases(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700583 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700584 }
585
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800586 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800587 value, sizeof(value));
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800588 if (err >= 0) {
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800589 str_parms_del(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC);
590 if (strcmp(value, AUDIO_PARAMETER_VALUE_TRUE) == 0)
591 platform_start_incall_music_usecase(adev->platform);
592 else
593 platform_stop_incall_music_usecase(adev->platform);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700594 }
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800595
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700596done:
597 ALOGV("%s: exit with code(%d)", __func__, ret);
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800598 free(kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700599 return ret;
600}
601
602void voice_init(struct audio_device *adev)
603{
604 int i = 0;
605
606 memset(&adev->voice, 0, sizeof(adev->voice));
607 adev->voice.tty_mode = TTY_MODE_OFF;
608 adev->voice.volume = 1.0f;
609 adev->voice.mic_mute = false;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700610 adev->voice.in_call = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700611 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
612 adev->voice.session[i].pcm_rx = NULL;
613 adev->voice.session[i].pcm_tx = NULL;
614 adev->voice.session[i].state.current = CALL_INACTIVE;
615 adev->voice.session[i].state.new = CALL_INACTIVE;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -0700616 adev->voice.session[i].vsid = VOICE_VSID;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700617 }
618
619 voice_extn_init(adev);
620}
621
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800622void voice_update_devices_for_all_voice_usecases(struct audio_device *adev)
623{
624 struct listnode *node;
625 struct audio_usecase *usecase;
626
627 list_for_each(node, &adev->usecase_list) {
628 usecase = node_to_item(node, struct audio_usecase, list);
629 if (usecase->type == VOICE_CALL) {
630 ALOGV("%s: updating device for usecase:%s", __func__,
631 use_case_table[usecase->id]);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700632 usecase->stream.out = adev->current_call_output;
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800633 select_devices(adev, usecase->id);
634 }
635 }
636}
637
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700638