blob: cc546f5860144d7fe208c9a07393487ca4ce3101 [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 Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005 * Copyright (C) 2013-2014 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 Maliyappanahalli3bb73582013-11-05 12:49:15 -080044extern const char * const use_case_table[AUDIO_USECASE_MAX];
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070045
46static struct voice_session *voice_get_session_from_use_case(struct audio_device *adev,
47 audio_usecase_t usecase_id)
48{
49 struct voice_session *session = NULL;
50 int ret = 0;
51
52 ret = voice_extn_get_session_from_use_case(adev, usecase_id, &session);
53 if (ret == -ENOSYS) {
54 session = &adev->voice.session[VOICE_SESS_IDX];
55 }
56
57 return session;
58}
59
60int stop_call(struct audio_device *adev, audio_usecase_t usecase_id)
61{
62 int i, ret = 0;
63 struct audio_usecase *uc_info;
64 struct voice_session *session = NULL;
65
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -080066 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070067
68 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
69 session->state.current = CALL_INACTIVE;
70
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -080071 ret = platform_stop_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070072
73 /* 1. Close the PCM devices */
74 if (session->pcm_rx) {
75 pcm_close(session->pcm_rx);
76 session->pcm_rx = NULL;
77 }
78 if (session->pcm_tx) {
79 pcm_close(session->pcm_tx);
80 session->pcm_tx = NULL;
81 }
82
83 uc_info = get_usecase_from_list(adev, usecase_id);
84 if (uc_info == NULL) {
85 ALOGE("%s: Could not find the usecase (%d) in the list",
86 __func__, usecase_id);
87 return -EINVAL;
88 }
89
90 /* 2. Get and set stream specific mixer controls */
91 disable_audio_route(adev, uc_info, true);
92
93 /* 3. Disable the rx and tx devices */
94 disable_snd_device(adev, uc_info->out_snd_device, false);
95 disable_snd_device(adev, uc_info->in_snd_device, true);
96
97 list_remove(&uc_info->list);
98 free(uc_info);
99
100 ALOGD("%s: exit: status(%d)", __func__, ret);
101 return ret;
102}
103
104int start_call(struct audio_device *adev, audio_usecase_t usecase_id)
105{
106 int i, ret = 0;
107 struct audio_usecase *uc_info;
108 int pcm_dev_rx_id, pcm_dev_tx_id;
109 struct voice_session *session = NULL;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700110 struct pcm_config voice_config = pcm_config_voice_call;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800111
112 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700113
114 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700115 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
116 uc_info->id = usecase_id;
117 uc_info->type = VOICE_CALL;
118 uc_info->stream.out = adev->primary_output;
119 uc_info->devices = adev->primary_output->devices;
120 uc_info->in_snd_device = SND_DEVICE_NONE;
121 uc_info->out_snd_device = SND_DEVICE_NONE;
122
123 list_add_tail(&adev->usecase_list, &uc_info->list);
124
125 select_devices(adev, usecase_id);
126
127 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
128 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
129
130 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
131 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
132 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
133 ret = -EIO;
134 goto error_start_voice;
135 }
136
137 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800138 __func__, adev->snd_card, pcm_dev_rx_id);
139 session->pcm_rx = pcm_open(adev->snd_card,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700140 pcm_dev_rx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700141 PCM_OUT, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700142 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
143 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
144 ret = -EIO;
145 goto error_start_voice;
146 }
147
148 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800149 __func__, adev->snd_card, pcm_dev_tx_id);
150 session->pcm_tx = pcm_open(adev->snd_card,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700151 pcm_dev_tx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700152 PCM_IN, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700153 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
154 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
155 ret = -EIO;
156 goto error_start_voice;
157 }
158 pcm_start(session->pcm_rx);
159 pcm_start(session->pcm_tx);
160
Shruthi Krishnaace10852013-10-25 14:32:12 -0700161 voice_set_volume(adev, adev->voice.volume);
162
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800163 ret = platform_start_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700164 if (ret < 0) {
165 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
166 goto error_start_voice;
167 }
168
169 session->state.current = CALL_ACTIVE;
170 return 0;
171
172error_start_voice:
173 stop_call(adev, usecase_id);
174
175 ALOGD("%s: exit: status(%d)", __func__, ret);
176 return ret;
177}
178
179bool voice_is_in_call(struct audio_device *adev)
180{
181 bool in_call = false;
182 int ret = 0;
183
184 ret = voice_extn_is_in_call(adev, &in_call);
185 if (ret == -ENOSYS) {
186 in_call = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
187 }
188
189 return in_call;
190}
191
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700192uint32_t voice_get_active_session_id(struct audio_device *adev)
193{
194 int ret = 0;
195 uint32_t session_id;
196
197 ret = voice_extn_get_active_session_id(adev, &session_id);
198 if (ret == -ENOSYS) {
199 session_id = VOICE_VSID;
200 }
201 return session_id;
202}
203
204int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800205 struct stream_in *in)
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700206{
207 int ret = 0;
208 uint32_t session_id;
209 int usecase_id;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800210 int rec_mode = INCALL_REC_NONE;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700211
212 if (voice_is_in_call(adev)) {
213 switch (in->source) {
214 case AUDIO_SOURCE_VOICE_UPLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800215 if (audio_extn_compr_cap_enabled() &&
216 audio_extn_compr_cap_format_supported(in->config.format)) {
217 in->usecase = USECASE_INCALL_REC_UPLINK_COMPRESS;
218 } else
219 in->usecase = USECASE_INCALL_REC_UPLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800220 rec_mode = INCALL_REC_UPLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700221 break;
222 case AUDIO_SOURCE_VOICE_DOWNLINK:
Helen Zenge56b4852013-12-03 16:54:40 -0800223 if (audio_extn_compr_cap_enabled() &&
224 audio_extn_compr_cap_format_supported(in->config.format)) {
225 in->usecase = USECASE_INCALL_REC_DOWNLINK_COMPRESS;
226 } else
227 in->usecase = USECASE_INCALL_REC_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800228 rec_mode = INCALL_REC_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700229 break;
230 case AUDIO_SOURCE_VOICE_CALL:
Helen Zenge56b4852013-12-03 16:54:40 -0800231 if (audio_extn_compr_cap_enabled() &&
232 audio_extn_compr_cap_format_supported(in->config.format)) {
233 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS;
234 } else
235 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800236 rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700237 break;
238 default:
239 ALOGV("%s: Source type %d doesnt match incall recording criteria",
240 __func__, in->source);
241 return ret;
242 }
243
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700244 session_id = voice_get_active_session_id(adev);
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800245 ret = platform_set_incall_recording_session_id(adev->platform,
246 session_id, rec_mode);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700247 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
248 } else {
249 ALOGV("%s: voice call not active", __func__);
250 }
251
252 return ret;
253}
254
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800255int voice_check_and_stop_incall_rec_usecase(struct audio_device *adev,
256 struct stream_in *in)
257{
258 int ret = 0;
259
260 if (in->source == AUDIO_SOURCE_VOICE_UPLINK ||
261 in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
262 in->source == AUDIO_SOURCE_VOICE_CALL) {
263 ret = platform_stop_incall_recording_usecase(adev->platform);
264 ALOGV("%s: Stop In-call recording", __func__);
265 }
266
267 return ret;
268}
269
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700270int voice_check_and_set_incall_music_usecase(struct audio_device *adev,
271 struct stream_out *out)
272{
273 int ret = 0;
274
275 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
276 if (ret == -ENOSYS) {
277 /* Incall music delivery is used only for LCH call state */
278 ret = -EINVAL;
279 }
280
281 return ret;
282}
283
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700284int voice_set_mic_mute(struct audio_device *adev, bool state)
285{
286 int err = 0;
287
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800288 adev->voice.mic_mute = state;
289 if (adev->mode == AUDIO_MODE_IN_CALL)
290 err = platform_set_mic_mute(adev->platform, state);
291 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
292 err = voice_extn_compress_voip_set_mic_mute(adev, state);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700293
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700294 return err;
295}
296
297bool voice_get_mic_mute(struct audio_device *adev)
298{
299 return adev->voice.mic_mute;
300}
301
302int voice_set_volume(struct audio_device *adev, float volume)
303{
304 int vol, err = 0;
305
Shruthi Krishnaace10852013-10-25 14:32:12 -0700306 adev->voice.volume = volume;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700307 if (adev->mode == AUDIO_MODE_IN_CALL) {
308 if (volume < 0.0) {
309 volume = 0.0;
310 } else if (volume > 1.0) {
311 volume = 1.0;
312 }
313
314 vol = lrint(volume * 100.0);
315
316 // Voice volume levels from android are mapped to driver volume levels as follows.
317 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
318 // So adjust the volume to get the correct volume index in driver
319 vol = 100 - vol;
320
321 err = platform_set_voice_volume(adev->platform, vol);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700322 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800323 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
324 err = voice_extn_compress_voip_set_volume(adev, volume);
325
Shruthi Krishnaace10852013-10-25 14:32:12 -0700326
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700327 return err;
328}
329
330int voice_start_call(struct audio_device *adev)
331{
332 int ret = 0;
333
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800334 ret = voice_extn_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700335 if (ret == -ENOSYS) {
336 ret = start_call(adev, USECASE_VOICE_CALL);
337 }
338
339 return ret;
340}
341
342int voice_stop_call(struct audio_device *adev)
343{
344 int ret = 0;
345
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800346 ret = voice_extn_stop_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700347 if (ret == -ENOSYS) {
348 ret = stop_call(adev, USECASE_VOICE_CALL);
349 }
350
351 return ret;
352}
353
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -0800354void voice_get_parameters(struct audio_device *adev,
355 struct str_parms *query,
356 struct str_parms *reply)
357{
358 voice_extn_get_parameters(adev, query, reply);
359}
360
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700361int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
362{
363 char *str;
364 char value[32];
365 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800366 int ret = 0, err;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700367
368 ALOGV("%s: enter: %s", __func__, str_parms_to_str(parms));
369
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800370 ret = voice_extn_set_parameters(adev, parms);
371 if (ret != 0)
372 goto done;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700373
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800374 ret = voice_extn_compress_voip_set_parameters(adev, parms);
375 if (ret != 0)
376 goto done;
377
378 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
379 if (err >= 0) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700380 int tty_mode;
381 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
382 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
383 tty_mode = TTY_MODE_OFF;
384 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
385 tty_mode = TTY_MODE_VCO;
386 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
387 tty_mode = TTY_MODE_HCO;
388 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
389 tty_mode = TTY_MODE_FULL;
390 else {
391 ret = -EINVAL;
392 goto done;
393 }
394
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700395 if (tty_mode != adev->voice.tty_mode) {
396 adev->voice.tty_mode = tty_mode;
397 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
398 if (voice_is_in_call(adev))
399 //todo: what about voice2, volte and qchat usecases?
400 select_devices(adev, USECASE_VOICE_CALL);
401 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700402 }
403
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800404 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800405 value, sizeof(value));
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800406 if (err >= 0) {
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800407 str_parms_del(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC);
408 if (strcmp(value, AUDIO_PARAMETER_VALUE_TRUE) == 0)
409 platform_start_incall_music_usecase(adev->platform);
410 else
411 platform_stop_incall_music_usecase(adev->platform);
412 }
413
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700414done:
415 ALOGV("%s: exit with code(%d)", __func__, ret);
416 return ret;
417}
418
419void voice_init(struct audio_device *adev)
420{
421 int i = 0;
422
423 memset(&adev->voice, 0, sizeof(adev->voice));
424 adev->voice.tty_mode = TTY_MODE_OFF;
425 adev->voice.volume = 1.0f;
426 adev->voice.mic_mute = false;
Shiv Maliyappanahalli07a9ea22014-01-06 14:53:52 -0800427 adev->voice.voice_device_set = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700428 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
429 adev->voice.session[i].pcm_rx = NULL;
430 adev->voice.session[i].pcm_tx = NULL;
431 adev->voice.session[i].state.current = CALL_INACTIVE;
432 adev->voice.session[i].state.new = CALL_INACTIVE;
433 adev->voice.session[i].vsid = 0;
434 }
435
436 voice_extn_init(adev);
437}
438
439