blob: f3250c940716f8aecdfeb27158a4d27587baa58b [file] [log] [blame]
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "voice"
18/*#define LOG_NDEBUG 0*/
19#define LOG_NDDEBUG 0
20
Vineeta Srivastava7c685a12015-03-20 15:34:33 -070021#include <stdlib.h>
Vineeta Srivastava4b89e372014-06-19 14:21:42 -070022#include <errno.h>
23#include <math.h>
24#include <cutils/log.h>
25#include <cutils/str_parms.h>
26
27#include "audio_hw.h"
28#include "voice.h"
29#include "voice_extn/voice_extn.h"
30#include "platform.h"
31#include "platform_api.h"
32
33struct pcm_config pcm_config_voice_call = {
34 .channels = 1,
35 .rate = 8000,
36 .period_size = 160,
37 .period_count = 2,
38 .format = PCM_FORMAT_S16_LE,
39};
40
Vineeta Srivastava4b89e372014-06-19 14:21:42 -070041static struct voice_session *voice_get_session_from_use_case(struct audio_device *adev,
42 audio_usecase_t usecase_id)
43{
44 struct voice_session *session = NULL;
45 int ret = 0;
46
47 ret = voice_extn_get_session_from_use_case(adev, usecase_id, &session);
48 if (ret == -ENOSYS) {
49 session = &adev->voice.session[VOICE_SESS_IDX];
50 }
51
52 return session;
53}
54
vivek mehta765eb642015-08-07 19:46:06 -070055static bool voice_is_sidetone_device(snd_device_t out_device,
56 char *mixer_path)
57{
58 bool is_sidetone_dev = true;
59
60 switch (out_device) {
61 case SND_DEVICE_OUT_VOICE_HAC_HANDSET:
62 strlcpy(mixer_path, "sidetone-hac-handset", MIXER_PATH_MAX_LENGTH);
63 break;
64 case SND_DEVICE_OUT_VOICE_HANDSET:
65 strlcpy(mixer_path, "sidetone-handset", MIXER_PATH_MAX_LENGTH);
66 break;
67 case SND_DEVICE_OUT_VOICE_HEADPHONES:
68 strlcpy(mixer_path, "sidetone-headphones", MIXER_PATH_MAX_LENGTH);
69 break;
70 default:
71 is_sidetone_dev = false;
72 break;
73 }
74
75 return is_sidetone_dev;
76}
77
78void voice_set_sidetone(struct audio_device *adev,
79 snd_device_t out_snd_device, bool enable)
80{
81 char mixer_path[MIXER_PATH_MAX_LENGTH];
82 bool is_sidetone_dev;
83
84 ALOGD("%s: %s, out_snd_device: %d\n",
85 __func__, (enable ? "enable" : "disable"),
86 out_snd_device);
87
88 is_sidetone_dev = voice_is_sidetone_device(out_snd_device, mixer_path);
89
90 if (!is_sidetone_dev) {
91 ALOGD("%s: device %d does not support sidetone\n",
92 __func__, out_snd_device);
93 return;
94 }
95
96 ALOGD("%s: sidetone out device = %s\n",
97 __func__, mixer_path);
98
99 if (enable)
100 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
101 else
102 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
103
104 return;
105}
106
Ravi Kumar Alamanda08dbcfc2014-08-20 16:24:38 -0700107int voice_stop_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700108{
109 int i, ret = 0;
110 struct audio_usecase *uc_info;
111 struct voice_session *session = NULL;
112
113 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
114
115 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
vivek mehta765eb642015-08-07 19:46:06 -0700116
117 uc_info = get_usecase_from_list(adev, usecase_id);
118 if (uc_info == NULL) {
119 ALOGE("%s: Could not find the usecase (%d) in the list",
120 __func__, usecase_id);
121 return -EINVAL;
122 }
123
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700124 session->state.current = CALL_INACTIVE;
125
vivek mehta765eb642015-08-07 19:46:06 -0700126 /* Disable sidetone only when no calls are active */
vivek mehta43e14822016-06-29 12:00:05 -0700127 if (!voice_is_call_state_active(adev)) {
128 adev->voice.in_call = false;
vivek mehta765eb642015-08-07 19:46:06 -0700129 voice_set_sidetone(adev, uc_info->out_snd_device, false);
vivek mehta43e14822016-06-29 12:00:05 -0700130 }
vivek mehta765eb642015-08-07 19:46:06 -0700131
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700132 ret = platform_stop_voice_call(adev->platform, session->vsid);
133
134 /* 1. Close the PCM devices */
135 if (session->pcm_rx) {
136 pcm_close(session->pcm_rx);
137 session->pcm_rx = NULL;
138 }
139 if (session->pcm_tx) {
140 pcm_close(session->pcm_tx);
141 session->pcm_tx = NULL;
142 }
143
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700144 /* 2. Get and set stream specific mixer controls */
145 disable_audio_route(adev, uc_info);
146
147 /* 3. Disable the rx and tx devices */
148 disable_snd_device(adev, uc_info->out_snd_device);
149 disable_snd_device(adev, uc_info->in_snd_device);
150
151 list_remove(&uc_info->list);
152 free(uc_info);
153
154 ALOGD("%s: exit: status(%d)", __func__, ret);
155 return ret;
156}
157
Eric Laurent75a2e1d2014-08-29 12:38:47 -0700158int voice_start_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700159{
160 int i, ret = 0;
161 struct audio_usecase *uc_info;
162 int pcm_dev_rx_id, pcm_dev_tx_id;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700163 struct voice_session *session = NULL;
164 struct pcm_config voice_config = pcm_config_voice_call;
165
166 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
167
168 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
169 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
170 uc_info->id = usecase_id;
171 uc_info->type = VOICE_CALL;
Eric Laurent75a2e1d2014-08-29 12:38:47 -0700172 uc_info->stream.out = adev->current_call_output ;
173 uc_info->devices = adev->current_call_output ->devices;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700174 uc_info->in_snd_device = SND_DEVICE_NONE;
175 uc_info->out_snd_device = SND_DEVICE_NONE;
176
177 list_add_tail(&adev->usecase_list, &uc_info->list);
178
179 select_devices(adev, usecase_id);
180
181 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
182 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
183
184 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
185 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
186 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
187 ret = -EIO;
188 goto error_start_voice;
189 }
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700190
vivek mehta765eb642015-08-07 19:46:06 -0700191 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
192 __func__, adev->snd_card, pcm_dev_tx_id);
193 session->pcm_tx = pcm_open(adev->snd_card,
194 pcm_dev_tx_id,
195 PCM_IN, &voice_config);
196 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
197 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
198 ret = -EIO;
199 goto error_start_voice;
200 }
201
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700202 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
203 __func__, adev->snd_card, pcm_dev_rx_id);
204 session->pcm_rx = pcm_open(adev->snd_card,
205 pcm_dev_rx_id,
206 PCM_OUT, &voice_config);
207 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
208 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
209 ret = -EIO;
210 goto error_start_voice;
211 }
212
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700213 pcm_start(session->pcm_tx);
vivek mehta765eb642015-08-07 19:46:06 -0700214 pcm_start(session->pcm_rx);
215
216 /* Enable sidetone only when no calls are already active */
217 if (!voice_is_call_state_active(adev))
218 voice_set_sidetone(adev, uc_info->out_snd_device, true);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700219
220 voice_set_volume(adev, adev->voice.volume);
221
222 ret = platform_start_voice_call(adev->platform, session->vsid);
223 if (ret < 0) {
224 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
225 goto error_start_voice;
226 }
227
228 session->state.current = CALL_ACTIVE;
Ravi Kumar Alamandaf1819242014-08-05 18:20:42 -0700229 goto done;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700230
231error_start_voice:
Ravi Kumar Alamanda08dbcfc2014-08-20 16:24:38 -0700232 voice_stop_usecase(adev, usecase_id);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700233
Ravi Kumar Alamandaf1819242014-08-05 18:20:42 -0700234done:
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700235 ALOGD("%s: exit: status(%d)", __func__, ret);
236 return ret;
237}
238
Ravi Kumar Alamandaf1819242014-08-05 18:20:42 -0700239bool voice_is_call_state_active(struct audio_device *adev)
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700240{
Ravi Kumar Alamandaf1819242014-08-05 18:20:42 -0700241 bool call_state = false;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700242 int ret = 0;
243
Ravi Kumar Alamandaf1819242014-08-05 18:20:42 -0700244 ret = voice_extn_is_call_state_active(adev, &call_state);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700245 if (ret == -ENOSYS) {
Ravi Kumar Alamandaf1819242014-08-05 18:20:42 -0700246 call_state = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700247 }
248
Ravi Kumar Alamandaf1819242014-08-05 18:20:42 -0700249 return call_state;
250}
251
252bool voice_is_in_call(struct audio_device *adev)
253{
254 return adev->voice.in_call;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700255}
256
257bool voice_is_in_call_rec_stream(struct stream_in *in)
258{
259 bool in_call_rec = false;
260 int ret = 0;
261
262 ret = voice_extn_is_in_call_rec_stream(in, &in_call_rec);
263 if (ret == -ENOSYS) {
264 in_call_rec = false;
265 }
266
267 return in_call_rec;
268}
269
270uint32_t voice_get_active_session_id(struct audio_device *adev)
271{
272 int ret = 0;
273 uint32_t session_id;
274
275 ret = voice_extn_get_active_session_id(adev, &session_id);
276 if (ret == -ENOSYS) {
277 session_id = VOICE_VSID;
278 }
279 return session_id;
280}
281
282int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
283 struct stream_in *in)
284{
285 int ret = 0;
286 uint32_t session_id;
287 int usecase_id;
288 int rec_mode = INCALL_REC_NONE;
289
Ravi Kumar Alamandaf1819242014-08-05 18:20:42 -0700290 if (voice_is_call_state_active(adev)) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700291 switch (in->source) {
292 case AUDIO_SOURCE_VOICE_UPLINK:
293 in->usecase = USECASE_INCALL_REC_UPLINK;
294 rec_mode = INCALL_REC_UPLINK;
295 break;
296 case AUDIO_SOURCE_VOICE_DOWNLINK:
297 in->usecase = USECASE_INCALL_REC_DOWNLINK;
298 rec_mode = INCALL_REC_DOWNLINK;
299 break;
300 case AUDIO_SOURCE_VOICE_CALL:
301 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
302 rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK;
303 break;
304 default:
305 ALOGV("%s: Source type %d doesnt match incall recording criteria",
306 __func__, in->source);
307 return ret;
308 }
309
310 session_id = voice_get_active_session_id(adev);
311 ret = platform_set_incall_recording_session_id(adev->platform,
312 session_id, rec_mode);
313 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
314 } else {
315 ALOGV("%s: voice call not active", __func__);
316 }
317
318 return ret;
319}
320
321int voice_check_and_stop_incall_rec_usecase(struct audio_device *adev,
322 struct stream_in *in)
323{
324 int ret = 0;
325
326 if (in->source == AUDIO_SOURCE_VOICE_UPLINK ||
327 in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
328 in->source == AUDIO_SOURCE_VOICE_CALL) {
329 ret = platform_stop_incall_recording_usecase(adev->platform);
330 ALOGV("%s: Stop In-call recording", __func__);
331 }
332
333 return ret;
334}
335
336int voice_check_and_set_incall_music_usecase(struct audio_device *adev,
337 struct stream_out *out)
338{
339 int ret = 0;
340
341 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
342 if (ret == -ENOSYS) {
343 /* Incall music delivery is used only for LCH call state */
344 ret = -EINVAL;
345 }
346
347 return ret;
348}
349
350int voice_set_mic_mute(struct audio_device *adev, bool state)
351{
352 int err = 0;
353
354 adev->voice.mic_mute = state;
Uday Kishore Pasupuletia1f48052015-09-08 22:49:18 +0900355 if (adev->mode == AUDIO_MODE_IN_CALL ||
356 adev->mode == AUDIO_MODE_IN_COMMUNICATION)
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700357 err = platform_set_mic_mute(adev->platform, state);
358
359 return err;
360}
361
362bool voice_get_mic_mute(struct audio_device *adev)
363{
364 return adev->voice.mic_mute;
365}
366
367int voice_set_volume(struct audio_device *adev, float volume)
368{
369 int vol, err = 0;
370
371 adev->voice.volume = volume;
372 if (adev->mode == AUDIO_MODE_IN_CALL) {
373 if (volume < 0.0) {
374 volume = 0.0;
375 } else if (volume > 1.0) {
376 volume = 1.0;
377 }
378
379 vol = lrint(volume * 100.0);
380
381 // Voice volume levels from android are mapped to driver volume levels as follows.
382 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
383 // So adjust the volume to get the correct volume index in driver
384 vol = 100 - vol;
385
386 err = platform_set_voice_volume(adev->platform, vol);
387 }
388
389 return err;
390}
391
Eric Laurent75a2e1d2014-08-29 12:38:47 -0700392int voice_start_call(struct audio_device *adev)
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700393{
394 int ret = 0;
395
Ravi Kumar Alamandab09e4a02014-10-20 17:07:43 -0700396 adev->voice.in_call = true;
Ravi Kumar Alamandaeecfa9a2015-03-20 17:31:46 -0700397
398 voice_set_mic_mute(adev, adev->voice.mic_mute);
399
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700400 ret = voice_extn_start_call(adev);
401 if (ret == -ENOSYS) {
Eric Laurent75a2e1d2014-08-29 12:38:47 -0700402 ret = voice_start_usecase(adev, USECASE_VOICE_CALL);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700403 }
404
405 return ret;
406}
407
408int voice_stop_call(struct audio_device *adev)
409{
410 int ret = 0;
411
Ravi Kumar Alamandaf1819242014-08-05 18:20:42 -0700412 adev->voice.in_call = false;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700413 ret = voice_extn_stop_call(adev);
414 if (ret == -ENOSYS) {
Ravi Kumar Alamanda08dbcfc2014-08-20 16:24:38 -0700415 ret = voice_stop_usecase(adev, USECASE_VOICE_CALL);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700416 }
417
418 return ret;
419}
420
421void voice_get_parameters(struct audio_device *adev,
422 struct str_parms *query,
423 struct str_parms *reply)
424{
425 voice_extn_get_parameters(adev, query, reply);
426}
427
428int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
429{
430 char *str;
431 char value[32];
432 int val;
433 int ret = 0, err;
434 char *kv_pairs = str_parms_to_str(parms);
435
436 ALOGV_IF(kv_pairs != NULL, "%s: enter: %s", __func__, kv_pairs);
437
438 ret = voice_extn_set_parameters(adev, parms);
Haynes Mathew Georgeff5d6512014-07-18 15:31:44 -0700439 if (ret != 0) {
440 if (ret == -ENOSYS) {
441 ret = 0; /* ignore error */
442 } else {
443 goto done;
444 }
445 }
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700446
447 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
448 if (err >= 0) {
449 int tty_mode;
450 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
451 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
452 tty_mode = TTY_MODE_OFF;
453 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
454 tty_mode = TTY_MODE_VCO;
455 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
456 tty_mode = TTY_MODE_HCO;
457 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
458 tty_mode = TTY_MODE_FULL;
459 else {
460 ret = -EINVAL;
461 goto done;
462 }
463
464 if (tty_mode != adev->voice.tty_mode) {
465 adev->voice.tty_mode = tty_mode;
466 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
Ravi Kumar Alamandaf1819242014-08-05 18:20:42 -0700467 if (voice_is_call_state_active(adev))
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700468 voice_update_devices_for_all_voice_usecases(adev);
469 }
470 }
471
Eric Laurent9d0d3f12014-07-25 12:40:29 -0500472 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HAC,
473 value, sizeof(value));
474 if (err >= 0) {
475 bool hac = false;
476 str_parms_del(parms, AUDIO_PARAMETER_KEY_HAC);
477 if (strcmp(value, AUDIO_PARAMETER_VALUE_HAC_ON) == 0)
478 hac = true;
479
480 if (hac != adev->voice.hac) {
481 adev->voice.hac = hac;
482 if (voice_is_in_call(adev))
483 voice_update_devices_for_all_voice_usecases(adev);
484 }
485 }
486
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700487 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC,
488 value, sizeof(value));
489 if (err >= 0) {
490 str_parms_del(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC);
491 if (strcmp(value, AUDIO_PARAMETER_VALUE_TRUE) == 0)
492 platform_start_incall_music_usecase(adev->platform);
493 else
494 platform_stop_incall_music_usecase(adev->platform);
495 }
496
497done:
498 ALOGV("%s: exit with code(%d)", __func__, ret);
499 free(kv_pairs);
500 return ret;
501}
502
503void voice_init(struct audio_device *adev)
504{
505 int i = 0;
506
507 memset(&adev->voice, 0, sizeof(adev->voice));
508 adev->voice.tty_mode = TTY_MODE_OFF;
Eric Laurent9d0d3f12014-07-25 12:40:29 -0500509 adev->voice.hac = false;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700510 adev->voice.volume = 1.0f;
511 adev->voice.mic_mute = false;
Ravi Kumar Alamandaf1819242014-08-05 18:20:42 -0700512 adev->voice.in_call = false;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700513 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
514 adev->voice.session[i].pcm_rx = NULL;
515 adev->voice.session[i].pcm_tx = NULL;
516 adev->voice.session[i].state.current = CALL_INACTIVE;
517 adev->voice.session[i].state.new = CALL_INACTIVE;
518 adev->voice.session[i].vsid = VOICE_VSID;
519 }
520
521 voice_extn_init(adev);
522}
523
524void voice_update_devices_for_all_voice_usecases(struct audio_device *adev)
525{
526 struct listnode *node;
527 struct audio_usecase *usecase;
528
529 list_for_each(node, &adev->usecase_list) {
530 usecase = node_to_item(node, struct audio_usecase, list);
531 if (usecase->type == VOICE_CALL) {
532 ALOGV("%s: updating device for usecase:%s", __func__,
533 use_case_table[usecase->id]);
Ravi Kumar Alamandaa4fc9022014-10-08 18:57:46 -0700534 usecase->stream.out = adev->current_call_output;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700535 select_devices(adev, usecase->id);
536 }
537 }
538}
539
540