blob: ac067a3ae0cfb6cea6e53da4ba89434be6af6ff3 [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>
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;
Helen Zeng6a16ad72014-02-23 22:04:44 -0800109 uint32_t sample_rate = 8000;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700110 struct voice_session *session = NULL;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700111 struct pcm_config voice_config = pcm_config_voice_call;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800112
113 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700114
115 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700116 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
117 uc_info->id = usecase_id;
118 uc_info->type = VOICE_CALL;
119 uc_info->stream.out = adev->primary_output;
120 uc_info->devices = adev->primary_output->devices;
121 uc_info->in_snd_device = SND_DEVICE_NONE;
122 uc_info->out_snd_device = SND_DEVICE_NONE;
123
124 list_add_tail(&adev->usecase_list, &uc_info->list);
125
126 select_devices(adev, usecase_id);
127
128 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
129 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
130
131 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
132 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
133 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
134 ret = -EIO;
135 goto error_start_voice;
136 }
Helen Zeng6a16ad72014-02-23 22:04:44 -0800137 ret = platform_get_sample_rate(adev->platform, &sample_rate);
138 if (ret < 0) {
139 ALOGE("platform_get_sample_rate error %d\n", ret);
140 } else {
141 voice_config.rate = sample_rate;
142 }
143 ALOGD("voice_config.rate %d\n", voice_config.rate);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700144
145 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800146 __func__, adev->snd_card, pcm_dev_rx_id);
147 session->pcm_rx = pcm_open(adev->snd_card,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700148 pcm_dev_rx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700149 PCM_OUT, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700150 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
151 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
152 ret = -EIO;
153 goto error_start_voice;
154 }
155
156 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800157 __func__, adev->snd_card, pcm_dev_tx_id);
158 session->pcm_tx = pcm_open(adev->snd_card,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700159 pcm_dev_tx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700160 PCM_IN, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700161 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
162 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
163 ret = -EIO;
164 goto error_start_voice;
165 }
166 pcm_start(session->pcm_rx);
167 pcm_start(session->pcm_tx);
168
Shruthi Krishnaace10852013-10-25 14:32:12 -0700169 voice_set_volume(adev, adev->voice.volume);
170
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800171 ret = platform_start_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700172 if (ret < 0) {
173 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
174 goto error_start_voice;
175 }
176
177 session->state.current = CALL_ACTIVE;
178 return 0;
179
180error_start_voice:
181 stop_call(adev, usecase_id);
182
183 ALOGD("%s: exit: status(%d)", __func__, ret);
184 return ret;
185}
186
187bool voice_is_in_call(struct audio_device *adev)
188{
189 bool in_call = false;
190 int ret = 0;
191
192 ret = voice_extn_is_in_call(adev, &in_call);
193 if (ret == -ENOSYS) {
194 in_call = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
195 }
196
197 return in_call;
198}
199
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700200uint32_t voice_get_active_session_id(struct audio_device *adev)
201{
202 int ret = 0;
203 uint32_t session_id;
204
205 ret = voice_extn_get_active_session_id(adev, &session_id);
206 if (ret == -ENOSYS) {
207 session_id = VOICE_VSID;
208 }
209 return session_id;
210}
211
212int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800213 struct stream_in *in)
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700214{
215 int ret = 0;
216 uint32_t session_id;
217 int usecase_id;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800218 int rec_mode = INCALL_REC_NONE;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700219
220 if (voice_is_in_call(adev)) {
221 switch (in->source) {
222 case AUDIO_SOURCE_VOICE_UPLINK:
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_UPLINK_COMPRESS;
226 } else
227 in->usecase = USECASE_INCALL_REC_UPLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800228 rec_mode = INCALL_REC_UPLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700229 break;
230 case AUDIO_SOURCE_VOICE_DOWNLINK:
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_DOWNLINK_COMPRESS;
234 } else
235 in->usecase = USECASE_INCALL_REC_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800236 rec_mode = INCALL_REC_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700237 break;
238 case AUDIO_SOURCE_VOICE_CALL:
Helen Zenge56b4852013-12-03 16:54:40 -0800239 if (audio_extn_compr_cap_enabled() &&
240 audio_extn_compr_cap_format_supported(in->config.format)) {
241 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS;
242 } else
243 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800244 rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700245 break;
246 default:
247 ALOGV("%s: Source type %d doesnt match incall recording criteria",
248 __func__, in->source);
249 return ret;
250 }
251
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700252 session_id = voice_get_active_session_id(adev);
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800253 ret = platform_set_incall_recording_session_id(adev->platform,
254 session_id, rec_mode);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700255 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
256 } else {
257 ALOGV("%s: voice call not active", __func__);
258 }
259
260 return ret;
261}
262
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800263int voice_check_and_stop_incall_rec_usecase(struct audio_device *adev,
264 struct stream_in *in)
265{
266 int ret = 0;
267
268 if (in->source == AUDIO_SOURCE_VOICE_UPLINK ||
269 in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
270 in->source == AUDIO_SOURCE_VOICE_CALL) {
271 ret = platform_stop_incall_recording_usecase(adev->platform);
272 ALOGV("%s: Stop In-call recording", __func__);
273 }
274
275 return ret;
276}
277
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700278int voice_check_and_set_incall_music_usecase(struct audio_device *adev,
279 struct stream_out *out)
280{
281 int ret = 0;
282
283 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
284 if (ret == -ENOSYS) {
285 /* Incall music delivery is used only for LCH call state */
286 ret = -EINVAL;
287 }
288
289 return ret;
290}
291
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700292int voice_set_mic_mute(struct audio_device *adev, bool state)
293{
294 int err = 0;
295
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800296 adev->voice.mic_mute = state;
297 if (adev->mode == AUDIO_MODE_IN_CALL)
298 err = platform_set_mic_mute(adev->platform, state);
299 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
300 err = voice_extn_compress_voip_set_mic_mute(adev, state);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700301
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700302 return err;
303}
304
305bool voice_get_mic_mute(struct audio_device *adev)
306{
307 return adev->voice.mic_mute;
308}
309
310int voice_set_volume(struct audio_device *adev, float volume)
311{
312 int vol, err = 0;
313
Shruthi Krishnaace10852013-10-25 14:32:12 -0700314 adev->voice.volume = volume;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700315 if (adev->mode == AUDIO_MODE_IN_CALL) {
316 if (volume < 0.0) {
317 volume = 0.0;
318 } else if (volume > 1.0) {
319 volume = 1.0;
320 }
321
322 vol = lrint(volume * 100.0);
323
324 // Voice volume levels from android are mapped to driver volume levels as follows.
325 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
326 // So adjust the volume to get the correct volume index in driver
327 vol = 100 - vol;
328
329 err = platform_set_voice_volume(adev->platform, vol);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700330 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800331 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
332 err = voice_extn_compress_voip_set_volume(adev, volume);
333
Shruthi Krishnaace10852013-10-25 14:32:12 -0700334
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700335 return err;
336}
337
338int voice_start_call(struct audio_device *adev)
339{
340 int ret = 0;
341
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800342 ret = voice_extn_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700343 if (ret == -ENOSYS) {
344 ret = start_call(adev, USECASE_VOICE_CALL);
345 }
346
347 return ret;
348}
349
350int voice_stop_call(struct audio_device *adev)
351{
352 int ret = 0;
353
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800354 ret = voice_extn_stop_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700355 if (ret == -ENOSYS) {
356 ret = stop_call(adev, USECASE_VOICE_CALL);
357 }
358
359 return ret;
360}
361
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -0800362void voice_get_parameters(struct audio_device *adev,
363 struct str_parms *query,
364 struct str_parms *reply)
365{
366 voice_extn_get_parameters(adev, query, reply);
367}
368
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700369int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
370{
371 char *str;
372 char value[32];
373 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800374 int ret = 0, err;
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800375 char *kv_pairs = str_parms_to_str(parms);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700376
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800377 ALOGV_IF(kv_pairs != NULL, "%s: enter: %s", __func__, kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700378
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800379 ret = voice_extn_set_parameters(adev, parms);
380 if (ret != 0)
381 goto done;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700382
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800383 ret = voice_extn_compress_voip_set_parameters(adev, parms);
384 if (ret != 0)
385 goto done;
386
387 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
388 if (err >= 0) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700389 int tty_mode;
390 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
391 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
392 tty_mode = TTY_MODE_OFF;
393 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
394 tty_mode = TTY_MODE_VCO;
395 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
396 tty_mode = TTY_MODE_HCO;
397 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
398 tty_mode = TTY_MODE_FULL;
399 else {
400 ret = -EINVAL;
401 goto done;
402 }
403
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700404 if (tty_mode != adev->voice.tty_mode) {
405 adev->voice.tty_mode = tty_mode;
406 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
407 if (voice_is_in_call(adev))
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800408 voice_update_devices_for_all_voice_usecases(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700409 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700410 }
411
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800412 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC,
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800413 value, sizeof(value));
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -0800414 if (err >= 0) {
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800415 str_parms_del(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC);
416 if (strcmp(value, AUDIO_PARAMETER_VALUE_TRUE) == 0)
417 platform_start_incall_music_usecase(adev->platform);
418 else
419 platform_stop_incall_music_usecase(adev->platform);
420 }
421
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700422done:
423 ALOGV("%s: exit with code(%d)", __func__, ret);
Krishnankutty Kolathappilly061a9492014-01-31 18:12:13 -0800424 free(kv_pairs);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700425 return ret;
426}
427
428void voice_init(struct audio_device *adev)
429{
430 int i = 0;
431
432 memset(&adev->voice, 0, sizeof(adev->voice));
433 adev->voice.tty_mode = TTY_MODE_OFF;
434 adev->voice.volume = 1.0f;
435 adev->voice.mic_mute = false;
Shiv Maliyappanahalli07a9ea22014-01-06 14:53:52 -0800436 adev->voice.voice_device_set = false;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700437 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
438 adev->voice.session[i].pcm_rx = NULL;
439 adev->voice.session[i].pcm_tx = NULL;
440 adev->voice.session[i].state.current = CALL_INACTIVE;
441 adev->voice.session[i].state.new = CALL_INACTIVE;
442 adev->voice.session[i].vsid = 0;
443 }
444
445 voice_extn_init(adev);
446}
447
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -0800448void voice_update_devices_for_all_voice_usecases(struct audio_device *adev)
449{
450 struct listnode *node;
451 struct audio_usecase *usecase;
452
453 list_for_each(node, &adev->usecase_list) {
454 usecase = node_to_item(node, struct audio_usecase, list);
455 if (usecase->type == VOICE_CALL) {
456 ALOGV("%s: updating device for usecase:%s", __func__,
457 use_case_table[usecase->id]);
458 select_devices(adev, usecase->id);
459 }
460 }
461}
462
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700463