voice.c revision 4b89e37ad290ef955abf8ac1d151728303311345
1/* 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 21#include <errno.h> 22#include <math.h> 23#include <cutils/log.h> 24#include <cutils/str_parms.h> 25 26#include "audio_hw.h" 27#include "voice.h" 28#include "voice_extn/voice_extn.h" 29#include "platform.h" 30#include "platform_api.h" 31 32struct pcm_config pcm_config_voice_call = { 33 .channels = 1, 34 .rate = 8000, 35 .period_size = 160, 36 .period_count = 2, 37 .format = PCM_FORMAT_S16_LE, 38}; 39 40extern const char * const use_case_table[AUDIO_USECASE_MAX]; 41 42static struct voice_session *voice_get_session_from_use_case(struct audio_device *adev, 43 audio_usecase_t usecase_id) 44{ 45 struct voice_session *session = NULL; 46 int ret = 0; 47 48 ret = voice_extn_get_session_from_use_case(adev, usecase_id, &session); 49 if (ret == -ENOSYS) { 50 session = &adev->voice.session[VOICE_SESS_IDX]; 51 } 52 53 return session; 54} 55 56int stop_call(struct audio_device *adev, audio_usecase_t usecase_id) 57{ 58 int i, ret = 0; 59 struct audio_usecase *uc_info; 60 struct voice_session *session = NULL; 61 62 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]); 63 64 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id); 65 session->state.current = CALL_INACTIVE; 66 67 ret = platform_stop_voice_call(adev->platform, session->vsid); 68 69 /* 1. Close the PCM devices */ 70 if (session->pcm_rx) { 71 pcm_close(session->pcm_rx); 72 session->pcm_rx = NULL; 73 } 74 if (session->pcm_tx) { 75 pcm_close(session->pcm_tx); 76 session->pcm_tx = NULL; 77 } 78 79 uc_info = get_usecase_from_list(adev, usecase_id); 80 if (uc_info == NULL) { 81 ALOGE("%s: Could not find the usecase (%d) in the list", 82 __func__, usecase_id); 83 return -EINVAL; 84 } 85 86 /* 2. Get and set stream specific mixer controls */ 87 disable_audio_route(adev, uc_info); 88 89 /* 3. Disable the rx and tx devices */ 90 disable_snd_device(adev, uc_info->out_snd_device); 91 disable_snd_device(adev, uc_info->in_snd_device); 92 93 list_remove(&uc_info->list); 94 free(uc_info); 95 96 ALOGD("%s: exit: status(%d)", __func__, ret); 97 return ret; 98} 99 100int start_call(struct audio_device *adev, audio_usecase_t usecase_id) 101{ 102 int i, ret = 0; 103 struct audio_usecase *uc_info; 104 int pcm_dev_rx_id, pcm_dev_tx_id; 105 uint32_t sample_rate = 8000; 106 struct voice_session *session = NULL; 107 struct pcm_config voice_config = pcm_config_voice_call; 108 109 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]); 110 111 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id); 112 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 113 uc_info->id = usecase_id; 114 uc_info->type = VOICE_CALL; 115 uc_info->stream.out = adev->primary_output; 116 uc_info->devices = adev->primary_output->devices; 117 uc_info->in_snd_device = SND_DEVICE_NONE; 118 uc_info->out_snd_device = SND_DEVICE_NONE; 119 120 list_add_tail(&adev->usecase_list, &uc_info->list); 121 122 select_devices(adev, usecase_id); 123 124 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK); 125 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE); 126 127 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) { 128 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)", 129 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id); 130 ret = -EIO; 131 goto error_start_voice; 132 } 133 ret = platform_get_sample_rate(adev->platform, &sample_rate); 134 if (ret < 0) { 135 ALOGE("platform_get_sample_rate error %d\n", ret); 136 } else { 137 voice_config.rate = sample_rate; 138 } 139 ALOGD("voice_config.rate %d\n", voice_config.rate); 140 141 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)", 142 __func__, adev->snd_card, pcm_dev_rx_id); 143 session->pcm_rx = pcm_open(adev->snd_card, 144 pcm_dev_rx_id, 145 PCM_OUT, &voice_config); 146 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) { 147 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx)); 148 ret = -EIO; 149 goto error_start_voice; 150 } 151 152 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)", 153 __func__, adev->snd_card, pcm_dev_tx_id); 154 session->pcm_tx = pcm_open(adev->snd_card, 155 pcm_dev_tx_id, 156 PCM_IN, &voice_config); 157 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) { 158 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx)); 159 ret = -EIO; 160 goto error_start_voice; 161 } 162 pcm_start(session->pcm_rx); 163 pcm_start(session->pcm_tx); 164 165 voice_set_volume(adev, adev->voice.volume); 166 167 ret = platform_start_voice_call(adev->platform, session->vsid); 168 if (ret < 0) { 169 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret); 170 goto error_start_voice; 171 } 172 173 session->state.current = CALL_ACTIVE; 174 return 0; 175 176error_start_voice: 177 stop_call(adev, usecase_id); 178 179 ALOGD("%s: exit: status(%d)", __func__, ret); 180 return ret; 181} 182 183bool voice_is_in_call(struct audio_device *adev) 184{ 185 bool in_call = false; 186 int ret = 0; 187 188 ret = voice_extn_is_in_call(adev, &in_call); 189 if (ret == -ENOSYS) { 190 in_call = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false; 191 } 192 193 return in_call; 194} 195 196bool voice_is_in_call_rec_stream(struct stream_in *in) 197{ 198 bool in_call_rec = false; 199 int ret = 0; 200 201 ret = voice_extn_is_in_call_rec_stream(in, &in_call_rec); 202 if (ret == -ENOSYS) { 203 in_call_rec = false; 204 } 205 206 return in_call_rec; 207} 208 209uint32_t voice_get_active_session_id(struct audio_device *adev) 210{ 211 int ret = 0; 212 uint32_t session_id; 213 214 ret = voice_extn_get_active_session_id(adev, &session_id); 215 if (ret == -ENOSYS) { 216 session_id = VOICE_VSID; 217 } 218 return session_id; 219} 220 221int voice_check_and_set_incall_rec_usecase(struct audio_device *adev, 222 struct stream_in *in) 223{ 224 int ret = 0; 225 uint32_t session_id; 226 int usecase_id; 227 int rec_mode = INCALL_REC_NONE; 228 229 if (voice_is_in_call(adev)) { 230 switch (in->source) { 231 case AUDIO_SOURCE_VOICE_UPLINK: 232 in->usecase = USECASE_INCALL_REC_UPLINK; 233 rec_mode = INCALL_REC_UPLINK; 234 break; 235 case AUDIO_SOURCE_VOICE_DOWNLINK: 236 in->usecase = USECASE_INCALL_REC_DOWNLINK; 237 rec_mode = INCALL_REC_DOWNLINK; 238 break; 239 case AUDIO_SOURCE_VOICE_CALL: 240 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK; 241 rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK; 242 break; 243 default: 244 ALOGV("%s: Source type %d doesnt match incall recording criteria", 245 __func__, in->source); 246 return ret; 247 } 248 249 session_id = voice_get_active_session_id(adev); 250 ret = platform_set_incall_recording_session_id(adev->platform, 251 session_id, rec_mode); 252 ALOGV("%s: Update usecase to %d",__func__, in->usecase); 253 } else { 254 ALOGV("%s: voice call not active", __func__); 255 } 256 257 return ret; 258} 259 260int voice_check_and_stop_incall_rec_usecase(struct audio_device *adev, 261 struct stream_in *in) 262{ 263 int ret = 0; 264 265 if (in->source == AUDIO_SOURCE_VOICE_UPLINK || 266 in->source == AUDIO_SOURCE_VOICE_DOWNLINK || 267 in->source == AUDIO_SOURCE_VOICE_CALL) { 268 ret = platform_stop_incall_recording_usecase(adev->platform); 269 ALOGV("%s: Stop In-call recording", __func__); 270 } 271 272 return ret; 273} 274 275int voice_check_and_set_incall_music_usecase(struct audio_device *adev, 276 struct stream_out *out) 277{ 278 int ret = 0; 279 280 ret = voice_extn_check_and_set_incall_music_usecase(adev, out); 281 if (ret == -ENOSYS) { 282 /* Incall music delivery is used only for LCH call state */ 283 ret = -EINVAL; 284 } 285 286 return ret; 287} 288 289int voice_set_mic_mute(struct audio_device *adev, bool state) 290{ 291 int err = 0; 292 293 adev->voice.mic_mute = state; 294 if (adev->mode == AUDIO_MODE_IN_CALL) 295 err = platform_set_mic_mute(adev->platform, state); 296 297 return err; 298} 299 300bool voice_get_mic_mute(struct audio_device *adev) 301{ 302 return adev->voice.mic_mute; 303} 304 305int voice_set_volume(struct audio_device *adev, float volume) 306{ 307 int vol, err = 0; 308 309 adev->voice.volume = volume; 310 if (adev->mode == AUDIO_MODE_IN_CALL) { 311 if (volume < 0.0) { 312 volume = 0.0; 313 } else if (volume > 1.0) { 314 volume = 1.0; 315 } 316 317 vol = lrint(volume * 100.0); 318 319 // Voice volume levels from android are mapped to driver volume levels as follows. 320 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0 321 // So adjust the volume to get the correct volume index in driver 322 vol = 100 - vol; 323 324 err = platform_set_voice_volume(adev->platform, vol); 325 } 326 327 return err; 328} 329 330int voice_start_call(struct audio_device *adev) 331{ 332 int ret = 0; 333 334 ret = voice_extn_start_call(adev); 335 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 346 ret = voice_extn_stop_call(adev); 347 if (ret == -ENOSYS) { 348 ret = stop_call(adev, USECASE_VOICE_CALL); 349 } 350 351 return ret; 352} 353 354void 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 361int voice_set_parameters(struct audio_device *adev, struct str_parms *parms) 362{ 363 char *str; 364 char value[32]; 365 int val; 366 int ret = 0, err; 367 char *kv_pairs = str_parms_to_str(parms); 368 369 ALOGV_IF(kv_pairs != NULL, "%s: enter: %s", __func__, kv_pairs); 370 371 ret = voice_extn_set_parameters(adev, parms); 372 if (ret != 0) 373 goto done; 374 375 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value)); 376 if (err >= 0) { 377 int tty_mode; 378 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE); 379 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0) 380 tty_mode = TTY_MODE_OFF; 381 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0) 382 tty_mode = TTY_MODE_VCO; 383 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0) 384 tty_mode = TTY_MODE_HCO; 385 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0) 386 tty_mode = TTY_MODE_FULL; 387 else { 388 ret = -EINVAL; 389 goto done; 390 } 391 392 if (tty_mode != adev->voice.tty_mode) { 393 adev->voice.tty_mode = tty_mode; 394 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode; 395 if (voice_is_in_call(adev)) 396 voice_update_devices_for_all_voice_usecases(adev); 397 } 398 } 399 400 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC, 401 value, sizeof(value)); 402 if (err >= 0) { 403 str_parms_del(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC); 404 if (strcmp(value, AUDIO_PARAMETER_VALUE_TRUE) == 0) 405 platform_start_incall_music_usecase(adev->platform); 406 else 407 platform_stop_incall_music_usecase(adev->platform); 408 } 409 410done: 411 ALOGV("%s: exit with code(%d)", __func__, ret); 412 free(kv_pairs); 413 return ret; 414} 415 416void voice_init(struct audio_device *adev) 417{ 418 int i = 0; 419 420 memset(&adev->voice, 0, sizeof(adev->voice)); 421 adev->voice.tty_mode = TTY_MODE_OFF; 422 adev->voice.volume = 1.0f; 423 adev->voice.mic_mute = false; 424 adev->voice.voice_device_set = false; 425 for (i = 0; i < MAX_VOICE_SESSIONS; i++) { 426 adev->voice.session[i].pcm_rx = NULL; 427 adev->voice.session[i].pcm_tx = NULL; 428 adev->voice.session[i].state.current = CALL_INACTIVE; 429 adev->voice.session[i].state.new = CALL_INACTIVE; 430 adev->voice.session[i].vsid = VOICE_VSID; 431 } 432 433 voice_extn_init(adev); 434} 435 436void voice_update_devices_for_all_voice_usecases(struct audio_device *adev) 437{ 438 struct listnode *node; 439 struct audio_usecase *usecase; 440 441 list_for_each(node, &adev->usecase_list) { 442 usecase = node_to_item(node, struct audio_usecase, list); 443 if (usecase->type == VOICE_CALL) { 444 ALOGV("%s: updating device for usecase:%s", __func__, 445 use_case_table[usecase->id]); 446 select_devices(adev, usecase->id); 447 } 448 } 449} 450 451 452