blob: 527856f195a0629743e70e5adfb065e8a59f19e5 [file] [log] [blame]
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001/*
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003 * Not a contribution.
4 *
Shiv Maliyappanahalli8911f282014-01-10 15:56:19 -08005 * Copyright (C) 2013 The Android Open Source Project
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "voice"
21/*#define LOG_NDEBUG 0*/
22#define LOG_NDDEBUG 0
23
24#include <errno.h>
Sharad Sangleb27354b2015-06-18 15:58:55 +053025#include <stdlib.h>
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070026#include <math.h>
27#include <cutils/log.h>
28#include <cutils/str_parms.h>
29
30#include "audio_hw.h"
31#include "voice.h"
32#include "voice_extn/voice_extn.h"
33#include "platform.h"
34#include "platform_api.h"
Kiran Kandi910e1862013-10-29 13:29:42 -070035#include "audio_extn.h"
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070036
37struct pcm_config pcm_config_voice_call = {
38 .channels = 1,
39 .rate = 8000,
40 .period_size = 160,
41 .period_count = 2,
42 .format = PCM_FORMAT_S16_LE,
43};
44
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070045static struct voice_session *voice_get_session_from_use_case(struct audio_device *adev,
46 audio_usecase_t usecase_id)
47{
48 struct voice_session *session = NULL;
49 int ret = 0;
50
51 ret = voice_extn_get_session_from_use_case(adev, usecase_id, &session);
52 if (ret == -ENOSYS) {
53 session = &adev->voice.session[VOICE_SESS_IDX];
54 }
55
56 return session;
57}
58
Bhalchandra Gajarefd72d3e2015-06-09 22:23:45 -070059static bool voice_is_sidetone_device(snd_device_t out_device,
60 char *mixer_path)
61{
62 bool is_sidetone_dev;
63
64 switch (out_device) {
65 case SND_DEVICE_OUT_VOICE_HANDSET:
66 is_sidetone_dev = true;
67 strlcpy(mixer_path, "sidetone-handset", MIXER_PATH_MAX_LENGTH);
68 break;
69 case SND_DEVICE_OUT_VOICE_HEADPHONES:
70 case SND_DEVICE_OUT_VOICE_ANC_HEADSET:
71 case SND_DEVICE_OUT_VOICE_ANC_FB_HEADSET:
72 is_sidetone_dev = true;
73 strlcpy(mixer_path, "sidetone-headphones", MIXER_PATH_MAX_LENGTH);
74 break;
75 default:
76 is_sidetone_dev = false;
77 break;
78 }
79
80 return is_sidetone_dev;
81}
82
83void voice_set_sidetone(struct audio_device *adev,
84 snd_device_t out_snd_device, bool enable)
85{
86 char mixer_path[MIXER_PATH_MAX_LENGTH];
87 bool is_sidetone_dev;
88
89 ALOGD("%s: %s, out_snd_device: %d\n",
90 __func__, (enable ? "enable" : "disable"),
91 out_snd_device);
92
93 is_sidetone_dev = voice_is_sidetone_device(out_snd_device, mixer_path);
94
95 if (!is_sidetone_dev) {
96 ALOGD("%s: device %d does not support sidetone\n",
97 __func__, out_snd_device);
98 return;
99 }
100
101 ALOGD("%s: sidetone out device = %s\n",
102 __func__, mixer_path);
103
104 if (enable)
105 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
106 else
107 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
108
109 return;
110}
111
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700112int voice_stop_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700113{
114 int i, ret = 0;
115 struct audio_usecase *uc_info;
116 struct voice_session *session = NULL;
117
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800118 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700119
120 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700121 if (!session) {
122 ALOGE("stop_call: couldn't find voice session");
123 return -EINVAL;
124 }
125
Bhalchandra Gajarefd72d3e2015-06-09 22:23:45 -0700126 uc_info = get_usecase_from_list(adev, usecase_id);
127 if (uc_info == NULL) {
128 ALOGE("%s: Could not find the usecase (%d) in the list",
129 __func__, usecase_id);
130 return -EINVAL;
131 }
132
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700133 session->state.current = CALL_INACTIVE;
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +0530134 if (adev->mode == AUDIO_MODE_NORMAL)
135 adev->voice.is_in_call = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700136
Bhalchandra Gajarefd72d3e2015-06-09 22:23:45 -0700137 /* Disable sidetone only when no calls are active */
138 if (!voice_is_call_state_active(adev))
139 voice_set_sidetone(adev, uc_info->out_snd_device, false);
140
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800141 ret = platform_stop_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700142
143 /* 1. Close the PCM devices */
144 if (session->pcm_rx) {
145 pcm_close(session->pcm_rx);
146 session->pcm_rx = NULL;
147 }
148 if (session->pcm_tx) {
149 pcm_close(session->pcm_tx);
150 session->pcm_tx = NULL;
151 }
152
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700153 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700154 disable_audio_route(adev, uc_info);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700155
156 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700157 disable_snd_device(adev, uc_info->out_snd_device);
158 disable_snd_device(adev, uc_info->in_snd_device);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700159
160 list_remove(&uc_info->list);
161 free(uc_info);
162
163 ALOGD("%s: exit: status(%d)", __func__, ret);
164 return ret;
165}
166
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700167int voice_start_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700168{
169 int i, ret = 0;
170 struct audio_usecase *uc_info;
171 int pcm_dev_rx_id, pcm_dev_tx_id;
Helen Zeng6a16ad72014-02-23 22:04:44 -0800172 uint32_t sample_rate = 8000;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700173 struct voice_session *session = NULL;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700174 struct pcm_config voice_config = pcm_config_voice_call;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800175
176 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700177
178 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700179 if (!session) {
180 ALOGE("start_call: couldn't find voice session");
181 return -EINVAL;
182 }
183
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700184 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700185 if (!uc_info) {
186 ALOGE("start_call: couldn't allocate mem for audio_usecase");
187 return -ENOMEM;
188 }
189
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700190 uc_info->id = usecase_id;
191 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700192 uc_info->stream.out = adev->current_call_output ;
193 uc_info->devices = adev->current_call_output ->devices;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700194 uc_info->in_snd_device = SND_DEVICE_NONE;
195 uc_info->out_snd_device = SND_DEVICE_NONE;
196
197 list_add_tail(&adev->usecase_list, &uc_info->list);
198
199 select_devices(adev, usecase_id);
200
201 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
202 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
203
204 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
205 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
206 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
207 ret = -EIO;
208 goto error_start_voice;
209 }
Helen Zeng6a16ad72014-02-23 22:04:44 -0800210 ret = platform_get_sample_rate(adev->platform, &sample_rate);
211 if (ret < 0) {
212 ALOGE("platform_get_sample_rate error %d\n", ret);
213 } else {
214 voice_config.rate = sample_rate;
215 }
216 ALOGD("voice_config.rate %d\n", voice_config.rate);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700217
Vidyakumar Athotab000e0b2015-04-09 17:45:20 -0700218 voice_set_mic_mute(adev, adev->voice.mic_mute);
219
Bhalchandra Gajarefd72d3e2015-06-09 22:23:45 -0700220 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
221 __func__, adev->snd_card, pcm_dev_tx_id);
222 session->pcm_tx = pcm_open(adev->snd_card,
223 pcm_dev_tx_id,
224 PCM_IN, &voice_config);
225 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
226 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
227 ret = -EIO;
228 goto error_start_voice;
229 }
230
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700231 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800232 __func__, adev->snd_card, pcm_dev_rx_id);
233 session->pcm_rx = pcm_open(adev->snd_card,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700234 pcm_dev_rx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700235 PCM_OUT, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700236 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
237 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
238 ret = -EIO;
239 goto error_start_voice;
240 }
241
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700242 pcm_start(session->pcm_tx);
Bhalchandra Gajarefd72d3e2015-06-09 22:23:45 -0700243 pcm_start(session->pcm_rx);
244
245 /* Enable sidetone only when no calls are already active */
246 if (!voice_is_call_state_active(adev))
247 voice_set_sidetone(adev, uc_info->out_snd_device, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700248
Shruthi Krishnaace10852013-10-25 14:32:12 -0700249 voice_set_volume(adev, adev->voice.volume);
250
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800251 ret = platform_start_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700252 if (ret < 0) {
253 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
254 goto error_start_voice;
255 }
256
257 session->state.current = CALL_ACTIVE;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700258 goto done;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700259
260error_start_voice:
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700261 voice_stop_usecase(adev, usecase_id);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700262
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700263done:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700264 ALOGD("%s: exit: status(%d)", __func__, ret);
265 return ret;
266}
267
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700268bool voice_is_call_state_active(struct audio_device *adev)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700269{
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700270 bool call_state = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700271 int ret = 0;
272
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700273 ret = voice_extn_is_call_state_active(adev, &call_state);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700274 if (ret == -ENOSYS) {
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700275 call_state = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700276 }
277
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700278 return call_state;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700279}
280
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700281bool voice_is_in_call(struct audio_device *adev)
282{
283 return adev->voice.in_call;
284}
285
kunleizc5a639b2014-04-24 18:46:22 +0800286bool voice_is_in_call_rec_stream(struct stream_in *in)
287{
288 bool in_call_rec = false;
kunleizc5a639b2014-04-24 18:46:22 +0800289
Anish Kumar50ebcbf2014-12-09 04:01:39 +0530290 if (!in) {
291 ALOGE("%s: input stream is NULL", __func__);
292 return in_call_rec;
293 }
294
Narsinga Rao Chellad06b0982014-11-20 16:59:57 -0800295 if(in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
296 in->source == AUDIO_SOURCE_VOICE_UPLINK ||
297 in->source == AUDIO_SOURCE_VOICE_CALL) {
298 in_call_rec = true;
kunleizc5a639b2014-04-24 18:46:22 +0800299 }
300
301 return in_call_rec;
302}
303
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700304uint32_t voice_get_active_session_id(struct audio_device *adev)
305{
306 int ret = 0;
307 uint32_t session_id;
308
309 ret = voice_extn_get_active_session_id(adev, &session_id);
310 if (ret == -ENOSYS) {
311 session_id = VOICE_VSID;
312 }
313 return session_id;
314}
315
316int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800317 struct stream_in *in)
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700318{
319 int ret = 0;
320 uint32_t session_id;
321 int usecase_id;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800322 int rec_mode = INCALL_REC_NONE;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700323
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700324 if (voice_is_call_state_active(adev)) {
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700325 switch (in->source) {
326 case AUDIO_SOURCE_VOICE_UPLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800327 if (audio_extn_compr_cap_enabled() &&
328 audio_extn_compr_cap_format_supported(in->config.format)) {
329 in->usecase = USECASE_INCALL_REC_UPLINK_COMPRESS;
330 } else
331 in->usecase = USECASE_INCALL_REC_UPLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800332 rec_mode = INCALL_REC_UPLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700333 break;
334 case AUDIO_SOURCE_VOICE_DOWNLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800335 if (audio_extn_compr_cap_enabled() &&
336 audio_extn_compr_cap_format_supported(in->config.format)) {
337 in->usecase = USECASE_INCALL_REC_DOWNLINK_COMPRESS;
338 } else
339 in->usecase = USECASE_INCALL_REC_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800340 rec_mode = INCALL_REC_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700341 break;
342 case AUDIO_SOURCE_VOICE_CALL:
Helen Zenge56b4852013-12-03 16:54:40 -0800343 if (audio_extn_compr_cap_enabled() &&
344 audio_extn_compr_cap_format_supported(in->config.format)) {
345 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS;
346 } else
347 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800348 rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700349 break;
350 default:
351 ALOGV("%s: Source type %d doesnt match incall recording criteria",
352 __func__, in->source);
353 return ret;
354 }
355
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700356 session_id = voice_get_active_session_id(adev);
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800357 ret = platform_set_incall_recording_session_id(adev->platform,
358 session_id, rec_mode);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700359 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
360 } else {
Venkata Narendra Kumar Gutta76440ba2015-03-30 19:16:14 +0530361 /*
362 * Reject the recording instances, where the recording is started
363 * with In-call voice recording source types but voice call is not
364 * active by the time input is started
365 */
366 if ((in->source == AUDIO_SOURCE_VOICE_UPLINK) ||
367 (in->source == AUDIO_SOURCE_VOICE_DOWNLINK) ||
368 (in->source == AUDIO_SOURCE_VOICE_CALL)) {
369 ret = -EINVAL;
370 ALOGE("%s: As voice call is not active, Incall rec usecase can't be \
371 selected for requested source:%d",__func__, in->source);
372 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700373 ALOGV("%s: voice call not active", __func__);
374 }
375
376 return ret;
377}
378
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800379int voice_check_and_stop_incall_rec_usecase(struct audio_device *adev,
380 struct stream_in *in)
381{
382 int ret = 0;
383
384 if (in->source == AUDIO_SOURCE_VOICE_UPLINK ||
385 in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
386 in->source == AUDIO_SOURCE_VOICE_CALL) {
387 ret = platform_stop_incall_recording_usecase(adev->platform);
388 ALOGV("%s: Stop In-call recording", __func__);
389 }
390
391 return ret;
392}
393
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800394snd_device_t voice_get_incall_rec_snd_device(snd_device_t in_snd_device)
395{
396 snd_device_t incall_record_device = in_snd_device;
397
398 /*
399 * For incall recording stream, AUDIO_COPP topology will be picked up
400 * from the calibration data of the input sound device which is nothing
401 * but the voice call's input device. But there are requirements to use
402 * AUDIO_COPP_MONO topology even if the voice call's input device is
403 * different. Hence override the input device with the one which uses
404 * the AUDIO_COPP_MONO topology.
405 */
406 switch(in_snd_device) {
407 case SND_DEVICE_IN_HANDSET_MIC:
408 case SND_DEVICE_IN_VOICE_DMIC:
409 case SND_DEVICE_IN_AANC_HANDSET_MIC:
410 incall_record_device = SND_DEVICE_IN_HANDSET_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800411 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800412 case SND_DEVICE_IN_VOICE_SPEAKER_MIC:
413 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC:
414 case SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BROADSIDE:
415 case SND_DEVICE_IN_VOICE_SPEAKER_QMIC:
416 incall_record_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800417 break;
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800418 default:
419 incall_record_device = in_snd_device;
420 }
421
Shiv Maliyappanahalli4d2d97c2015-02-19 14:29:01 -0800422 ALOGD("%s: in_snd_device(%d: %s) incall_record_device(%d: %s)", __func__,
423 in_snd_device, platform_get_snd_device_name(in_snd_device),
424 incall_record_device, platform_get_snd_device_name(incall_record_device));
425
Narsinga Rao Chella212e2542014-11-17 19:57:04 -0800426 return incall_record_device;
427}
428
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700429int voice_check_and_set_incall_music_usecase(struct audio_device *adev,
430 struct stream_out *out)
431{
432 int ret = 0;
433
434 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
435 if (ret == -ENOSYS) {
436 /* Incall music delivery is used only for LCH call state */
437 ret = -EINVAL;
438 }
439
440 return ret;
441}
442
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700443int voice_set_mic_mute(struct audio_device *adev, bool state)
444{
445 int err = 0;
446
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800447 adev->voice.mic_mute = state;
448 if (adev->mode == AUDIO_MODE_IN_CALL)
449 err = platform_set_mic_mute(adev->platform, state);
450 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
451 err = voice_extn_compress_voip_set_mic_mute(adev, state);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700452
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700453 return err;
454}
455
456bool voice_get_mic_mute(struct audio_device *adev)
457{
458 return adev->voice.mic_mute;
459}
460
461int voice_set_volume(struct audio_device *adev, float volume)
462{
463 int vol, err = 0;
464
Shruthi Krishnaace10852013-10-25 14:32:12 -0700465 adev->voice.volume = volume;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700466 if (adev->mode == AUDIO_MODE_IN_CALL) {
467 if (volume < 0.0) {
468 volume = 0.0;
469 } else if (volume > 1.0) {
470 volume = 1.0;
471 }
472
473 vol = lrint(volume * 100.0);
474
475 // Voice volume levels from android are mapped to driver volume levels as follows.
476 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
477 // So adjust the volume to get the correct volume index in driver
478 vol = 100 - vol;
479
480 err = platform_set_voice_volume(adev->platform, vol);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700481 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800482 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
483 err = voice_extn_compress_voip_set_volume(adev, volume);
484
Shruthi Krishnaace10852013-10-25 14:32:12 -0700485
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700486 return err;
487}
488
489int voice_start_call(struct audio_device *adev)
490{
491 int ret = 0;
492
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -0700493 adev->voice.in_call = true;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800494 ret = voice_extn_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700495 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700496 ret = voice_start_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700497 }
498
499 return ret;
500}
501
502int voice_stop_call(struct audio_device *adev)
503{
504 int ret = 0;
505
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700506 adev->voice.in_call = false;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800507 ret = voice_extn_stop_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700508 if (ret == -ENOSYS) {
Ravi Kumar Alamanda263d80c2014-08-20 16:24:38 -0700509 ret = voice_stop_usecase(adev, USECASE_VOICE_CALL);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700510 }
511
512 return ret;
513}
514
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -0800515void voice_get_parameters(struct audio_device *adev,
516 struct str_parms *query,
517 struct str_parms *reply)
518{
519 voice_extn_get_parameters(adev, query, reply);
520}
521
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700522int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
523{
524 char *str;
525 char value[32];
526 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800527 int ret = 0, err;
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800528 char *kv_pairs = str_parms_to_str(parms);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700529
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800530 ALOGV_IF(kv_pairs != NULL, "%s: enter: %s", __func__, kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700531
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800532 ret = voice_extn_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700533 if (ret != 0) {
534 if (ret == -ENOSYS)
535 ret = 0;
536 else
537 goto done;
538 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700539
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800540 ret = voice_extn_compress_voip_set_parameters(adev, parms);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700541 if (ret != 0) {
542 if (ret == -ENOSYS)
543 ret = 0;
544 else
545 goto done;
546 }
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800547
548 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
549 if (err >= 0) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700550 int tty_mode;
551 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
552 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
553 tty_mode = TTY_MODE_OFF;
554 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
555 tty_mode = TTY_MODE_VCO;
556 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
557 tty_mode = TTY_MODE_HCO;
558 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
559 tty_mode = TTY_MODE_FULL;
560 else {
561 ret = -EINVAL;
562 goto done;
563 }
564
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700565 if (tty_mode != adev->voice.tty_mode) {
566 adev->voice.tty_mode = tty_mode;
567 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700568 if (voice_is_call_state_active(adev))
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800569 voice_update_devices_for_all_voice_usecases(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700570 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700571 }
572
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800573 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800574 value, sizeof(value));
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800575 if (err >= 0) {
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800576 str_parms_del(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC);
577 if (strcmp(value, AUDIO_PARAMETER_VALUE_TRUE) == 0)
578 platform_start_incall_music_usecase(adev->platform);
579 else
580 platform_stop_incall_music_usecase(adev->platform);
Vidyakumar Athota8d931f02014-07-21 14:51:44 -0700581 }
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800582
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700583done:
584 ALOGV("%s: exit with code(%d)", __func__, ret);
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800585 free(kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700586 return ret;
587}
588
589void voice_init(struct audio_device *adev)
590{
591 int i = 0;
592
593 memset(&adev->voice, 0, sizeof(adev->voice));
594 adev->voice.tty_mode = TTY_MODE_OFF;
595 adev->voice.volume = 1.0f;
596 adev->voice.mic_mute = false;
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700597 adev->voice.in_call = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700598 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
599 adev->voice.session[i].pcm_rx = NULL;
600 adev->voice.session[i].pcm_tx = NULL;
601 adev->voice.session[i].state.current = CALL_INACTIVE;
602 adev->voice.session[i].state.new = CALL_INACTIVE;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -0700603 adev->voice.session[i].vsid = VOICE_VSID;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700604 }
605
606 voice_extn_init(adev);
607}
608
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800609void voice_update_devices_for_all_voice_usecases(struct audio_device *adev)
610{
611 struct listnode *node;
612 struct audio_usecase *usecase;
613
614 list_for_each(node, &adev->usecase_list) {
615 usecase = node_to_item(node, struct audio_usecase, list);
616 if (usecase->type == VOICE_CALL) {
617 ALOGV("%s: updating device for usecase:%s", __func__,
618 use_case_table[usecase->id]);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700619 usecase->stream.out = adev->current_call_output;
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800620 select_devices(adev, usecase->id);
621 }
622 }
623}
624
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700625