blob: 729ab27f7b0d7bce6629d1b1d321bf99c57f896b [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:
Samyak Jainfd24f1e2019-04-30 11:58:43 +053076 case SND_DEVICE_OUT_VOICE_HEADSET:
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070077 case SND_DEVICE_OUT_VOICE_ANC_HEADSET:
78 case SND_DEVICE_OUT_VOICE_ANC_FB_HEADSET:
79 is_sidetone_dev = true;
80 strlcpy(mixer_path, "sidetone-headphones", MIXER_PATH_MAX_LENGTH);
81 break;
Aalique Grahame22e49102018-12-18 14:23:57 -080082 case SND_DEVICE_OUT_VOICE_USB_HEADSET:
Kuirong Wang1cad7142016-05-24 15:21:56 -070083 case SND_DEVICE_OUT_USB_HEADSET:
Aalique Grahame22e49102018-12-18 14:23:57 -080084 // USB does not use a QC mixer.
85 mixer_path[0] = '\0';
Kuirong Wang1cad7142016-05-24 15:21:56 -070086 is_sidetone_dev = true;
87 break;
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070088 default:
Aalique Grahame22e49102018-12-18 14:23:57 -080089 ALOGW("%s: %d is not a sidetone device", __func__, out_device);
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070090 is_sidetone_dev = false;
91 break;
92 }
93
94 return is_sidetone_dev;
95}
96
97void voice_set_sidetone(struct audio_device *adev,
98 snd_device_t out_snd_device, bool enable)
99{
100 char mixer_path[MIXER_PATH_MAX_LENGTH];
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700101 ALOGD("%s: %s, out_snd_device: %d\n",
102 __func__, (enable ? "enable" : "disable"),
103 out_snd_device);
Kuirong Wang1cad7142016-05-24 15:21:56 -0700104 if (voice_is_sidetone_device(out_snd_device, mixer_path))
105 platform_set_sidetone(adev, out_snd_device, enable, mixer_path);
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700106 return;
107}
108
Vidyakumar Athotaea269c62016-10-31 09:05:59 -0700109static bool voice_is_aanc_device(snd_device_t out_device,
110 char *mixer_path)
111{
112 bool is_aanc_dev;
113
114 switch (out_device) {
115 case SND_DEVICE_OUT_ANC_HANDSET:
116 is_aanc_dev = true;
117 strlcpy(mixer_path, "aanc-path", MIXER_PATH_MAX_LENGTH);
118 break;
119 default:
120 is_aanc_dev = false;
121 break;
122 }
123
124 return is_aanc_dev;
125}
126
127void voice_check_and_update_aanc_path(struct audio_device *adev,
128 snd_device_t out_snd_device,
129 bool enable)
130{
131 char mixer_path[MIXER_PATH_MAX_LENGTH];
132
133 ALOGV("%s: %s, out_snd_device: %d\n",
134 __func__, (enable ? "enable" : "disable"), out_snd_device);
135
136 if (voice_is_aanc_device(out_snd_device, mixer_path))
137 platform_update_aanc_path(adev, out_snd_device, enable, mixer_path);
138
139 return;
140}
141
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700142int voice_stop_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700143{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530144 int ret = 0;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700145 struct audio_usecase *uc_info;
146 struct voice_session *session = NULL;
147
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800148 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700149
150 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700151 if (!session) {
152 ALOGE("stop_call: couldn't find voice session");
153 return -EINVAL;
154 }
155
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700156 uc_info = get_usecase_from_list(adev, usecase_id);
157 if (uc_info == NULL) {
158 ALOGE("%s: Could not find the usecase (%d) in the list",
159 __func__, usecase_id);
160 return -EINVAL;
161 }
162
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700163 session->state.current = CALL_INACTIVE;
164
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700165 /* Disable sidetone only when no calls are active */
166 if (!voice_is_call_state_active(adev))
167 voice_set_sidetone(adev, uc_info->out_snd_device, false);
168
Vidyakumar Athotaea269c62016-10-31 09:05:59 -0700169 /* Disable aanc only when no calls are active */
170 if (!voice_is_call_state_active(adev))
171 voice_check_and_update_aanc_path(adev, uc_info->out_snd_device, false);
172
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800173 ret = platform_stop_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700174
175 /* 1. Close the PCM devices */
176 if (session->pcm_rx) {
177 pcm_close(session->pcm_rx);
178 session->pcm_rx = NULL;
179 }
180 if (session->pcm_tx) {
181 pcm_close(session->pcm_tx);
182 session->pcm_tx = NULL;
183 }
184
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700185 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700186 disable_audio_route(adev, uc_info);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700187
188 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700189 disable_snd_device(adev, uc_info->out_snd_device);
190 disable_snd_device(adev, uc_info->in_snd_device);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700191
192 list_remove(&uc_info->list);
193 free(uc_info);
194
195 ALOGD("%s: exit: status(%d)", __func__, ret);
196 return ret;
197}
198
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700199int voice_start_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700200{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530201 int ret = 0;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700202 struct audio_usecase *uc_info;
203 int pcm_dev_rx_id, pcm_dev_tx_id;
Helen Zeng6a16ad72014-02-23 22:04:44 -0800204 uint32_t sample_rate = 8000;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700205 struct voice_session *session = NULL;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700206 struct pcm_config voice_config = pcm_config_voice_call;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800207
208 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700209
210 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700211 if (!session) {
212 ALOGE("start_call: couldn't find voice session");
213 return -EINVAL;
214 }
215
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700216 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700217 if (!uc_info) {
218 ALOGE("start_call: couldn't allocate mem for audio_usecase");
219 return -ENOMEM;
220 }
221
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700222 uc_info->id = usecase_id;
223 uc_info->type = VOICE_CALL;
kunleiz3251d232017-11-15 16:28:55 +0800224 uc_info->stream.out = adev->current_call_output;
225 uc_info->devices = adev->current_call_output->devices;
226
227 if (popcount(uc_info->devices) == 2) {
228 ALOGE("%s: Invalid combo device(%#x) for voice call", __func__,
229 uc_info->devices);
230 ret = -EIO;
231 goto error_start_voice;
232 }
233
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700234 uc_info->in_snd_device = SND_DEVICE_NONE;
235 uc_info->out_snd_device = SND_DEVICE_NONE;
Arun Mirpurief53ce52018-09-11 18:00:09 -0700236 adev->voice.use_device_mute = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700237
kunleizf175f672017-09-12 17:02:52 +0800238 if (audio_is_bluetooth_sco_device(uc_info->devices) && !adev->bt_sco_on) {
239 ALOGE("start_call: couldn't find BT SCO, SCO is not ready");
240 adev->voice.in_call = false;
241 ret = -EIO;
242 goto error_start_voice;
243 }
244
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700245 list_add_tail(&adev->usecase_list, &uc_info->list);
246
247 select_devices(adev, usecase_id);
248
249 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
250 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
251
252 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
253 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
254 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
255 ret = -EIO;
256 goto error_start_voice;
257 }
Helen Zeng6a16ad72014-02-23 22:04:44 -0800258 ret = platform_get_sample_rate(adev->platform, &sample_rate);
259 if (ret < 0) {
260 ALOGE("platform_get_sample_rate error %d\n", ret);
261 } else {
262 voice_config.rate = sample_rate;
263 }
264 ALOGD("voice_config.rate %d\n", voice_config.rate);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700265
Vidyakumar Athotab000e0b2015-04-09 17:45:20 -0700266 voice_set_mic_mute(adev, adev->voice.mic_mute);
267
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700268 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
269 __func__, adev->snd_card, pcm_dev_tx_id);
270 session->pcm_tx = pcm_open(adev->snd_card,
271 pcm_dev_tx_id,
272 PCM_IN, &voice_config);
273 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
274 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
275 ret = -EIO;
276 goto error_start_voice;
277 }
278
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700279 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800280 __func__, adev->snd_card, pcm_dev_rx_id);
281 session->pcm_rx = pcm_open(adev->snd_card,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700282 pcm_dev_rx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700283 PCM_OUT, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700284 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
285 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
286 ret = -EIO;
287 goto error_start_voice;
288 }
289
Vignesh Kulothungan7d374312018-02-21 17:12:00 -0800290 if(adev->mic_break_enabled)
291 platform_set_mic_break_det(adev->platform, true);
292
Vignesh Kulothungancf4b9322019-05-03 13:52:50 -0700293 ret = pcm_start(session->pcm_tx);
294 if (ret != 0) {
295 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
296 goto error_start_voice;
297 }
298
299 ret = pcm_start(session->pcm_rx);
300 if (ret != 0) {
301 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
302 goto error_start_voice;
303 }
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700304
Vidyakumar Athotaea269c62016-10-31 09:05:59 -0700305 /* Enable aanc only when no calls are active */
306 if (!voice_is_call_state_active(adev))
307 voice_check_and_update_aanc_path(adev, uc_info->out_snd_device, true);
308
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700309 /* Enable sidetone only when no calls are already active */
310 if (!voice_is_call_state_active(adev))
311 voice_set_sidetone(adev, uc_info->out_snd_device, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700312
Shruthi Krishnaace10852013-10-25 14:32:12 -0700313 voice_set_volume(adev, adev->voice.volume);
314
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800315 ret = platform_start_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700316 if (ret < 0) {
317 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
318 goto error_start_voice;
319 }
320
321 session->state.current = CALL_ACTIVE;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700322 goto done;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700323
324error_start_voice:
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700325 voice_stop_usecase(adev, usecase_id);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700326
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700327done:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700328 ALOGD("%s: exit: status(%d)", __func__, ret);
329 return ret;
330}
331
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700332bool voice_is_call_state_active(struct audio_device *adev)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700333{
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700334 bool call_state = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700335 int ret = 0;
336
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700337 ret = voice_extn_is_call_state_active(adev, &call_state);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700338 if (ret == -ENOSYS) {
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700339 call_state = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700340 }
341
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700342 return call_state;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700343}
344
Alexy Josephb1379942016-01-29 15:49:38 -0800345bool voice_is_in_call(const struct audio_device *adev)
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700346{
347 return adev->voice.in_call;
348}
349
Alexy Josephb1379942016-01-29 15:49:38 -0800350bool voice_is_in_call_rec_stream(const struct stream_in *in)
kunleizc5a639b2014-04-24 18:46:22 +0800351{
352 bool in_call_rec = false;
kunleizc5a639b2014-04-24 18:46:22 +0800353
Anish Kumar50ebcbf2014-12-09 04:01:39 +0530354 if (!in) {
355 ALOGE("%s: input stream is NULL", __func__);
356 return in_call_rec;
357 }
358
Arun Mirpurief53ce52018-09-11 18:00:09 -0700359 if (in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
360 in->source == AUDIO_SOURCE_VOICE_UPLINK ||
361 in->source == AUDIO_SOURCE_VOICE_CALL) {
362 in_call_rec = true;
kunleizc5a639b2014-04-24 18:46:22 +0800363 }
364
365 return in_call_rec;
366}
367
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700368uint32_t voice_get_active_session_id(struct audio_device *adev)
369{
370 int ret = 0;
371 uint32_t session_id;
372
373 ret = voice_extn_get_active_session_id(adev, &session_id);
374 if (ret == -ENOSYS) {
375 session_id = VOICE_VSID;
376 }
377 return session_id;
378}
379
380int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800381 struct stream_in *in)
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700382{
383 int ret = 0;
384 uint32_t session_id;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800385 int rec_mode = INCALL_REC_NONE;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700386
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700387 if (voice_is_call_state_active(adev)) {
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700388 switch (in->source) {
389 case AUDIO_SOURCE_VOICE_UPLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800390 if (audio_extn_compr_cap_enabled() &&
391 audio_extn_compr_cap_format_supported(in->config.format)) {
392 in->usecase = USECASE_INCALL_REC_UPLINK_COMPRESS;
393 } else
394 in->usecase = USECASE_INCALL_REC_UPLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800395 rec_mode = INCALL_REC_UPLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700396 break;
397 case AUDIO_SOURCE_VOICE_DOWNLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800398 if (audio_extn_compr_cap_enabled() &&
399 audio_extn_compr_cap_format_supported(in->config.format)) {
400 in->usecase = USECASE_INCALL_REC_DOWNLINK_COMPRESS;
401 } else
402 in->usecase = USECASE_INCALL_REC_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800403 rec_mode = INCALL_REC_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700404 break;
405 case AUDIO_SOURCE_VOICE_CALL:
Helen Zenge56b4852013-12-03 16:54:40 -0800406 if (audio_extn_compr_cap_enabled() &&
407 audio_extn_compr_cap_format_supported(in->config.format)) {
408 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS;
409 } else
410 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800411 rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700412 break;
413 default:
414 ALOGV("%s: Source type %d doesnt match incall recording criteria",
415 __func__, in->source);
416 return ret;
417 }
418
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700419 session_id = voice_get_active_session_id(adev);
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800420 ret = platform_set_incall_recording_session_id(adev->platform,
421 session_id, rec_mode);
Yunfei Zhang3714bd12019-04-11 09:04:28 +0800422 platform_set_incall_recording_session_channels(adev->platform,
423 in->config.channels);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700424 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
425 } else {
Venkata Narendra Kumar Gutta76440ba2015-03-30 19:16:14 +0530426 /*
427 * Reject the recording instances, where the recording is started
428 * with In-call voice recording source types but voice call is not
429 * active by the time input is started
430 */
431 if ((in->source == AUDIO_SOURCE_VOICE_UPLINK) ||
432 (in->source == AUDIO_SOURCE_VOICE_DOWNLINK) ||
433 (in->source == AUDIO_SOURCE_VOICE_CALL)) {
434 ret = -EINVAL;
435 ALOGE("%s: As voice call is not active, Incall rec usecase can't be \
436 selected for requested source:%d",__func__, in->source);
437 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700438 ALOGV("%s: voice call not active", __func__);
439 }
440
441 return ret;
442}
443
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800444int voice_check_and_stop_incall_rec_usecase(struct audio_device *adev,
445 struct stream_in *in)
446{
447 int ret = 0;
448
449 if (in->source == AUDIO_SOURCE_VOICE_UPLINK ||
450 in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
451 in->source == AUDIO_SOURCE_VOICE_CALL) {
452 ret = platform_stop_incall_recording_usecase(adev->platform);
453 ALOGV("%s: Stop In-call recording", __func__);
454 }
455
456 return ret;
457}
458
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530459snd_device_t voice_get_incall_rec_backend_device(struct stream_in *in)
460{
461 snd_device_t incall_record_device = {0};
462
Aditya Bavanari9e957d82019-01-29 17:47:13 +0530463 if (!in) {
464 ALOGE("%s: input stream is NULL", __func__);
465 return 0;
466 }
467
Divya Narayanan Poojary85d0a592018-02-06 14:25:16 +0530468 switch(in->source) {
469 case AUDIO_SOURCE_VOICE_UPLINK:
470 incall_record_device = SND_DEVICE_IN_INCALL_REC_TX;
471 break;
472 case AUDIO_SOURCE_VOICE_DOWNLINK:
473 incall_record_device = SND_DEVICE_IN_INCALL_REC_RX;
474 break;
475 case AUDIO_SOURCE_VOICE_CALL:
476 incall_record_device = SND_DEVICE_IN_INCALL_REC_RX_TX;
477 break;
478 default:
479 ALOGI("Invalid source %d", in->source);
480 }
481
482 return incall_record_device;
483}
484
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800485snd_device_t voice_get_incall_rec_snd_device(snd_device_t in_snd_device)
486{
487 snd_device_t incall_record_device = in_snd_device;
488
489 /*
490 * For incall recording stream, AUDIO_COPP topology will be picked up
491 * from the calibration data of the input sound device which is nothing
492 * but the voice call's input device. But there are requirements to use
493 * AUDIO_COPP_MONO topology even if the voice call's input device is
494 * different. Hence override the input device with the one which uses
495 * the AUDIO_COPP_MONO topology.
496 */
497 switch(in_snd_device) {
498 case SND_DEVICE_IN_HANDSET_MIC:
499 case SND_DEVICE_IN_VOICE_DMIC:
500 case SND_DEVICE_IN_AANC_HANDSET_MIC:
501 incall_record_device = SND_DEVICE_IN_HANDSET_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800502 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800503 case SND_DEVICE_IN_VOICE_SPEAKER_MIC:
504 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC:
505 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE:
506 case SND_DEVICE_IN_VOICE_SPEAKER_QMIC:
507 incall_record_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800508 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800509 default:
510 incall_record_device = in_snd_device;
511 }
512
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800513 ALOGD("%s: in_snd_device(%d: %s) incall_record_device(%d: %s)", __func__,
514 in_snd_device, platform_get_snd_device_name(in_snd_device),
515 incall_record_device, platform_get_snd_device_name(incall_record_device));
516
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800517 return incall_record_device;
518}
519
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700520int voice_set_mic_mute(struct audio_device *adev, bool state)
521{
522 int err = 0;
523
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800524 adev->voice.mic_mute = state;
Arun Mirpurief53ce52018-09-11 18:00:09 -0700525
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530526 if (audio_extn_hfp_is_active(adev)) {
Arun Mirpurie008ed22019-03-21 11:21:04 -0700527 err = audio_extn_hfp_set_mic_mute2(adev, state);
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530528 } else if (adev->mode == AUDIO_MODE_IN_CALL) {
Arun Mirpurief53ce52018-09-11 18:00:09 -0700529 /* Use device mute if incall music delivery usecase is in progress */
530 if (adev->voice.use_device_mute)
531 err = platform_set_device_mute(adev->platform, state, "tx");
532 else
533 err = platform_set_mic_mute(adev->platform, state);
534 ALOGV("%s: voice mute status=%d, use_device_mute flag=%d",
535 __func__, state, adev->voice.use_device_mute);
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530536 } else if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800537 err = voice_extn_compress_voip_set_mic_mute(adev, state);
Dhanalakshmi Siddani823dc5a2016-09-28 14:47:26 +0530538 }
Arun Mirpurief53ce52018-09-11 18:00:09 -0700539
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700540 return err;
541}
542
543bool voice_get_mic_mute(struct audio_device *adev)
544{
545 return adev->voice.mic_mute;
546}
547
Arun Mirpurief53ce52018-09-11 18:00:09 -0700548/*
549 * Following function is called when incall music uplink usecase is
550 * created or destroyed while mic is muted. If incall music uplink
551 * usecase is active, apply voice device mute to mute only voice Tx
552 * path and not the mixed voice Tx + inncall-music path. Revert to
553 * voice stream mute once incall music uplink usecase is inactive
554 */
555void voice_set_device_mute_flag(struct audio_device *adev, bool state)
556{
557 if (adev->voice.mic_mute) {
558 if (state) {
559 platform_set_device_mute(adev->platform, true, "tx");
560 platform_set_mic_mute(adev->platform, false);
561 } else {
562 platform_set_mic_mute(adev->platform, true);
563 platform_set_device_mute(adev->platform, false, "tx");
564 }
565 }
566 adev->voice.use_device_mute = state;
567}
568
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700569int voice_set_volume(struct audio_device *adev, float volume)
570{
571 int vol, err = 0;
572
Shruthi Krishnaace10852013-10-25 14:32:12 -0700573 adev->voice.volume = volume;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700574 if (adev->mode == AUDIO_MODE_IN_CALL) {
575 if (volume < 0.0) {
576 volume = 0.0;
577 } else if (volume > 1.0) {
578 volume = 1.0;
579 }
580
581 vol = lrint(volume * 100.0);
582
583 // Voice volume levels from android are mapped to driver volume levels as follows.
584 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
585 // So adjust the volume to get the correct volume index in driver
586 vol = 100 - vol;
587
588 err = platform_set_voice_volume(adev->platform, vol);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700589 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800590 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
591 err = voice_extn_compress_voip_set_volume(adev, volume);
592
Shruthi Krishnaace10852013-10-25 14:32:12 -0700593
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700594 return err;
595}
596
597int voice_start_call(struct audio_device *adev)
598{
599 int ret = 0;
600
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -0700601 adev->voice.in_call = true;
Aalique Grahame22e49102018-12-18 14:23:57 -0800602
603 voice_set_mic_mute(adev, adev->voice.mic_mute);
604
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800605 ret = voice_extn_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700606 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700607 ret = voice_start_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700608 }
609
610 return ret;
611}
612
613int voice_stop_call(struct audio_device *adev)
614{
615 int ret = 0;
616
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700617 adev->voice.in_call = false;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800618 ret = voice_extn_stop_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700619 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700620 ret = voice_stop_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700621 }
622
623 return ret;
624}
625
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -0800626void voice_get_parameters(struct audio_device *adev,
627 struct str_parms *query,
628 struct str_parms *reply)
629{
630 voice_extn_get_parameters(adev, query, reply);
631}
632
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700633int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
634{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700635 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800636 int ret = 0, err;
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800637 char *kv_pairs = str_parms_to_str(parms);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700638
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800639 ALOGV_IF(kv_pairs != NULL, "%s: enter: %s", __func__, kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700640
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800641 ret = voice_extn_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700642 if (ret != 0) {
643 if (ret == -ENOSYS)
644 ret = 0;
645 else
646 goto done;
647 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700648
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800649 ret = voice_extn_compress_voip_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700650 if (ret != 0) {
651 if (ret == -ENOSYS)
652 ret = 0;
653 else
654 goto done;
655 }
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800656
657 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
658 if (err >= 0) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700659 int tty_mode;
660 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
661 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
662 tty_mode = TTY_MODE_OFF;
663 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
664 tty_mode = TTY_MODE_VCO;
665 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
666 tty_mode = TTY_MODE_HCO;
667 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
668 tty_mode = TTY_MODE_FULL;
669 else {
670 ret = -EINVAL;
671 goto done;
672 }
673
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700674 if (tty_mode != adev->voice.tty_mode) {
675 adev->voice.tty_mode = tty_mode;
676 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700677 if (voice_is_call_state_active(adev))
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800678 voice_update_devices_for_all_voice_usecases(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700679 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700680 }
681
Aalique Grahame22e49102018-12-18 14:23:57 -0800682 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HAC,
683 value, sizeof(value));
684 if (err >= 0) {
685 bool hac = false;
686 str_parms_del(parms, AUDIO_PARAMETER_KEY_HAC);
687 if (strcmp(value, AUDIO_PARAMETER_VALUE_HAC_ON) == 0)
688 hac = true;
689
690 if (hac != adev->voice.hac) {
691 adev->voice.hac = hac;
692 if (voice_is_in_call(adev))
693 voice_update_devices_for_all_voice_usecases(adev);
694 }
695 }
696
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800697 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800698 value, sizeof(value));
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800699 if (err >= 0) {
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800700 str_parms_del(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC);
701 if (strcmp(value, AUDIO_PARAMETER_VALUE_TRUE) == 0)
702 platform_start_incall_music_usecase(adev->platform);
703 else
704 platform_stop_incall_music_usecase(adev->platform);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700705 }
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800706
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700707done:
708 ALOGV("%s: exit with code(%d)", __func__, ret);
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800709 free(kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700710 return ret;
711}
712
713void voice_init(struct audio_device *adev)
714{
715 int i = 0;
716
717 memset(&adev->voice, 0, sizeof(adev->voice));
718 adev->voice.tty_mode = TTY_MODE_OFF;
Aalique Grahame22e49102018-12-18 14:23:57 -0800719 adev->voice.hac = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700720 adev->voice.volume = 1.0f;
721 adev->voice.mic_mute = false;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700722 adev->voice.in_call = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700723 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
724 adev->voice.session[i].pcm_rx = NULL;
725 adev->voice.session[i].pcm_tx = NULL;
726 adev->voice.session[i].state.current = CALL_INACTIVE;
727 adev->voice.session[i].state.new = CALL_INACTIVE;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -0700728 adev->voice.session[i].vsid = VOICE_VSID;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700729 }
730
731 voice_extn_init(adev);
732}
733
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800734void voice_update_devices_for_all_voice_usecases(struct audio_device *adev)
735{
736 struct listnode *node;
737 struct audio_usecase *usecase;
738
739 list_for_each(node, &adev->usecase_list) {
740 usecase = node_to_item(node, struct audio_usecase, list);
741 if (usecase->type == VOICE_CALL) {
742 ALOGV("%s: updating device for usecase:%s", __func__,
743 use_case_table[usecase->id]);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700744 usecase->stream.out = adev->current_call_output;
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800745 select_devices(adev, usecase->id);
746 }
747 }
748}
749
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700750