blob: b1532f6db6c362f451c72d398c2a861bd53d2378 [file] [log] [blame]
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001/*
Bhalchandra Gajare45fee282015-06-09 22:23:45 -07002 * Copyright (c) 2013-2015, 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>
25#include <math.h>
26#include <cutils/log.h>
27#include <cutils/str_parms.h>
28
29#include "audio_hw.h"
30#include "voice.h"
31#include "voice_extn/voice_extn.h"
32#include "platform.h"
33#include "platform_api.h"
Kiran Kandi910e1862013-10-29 13:29:42 -070034#include "audio_extn.h"
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070035
36struct pcm_config pcm_config_voice_call = {
37 .channels = 1,
38 .rate = 8000,
39 .period_size = 160,
40 .period_count = 2,
41 .format = PCM_FORMAT_S16_LE,
42};
43
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070044static struct voice_session *voice_get_session_from_use_case(struct audio_device *adev,
45 audio_usecase_t usecase_id)
46{
47 struct voice_session *session = NULL;
48 int ret = 0;
49
50 ret = voice_extn_get_session_from_use_case(adev, usecase_id, &session);
51 if (ret == -ENOSYS) {
52 session = &adev->voice.session[VOICE_SESS_IDX];
53 }
54
55 return session;
56}
57
Bhalchandra Gajare45fee282015-06-09 22:23:45 -070058static bool voice_is_sidetone_device(snd_device_t out_device,
59 char *mixer_path)
60{
61 bool is_sidetone_dev;
62
63 switch (out_device) {
64 case SND_DEVICE_OUT_VOICE_HANDSET:
65 is_sidetone_dev = true;
66 strlcpy(mixer_path, "sidetone-handset", MIXER_PATH_MAX_LENGTH);
67 break;
68 case SND_DEVICE_OUT_VOICE_HEADPHONES:
69 case SND_DEVICE_OUT_VOICE_ANC_HEADSET:
70 case SND_DEVICE_OUT_VOICE_ANC_FB_HEADSET:
71 is_sidetone_dev = true;
72 strlcpy(mixer_path, "sidetone-headphones", MIXER_PATH_MAX_LENGTH);
73 break;
74 default:
75 is_sidetone_dev = false;
76 break;
77 }
78
79 return is_sidetone_dev;
80}
81
82void voice_set_sidetone(struct audio_device *adev,
83 snd_device_t out_snd_device, bool enable)
84{
85 char mixer_path[MIXER_PATH_MAX_LENGTH];
86 bool is_sidetone_dev;
87
88 ALOGD("%s: %s, out_snd_device: %d\n",
89 __func__, (enable ? "enable" : "disable"),
90 out_snd_device);
91
92 is_sidetone_dev = voice_is_sidetone_device(out_snd_device, mixer_path);
93
94 if (!is_sidetone_dev) {
95 ALOGD("%s: device %d does not support sidetone\n",
96 __func__, out_snd_device);
97 return;
98 }
99
100 ALOGD("%s: sidetone out device = %s\n",
101 __func__, mixer_path);
102
103 if (enable)
104 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
105 else
106 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
107
108 return;
109}
110
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700111int voice_stop_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700112{
113 int i, ret = 0;
114 struct audio_usecase *uc_info;
115 struct voice_session *session = NULL;
116
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800117 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700118
119 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700120 if (!session) {
121 ALOGE("stop_call: couldn't find voice session");
122 return -EINVAL;
123 }
124
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700125 uc_info = get_usecase_from_list(adev, usecase_id);
126 if (uc_info == NULL) {
127 ALOGE("%s: Could not find the usecase (%d) in the list",
128 __func__, usecase_id);
129 return -EINVAL;
130 }
131
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700132 session->state.current = CALL_INACTIVE;
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +0530133 if (adev->mode == AUDIO_MODE_NORMAL)
134 adev->voice.is_in_call = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700135
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700136 /* Disable sidetone only when no calls are active */
137 if (!voice_is_call_state_active(adev))
138 voice_set_sidetone(adev, uc_info->out_snd_device, false);
139
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800140 ret = platform_stop_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700141
142 /* 1. Close the PCM devices */
143 if (session->pcm_rx) {
144 pcm_close(session->pcm_rx);
145 session->pcm_rx = NULL;
146 }
147 if (session->pcm_tx) {
148 pcm_close(session->pcm_tx);
149 session->pcm_tx = NULL;
150 }
151
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700152 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700153 disable_audio_route(adev, uc_info);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700154
155 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700156 disable_snd_device(adev, uc_info->out_snd_device);
157 disable_snd_device(adev, uc_info->in_snd_device);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700158
159 list_remove(&uc_info->list);
160 free(uc_info);
161
162 ALOGD("%s: exit: status(%d)", __func__, ret);
163 return ret;
164}
165
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700166int voice_start_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700167{
168 int i, ret = 0;
169 struct audio_usecase *uc_info;
170 int pcm_dev_rx_id, pcm_dev_tx_id;
Helen Zeng6a16ad72014-02-23 22:04:44 -0800171 uint32_t sample_rate = 8000;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700172 struct voice_session *session = NULL;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700173 struct pcm_config voice_config = pcm_config_voice_call;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800174
175 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700176
177 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700178 if (!session) {
179 ALOGE("start_call: couldn't find voice session");
180 return -EINVAL;
181 }
182
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700183 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700184 if (!uc_info) {
185 ALOGE("start_call: couldn't allocate mem for audio_usecase");
186 return -ENOMEM;
187 }
188
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700189 uc_info->id = usecase_id;
190 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700191 uc_info->stream.out = adev->current_call_output ;
192 uc_info->devices = adev->current_call_output ->devices;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700193 uc_info->in_snd_device = SND_DEVICE_NONE;
194 uc_info->out_snd_device = SND_DEVICE_NONE;
195
196 list_add_tail(&adev->usecase_list, &uc_info->list);
197
198 select_devices(adev, usecase_id);
199
200 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
201 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
202
203 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
204 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
205 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
206 ret = -EIO;
207 goto error_start_voice;
208 }
Helen Zeng6a16ad72014-02-23 22:04:44 -0800209 ret = platform_get_sample_rate(adev->platform, &sample_rate);
210 if (ret < 0) {
211 ALOGE("platform_get_sample_rate error %d\n", ret);
212 } else {
213 voice_config.rate = sample_rate;
214 }
215 ALOGD("voice_config.rate %d\n", voice_config.rate);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700216
Vidyakumar Athotab000e0b2015-04-09 17:45:20 -0700217 voice_set_mic_mute(adev, adev->voice.mic_mute);
218
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700219 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
220 __func__, adev->snd_card, pcm_dev_tx_id);
221 session->pcm_tx = pcm_open(adev->snd_card,
222 pcm_dev_tx_id,
223 PCM_IN, &voice_config);
224 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
225 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
226 ret = -EIO;
227 goto error_start_voice;
228 }
229
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700230 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800231 __func__, adev->snd_card, pcm_dev_rx_id);
232 session->pcm_rx = pcm_open(adev->snd_card,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700233 pcm_dev_rx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700234 PCM_OUT, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700235 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
236 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
237 ret = -EIO;
238 goto error_start_voice;
239 }
240
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700241 pcm_start(session->pcm_tx);
Bhalchandra Gajare45fee282015-06-09 22:23:45 -0700242 pcm_start(session->pcm_rx);
243
244 /* Enable sidetone only when no calls are already active */
245 if (!voice_is_call_state_active(adev))
246 voice_set_sidetone(adev, uc_info->out_snd_device, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700247
Shruthi Krishnaace10852013-10-25 14:32:12 -0700248 voice_set_volume(adev, adev->voice.volume);
249
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800250 ret = platform_start_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700251 if (ret < 0) {
252 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
253 goto error_start_voice;
254 }
255
256 session->state.current = CALL_ACTIVE;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700257 goto done;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700258
259error_start_voice:
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700260 voice_stop_usecase(adev, usecase_id);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700261
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700262done:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700263 ALOGD("%s: exit: status(%d)", __func__, ret);
264 return ret;
265}
266
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700267bool voice_is_call_state_active(struct audio_device *adev)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700268{
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700269 bool call_state = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700270 int ret = 0;
271
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700272 ret = voice_extn_is_call_state_active(adev, &call_state);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700273 if (ret == -ENOSYS) {
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700274 call_state = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700275 }
276
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700277 return call_state;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700278}
279
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700280bool voice_is_in_call(struct audio_device *adev)
281{
282 return adev->voice.in_call;
283}
284
kunleizc5a639b2014-04-24 18:46:22 +0800285bool voice_is_in_call_rec_stream(struct stream_in *in)
286{
287 bool in_call_rec = false;
kunleizc5a639b2014-04-24 18:46:22 +0800288
Anish Kumar50ebcbf2014-12-09 04:01:39 +0530289 if (!in) {
290 ALOGE("%s: input stream is NULL", __func__);
291 return in_call_rec;
292 }
293
Narsinga Rao Chellad06b0982014-11-20 16:59:57 -0800294 if(in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
295 in->source == AUDIO_SOURCE_VOICE_UPLINK ||
296 in->source == AUDIO_SOURCE_VOICE_CALL) {
297 in_call_rec = true;
kunleizc5a639b2014-04-24 18:46:22 +0800298 }
299
300 return in_call_rec;
301}
302
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700303uint32_t voice_get_active_session_id(struct audio_device *adev)
304{
305 int ret = 0;
306 uint32_t session_id;
307
308 ret = voice_extn_get_active_session_id(adev, &session_id);
309 if (ret == -ENOSYS) {
310 session_id = VOICE_VSID;
311 }
312 return session_id;
313}
314
315int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800316 struct stream_in *in)
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700317{
318 int ret = 0;
319 uint32_t session_id;
320 int usecase_id;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800321 int rec_mode = INCALL_REC_NONE;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700322
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700323 if (voice_is_call_state_active(adev)) {
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700324 switch (in->source) {
325 case AUDIO_SOURCE_VOICE_UPLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800326 if (audio_extn_compr_cap_enabled() &&
327 audio_extn_compr_cap_format_supported(in->config.format)) {
328 in->usecase = USECASE_INCALL_REC_UPLINK_COMPRESS;
329 } else
330 in->usecase = USECASE_INCALL_REC_UPLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800331 rec_mode = INCALL_REC_UPLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700332 break;
333 case AUDIO_SOURCE_VOICE_DOWNLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800334 if (audio_extn_compr_cap_enabled() &&
335 audio_extn_compr_cap_format_supported(in->config.format)) {
336 in->usecase = USECASE_INCALL_REC_DOWNLINK_COMPRESS;
337 } else
338 in->usecase = USECASE_INCALL_REC_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800339 rec_mode = INCALL_REC_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700340 break;
341 case AUDIO_SOURCE_VOICE_CALL:
Helen Zenge56b4852013-12-03 16:54:40 -0800342 if (audio_extn_compr_cap_enabled() &&
343 audio_extn_compr_cap_format_supported(in->config.format)) {
344 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS;
345 } else
346 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800347 rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700348 break;
349 default:
350 ALOGV("%s: Source type %d doesnt match incall recording criteria",
351 __func__, in->source);
352 return ret;
353 }
354
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700355 session_id = voice_get_active_session_id(adev);
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800356 ret = platform_set_incall_recording_session_id(adev->platform,
357 session_id, rec_mode);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700358 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
359 } else {
Venkata Narendra Kumar Gutta76440ba2015-03-30 19:16:14 +0530360 /*
361 * Reject the recording instances, where the recording is started
362 * with In-call voice recording source types but voice call is not
363 * active by the time input is started
364 */
365 if ((in->source == AUDIO_SOURCE_VOICE_UPLINK) ||
366 (in->source == AUDIO_SOURCE_VOICE_DOWNLINK) ||
367 (in->source == AUDIO_SOURCE_VOICE_CALL)) {
368 ret = -EINVAL;
369 ALOGE("%s: As voice call is not active, Incall rec usecase can't be \
370 selected for requested source:%d",__func__, in->source);
371 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700372 ALOGV("%s: voice call not active", __func__);
373 }
374
375 return ret;
376}
377
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800378int voice_check_and_stop_incall_rec_usecase(struct audio_device *adev,
379 struct stream_in *in)
380{
381 int ret = 0;
382
383 if (in->source == AUDIO_SOURCE_VOICE_UPLINK ||
384 in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
385 in->source == AUDIO_SOURCE_VOICE_CALL) {
386 ret = platform_stop_incall_recording_usecase(adev->platform);
387 ALOGV("%s: Stop In-call recording", __func__);
388 }
389
390 return ret;
391}
392
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800393snd_device_t voice_get_incall_rec_snd_device(snd_device_t in_snd_device)
394{
395 snd_device_t incall_record_device = in_snd_device;
396
397 /*
398 * For incall recording stream, AUDIO_COPP topology will be picked up
399 * from the calibration data of the input sound device which is nothing
400 * but the voice call's input device. But there are requirements to use
401 * AUDIO_COPP_MONO topology even if the voice call's input device is
402 * different. Hence override the input device with the one which uses
403 * the AUDIO_COPP_MONO topology.
404 */
405 switch(in_snd_device) {
406 case SND_DEVICE_IN_HANDSET_MIC:
407 case SND_DEVICE_IN_VOICE_DMIC:
408 case SND_DEVICE_IN_AANC_HANDSET_MIC:
409 incall_record_device = SND_DEVICE_IN_HANDSET_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800410 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800411 case SND_DEVICE_IN_VOICE_SPEAKER_MIC:
412 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC:
413 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE:
414 case SND_DEVICE_IN_VOICE_SPEAKER_QMIC:
415 incall_record_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800416 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800417 default:
418 incall_record_device = in_snd_device;
419 }
420
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800421 ALOGD("%s: in_snd_device(%d: %s) incall_record_device(%d: %s)", __func__,
422 in_snd_device, platform_get_snd_device_name(in_snd_device),
423 incall_record_device, platform_get_snd_device_name(incall_record_device));
424
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800425 return incall_record_device;
426}
427
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700428int voice_check_and_set_incall_music_usecase(struct audio_device *adev,
429 struct stream_out *out)
430{
431 int ret = 0;
432
433 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
434 if (ret == -ENOSYS) {
435 /* Incall music delivery is used only for LCH call state */
436 ret = -EINVAL;
437 }
438
439 return ret;
440}
441
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700442int voice_set_mic_mute(struct audio_device *adev, bool state)
443{
444 int err = 0;
445
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800446 adev->voice.mic_mute = state;
447 if (adev->mode == AUDIO_MODE_IN_CALL)
448 err = platform_set_mic_mute(adev->platform, state);
449 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
450 err = voice_extn_compress_voip_set_mic_mute(adev, state);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700451
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700452 return err;
453}
454
455bool voice_get_mic_mute(struct audio_device *adev)
456{
457 return adev->voice.mic_mute;
458}
459
460int voice_set_volume(struct audio_device *adev, float volume)
461{
462 int vol, err = 0;
463
Shruthi Krishnaace10852013-10-25 14:32:12 -0700464 adev->voice.volume = volume;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700465 if (adev->mode == AUDIO_MODE_IN_CALL) {
466 if (volume < 0.0) {
467 volume = 0.0;
468 } else if (volume > 1.0) {
469 volume = 1.0;
470 }
471
472 vol = lrint(volume * 100.0);
473
474 // Voice volume levels from android are mapped to driver volume levels as follows.
475 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
476 // So adjust the volume to get the correct volume index in driver
477 vol = 100 - vol;
478
479 err = platform_set_voice_volume(adev->platform, vol);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700480 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800481 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
482 err = voice_extn_compress_voip_set_volume(adev, volume);
483
Shruthi Krishnaace10852013-10-25 14:32:12 -0700484
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700485 return err;
486}
487
488int voice_start_call(struct audio_device *adev)
489{
490 int ret = 0;
491
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -0700492 adev->voice.in_call = true;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800493 ret = voice_extn_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700494 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700495 ret = voice_start_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700496 }
497
498 return ret;
499}
500
501int voice_stop_call(struct audio_device *adev)
502{
503 int ret = 0;
504
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700505 adev->voice.in_call = false;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800506 ret = voice_extn_stop_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700507 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700508 ret = voice_stop_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700509 }
510
511 return ret;
512}
513
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -0800514void voice_get_parameters(struct audio_device *adev,
515 struct str_parms *query,
516 struct str_parms *reply)
517{
518 voice_extn_get_parameters(adev, query, reply);
519}
520
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700521int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
522{
523 char *str;
524 char value[32];
525 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800526 int ret = 0, err;
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800527 char *kv_pairs = str_parms_to_str(parms);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700528
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800529 ALOGV_IF(kv_pairs != NULL, "%s: enter: %s", __func__, kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700530
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800531 ret = voice_extn_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700532 if (ret != 0) {
533 if (ret == -ENOSYS)
534 ret = 0;
535 else
536 goto done;
537 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700538
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800539 ret = voice_extn_compress_voip_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700540 if (ret != 0) {
541 if (ret == -ENOSYS)
542 ret = 0;
543 else
544 goto done;
545 }
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800546
547 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
548 if (err >= 0) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700549 int tty_mode;
550 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
551 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
552 tty_mode = TTY_MODE_OFF;
553 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
554 tty_mode = TTY_MODE_VCO;
555 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
556 tty_mode = TTY_MODE_HCO;
557 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
558 tty_mode = TTY_MODE_FULL;
559 else {
560 ret = -EINVAL;
561 goto done;
562 }
563
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700564 if (tty_mode != adev->voice.tty_mode) {
565 adev->voice.tty_mode = tty_mode;
566 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700567 if (voice_is_call_state_active(adev))
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800568 voice_update_devices_for_all_voice_usecases(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700569 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700570 }
571
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800572 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800573 value, sizeof(value));
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800574 if (err >= 0) {
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800575 str_parms_del(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC);
576 if (strcmp(value, AUDIO_PARAMETER_VALUE_TRUE) == 0)
577 platform_start_incall_music_usecase(adev->platform);
578 else
579 platform_stop_incall_music_usecase(adev->platform);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700580 }
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800581
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700582done:
583 ALOGV("%s: exit with code(%d)", __func__, ret);
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800584 free(kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700585 return ret;
586}
587
588void voice_init(struct audio_device *adev)
589{
590 int i = 0;
591
592 memset(&adev->voice, 0, sizeof(adev->voice));
593 adev->voice.tty_mode = TTY_MODE_OFF;
594 adev->voice.volume = 1.0f;
595 adev->voice.mic_mute = false;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700596 adev->voice.in_call = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700597 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
598 adev->voice.session[i].pcm_rx = NULL;
599 adev->voice.session[i].pcm_tx = NULL;
600 adev->voice.session[i].state.current = CALL_INACTIVE;
601 adev->voice.session[i].state.new = CALL_INACTIVE;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -0700602 adev->voice.session[i].vsid = VOICE_VSID;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700603 }
604
605 voice_extn_init(adev);
606}
607
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800608void voice_update_devices_for_all_voice_usecases(struct audio_device *adev)
609{
610 struct listnode *node;
611 struct audio_usecase *usecase;
612
613 list_for_each(node, &adev->usecase_list) {
614 usecase = node_to_item(node, struct audio_usecase, list);
615 if (usecase->type == VOICE_CALL) {
616 ALOGV("%s: updating device for usecase:%s", __func__,
617 use_case_table[usecase->id]);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700618 usecase->stream.out = adev->current_call_output;
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800619 select_devices(adev, usecase->id);
620 }
621 }
622}
623
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700624