blob: f9e35626099b27a94e06e4a23a675b4330887236 [file] [log] [blame]
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001/*
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +05302 * Copyright (c) 2013-2017, 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
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053037#ifdef DYNAMIC_LOG_ENABLED
38#include <log_xml_parser.h>
39#define LOG_MASK HAL_MOD_FILE_VOICE
40#include <log_utils.h>
41#endif
42
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070043struct pcm_config pcm_config_voice_call = {
44 .channels = 1,
45 .rate = 8000,
46 .period_size = 160,
47 .period_count = 2,
48 .format = PCM_FORMAT_S16_LE,
49};
50
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070051static struct voice_session *voice_get_session_from_use_case(struct audio_device *adev,
52 audio_usecase_t usecase_id)
53{
54 struct voice_session *session = NULL;
55 int ret = 0;
56
57 ret = voice_extn_get_session_from_use_case(adev, usecase_id, &session);
58 if (ret == -ENOSYS) {
59 session = &adev->voice.session[VOICE_SESS_IDX];
60 }
61
62 return session;
63}
64
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070065static bool voice_is_sidetone_device(snd_device_t out_device,
66 char *mixer_path)
67{
68 bool is_sidetone_dev;
69
70 switch (out_device) {
71 case SND_DEVICE_OUT_VOICE_HANDSET:
72 is_sidetone_dev = true;
73 strlcpy(mixer_path, "sidetone-handset", MIXER_PATH_MAX_LENGTH);
74 break;
75 case SND_DEVICE_OUT_VOICE_HEADPHONES:
76 case SND_DEVICE_OUT_VOICE_ANC_HEADSET:
77 case SND_DEVICE_OUT_VOICE_ANC_FB_HEADSET:
78 is_sidetone_dev = true;
79 strlcpy(mixer_path, "sidetone-headphones", MIXER_PATH_MAX_LENGTH);
80 break;
Kuirong Wang1cad7142016-05-24 15:21:56 -070081 case SND_DEVICE_OUT_USB_HEADSET:
82 is_sidetone_dev = true;
83 break;
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070084 default:
85 is_sidetone_dev = false;
86 break;
87 }
88
89 return is_sidetone_dev;
90}
91
92void voice_set_sidetone(struct audio_device *adev,
93 snd_device_t out_snd_device, bool enable)
94{
95 char mixer_path[MIXER_PATH_MAX_LENGTH];
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070096 ALOGD("%s: %s, out_snd_device: %d\n",
97 __func__, (enable ? "enable" : "disable"),
98 out_snd_device);
Kuirong Wang1cad7142016-05-24 15:21:56 -070099 if (voice_is_sidetone_device(out_snd_device, mixer_path))
100 platform_set_sidetone(adev, out_snd_device, enable, mixer_path);
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700101 return;
102}
103
Vidyakumar Athotaea269c62016-10-31 09:05:59 -0700104static bool voice_is_aanc_device(snd_device_t out_device,
105 char *mixer_path)
106{
107 bool is_aanc_dev;
108
109 switch (out_device) {
110 case SND_DEVICE_OUT_ANC_HANDSET:
111 is_aanc_dev = true;
112 strlcpy(mixer_path, "aanc-path", MIXER_PATH_MAX_LENGTH);
113 break;
114 default:
115 is_aanc_dev = false;
116 break;
117 }
118
119 return is_aanc_dev;
120}
121
122void voice_check_and_update_aanc_path(struct audio_device *adev,
123 snd_device_t out_snd_device,
124 bool enable)
125{
126 char mixer_path[MIXER_PATH_MAX_LENGTH];
127
128 ALOGV("%s: %s, out_snd_device: %d\n",
129 __func__, (enable ? "enable" : "disable"), out_snd_device);
130
131 if (voice_is_aanc_device(out_snd_device, mixer_path))
132 platform_update_aanc_path(adev, out_snd_device, enable, mixer_path);
133
134 return;
135}
136
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700137int voice_stop_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700138{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530139 int ret = 0;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700140 struct audio_usecase *uc_info;
141 struct voice_session *session = NULL;
142
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800143 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700144
145 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700146 if (!session) {
147 ALOGE("stop_call: couldn't find voice session");
148 return -EINVAL;
149 }
150
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700151 uc_info = get_usecase_from_list(adev, usecase_id);
152 if (uc_info == NULL) {
153 ALOGE("%s: Could not find the usecase (%d) in the list",
154 __func__, usecase_id);
155 return -EINVAL;
156 }
157
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700158 session->state.current = CALL_INACTIVE;
159
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700160 /* Disable sidetone only when no calls are active */
161 if (!voice_is_call_state_active(adev))
162 voice_set_sidetone(adev, uc_info->out_snd_device, false);
163
Vidyakumar Athotaea269c62016-10-31 09:05:59 -0700164 /* Disable aanc only when no calls are active */
165 if (!voice_is_call_state_active(adev))
166 voice_check_and_update_aanc_path(adev, uc_info->out_snd_device, false);
167
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800168 ret = platform_stop_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700169
170 /* 1. Close the PCM devices */
171 if (session->pcm_rx) {
172 pcm_close(session->pcm_rx);
173 session->pcm_rx = NULL;
174 }
175 if (session->pcm_tx) {
176 pcm_close(session->pcm_tx);
177 session->pcm_tx = NULL;
178 }
179
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700180 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700181 disable_audio_route(adev, uc_info);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700182
183 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700184 disable_snd_device(adev, uc_info->out_snd_device);
185 disable_snd_device(adev, uc_info->in_snd_device);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700186
187 list_remove(&uc_info->list);
188 free(uc_info);
189
190 ALOGD("%s: exit: status(%d)", __func__, ret);
191 return ret;
192}
193
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700194int voice_start_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700195{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530196 int ret = 0;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700197 struct audio_usecase *uc_info;
198 int pcm_dev_rx_id, pcm_dev_tx_id;
Helen Zeng6a16ad72014-02-23 22:04:44 -0800199 uint32_t sample_rate = 8000;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700200 struct voice_session *session = NULL;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700201 struct pcm_config voice_config = pcm_config_voice_call;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800202
203 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700204
205 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700206 if (!session) {
207 ALOGE("start_call: couldn't find voice session");
208 return -EINVAL;
209 }
210
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700211 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700212 if (!uc_info) {
213 ALOGE("start_call: couldn't allocate mem for audio_usecase");
214 return -ENOMEM;
215 }
216
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700217 uc_info->id = usecase_id;
218 uc_info->type = VOICE_CALL;
kunleiz3251d232017-11-15 16:28:55 +0800219 uc_info->stream.out = adev->current_call_output;
220 uc_info->devices = adev->current_call_output->devices;
221
222 if (popcount(uc_info->devices) == 2) {
223 ALOGE("%s: Invalid combo device(%#x) for voice call", __func__,
224 uc_info->devices);
225 ret = -EIO;
226 goto error_start_voice;
227 }
228
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700229 uc_info->in_snd_device = SND_DEVICE_NONE;
230 uc_info->out_snd_device = SND_DEVICE_NONE;
231
kunleizf175f672017-09-12 17:02:52 +0800232 if (audio_is_bluetooth_sco_device(uc_info->devices) && !adev->bt_sco_on) {
233 ALOGE("start_call: couldn't find BT SCO, SCO is not ready");
234 adev->voice.in_call = false;
235 ret = -EIO;
236 goto error_start_voice;
237 }
238
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700239 list_add_tail(&adev->usecase_list, &uc_info->list);
240
241 select_devices(adev, usecase_id);
242
243 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
244 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
245
246 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
247 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
248 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
249 ret = -EIO;
250 goto error_start_voice;
251 }
Helen Zeng6a16ad72014-02-23 22:04:44 -0800252 ret = platform_get_sample_rate(adev->platform, &sample_rate);
253 if (ret < 0) {
254 ALOGE("platform_get_sample_rate error %d\n", ret);
255 } else {
256 voice_config.rate = sample_rate;
257 }
258 ALOGD("voice_config.rate %d\n", voice_config.rate);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700259
Vidyakumar Athotab000e0b2015-04-09 17:45:20 -0700260 voice_set_mic_mute(adev, adev->voice.mic_mute);
261
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700262 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
263 __func__, adev->snd_card, pcm_dev_tx_id);
264 session->pcm_tx = pcm_open(adev->snd_card,
265 pcm_dev_tx_id,
266 PCM_IN, &voice_config);
267 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
268 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
269 ret = -EIO;
270 goto error_start_voice;
271 }
272
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700273 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800274 __func__, adev->snd_card, pcm_dev_rx_id);
275 session->pcm_rx = pcm_open(adev->snd_card,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700276 pcm_dev_rx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700277 PCM_OUT, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700278 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
279 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
280 ret = -EIO;
281 goto error_start_voice;
282 }
283
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700284 pcm_start(session->pcm_tx);
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700285 pcm_start(session->pcm_rx);
286
Vidyakumar Athotaea269c62016-10-31 09:05:59 -0700287 /* Enable aanc only when no calls are active */
288 if (!voice_is_call_state_active(adev))
289 voice_check_and_update_aanc_path(adev, uc_info->out_snd_device, true);
290
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700291 /* Enable sidetone only when no calls are already active */
292 if (!voice_is_call_state_active(adev))
293 voice_set_sidetone(adev, uc_info->out_snd_device, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700294
Shruthi Krishnaace10852013-10-25 14:32:12 -0700295 voice_set_volume(adev, adev->voice.volume);
296
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800297 ret = platform_start_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700298 if (ret < 0) {
299 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
300 goto error_start_voice;
301 }
302
303 session->state.current = CALL_ACTIVE;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700304 goto done;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700305
306error_start_voice:
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700307 voice_stop_usecase(adev, usecase_id);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700308
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700309done:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700310 ALOGD("%s: exit: status(%d)", __func__, ret);
311 return ret;
312}
313
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700314bool voice_is_call_state_active(struct audio_device *adev)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700315{
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700316 bool call_state = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700317 int ret = 0;
318
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700319 ret = voice_extn_is_call_state_active(adev, &call_state);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700320 if (ret == -ENOSYS) {
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700321 call_state = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700322 }
323
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700324 return call_state;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700325}
326
Alexy Josephb1379942016-01-29 15:49:38 -0800327bool voice_is_in_call(const struct audio_device *adev)
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700328{
329 return adev->voice.in_call;
330}
331
Alexy Josephb1379942016-01-29 15:49:38 -0800332bool voice_is_in_call_rec_stream(const struct stream_in *in)
kunleizc5a639b2014-04-24 18:46:22 +0800333{
334 bool in_call_rec = false;
kunleizc5a639b2014-04-24 18:46:22 +0800335
Anish Kumar50ebcbf2014-12-09 04:01:39 +0530336 if (!in) {
337 ALOGE("%s: input stream is NULL", __func__);
338 return in_call_rec;
339 }
340
Narsinga Rao Chellad06b0982014-11-20 16:59:57 -0800341 if(in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
342 in->source == AUDIO_SOURCE_VOICE_UPLINK ||
343 in->source == AUDIO_SOURCE_VOICE_CALL) {
344 in_call_rec = true;
kunleizc5a639b2014-04-24 18:46:22 +0800345 }
346
347 return in_call_rec;
348}
349
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700350uint32_t voice_get_active_session_id(struct audio_device *adev)
351{
352 int ret = 0;
353 uint32_t session_id;
354
355 ret = voice_extn_get_active_session_id(adev, &session_id);
356 if (ret == -ENOSYS) {
357 session_id = VOICE_VSID;
358 }
359 return session_id;
360}
361
362int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800363 struct stream_in *in)
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700364{
365 int ret = 0;
366 uint32_t session_id;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800367 int rec_mode = INCALL_REC_NONE;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700368
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700369 if (voice_is_call_state_active(adev)) {
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700370 switch (in->source) {
371 case AUDIO_SOURCE_VOICE_UPLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800372 if (audio_extn_compr_cap_enabled() &&
373 audio_extn_compr_cap_format_supported(in->config.format)) {
374 in->usecase = USECASE_INCALL_REC_UPLINK_COMPRESS;
375 } else
376 in->usecase = USECASE_INCALL_REC_UPLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800377 rec_mode = INCALL_REC_UPLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700378 break;
379 case AUDIO_SOURCE_VOICE_DOWNLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800380 if (audio_extn_compr_cap_enabled() &&
381 audio_extn_compr_cap_format_supported(in->config.format)) {
382 in->usecase = USECASE_INCALL_REC_DOWNLINK_COMPRESS;
383 } else
384 in->usecase = USECASE_INCALL_REC_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800385 rec_mode = INCALL_REC_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700386 break;
387 case AUDIO_SOURCE_VOICE_CALL:
Helen Zenge56b4852013-12-03 16:54:40 -0800388 if (audio_extn_compr_cap_enabled() &&
389 audio_extn_compr_cap_format_supported(in->config.format)) {
390 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS;
391 } else
392 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800393 rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700394 break;
395 default:
396 ALOGV("%s: Source type %d doesnt match incall recording criteria",
397 __func__, in->source);
398 return ret;
399 }
400
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700401 session_id = voice_get_active_session_id(adev);
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800402 ret = platform_set_incall_recording_session_id(adev->platform,
403 session_id, rec_mode);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700404 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
405 } else {
Venkata Narendra Kumar Gutta76440ba2015-03-30 19:16:14 +0530406 /*
407 * Reject the recording instances, where the recording is started
408 * with In-call voice recording source types but voice call is not
409 * active by the time input is started
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 = -EINVAL;
415 ALOGE("%s: As voice call is not active, Incall rec usecase can't be \
416 selected for requested source:%d",__func__, in->source);
417 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700418 ALOGV("%s: voice call not active", __func__);
419 }
420
421 return ret;
422}
423
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800424int voice_check_and_stop_incall_rec_usecase(struct audio_device *adev,
425 struct stream_in *in)
426{
427 int ret = 0;
428
429 if (in->source == AUDIO_SOURCE_VOICE_UPLINK ||
430 in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
431 in->source == AUDIO_SOURCE_VOICE_CALL) {
432 ret = platform_stop_incall_recording_usecase(adev->platform);
433 ALOGV("%s: Stop In-call recording", __func__);
434 }
435
436 return ret;
437}
438
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800439snd_device_t voice_get_incall_rec_snd_device(snd_device_t in_snd_device)
440{
441 snd_device_t incall_record_device = in_snd_device;
442
443 /*
444 * For incall recording stream, AUDIO_COPP topology will be picked up
445 * from the calibration data of the input sound device which is nothing
446 * but the voice call's input device. But there are requirements to use
447 * AUDIO_COPP_MONO topology even if the voice call's input device is
448 * different. Hence override the input device with the one which uses
449 * the AUDIO_COPP_MONO topology.
450 */
451 switch(in_snd_device) {
452 case SND_DEVICE_IN_HANDSET_MIC:
453 case SND_DEVICE_IN_VOICE_DMIC:
454 case SND_DEVICE_IN_AANC_HANDSET_MIC:
455 incall_record_device = SND_DEVICE_IN_HANDSET_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800456 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800457 case SND_DEVICE_IN_VOICE_SPEAKER_MIC:
458 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC:
459 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE:
460 case SND_DEVICE_IN_VOICE_SPEAKER_QMIC:
461 incall_record_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800462 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800463 default:
464 incall_record_device = in_snd_device;
465 }
466
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800467 ALOGD("%s: in_snd_device(%d: %s) incall_record_device(%d: %s)", __func__,
468 in_snd_device, platform_get_snd_device_name(in_snd_device),
469 incall_record_device, platform_get_snd_device_name(incall_record_device));
470
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800471 return incall_record_device;
472}
473
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700474int voice_set_mic_mute(struct audio_device *adev, bool state)
475{
476 int err = 0;
477
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800478 adev->voice.mic_mute = state;
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530479 if (audio_extn_hfp_is_active(adev)) {
480 err = hfp_set_mic_mute(adev, state);
481 } else if (adev->mode == AUDIO_MODE_IN_CALL) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800482 err = platform_set_mic_mute(adev->platform, state);
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530483 } else if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800484 err = voice_extn_compress_voip_set_mic_mute(adev, state);
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530485 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700486 return err;
487}
488
489bool voice_get_mic_mute(struct audio_device *adev)
490{
491 return adev->voice.mic_mute;
492}
493
494int voice_set_volume(struct audio_device *adev, float volume)
495{
496 int vol, err = 0;
497
Shruthi Krishnaace10852013-10-25 14:32:12 -0700498 adev->voice.volume = volume;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700499 if (adev->mode == AUDIO_MODE_IN_CALL) {
500 if (volume < 0.0) {
501 volume = 0.0;
502 } else if (volume > 1.0) {
503 volume = 1.0;
504 }
505
506 vol = lrint(volume * 100.0);
507
508 // Voice volume levels from android are mapped to driver volume levels as follows.
509 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
510 // So adjust the volume to get the correct volume index in driver
511 vol = 100 - vol;
512
513 err = platform_set_voice_volume(adev->platform, vol);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700514 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800515 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
516 err = voice_extn_compress_voip_set_volume(adev, volume);
517
Shruthi Krishnaace10852013-10-25 14:32:12 -0700518
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700519 return err;
520}
521
522int voice_start_call(struct audio_device *adev)
523{
524 int ret = 0;
525
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -0700526 adev->voice.in_call = true;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800527 ret = voice_extn_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700528 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700529 ret = voice_start_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700530 }
531
532 return ret;
533}
534
535int voice_stop_call(struct audio_device *adev)
536{
537 int ret = 0;
538
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700539 adev->voice.in_call = false;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800540 ret = voice_extn_stop_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700541 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700542 ret = voice_stop_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700543 }
544
545 return ret;
546}
547
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -0800548void voice_get_parameters(struct audio_device *adev,
549 struct str_parms *query,
550 struct str_parms *reply)
551{
552 voice_extn_get_parameters(adev, query, reply);
553}
554
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700555int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
556{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700557 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800558 int ret = 0, err;
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800559 char *kv_pairs = str_parms_to_str(parms);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700560
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800561 ALOGV_IF(kv_pairs != NULL, "%s: enter: %s", __func__, kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700562
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800563 ret = voice_extn_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700564 if (ret != 0) {
565 if (ret == -ENOSYS)
566 ret = 0;
567 else
568 goto done;
569 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700570
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800571 ret = voice_extn_compress_voip_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700572 if (ret != 0) {
573 if (ret == -ENOSYS)
574 ret = 0;
575 else
576 goto done;
577 }
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800578
579 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
580 if (err >= 0) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700581 int tty_mode;
582 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
583 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
584 tty_mode = TTY_MODE_OFF;
585 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
586 tty_mode = TTY_MODE_VCO;
587 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
588 tty_mode = TTY_MODE_HCO;
589 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
590 tty_mode = TTY_MODE_FULL;
591 else {
592 ret = -EINVAL;
593 goto done;
594 }
595
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700596 if (tty_mode != adev->voice.tty_mode) {
597 adev->voice.tty_mode = tty_mode;
598 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700599 if (voice_is_call_state_active(adev))
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800600 voice_update_devices_for_all_voice_usecases(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700601 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700602 }
603
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800604 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800605 value, sizeof(value));
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800606 if (err >= 0) {
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800607 str_parms_del(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC);
608 if (strcmp(value, AUDIO_PARAMETER_VALUE_TRUE) == 0)
609 platform_start_incall_music_usecase(adev->platform);
610 else
611 platform_stop_incall_music_usecase(adev->platform);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700612 }
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800613
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700614done:
615 ALOGV("%s: exit with code(%d)", __func__, ret);
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800616 free(kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700617 return ret;
618}
619
620void voice_init(struct audio_device *adev)
621{
622 int i = 0;
623
624 memset(&adev->voice, 0, sizeof(adev->voice));
625 adev->voice.tty_mode = TTY_MODE_OFF;
626 adev->voice.volume = 1.0f;
627 adev->voice.mic_mute = false;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700628 adev->voice.in_call = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700629 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
630 adev->voice.session[i].pcm_rx = NULL;
631 adev->voice.session[i].pcm_tx = NULL;
632 adev->voice.session[i].state.current = CALL_INACTIVE;
633 adev->voice.session[i].state.new = CALL_INACTIVE;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -0700634 adev->voice.session[i].vsid = VOICE_VSID;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700635 }
636
637 voice_extn_init(adev);
638}
639
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800640void voice_update_devices_for_all_voice_usecases(struct audio_device *adev)
641{
642 struct listnode *node;
643 struct audio_usecase *usecase;
644
645 list_for_each(node, &adev->usecase_list) {
646 usecase = node_to_item(node, struct audio_usecase, list);
647 if (usecase->type == VOICE_CALL) {
648 ALOGV("%s: updating device for usecase:%s", __func__,
649 use_case_table[usecase->id]);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700650 usecase->stream.out = adev->current_call_output;
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800651 select_devices(adev, usecase->id);
652 }
653 }
654}
655
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700656