blob: b0b75e569af959ab7c17a37e3140fc4d15b7fa62 [file] [log] [blame]
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002 * Copyright (c) 2013-2019, 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>
Aalique Grahame22e49102018-12-18 14:23:57 -080027#include <log/log.h>
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070028#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;
Aalique Grahame22e49102018-12-18 14:23:57 -080081 case SND_DEVICE_OUT_VOICE_USB_HEADSET:
Kuirong Wang1cad7142016-05-24 15:21:56 -070082 case SND_DEVICE_OUT_USB_HEADSET:
Aalique Grahame22e49102018-12-18 14:23:57 -080083 // USB does not use a QC mixer.
84 mixer_path[0] = '\0';
Kuirong Wang1cad7142016-05-24 15:21:56 -070085 is_sidetone_dev = true;
86 break;
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070087 default:
Aalique Grahame22e49102018-12-18 14:23:57 -080088 ALOGW("%s: %d is not a sidetone device", __func__, out_device);
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070089 is_sidetone_dev = false;
90 break;
91 }
92
93 return is_sidetone_dev;
94}
95
96void voice_set_sidetone(struct audio_device *adev,
97 snd_device_t out_snd_device, bool enable)
98{
99 char mixer_path[MIXER_PATH_MAX_LENGTH];
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700100 ALOGD("%s: %s, out_snd_device: %d\n",
101 __func__, (enable ? "enable" : "disable"),
102 out_snd_device);
Kuirong Wang1cad7142016-05-24 15:21:56 -0700103 if (voice_is_sidetone_device(out_snd_device, mixer_path))
104 platform_set_sidetone(adev, out_snd_device, enable, mixer_path);
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700105 return;
106}
107
Vidyakumar Athotaea269c62016-10-31 09:05:59 -0700108static bool voice_is_aanc_device(snd_device_t out_device,
109 char *mixer_path)
110{
111 bool is_aanc_dev;
112
113 switch (out_device) {
114 case SND_DEVICE_OUT_ANC_HANDSET:
115 is_aanc_dev = true;
116 strlcpy(mixer_path, "aanc-path", MIXER_PATH_MAX_LENGTH);
117 break;
118 default:
119 is_aanc_dev = false;
120 break;
121 }
122
123 return is_aanc_dev;
124}
125
126void voice_check_and_update_aanc_path(struct audio_device *adev,
127 snd_device_t out_snd_device,
128 bool enable)
129{
130 char mixer_path[MIXER_PATH_MAX_LENGTH];
131
132 ALOGV("%s: %s, out_snd_device: %d\n",
133 __func__, (enable ? "enable" : "disable"), out_snd_device);
134
135 if (voice_is_aanc_device(out_snd_device, mixer_path))
136 platform_update_aanc_path(adev, out_snd_device, enable, mixer_path);
137
138 return;
139}
140
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700141int voice_stop_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700142{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530143 int ret = 0;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700144 struct audio_usecase *uc_info;
145 struct voice_session *session = NULL;
146
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800147 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700148
149 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700150 if (!session) {
151 ALOGE("stop_call: couldn't find voice session");
152 return -EINVAL;
153 }
154
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700155 uc_info = get_usecase_from_list(adev, usecase_id);
156 if (uc_info == NULL) {
157 ALOGE("%s: Could not find the usecase (%d) in the list",
158 __func__, usecase_id);
159 return -EINVAL;
160 }
161
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700162 session->state.current = CALL_INACTIVE;
163
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700164 /* Disable sidetone only when no calls are active */
165 if (!voice_is_call_state_active(adev))
166 voice_set_sidetone(adev, uc_info->out_snd_device, false);
167
Vidyakumar Athotaea269c62016-10-31 09:05:59 -0700168 /* Disable aanc only when no calls are active */
169 if (!voice_is_call_state_active(adev))
170 voice_check_and_update_aanc_path(adev, uc_info->out_snd_device, false);
171
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800172 ret = platform_stop_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700173
174 /* 1. Close the PCM devices */
175 if (session->pcm_rx) {
176 pcm_close(session->pcm_rx);
177 session->pcm_rx = NULL;
178 }
179 if (session->pcm_tx) {
180 pcm_close(session->pcm_tx);
181 session->pcm_tx = NULL;
182 }
183
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700184 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700185 disable_audio_route(adev, uc_info);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700186
187 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700188 disable_snd_device(adev, uc_info->out_snd_device);
189 disable_snd_device(adev, uc_info->in_snd_device);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700190
191 list_remove(&uc_info->list);
192 free(uc_info);
193
194 ALOGD("%s: exit: status(%d)", __func__, ret);
195 return ret;
196}
197
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700198int voice_start_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700199{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530200 int ret = 0;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700201 struct audio_usecase *uc_info;
202 int pcm_dev_rx_id, pcm_dev_tx_id;
Helen Zeng6a16ad72014-02-23 22:04:44 -0800203 uint32_t sample_rate = 8000;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700204 struct voice_session *session = NULL;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700205 struct pcm_config voice_config = pcm_config_voice_call;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800206
207 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700208
209 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700210 if (!session) {
211 ALOGE("start_call: couldn't find voice session");
212 return -EINVAL;
213 }
214
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700215 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700216 if (!uc_info) {
217 ALOGE("start_call: couldn't allocate mem for audio_usecase");
218 return -ENOMEM;
219 }
220
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700221 uc_info->id = usecase_id;
222 uc_info->type = VOICE_CALL;
kunleiz3251d232017-11-15 16:28:55 +0800223 uc_info->stream.out = adev->current_call_output;
224 uc_info->devices = adev->current_call_output->devices;
225
226 if (popcount(uc_info->devices) == 2) {
227 ALOGE("%s: Invalid combo device(%#x) for voice call", __func__,
228 uc_info->devices);
229 ret = -EIO;
230 goto error_start_voice;
231 }
232
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700233 uc_info->in_snd_device = SND_DEVICE_NONE;
234 uc_info->out_snd_device = SND_DEVICE_NONE;
Arun Mirpurief53ce52018-09-11 18:00:09 -0700235 adev->voice.use_device_mute = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700236
kunleizf175f672017-09-12 17:02:52 +0800237 if (audio_is_bluetooth_sco_device(uc_info->devices) && !adev->bt_sco_on) {
238 ALOGE("start_call: couldn't find BT SCO, SCO is not ready");
239 adev->voice.in_call = false;
240 ret = -EIO;
241 goto error_start_voice;
242 }
243
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700244 list_add_tail(&adev->usecase_list, &uc_info->list);
245
246 select_devices(adev, usecase_id);
247
248 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
249 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
250
251 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
252 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
253 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
254 ret = -EIO;
255 goto error_start_voice;
256 }
Helen Zeng6a16ad72014-02-23 22:04:44 -0800257 ret = platform_get_sample_rate(adev->platform, &sample_rate);
258 if (ret < 0) {
259 ALOGE("platform_get_sample_rate error %d\n", ret);
260 } else {
261 voice_config.rate = sample_rate;
262 }
263 ALOGD("voice_config.rate %d\n", voice_config.rate);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700264
Vidyakumar Athotab000e0b2015-04-09 17:45:20 -0700265 voice_set_mic_mute(adev, adev->voice.mic_mute);
266
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700267 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
268 __func__, adev->snd_card, pcm_dev_tx_id);
269 session->pcm_tx = pcm_open(adev->snd_card,
270 pcm_dev_tx_id,
271 PCM_IN, &voice_config);
272 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
273 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
274 ret = -EIO;
275 goto error_start_voice;
276 }
277
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700278 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800279 __func__, adev->snd_card, pcm_dev_rx_id);
280 session->pcm_rx = pcm_open(adev->snd_card,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700281 pcm_dev_rx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700282 PCM_OUT, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700283 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
284 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
285 ret = -EIO;
286 goto error_start_voice;
287 }
288
Vignesh Kulothungan7d374312018-02-21 17:12:00 -0800289 if(adev->mic_break_enabled)
290 platform_set_mic_break_det(adev->platform, true);
291
Vignesh Kulothungancf4b9322019-05-03 13:52:50 -0700292 ret = pcm_start(session->pcm_tx);
293 if (ret != 0) {
294 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
295 goto error_start_voice;
296 }
297
298 ret = pcm_start(session->pcm_rx);
299 if (ret != 0) {
300 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
301 goto error_start_voice;
302 }
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700303
Vidyakumar Athotaea269c62016-10-31 09:05:59 -0700304 /* Enable aanc only when no calls are active */
305 if (!voice_is_call_state_active(adev))
306 voice_check_and_update_aanc_path(adev, uc_info->out_snd_device, true);
307
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700308 /* Enable sidetone only when no calls are already active */
309 if (!voice_is_call_state_active(adev))
310 voice_set_sidetone(adev, uc_info->out_snd_device, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700311
Shruthi Krishnaace10852013-10-25 14:32:12 -0700312 voice_set_volume(adev, adev->voice.volume);
313
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800314 ret = platform_start_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700315 if (ret < 0) {
316 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
317 goto error_start_voice;
318 }
319
320 session->state.current = CALL_ACTIVE;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700321 goto done;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700322
323error_start_voice:
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700324 voice_stop_usecase(adev, usecase_id);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700325
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700326done:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700327 ALOGD("%s: exit: status(%d)", __func__, ret);
328 return ret;
329}
330
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700331bool voice_is_call_state_active(struct audio_device *adev)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700332{
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700333 bool call_state = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700334 int ret = 0;
335
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700336 ret = voice_extn_is_call_state_active(adev, &call_state);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700337 if (ret == -ENOSYS) {
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700338 call_state = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700339 }
340
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700341 return call_state;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700342}
343
Alexy Josephb1379942016-01-29 15:49:38 -0800344bool voice_is_in_call(const struct audio_device *adev)
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700345{
346 return adev->voice.in_call;
347}
348
Alexy Josephb1379942016-01-29 15:49:38 -0800349bool voice_is_in_call_rec_stream(const struct stream_in *in)
kunleizc5a639b2014-04-24 18:46:22 +0800350{
351 bool in_call_rec = false;
kunleizc5a639b2014-04-24 18:46:22 +0800352
Anish Kumar50ebcbf2014-12-09 04:01:39 +0530353 if (!in) {
354 ALOGE("%s: input stream is NULL", __func__);
355 return in_call_rec;
356 }
357
Arun Mirpurief53ce52018-09-11 18:00:09 -0700358 if (in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
359 in->source == AUDIO_SOURCE_VOICE_UPLINK ||
360 in->source == AUDIO_SOURCE_VOICE_CALL) {
361 in_call_rec = true;
kunleizc5a639b2014-04-24 18:46:22 +0800362 }
363
364 return in_call_rec;
365}
366
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700367uint32_t voice_get_active_session_id(struct audio_device *adev)
368{
369 int ret = 0;
370 uint32_t session_id;
371
372 ret = voice_extn_get_active_session_id(adev, &session_id);
373 if (ret == -ENOSYS) {
374 session_id = VOICE_VSID;
375 }
376 return session_id;
377}
378
379int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800380 struct stream_in *in)
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700381{
382 int ret = 0;
383 uint32_t session_id;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800384 int rec_mode = INCALL_REC_NONE;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700385
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700386 if (voice_is_call_state_active(adev)) {
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700387 switch (in->source) {
388 case AUDIO_SOURCE_VOICE_UPLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800389 if (audio_extn_compr_cap_enabled() &&
390 audio_extn_compr_cap_format_supported(in->config.format)) {
391 in->usecase = USECASE_INCALL_REC_UPLINK_COMPRESS;
392 } else
393 in->usecase = USECASE_INCALL_REC_UPLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800394 rec_mode = INCALL_REC_UPLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700395 break;
396 case AUDIO_SOURCE_VOICE_DOWNLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800397 if (audio_extn_compr_cap_enabled() &&
398 audio_extn_compr_cap_format_supported(in->config.format)) {
399 in->usecase = USECASE_INCALL_REC_DOWNLINK_COMPRESS;
400 } else
401 in->usecase = USECASE_INCALL_REC_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800402 rec_mode = INCALL_REC_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700403 break;
404 case AUDIO_SOURCE_VOICE_CALL:
Helen Zenge56b4852013-12-03 16:54:40 -0800405 if (audio_extn_compr_cap_enabled() &&
406 audio_extn_compr_cap_format_supported(in->config.format)) {
407 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS;
408 } else
409 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800410 rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700411 break;
412 default:
413 ALOGV("%s: Source type %d doesnt match incall recording criteria",
414 __func__, in->source);
415 return ret;
416 }
417
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700418 session_id = voice_get_active_session_id(adev);
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800419 ret = platform_set_incall_recording_session_id(adev->platform,
420 session_id, rec_mode);
Yunfei Zhang3714bd12019-04-11 09:04:28 +0800421 platform_set_incall_recording_session_channels(adev->platform,
422 in->config.channels);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700423 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
424 } else {
Venkata Narendra Kumar Gutta76440ba2015-03-30 19:16:14 +0530425 /*
426 * Reject the recording instances, where the recording is started
427 * with In-call voice recording source types but voice call is not
428 * active by the time input is started
429 */
430 if ((in->source == AUDIO_SOURCE_VOICE_UPLINK) ||
431 (in->source == AUDIO_SOURCE_VOICE_DOWNLINK) ||
432 (in->source == AUDIO_SOURCE_VOICE_CALL)) {
433 ret = -EINVAL;
434 ALOGE("%s: As voice call is not active, Incall rec usecase can't be \
435 selected for requested source:%d",__func__, in->source);
436 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700437 ALOGV("%s: voice call not active", __func__);
438 }
439
440 return ret;
441}
442
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800443int voice_check_and_stop_incall_rec_usecase(struct audio_device *adev,
444 struct stream_in *in)
445{
446 int ret = 0;
447
448 if (in->source == AUDIO_SOURCE_VOICE_UPLINK ||
449 in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
450 in->source == AUDIO_SOURCE_VOICE_CALL) {
451 ret = platform_stop_incall_recording_usecase(adev->platform);
452 ALOGV("%s: Stop In-call recording", __func__);
453 }
454
455 return ret;
456}
457
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530458snd_device_t voice_get_incall_rec_backend_device(struct stream_in *in)
459{
460 snd_device_t incall_record_device = {0};
461
Aditya Bavanari9e957d82019-01-29 17:47:13 +0530462 if (!in) {
463 ALOGE("%s: input stream is NULL", __func__);
464 return 0;
465 }
466
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530467 switch(in->source) {
468 case AUDIO_SOURCE_VOICE_UPLINK:
469 incall_record_device = SND_DEVICE_IN_INCALL_REC_TX;
470 break;
471 case AUDIO_SOURCE_VOICE_DOWNLINK:
472 incall_record_device = SND_DEVICE_IN_INCALL_REC_RX;
473 break;
474 case AUDIO_SOURCE_VOICE_CALL:
475 incall_record_device = SND_DEVICE_IN_INCALL_REC_RX_TX;
476 break;
477 default:
478 ALOGI("Invalid source %d", in->source);
479 }
480
481 return incall_record_device;
482}
483
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800484snd_device_t voice_get_incall_rec_snd_device(snd_device_t in_snd_device)
485{
486 snd_device_t incall_record_device = in_snd_device;
487
488 /*
489 * For incall recording stream, AUDIO_COPP topology will be picked up
490 * from the calibration data of the input sound device which is nothing
491 * but the voice call's input device. But there are requirements to use
492 * AUDIO_COPP_MONO topology even if the voice call's input device is
493 * different. Hence override the input device with the one which uses
494 * the AUDIO_COPP_MONO topology.
495 */
496 switch(in_snd_device) {
497 case SND_DEVICE_IN_HANDSET_MIC:
498 case SND_DEVICE_IN_VOICE_DMIC:
499 case SND_DEVICE_IN_AANC_HANDSET_MIC:
500 incall_record_device = SND_DEVICE_IN_HANDSET_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800501 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800502 case SND_DEVICE_IN_VOICE_SPEAKER_MIC:
503 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC:
504 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE:
505 case SND_DEVICE_IN_VOICE_SPEAKER_QMIC:
506 incall_record_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800507 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800508 default:
509 incall_record_device = in_snd_device;
510 }
511
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800512 ALOGD("%s: in_snd_device(%d: %s) incall_record_device(%d: %s)", __func__,
513 in_snd_device, platform_get_snd_device_name(in_snd_device),
514 incall_record_device, platform_get_snd_device_name(incall_record_device));
515
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800516 return incall_record_device;
517}
518
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700519int voice_set_mic_mute(struct audio_device *adev, bool state)
520{
521 int err = 0;
522
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800523 adev->voice.mic_mute = state;
Arun Mirpurief53ce52018-09-11 18:00:09 -0700524
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530525 if (audio_extn_hfp_is_active(adev)) {
Arun Mirpurie008ed22019-03-21 11:21:04 -0700526 err = audio_extn_hfp_set_mic_mute2(adev, state);
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530527 } else if (adev->mode == AUDIO_MODE_IN_CALL) {
Arun Mirpurief53ce52018-09-11 18:00:09 -0700528 /* Use device mute if incall music delivery usecase is in progress */
529 if (adev->voice.use_device_mute)
530 err = platform_set_device_mute(adev->platform, state, "tx");
531 else
532 err = platform_set_mic_mute(adev->platform, state);
533 ALOGV("%s: voice mute status=%d, use_device_mute flag=%d",
534 __func__, state, adev->voice.use_device_mute);
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530535 } else if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800536 err = voice_extn_compress_voip_set_mic_mute(adev, state);
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530537 }
Arun Mirpurief53ce52018-09-11 18:00:09 -0700538
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700539 return err;
540}
541
542bool voice_get_mic_mute(struct audio_device *adev)
543{
544 return adev->voice.mic_mute;
545}
546
Arun Mirpurief53ce52018-09-11 18:00:09 -0700547/*
548 * Following function is called when incall music uplink usecase is
549 * created or destroyed while mic is muted. If incall music uplink
550 * usecase is active, apply voice device mute to mute only voice Tx
551 * path and not the mixed voice Tx + inncall-music path. Revert to
552 * voice stream mute once incall music uplink usecase is inactive
553 */
554void voice_set_device_mute_flag(struct audio_device *adev, bool state)
555{
556 if (adev->voice.mic_mute) {
557 if (state) {
558 platform_set_device_mute(adev->platform, true, "tx");
559 platform_set_mic_mute(adev->platform, false);
560 } else {
561 platform_set_mic_mute(adev->platform, true);
562 platform_set_device_mute(adev->platform, false, "tx");
563 }
564 }
565 adev->voice.use_device_mute = state;
566}
567
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700568int voice_set_volume(struct audio_device *adev, float volume)
569{
570 int vol, err = 0;
571
Shruthi Krishnaace10852013-10-25 14:32:12 -0700572 adev->voice.volume = volume;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700573 if (adev->mode == AUDIO_MODE_IN_CALL) {
574 if (volume < 0.0) {
575 volume = 0.0;
576 } else if (volume > 1.0) {
577 volume = 1.0;
578 }
579
580 vol = lrint(volume * 100.0);
581
582 // Voice volume levels from android are mapped to driver volume levels as follows.
583 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
584 // So adjust the volume to get the correct volume index in driver
585 vol = 100 - vol;
586
587 err = platform_set_voice_volume(adev->platform, vol);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700588 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800589 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
590 err = voice_extn_compress_voip_set_volume(adev, volume);
591
Shruthi Krishnaace10852013-10-25 14:32:12 -0700592
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700593 return err;
594}
595
596int voice_start_call(struct audio_device *adev)
597{
598 int ret = 0;
599
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -0700600 adev->voice.in_call = true;
Aalique Grahame22e49102018-12-18 14:23:57 -0800601
602 voice_set_mic_mute(adev, adev->voice.mic_mute);
603
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800604 ret = voice_extn_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700605 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700606 ret = voice_start_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700607 }
608
609 return ret;
610}
611
612int voice_stop_call(struct audio_device *adev)
613{
614 int ret = 0;
615
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700616 adev->voice.in_call = false;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800617 ret = voice_extn_stop_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700618 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700619 ret = voice_stop_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700620 }
621
622 return ret;
623}
624
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -0800625void voice_get_parameters(struct audio_device *adev,
626 struct str_parms *query,
627 struct str_parms *reply)
628{
629 voice_extn_get_parameters(adev, query, reply);
630}
631
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700632int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
633{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700634 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800635 int ret = 0, err;
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800636 char *kv_pairs = str_parms_to_str(parms);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700637
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800638 ALOGV_IF(kv_pairs != NULL, "%s: enter: %s", __func__, kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700639
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800640 ret = voice_extn_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700641 if (ret != 0) {
642 if (ret == -ENOSYS)
643 ret = 0;
644 else
645 goto done;
646 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700647
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800648 ret = voice_extn_compress_voip_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700649 if (ret != 0) {
650 if (ret == -ENOSYS)
651 ret = 0;
652 else
653 goto done;
654 }
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800655
656 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
657 if (err >= 0) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700658 int tty_mode;
659 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
660 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
661 tty_mode = TTY_MODE_OFF;
662 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
663 tty_mode = TTY_MODE_VCO;
664 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
665 tty_mode = TTY_MODE_HCO;
666 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
667 tty_mode = TTY_MODE_FULL;
668 else {
669 ret = -EINVAL;
670 goto done;
671 }
672
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700673 if (tty_mode != adev->voice.tty_mode) {
674 adev->voice.tty_mode = tty_mode;
675 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700676 if (voice_is_call_state_active(adev))
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800677 voice_update_devices_for_all_voice_usecases(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700678 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700679 }
680
Aalique Grahame22e49102018-12-18 14:23:57 -0800681 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HAC,
682 value, sizeof(value));
683 if (err >= 0) {
684 bool hac = false;
685 str_parms_del(parms, AUDIO_PARAMETER_KEY_HAC);
686 if (strcmp(value, AUDIO_PARAMETER_VALUE_HAC_ON) == 0)
687 hac = true;
688
689 if (hac != adev->voice.hac) {
690 adev->voice.hac = hac;
691 if (voice_is_in_call(adev))
692 voice_update_devices_for_all_voice_usecases(adev);
693 }
694 }
695
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800696 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800697 value, sizeof(value));
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800698 if (err >= 0) {
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800699 str_parms_del(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC);
700 if (strcmp(value, AUDIO_PARAMETER_VALUE_TRUE) == 0)
701 platform_start_incall_music_usecase(adev->platform);
702 else
703 platform_stop_incall_music_usecase(adev->platform);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700704 }
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800705
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700706done:
707 ALOGV("%s: exit with code(%d)", __func__, ret);
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800708 free(kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700709 return ret;
710}
711
712void voice_init(struct audio_device *adev)
713{
714 int i = 0;
715
716 memset(&adev->voice, 0, sizeof(adev->voice));
717 adev->voice.tty_mode = TTY_MODE_OFF;
Aalique Grahame22e49102018-12-18 14:23:57 -0800718 adev->voice.hac = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700719 adev->voice.volume = 1.0f;
720 adev->voice.mic_mute = false;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700721 adev->voice.in_call = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700722 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
723 adev->voice.session[i].pcm_rx = NULL;
724 adev->voice.session[i].pcm_tx = NULL;
725 adev->voice.session[i].state.current = CALL_INACTIVE;
726 adev->voice.session[i].state.new = CALL_INACTIVE;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -0700727 adev->voice.session[i].vsid = VOICE_VSID;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700728 }
729
730 voice_extn_init(adev);
731}
732
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800733void voice_update_devices_for_all_voice_usecases(struct audio_device *adev)
734{
735 struct listnode *node;
736 struct audio_usecase *usecase;
737
738 list_for_each(node, &adev->usecase_list) {
739 usecase = node_to_item(node, struct audio_usecase, list);
740 if (usecase->type == VOICE_CALL) {
741 ALOGV("%s: updating device for usecase:%s", __func__,
742 use_case_table[usecase->id]);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700743 usecase->stream.out = adev->current_call_output;
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800744 select_devices(adev, usecase->id);
745 }
746 }
747}
748
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700749