rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include "AudioParamParser.h" |
| 2 | #include "AudioParamParserPriv.h" |
| 3 | #include "xml_parser_def.h" |
| 4 | #include "default_para.h" |
| 5 | #include "speech_drv.h" |
| 6 | #include "modem_afe_ctrl.h" |
| 7 | #include "audio_ctrl_service_inner_api.h" |
| 8 | |
| 9 | #include "speech_UT_header.h" |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <unistd.h> |
| 15 | #include <limits.h> |
| 16 | #include <pthread.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <errno.h> |
| 21 | #include <assert.h> |
| 22 | |
| 23 | #include <syslog.h> |
| 24 | #include <samplerate.h> |
| 25 | |
| 26 | //#define RECORD_USE_UT_SOURCE |
| 27 | //#define PLAYBACK_USE_UT_SINK |
| 28 | #include <log/log.h> |
| 29 | #define ALOGD SLOGD |
| 30 | #define ALOGI SLOGI |
| 31 | #define ALOGW SLOGW |
| 32 | #define ALOGE SLOGE |
| 33 | #ifdef LOG_TAG |
| 34 | #undef LOG_TAG |
| 35 | #endif |
| 36 | #define LOG_TAG "audio-ctrl-service" |
| 37 | |
| 38 | //#define UT_LOG |
| 39 | #ifdef UT_LOG |
| 40 | #define AUDIO_V_LOG ALOGD |
| 41 | #else |
| 42 | #define AUDIO_V_LOG |
| 43 | #endif |
| 44 | |
| 45 | #define SPEECH_DRV_RETRY_TIME 3 |
| 46 | #define SPEECH_DRV_RETRY_US 20000 //20ms |
| 47 | #define SPEECH_DRV_BGS_INTERVAL_US 20000 //20ms |
| 48 | |
| 49 | #define MAX_REC_BUF_SIZE 3200//16000Hz 16bytes 100ms |
| 50 | #define MD_PCM_RECORD_HEADER_SYNC 0x1234 |
| 51 | |
| 52 | char *g_record_buf; |
| 53 | int g_want_to_speech_off_after_record_off; |
| 54 | |
| 55 | int g_record_buf_size; |
| 56 | int g_record_read_virt_idx; |
| 57 | int g_record_write_virt_idx; |
| 58 | int g_record_virt_boundry; |
| 59 | |
| 60 | int g_is_bgs_on; |
| 61 | |
| 62 | /* SRC for inCall record UL*/ |
| 63 | |
| 64 | #define RECORD_SRC_BUF_SIZE 160 //in frame |
| 65 | #define RECORD_DST_BUF_SIZE 320 //in frame |
| 66 | #define RECORD_SRC_RATIO 2.0 |
| 67 | const int g_converter_type = SRC_SINC_MEDIUM_QUALITY; |
| 68 | SRC_DATA g_incall_record_src_data; |
| 69 | SRC_STATE *g_incall_record_src_state; |
| 70 | float *g_incall_record_src_buffer; |
| 71 | float *g_incall_record_dst_buffer; |
| 72 | int16_t g_incall_record_dst_int_buffer[RECORD_DST_BUF_SIZE]; |
| 73 | |
| 74 | pthread_mutex_t g_mutex_record = PTHREAD_MUTEX_INITIALIZER; |
| 75 | pthread_mutex_t g_mutex_playback = PTHREAD_MUTEX_INITIALIZER; |
| 76 | |
| 77 | /* modem pcm record package header*/ |
| 78 | typedef struct md_pcm_record_header { |
| 79 | uint16_t u16SyncWord; |
| 80 | uint16_t u16RawPcmDir; |
| 81 | uint16_t u16Freq; |
| 82 | uint16_t u16Length; |
| 83 | uint16_t u16Channel; |
| 84 | uint16_t u16BitFormat; |
| 85 | } MD_PCM_RECORD_HEADER; |
| 86 | |
| 87 | |
| 88 | static void parameterChanged(AppHandle *appHandle, const char *audioTypeName); |
| 89 | |
| 90 | static void *speech_flow_upper_func_operator(void *arg); |
| 91 | static void *speech_flow_lower_func_operator(void *arg); |
| 92 | static void *speech_flow_dummy_sender(void *arg); |
| 93 | |
| 94 | static void updateVolume(int xmlType); |
| 95 | static void updateDlGain(void); |
| 96 | static void updateUlGain(void); |
| 97 | |
| 98 | const struct xml_param paramVolume[PARAM_NUM] = { |
| 99 | { |
| 100 | .id = XML_SPEECH_VOLUME, |
| 101 | .xml_type_name = "SpeechVol", |
| 102 | .update_xml_callback = updateVolume, |
| 103 | }, |
| 104 | { |
| 105 | .id = XML_VOLUME, |
| 106 | .xml_type_name = "Volume", |
| 107 | .update_xml_callback = updateVolume, |
| 108 | }, |
| 109 | { |
| 110 | .id = XML_GAIN_MAP_DL, |
| 111 | .xml_type_name = "VolumeGainMapUL", |
| 112 | .update_xml_callback = updateVolume, |
| 113 | }, |
| 114 | { |
| 115 | .id = XML_GAIN_MAP_UL, |
| 116 | .xml_type_name = "VolumeGainMap", |
| 117 | .update_xml_callback = updateVolume, |
| 118 | }, |
| 119 | }; |
| 120 | #define DL_VOLUME_IDX_MIN 1 |
| 121 | #define DL_VOLUME_IDX_MAX 7 |
| 122 | |
| 123 | static int g_is_speech_on; |
| 124 | static int g_current_output_device; |
| 125 | |
| 126 | static int g_bt_wbs_on = FUNC_SET_BT_WB_default; |
| 127 | static int g_bt_client_has_ecnr; |
| 128 | static int g_volume_index; |
| 129 | static int g_speaker_type; |
| 130 | |
| 131 | |
| 132 | /* IPC variable*/ |
| 133 | static pthread_t g_pthread_speech_flow_upper_rcv; |
| 134 | static pthread_t g_pthread_speech_flow_lower_rcv; |
| 135 | static pthread_t g_pthread_speech_flow_dummy_send; |
| 136 | |
| 137 | static int g_ipc_upper_rcv_handler; |
| 138 | static int g_ipc_upper_send_handler; |
| 139 | static int g_ipc_lower_rcv_handler; |
| 140 | static int g_ipc_lower_send_handler; |
| 141 | |
| 142 | |
| 143 | int main() { |
| 144 | int paramIndex = 0; |
| 145 | AppOps *appOps = NULL; |
| 146 | AppHandle *appHandle = NULL; |
| 147 | struct stat st = {0}; |
| 148 | |
| 149 | #if !defined(MTK_DUMMY_AUDIO_PARSER) |
| 150 | appOps = appOpsGetInstance(); |
| 151 | if (appOps == NULL) { |
| 152 | ALOGE("%s(), line %d ERROR", __func__, __LINE__); |
| 153 | return 1; |
| 154 | } else { |
| 155 | appHandle = appOps->appHandleGetInstance(); |
| 156 | |
| 157 | /* Wait for initial done */ |
| 158 | usleep(100 * 1000);//100ms |
| 159 | |
| 160 | /* Set the debug level to warn*/ |
| 161 | appOps->appSetDebugLevel(WARN_LEVEL); |
| 162 | |
| 163 | /* register callback func */ |
| 164 | appOps->appHandleRegXmlChangedCb(appHandle, parameterChanged); |
| 165 | } |
| 166 | #endif |
| 167 | if (stat("/tmp/audio_ctrl_service", &st) == -1) { |
| 168 | mkdir("/tmp/audio_ctrl_service", 0700); |
| 169 | } |
| 170 | |
| 171 | /* for service init */ |
| 172 | mkfifo(ACS_IPC_FOR_UPPER_RCV, 0600); |
| 173 | mkfifo(ACS_IPC_FOR_UPPER_SEND, 0600); |
| 174 | mkfifo(ACS_IPC_FOR_LOWER_RCV, 0600); |
| 175 | mkfifo(ACS_IPC_FOR_LOWER_SEND, 0600); |
| 176 | |
| 177 | pthread_create(&g_pthread_speech_flow_upper_rcv, NULL, speech_flow_upper_func_operator, NULL); |
| 178 | pthread_create(&g_pthread_speech_flow_lower_rcv, NULL, speech_flow_lower_func_operator, NULL); |
| 179 | pthread_create(&g_pthread_speech_flow_dummy_send, NULL, speech_flow_dummy_sender, NULL); |
| 180 | |
| 181 | while (1) { |
| 182 | sleep(1000000); |
| 183 | } |
| 184 | #if !defined(MTK_DUMMY_AUDIO_PARSER) |
| 185 | /* Release appHandle resources */ |
| 186 | appOps->appHandleUninit(appHandle); |
| 187 | #endif |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static void *speech_flow_upper_func_operator(void *arg) { |
| 193 | char buf[IPC_DATA_SIZE_MAX]; |
| 194 | int size = 0, function_type = 0, first_param = 0; |
| 195 | int str_output_size = 0; |
| 196 | int func_result; |
| 197 | char *first_param_str; |
| 198 | char *second_param_str; |
| 199 | char buf_response[IPC_DATA_SIZE_MAX]; |
| 200 | char *buf_for_get_data = buf; //use the buffer to get data out for ioplugin |
| 201 | |
| 202 | ALOGD("%s()", __func__); |
| 203 | g_ipc_upper_rcv_handler = open(ACS_IPC_FOR_UPPER_RCV, O_RDONLY); |
| 204 | g_ipc_upper_send_handler = open(ACS_IPC_FOR_UPPER_SEND, O_WRONLY); |
| 205 | set_phonecall_rate(PHONECALL_SAMPLE_RATE); |
| 206 | g_volume_index = 4;//valid range: 0~7, 0 for mute |
| 207 | g_current_output_device = AUDIO_DEVICE_OUT_SPEAKER; |
| 208 | g_speaker_type = AUDIO_SPK_EXTAMP_LO; |
| 209 | |
| 210 | while (1) { |
| 211 | size = read(g_ipc_upper_rcv_handler, buf, IPC_DATA_SIZE_MAX); |
| 212 | if (!size) { |
| 213 | ALOGE("%s(), IPC data size=%d, reset fifo!", __func__, size); |
| 214 | close(g_ipc_upper_rcv_handler); |
| 215 | g_ipc_upper_rcv_handler = open(ACS_IPC_FOR_UPPER_RCV, O_RDONLY); |
| 216 | continue; |
| 217 | } |
| 218 | /* finish string */ |
| 219 | buf[size] = '\0'; |
| 220 | |
| 221 | /* parse string */ |
| 222 | strtok_r(buf, ",", &first_param_str); |
| 223 | function_type = atoi(buf); |
| 224 | AUDIO_V_LOG("%s(), function_type=%d", __func__, function_type); |
| 225 | switch (function_type) { |
| 226 | case FUNC_AUDIO_CTRL_SERVICE_SPEECH_ON: |
| 227 | first_param = atoi(first_param_str); |
| 228 | pthread_mutex_lock(&g_mutex_record); //for g_want_to_speech_off_after_record_off |
| 229 | func_result = audio_ctrl_service_speech_on_inner(first_param, AUDIO_DEVICE_OUT_SPEAKER); |
| 230 | pthread_mutex_unlock(&g_mutex_record); |
| 231 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 232 | break; |
| 233 | case FUNC_AUDIO_CTRL_M2M_CALL_ON: |
| 234 | first_param = atoi(first_param_str); |
| 235 | strtok_r(first_param_str, ",", &second_param_str); |
| 236 | pthread_mutex_lock(&g_mutex_playback); |
| 237 | func_result = audio_ctrl_service_M2M_Call_on_inner(first_param); |
| 238 | pthread_mutex_unlock(&g_mutex_playback); |
| 239 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 240 | break; |
| 241 | case FUNC_AUDIO_CTRL_ECALL_ON: |
| 242 | first_param = atoi(first_param_str); |
| 243 | pthread_mutex_lock(&g_mutex_playback); |
| 244 | func_result = audio_ctrl_service_ECall_on_inner(first_param); |
| 245 | pthread_mutex_unlock(&g_mutex_playback); |
| 246 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 247 | break; |
| 248 | case FUNC_AUDIO_CTRL_SERVICE_SET_DL_VOLUME: |
| 249 | case FUNC_AUDIO_CTRL_SERVICE_SET_BT_DL_GAIN: |
| 250 | first_param = atoi(first_param_str); |
| 251 | func_result = audio_ctrl_service_set_volume_index_inner(first_param); |
| 252 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 253 | break; |
| 254 | case FUNC_AUDIO_CTRL_SERVICE_GET_BT_DL_GAIN: |
| 255 | func_result = audio_ctrl_service_get_volume_index_inner(); |
| 256 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 257 | break; |
| 258 | case FUNC_AUDIO_CTRL_SERVICE_SET_SPEAKER_TYPE: |
| 259 | first_param = atoi(first_param_str); |
| 260 | func_result = audio_ctrl_service_set_speaker_type_inner(first_param); |
| 261 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 262 | break; |
| 263 | |
| 264 | #if defined(MTK_SPEECH_RECORD_SUPPORT) |
| 265 | case FUNC_AUDIO_CTRL_INCALL_RECORD_START: |
| 266 | first_param = atoi(first_param_str); |
| 267 | pthread_mutex_lock(&g_mutex_record); |
| 268 | func_result = audio_ctrl_service_inCall_record_start_inner(first_param); |
| 269 | pthread_mutex_unlock(&g_mutex_record); |
| 270 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 271 | break; |
| 272 | case FUNC_AUDIO_CTRL_INCALL_RECORD_STOP: |
| 273 | pthread_mutex_lock(&g_mutex_record); |
| 274 | func_result = audio_ctrl_service_inCall_record_stop_inner(); |
| 275 | pthread_mutex_unlock(&g_mutex_record); |
| 276 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 277 | break; |
| 278 | case FUNC_AUDIO_CTRL_INCALL_RECORD_POINTER: |
| 279 | pthread_mutex_lock(&g_mutex_record); |
| 280 | func_result = audio_ctrl_service_inCall_record_pointer_inner(); |
| 281 | pthread_mutex_unlock(&g_mutex_record); |
| 282 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 283 | break; |
| 284 | case FUNC_AUDIO_CTRL_INCALL_RECORD_GET_WATERMARK: |
| 285 | pthread_mutex_lock(&g_mutex_record); |
| 286 | func_result = audio_ctrl_service_inCall_record_get_watermark_inner(); |
| 287 | pthread_mutex_unlock(&g_mutex_record); |
| 288 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 289 | break; |
| 290 | case FUNC_AUDIO_CTRL_INCALL_RECORD_GET_DATA: |
| 291 | first_param = atoi(first_param_str); |
| 292 | //use the "buf" to get data out for ioplugin |
| 293 | pthread_mutex_lock(&g_mutex_record); |
| 294 | func_result = audio_ctrl_service_inCall_record_get_data_inner |
| 295 | (first_param, buf_for_get_data); |
| 296 | pthread_mutex_unlock(&g_mutex_record); |
| 297 | str_output_size = sprintf(buf_response, "%d,", func_result); |
| 298 | if (func_result > 0) { |
| 299 | memcpy(buf_response + str_output_size, buf_for_get_data, |
| 300 | func_result); |
| 301 | str_output_size += func_result; |
| 302 | } |
| 303 | break; |
| 304 | #endif |
| 305 | #if defined(MTK_SPEECH_BGS_SUPPORT) |
| 306 | case FUNC_AUDIO_CTRL_INCALL_PLAYBACK_START: |
| 307 | pthread_mutex_lock(&g_mutex_playback); |
| 308 | func_result = audio_ctrl_service_inCall_playback_start_inner(); |
| 309 | pthread_mutex_unlock(&g_mutex_playback); |
| 310 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 311 | break; |
| 312 | case FUNC_AUDIO_CTRL_INCALL_PLAYBACK_STOP: |
| 313 | pthread_mutex_lock(&g_mutex_playback); |
| 314 | func_result = audio_ctrl_service_inCall_playback_stop_inner(); |
| 315 | pthread_mutex_unlock(&g_mutex_playback); |
| 316 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 317 | break; |
| 318 | case FUNC_AUDIO_CTRL_INCALL_PLAYBACK_SEND_DATA: |
| 319 | first_param = atoi(first_param_str); |
| 320 | strtok_r(first_param_str, ",", &second_param_str); |
| 321 | pthread_mutex_lock(&g_mutex_playback); |
| 322 | func_result = audio_ctrl_service_inCall_playback_send_data_inner |
| 323 | (first_param, second_param_str); |
| 324 | pthread_mutex_unlock(&g_mutex_playback); |
| 325 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 326 | break; |
| 327 | case FUNC_AUDIO_CTRL_INCALL_PLAYBACK_SIMULATION: |
| 328 | first_param = atoi(first_param_str); |
| 329 | pthread_mutex_lock(&g_mutex_playback); |
| 330 | func_result = audio_ctrl_service_inCall_playback_simulation(first_param); |
| 331 | pthread_mutex_unlock(&g_mutex_playback); |
| 332 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 333 | break; |
| 334 | #endif |
| 335 | case FUNC_AUDIO_CTRL_SERVICE_IS_SPEECH_ON: |
| 336 | func_result = audio_ctrl_service_is_speech_on_inner(AUDIO_DEVICE_OUT_SPEAKER); |
| 337 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 338 | break; |
| 339 | case FUNC_AUDIO_CTRL_SERVICE_GET_RECORD_PERIOD_SIZE: |
| 340 | func_result = audio_ctrl_service_get_record_period_size_inner(); |
| 341 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 342 | break; |
| 343 | case FUNC_AUDIO_CTRL_SERVICE_GET_PLAYBACK_PERIOD_SIZE: |
| 344 | func_result = audio_ctrl_service_get_playback_period_size_inner(); |
| 345 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 346 | break; |
| 347 | case FUNC_AUDIO_CTRL_SERVICE_GET_RECORD_MAX_BUFFER_SIZE: |
| 348 | func_result = audio_ctrl_service_get_record_max_buffer_size_inner(); |
| 349 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 350 | break; |
| 351 | case FUNC_AUDIO_CTRL_SERVICE_GET_PLAYBACK_MAX_BUFFER_SIZE: |
| 352 | func_result = audio_ctrl_service_get_playback_max_buffer_size_inner(); |
| 353 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 354 | break; |
| 355 | |
| 356 | #if defined(MTK_SPEECH_VM_SUPPORT) |
| 357 | case FUNC_AUDIO_CTRL_SERVICE_VMLOG_ON: |
| 358 | first_param = atoi(first_param_str); |
| 359 | func_result = audio_ctrl_service_vmlog_on_inner(first_param); |
| 360 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 361 | break; |
| 362 | case FUNC_AUDIO_CTRL_SERVICE_GET_VMLOG_ON: |
| 363 | func_result = audio_ctrl_service_get_vmlog_on_inner(); |
| 364 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 365 | break; |
| 366 | #endif |
| 367 | case FUNC_AUDIO_CTRL_SERVICE_BT_SPEECH_ON: |
| 368 | first_param = atoi(first_param_str); |
| 369 | func_result = audio_ctrl_service_speech_on_inner(first_param, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET); |
| 370 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 371 | break; |
| 372 | case FUNC_AUDIO_CTRL_SERVICE_IS_BT_SPEECH_ON: |
| 373 | func_result = audio_ctrl_service_is_speech_on_inner(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET); |
| 374 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 375 | break; |
| 376 | case FUNC_AUDIO_CTRL_SERVICE_SET_BT_WBS: |
| 377 | first_param = atoi(first_param_str); |
| 378 | func_result = audio_ctrl_service_set_bt_wbs_inner(first_param); |
| 379 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 380 | break; |
| 381 | case FUNC_AUDIO_CTRL_SERVICE_GET_BT_WBS: |
| 382 | func_result = audio_ctrl_service_get_bt_wbs_inner(); |
| 383 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 384 | break; |
| 385 | #if !defined(MTK_AUDIO_BRINGUP) |
| 386 | case FUNC_AUDIO_CTRL_SERVICE_SET_BT_CLIENT_HAS_ECNR: |
| 387 | first_param = atoi(first_param_str); |
| 388 | func_result = audio_ctrl_service_set_bt_client_has_ecnr_inner(first_param); |
| 389 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 390 | break; |
| 391 | case FUNC_AUDIO_CTRL_SERVICE_GET_BT_CLIENT_HAS_ECNR: |
| 392 | func_result = audio_ctrl_service_get_bt_client_has_ecnr_inner(); |
| 393 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 394 | break; |
| 395 | case FUNC_AUDIO_CTRL_SERVICE_SET_USE_BT_IN_CALL: |
| 396 | first_param = atoi(first_param_str); |
| 397 | func_result = audio_ctrl_service_set_use_bt_in_call_inner(first_param); |
| 398 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 399 | break; |
| 400 | #endif |
| 401 | case FUNC_AUDIO_CTRL_SERVICE_GET_USE_BT_IN_CALL: |
| 402 | func_result = audio_ctrl_service_get_use_bt_in_call_inner(); |
| 403 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 404 | break; |
| 405 | case FUNC_AUDIO_CTRL_SERVICE_RESET_INNER: |
| 406 | func_result = audio_ctrl_service_reset_inner(); |
| 407 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 408 | break; |
| 409 | case FUNC_AUDIO_CTRL_SERVICE_SET_DL_MUTE: |
| 410 | first_param = atoi(first_param_str); |
| 411 | func_result = audio_ctrl_service_set_mute_inner(SPEECH_DL, first_param); |
| 412 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 413 | break; |
| 414 | case FUNC_AUDIO_CTRL_SERVICE_SET_UL_MUTE: |
| 415 | first_param = atoi(first_param_str); |
| 416 | func_result = audio_ctrl_service_set_mute_inner(SPEECH_UL, first_param); |
| 417 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 418 | break; |
| 419 | case FUNC_AUDIO_CTRL_SERVICE_GET_MUTE: |
| 420 | first_param = atoi(first_param_str); |
| 421 | func_result = audio_ctrl_service_get_mute_inner(first_param); |
| 422 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 423 | break; |
| 424 | default: |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | /*send result*/ |
| 429 | write(g_ipc_upper_send_handler, buf_response, str_output_size); |
| 430 | |
| 431 | } |
| 432 | close(g_ipc_upper_send_handler); |
| 433 | close(g_ipc_upper_rcv_handler); |
| 434 | } |
| 435 | |
| 436 | static void *speech_flow_lower_func_operator(void *arg) { |
| 437 | char buf[IPC_DATA_SIZE_MAX]; |
| 438 | int size = 0, function_type = 0, first_param = 0; |
| 439 | int str_output_size = 0; |
| 440 | int func_result; |
| 441 | char *first_param_str; |
| 442 | char *second_param_str; |
| 443 | char buf_response[IPC_DATA_SIZE_MAX]; |
| 444 | char *buf_for_get_data = buf; //use the buffer to get data out for ioplugin |
| 445 | |
| 446 | AUDIO_V_LOG("%s()", __func__); |
| 447 | g_ipc_lower_rcv_handler = open(ACS_IPC_FOR_LOWER_RCV, O_RDONLY); |
| 448 | g_ipc_lower_send_handler = open(ACS_IPC_FOR_LOWER_SEND, O_WRONLY); |
| 449 | while (1) { |
| 450 | size = read(g_ipc_lower_rcv_handler, buf, IPC_DATA_SIZE_MAX); |
| 451 | if (!size) { |
| 452 | ALOGE("%s(), IPC data size=%d, reset fifo!", __func__, size); |
| 453 | close(g_ipc_lower_rcv_handler); |
| 454 | g_ipc_lower_rcv_handler = open(ACS_IPC_FOR_LOWER_RCV, O_RDONLY); |
| 455 | continue; |
| 456 | } |
| 457 | /* finish string */ |
| 458 | buf[size] = '\0'; |
| 459 | |
| 460 | /* parse string */ |
| 461 | strtok_r(buf, ",", &first_param_str); |
| 462 | function_type = atoi(buf); |
| 463 | switch (function_type) { |
| 464 | #if defined(MTK_SPEECH_RECORD_SUPPORT) |
| 465 | case FUNC_AUDIO_CTRL_INCALL_SERVICE_RECORD_DATA_CB: |
| 466 | first_param = atoi(first_param_str); |
| 467 | strtok_r(first_param_str, ",", &second_param_str); |
| 468 | pthread_mutex_lock(&g_mutex_record); |
| 469 | func_result = audio_ctrl_service_inCall_record_data_cb_inner |
| 470 | (first_param, second_param_str); |
| 471 | pthread_mutex_unlock(&g_mutex_record); |
| 472 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 473 | break; |
| 474 | #endif |
| 475 | default: |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | /*send result*/ |
| 480 | write(g_ipc_lower_send_handler, buf_response, str_output_size); |
| 481 | |
| 482 | } |
| 483 | close(g_ipc_lower_send_handler); |
| 484 | close(g_ipc_lower_rcv_handler); |
| 485 | } |
| 486 | |
| 487 | |
| 488 | static void *speech_flow_dummy_sender(void *arg) { |
| 489 | int dummy_handler = 0; |
| 490 | AUDIO_V_LOG("%s()", __func__); |
| 491 | dummy_handler = open(ACS_IPC_FOR_UPPER_RCV, O_WRONLY); |
| 492 | dummy_handler = open(ACS_IPC_FOR_LOWER_RCV, O_WRONLY); |
| 493 | while (1) { |
| 494 | sleep(1000000); |
| 495 | } |
| 496 | close(dummy_handler); |
| 497 | } |
| 498 | |
| 499 | static void parameterChanged(AppHandle *appHandle, const char *audioTypeName) { |
| 500 | AppOps *appOps = NULL; |
| 501 | int paramIndex; |
| 502 | AUDIO_V_LOG("%s()", __func__); |
| 503 | #if defined(MTK_DUMMY_AUDIO_PARSER) |
| 504 | return; |
| 505 | #else |
| 506 | appOps = appOpsGetInstance(); |
| 507 | if (appOps == NULL) { |
| 508 | ALOGE("%s(), LINE %d ERROR", __func__, __LINE__); |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | if (appOps->appHandleReloadAudioType(appHandle, audioTypeName) == APP_ERROR) { |
| 513 | ALOGE("%s(), Reload xml fail! (audioType = %s)", __func__, audioTypeName); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | for (paramIndex = 0; paramIndex < PARAM_NUM; paramIndex++) { |
| 518 | if (!strcmp(audioTypeName, paramVolume[paramIndex].xml_type_name) |
| 519 | && paramVolume[paramIndex].update_xml_callback) { |
| 520 | paramVolume[paramIndex].update_xml_callback(paramVolume[paramIndex].id); |
| 521 | } |
| 522 | } |
| 523 | #endif |
| 524 | } |
| 525 | |
| 526 | |
| 527 | static void updateUlAnalogGain() { |
| 528 | int ul_ana_gain = 0, ret = 0; |
| 529 | |
| 530 | /* digital part */ |
| 531 | #if !defined(MTK_AUDIO_BRINGUP) |
| 532 | ul_ana_gain = speechdrv_get_ul_analog_gain(); |
| 533 | #endif |
| 534 | ALOGD("%s(), get ul analog gain value: %d, valid range(0~30 dB)", __func__, ul_ana_gain); |
| 535 | /* analog part */ |
| 536 | ret = set_UL_analog_gain(ul_ana_gain); |
| 537 | if (ret < 0) |
| 538 | ALOGE("%s(), set ul analog volume error, gain value: %d, errno: %d", |
| 539 | __func__, ul_ana_gain, ret); |
| 540 | } |
| 541 | |
| 542 | static void updateDlAnalogGain(void) { |
| 543 | int dl_ana_gain = 0, ret = 0; |
| 544 | |
| 545 | /* analog part */ |
| 546 | #if !defined(MTK_AUDIO_BRINGUP) |
| 547 | dl_ana_gain = speechdrv_get_dl_analog_gain(); |
| 548 | #endif |
| 549 | ALOGD("%s(), get dl analog gain value: %d, valid range(-40~8 dB)", __func__, dl_ana_gain); |
| 550 | |
| 551 | /* analog part */ |
| 552 | ret = set_DL_analog_gain(dl_ana_gain); |
| 553 | if (ret < 0) |
| 554 | ALOGE("%s(), set dl analog volume error, gain value: %d, errno: %d", |
| 555 | __func__, dl_ana_gain, ret); |
| 556 | } |
| 557 | |
| 558 | static void updateDigitalGain() { |
| 559 | int ret = 0, retry_counter_analog = 5, retry = 1; |
| 560 | int retry_speech_drv_ite = 0; |
| 561 | int dl_ana_gain = 0; |
| 562 | |
| 563 | /* digital part ul/dl*/ |
| 564 | do { |
| 565 | #if !defined(MTK_AUDIO_BRINGUP) |
| 566 | ret = speechdrv_set_volume_index(g_volume_index, g_current_output_device); |
| 567 | #endif |
| 568 | ++retry_speech_drv_ite; |
| 569 | if (!ret) { |
| 570 | break; |
| 571 | } |
| 572 | usleep(SPEECH_DRV_RETRY_US); |
| 573 | } while (ret && retry_speech_drv_ite < SPEECH_DRV_RETRY_TIME); |
| 574 | |
| 575 | if (ret < 0) |
| 576 | ALOGE("%s(), set dl volume index error, g_volume_index: %d, errno: %d", |
| 577 | __func__, g_volume_index, ret); |
| 578 | |
| 579 | } |
| 580 | |
| 581 | static void updateVolume(int xmlType) { |
| 582 | ALOGD("%s(), xml name = %s", __func__, paramVolume[xmlType].xml_type_name); |
| 583 | updateDigitalGain(); |
| 584 | if (xmlType != XML_GAIN_MAP_UL) { |
| 585 | updateDlAnalogGain(); |
| 586 | } |
| 587 | if (xmlType != XML_GAIN_MAP_DL) { |
| 588 | updateUlAnalogGain(); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | //for bring up |
| 593 | int audio_ctrl_service_speech_on_inner(int enable, int output_device) { |
| 594 | int ret; |
| 595 | int retry_ite = 0; |
| 596 | int device_mode = 0; |
| 597 | int input_device; |
| 598 | int outputDevice; |
| 599 | |
| 600 | ALOGD("%s(), enable=%d, output_device=0x%x", __func__, enable, output_device); |
| 601 | if (enable == g_is_speech_on) { |
| 602 | ALOGE("%s(), speech on status no change! g_is_speech_on: %d", |
| 603 | __func__, g_is_speech_on); |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | g_is_speech_on = enable; |
| 608 | if (output_device == AUDIO_DEVICE_OUT_SPEAKER) { |
| 609 | input_device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 610 | g_current_output_device = AUDIO_DEVICE_OUT_SPEAKER; |
| 611 | } else { |
| 612 | input_device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 613 | g_current_output_device = AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 614 | } |
| 615 | |
| 616 | do { |
| 617 | if (enable || !g_record_buf) { |
| 618 | //force to speaker , main mic |
| 619 | ret = speechdrv_set_speech_on(enable, input_device, g_current_output_device); |
| 620 | ++retry_ite; |
| 621 | if (!ret) { |
| 622 | break; |
| 623 | } |
| 624 | } else { //recording!! |
| 625 | ALOGE("%s(), record is still on, Will auto sppech off after record off, don't worry!", __func__); |
| 626 | g_want_to_speech_off_after_record_off = 1; |
| 627 | ret = 0; |
| 628 | break; |
| 629 | } |
| 630 | usleep(SPEECH_DRV_RETRY_US); |
| 631 | } while (retry_ite < SPEECH_DRV_RETRY_TIME); |
| 632 | |
| 633 | if (ret) { |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | if (enable) { |
| 638 | if (output_device == AUDIO_DEVICE_OUT_SPEAKER) { |
| 639 | device_mode = get_default_device(); |
| 640 | enable_phone_call_AFE_path(device_mode); |
| 641 | usleep(100 * 1000);//100ms |
| 642 | updateVolume(XML_SPEECH_VOLUME); |
| 643 | } else { |
| 644 | enable_phone_call_AFE_path(DEV_BT); |
| 645 | usleep(100 * 1000);//100ms |
| 646 | updateDigitalGain(); |
| 647 | } |
| 648 | } else { |
| 649 | disable_phone_call_AFE_path(); |
| 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | int audio_ctrl_service_M2M_Call_on_inner(int enable) { |
| 656 | int ret; |
| 657 | int retry_ite = 0; |
| 658 | int input_device; |
| 659 | |
| 660 | ALOGD("%s(), enable=%d", __func__, enable); |
| 661 | if (enable == g_is_speech_on) { |
| 662 | ALOGE("%s(), speech on status no change! g_is_speech_on: %d", |
| 663 | __func__, g_is_speech_on); |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | g_is_speech_on = enable; |
| 668 | |
| 669 | input_device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 670 | g_current_output_device = AUDIO_DEVICE_OUT_SPEAKER; |
| 671 | |
| 672 | do { |
| 673 | if (enable || !g_record_buf) { |
| 674 | //force to speaker , main mic |
| 675 | ret = speechdrv_set_speech_on(enable, input_device, g_current_output_device); |
| 676 | ++retry_ite; |
| 677 | if (!ret) { |
| 678 | break; |
| 679 | } |
| 680 | } else { //recording!! |
| 681 | ALOGE("%s(), record is still on, Will auto sppech off after record off, don't worry!", __func__); |
| 682 | g_want_to_speech_off_after_record_off = 1; |
| 683 | ret = 0; |
| 684 | break; |
| 685 | } |
| 686 | usleep(SPEECH_DRV_RETRY_US); |
| 687 | } while (retry_ite < SPEECH_DRV_RETRY_TIME); |
| 688 | |
| 689 | if (ret) { |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | int audio_ctrl_service_ECall_on_inner(int enable) { |
| 697 | int ret; |
| 698 | int retry_ite = 0; |
| 699 | int device_mode = 0; |
| 700 | int input_device; |
| 701 | int outputDevice; |
| 702 | |
| 703 | ALOGD("%s(), enable=%d", __func__, enable); |
| 704 | if (enable == g_is_speech_on) { |
| 705 | ALOGE("%s(), speech on status no change! g_is_speech_on: %d", |
| 706 | __func__, g_is_speech_on); |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | g_is_speech_on = enable; |
| 711 | input_device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 712 | g_current_output_device = AUDIO_DEVICE_OUT_SPEAKER; |
| 713 | |
| 714 | do { |
| 715 | if (enable || !g_record_buf) { |
| 716 | //force to speaker , main mic |
| 717 | ret = speechdrv_set_speech_on(enable, input_device, g_current_output_device); |
| 718 | ++retry_ite; |
| 719 | if (!ret) { |
| 720 | break; |
| 721 | } |
| 722 | } else { //recording!! |
| 723 | ALOGE("%s(), record is still on, Will auto sppech off after record off, don't worry!", __func__); |
| 724 | g_want_to_speech_off_after_record_off = 1; |
| 725 | ret = 0; |
| 726 | break; |
| 727 | } |
| 728 | usleep(SPEECH_DRV_RETRY_US); |
| 729 | } while (retry_ite < SPEECH_DRV_RETRY_TIME); |
| 730 | |
| 731 | if (ret) { |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | if (enable) { |
| 736 | device_mode = get_default_device(); |
| 737 | enable_phone_call_AFE_path(device_mode); |
| 738 | usleep(100 * 1000);//100ms |
| 739 | updateVolume(XML_SPEECH_VOLUME); |
| 740 | } else { |
| 741 | disable_phone_call_AFE_path(); |
| 742 | } |
| 743 | |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | int audio_ctrl_service_set_volume_index_inner(int volumeIdx) { |
| 748 | ALOGD("%s(), volumeIdx=%d, valid range (%d~%d)", __func__, volumeIdx, DL_VOLUME_IDX_MIN, DL_VOLUME_IDX_MAX); |
| 749 | if (volumeIdx > DL_VOLUME_IDX_MAX || volumeIdx < DL_VOLUME_IDX_MIN) { |
| 750 | ALOGE("%s(), Invalid DL gain value: %d, valid range (%d~%d)", |
| 751 | __func__, volumeIdx, DL_VOLUME_IDX_MIN, DL_VOLUME_IDX_MAX); |
| 752 | return -1; |
| 753 | } |
| 754 | g_volume_index = volumeIdx; |
| 755 | updateVolume(XML_SPEECH_VOLUME); |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | int audio_ctrl_service_set_speaker_type_inner(int type) { |
| 760 | if (g_speaker_type == type) { |
| 761 | AUDIO_V_LOG("%s(), the same speaker type(0x%x), return.", __func__, g_speaker_type); |
| 762 | return 0; |
| 763 | } |
| 764 | speechdrv_set_speaker_type(type); |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | int audio_ctrl_service_get_volume_index_inner() { |
| 769 | AUDIO_V_LOG("%s()", __func__); |
| 770 | return g_volume_index; |
| 771 | } |
| 772 | |
| 773 | #if defined(MTK_SPEECH_RECORD_SUPPORT) |
| 774 | int audio_ctrl_service_inCall_record_start_inner(int buf_size) { |
| 775 | int ret; |
| 776 | |
| 777 | AUDIO_V_LOG("%s()", __func__); |
| 778 | if (buf_size > MAX_REC_BUF_SIZE || buf_size <= 0 || buf_size % 2) { |
| 779 | return -EINVAL; |
| 780 | } |
| 781 | if (g_record_buf) { |
| 782 | ALOGE("%s(), record already open!", __func__); |
| 783 | return -EBUSY; |
| 784 | } |
| 785 | g_record_buf_size = buf_size; |
| 786 | g_record_buf = malloc(g_record_buf_size); |
| 787 | g_record_read_virt_idx = 0; |
| 788 | g_record_write_virt_idx = 0; |
| 789 | g_record_virt_boundry = buf_size * 1000; |
| 790 | |
| 791 | #ifndef RECORD_USE_UT_SOURCE |
| 792 | // ret = speechdrv_set_RecordOn(RECORD_TYPE_MIX); |
| 793 | #else |
| 794 | ret = tmp_recordOn(); |
| 795 | #endif |
| 796 | |
| 797 | if (ret < 0) { |
| 798 | free(g_record_buf); |
| 799 | } |
| 800 | |
| 801 | return ret; |
| 802 | } |
| 803 | |
| 804 | int audio_ctrl_service_inCall_record_stop_inner() { |
| 805 | int ret; |
| 806 | |
| 807 | AUDIO_V_LOG("%s()", __func__); |
| 808 | if (!g_record_buf) { |
| 809 | ALOGE("%s(), record not start!", __func__); |
| 810 | return -EINVAL; |
| 811 | } |
| 812 | #ifndef RECORD_USE_UT_SOURCE |
| 813 | |
| 814 | // ret = speechdrv_set_RecordOff(RECORD_TYPE_MIX); |
| 815 | |
| 816 | if (g_want_to_speech_off_after_record_off) { |
| 817 | // int ret_speech_off = speechdrv_set_speech_on(0, 0); |
| 818 | if (ret_speech_off) { |
| 819 | ALOGE("%s(), speechdrv_set_speech_on failed! err %d", __func__, |
| 820 | ret_speech_off); |
| 821 | } |
| 822 | g_want_to_speech_off_after_record_off = 0; |
| 823 | } |
| 824 | #else |
| 825 | ret = tmp_recordOff(); |
| 826 | #endif |
| 827 | free(g_record_buf); |
| 828 | g_record_buf = 0; |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | |
| 833 | int audio_ctrl_service_inCall_record_get_watermark_inner() { |
| 834 | int watermark; |
| 835 | |
| 836 | if (!g_record_buf) { |
| 837 | ALOGE("%s(), record not start!", __func__); |
| 838 | return -EINVAL; |
| 839 | } |
| 840 | watermark = (g_record_write_virt_idx - g_record_read_virt_idx + g_record_virt_boundry) % g_record_virt_boundry; |
| 841 | if (watermark > g_record_buf_size) { //XRUN |
| 842 | ALOGE("%s(), XRUN reset ptr", __func__); |
| 843 | g_record_write_virt_idx = g_record_read_virt_idx; |
| 844 | return -EPIPE; |
| 845 | } |
| 846 | |
| 847 | return watermark; |
| 848 | } |
| 849 | |
| 850 | int audio_ctrl_service_inCall_record_pointer_inner() { |
| 851 | int watermark; |
| 852 | |
| 853 | AUDIO_V_LOG("%s()", __func__); |
| 854 | if (!g_record_buf) { |
| 855 | ALOGE("%s(), record not start!", __func__); |
| 856 | return -EINVAL; |
| 857 | } |
| 858 | |
| 859 | watermark = audio_ctrl_service_inCall_record_get_watermark_inner(); |
| 860 | if (watermark < 0) { |
| 861 | return watermark; |
| 862 | } |
| 863 | return (g_record_write_virt_idx % g_record_buf_size); |
| 864 | } |
| 865 | |
| 866 | int check_copy_twice(int virt_ptr, int buf_size, int size) { |
| 867 | if (virt_ptr % buf_size + size > buf_size) { |
| 868 | return 1; |
| 869 | } else { |
| 870 | return 0; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | int audio_ctrl_service_inCall_record_get_data_inner(int data_size, char *dest) { |
| 875 | int watermark; |
| 876 | int size_to_take; |
| 877 | int copy_twice = 0; |
| 878 | int read_phy_idx = g_record_read_virt_idx % g_record_buf_size; |
| 879 | int first_copy_size; |
| 880 | |
| 881 | AUDIO_V_LOG("%s()", __func__); |
| 882 | if (!g_record_buf) { |
| 883 | ALOGE("%s(), record not start!", __func__); |
| 884 | return -EINVAL; |
| 885 | } |
| 886 | |
| 887 | watermark = audio_ctrl_service_inCall_record_get_watermark_inner(); |
| 888 | if (watermark < 0) { |
| 889 | return watermark; |
| 890 | } |
| 891 | |
| 892 | size_to_take = (data_size < watermark) ? data_size : watermark; |
| 893 | copy_twice = check_copy_twice(g_record_read_virt_idx, g_record_buf_size, size_to_take); |
| 894 | |
| 895 | if (!copy_twice) { |
| 896 | memcpy(dest, g_record_buf + read_phy_idx, size_to_take); |
| 897 | } else { |
| 898 | first_copy_size = g_record_buf_size - read_phy_idx; |
| 899 | memcpy(dest, g_record_buf + read_phy_idx, first_copy_size); |
| 900 | dest += first_copy_size; |
| 901 | memcpy(dest, g_record_buf, size_to_take - first_copy_size); |
| 902 | } |
| 903 | g_record_read_virt_idx += size_to_take; |
| 904 | g_record_read_virt_idx %= g_record_virt_boundry; |
| 905 | return size_to_take; |
| 906 | } |
| 907 | |
| 908 | int audio_ctrl_service_inCall_record_data_cb_pcm(int data_size, char *data) { |
| 909 | int copy_twice = 0; |
| 910 | int write_phy_idx = g_record_write_virt_idx % g_record_buf_size; |
| 911 | int first_copy_size; |
| 912 | |
| 913 | if (!g_record_buf) { |
| 914 | return -EINVAL; |
| 915 | } |
| 916 | |
| 917 | copy_twice = check_copy_twice(g_record_write_virt_idx, g_record_buf_size, data_size); |
| 918 | if (!copy_twice) { |
| 919 | memcpy(g_record_buf + write_phy_idx, data, data_size); |
| 920 | } else { |
| 921 | first_copy_size = g_record_buf_size - write_phy_idx; |
| 922 | memcpy(g_record_buf + write_phy_idx, data, first_copy_size); |
| 923 | data += first_copy_size; |
| 924 | memcpy(g_record_buf, data, data_size - first_copy_size); |
| 925 | } |
| 926 | |
| 927 | g_record_write_virt_idx += data_size; |
| 928 | g_record_write_virt_idx %= g_record_virt_boundry; |
| 929 | return data_size; |
| 930 | } |
| 931 | |
| 932 | int16_t *do_record_8k_to_16k_src(int16_t *src, int size_byte) { |
| 933 | int offset, err; |
| 934 | int16_t *dst_buf; |
| 935 | assert(size_byte == RECORD_SRC_BUF_SIZE * 2); |
| 936 | if (!g_incall_record_src_state) { //not init |
| 937 | g_incall_record_src_state = src_new(g_converter_type, 1, &err); |
| 938 | if (!g_incall_record_src_state) { |
| 939 | ALOGE("%s(), src_new failed err=%d", __func__, err); |
| 940 | return NULL; |
| 941 | } |
| 942 | assert(!g_incall_record_src_buffer); |
| 943 | assert(!g_incall_record_dst_buffer); |
| 944 | g_incall_record_src_buffer = calloc(1, sizeof(float) * RECORD_SRC_BUF_SIZE); |
| 945 | g_incall_record_dst_buffer = calloc(1, sizeof(float) * RECORD_DST_BUF_SIZE); |
| 946 | |
| 947 | g_incall_record_src_data.data_in = g_incall_record_src_buffer; |
| 948 | g_incall_record_src_data.data_out = g_incall_record_dst_buffer; |
| 949 | g_incall_record_src_data.src_ratio = RECORD_SRC_RATIO; |
| 950 | g_incall_record_src_data.end_of_input = 0; |
| 951 | } |
| 952 | |
| 953 | /* do convert */ |
| 954 | g_incall_record_src_data.input_frames = RECORD_SRC_BUF_SIZE; |
| 955 | g_incall_record_src_data.output_frames = RECORD_DST_BUF_SIZE; |
| 956 | src_short_to_float_array(src, g_incall_record_src_buffer, RECORD_SRC_BUF_SIZE); |
| 957 | err = src_process(g_incall_record_src_state, &g_incall_record_src_data); |
| 958 | if (err) { |
| 959 | ALOGE("%s(), src_process failed %d", __func__, err); |
| 960 | return NULL; |
| 961 | } |
| 962 | |
| 963 | dst_buf = g_incall_record_dst_int_buffer; |
| 964 | if (g_incall_record_src_data.output_frames > g_incall_record_src_data.output_frames_gen) { |
| 965 | offset = g_incall_record_src_data.output_frames - g_incall_record_src_data.output_frames_gen; |
| 966 | } else { |
| 967 | offset = 0; |
| 968 | } |
| 969 | |
| 970 | src_float_to_short_array(g_incall_record_dst_buffer, dst_buf + offset, |
| 971 | g_incall_record_src_data.output_frames_gen); |
| 972 | |
| 973 | return dst_buf; |
| 974 | } |
| 975 | |
| 976 | int audio_ctrl_service_inCall_record_data_cb_inner(int data_size, char *data) { |
| 977 | MD_PCM_RECORD_HEADER *md_pcm_record_header; |
| 978 | char *data_process_ptr = data; |
| 979 | char *pcm_data_ptr; |
| 980 | uint16_t syncWord; |
| 981 | uint16_t rawPcmDir; |
| 982 | uint16_t freq; |
| 983 | uint16_t pcmLength; |
| 984 | uint16_t channel; |
| 985 | uint16_t bitFormat; |
| 986 | |
| 987 | int16_t *ul_data_ptr = 0; |
| 988 | int ul_data_ptr_size; |
| 989 | int ul_data_rate; |
| 990 | int16_t *dl_data_ptr = 0; |
| 991 | int dl_data_ptr_size; |
| 992 | int dl_data_rate; |
| 993 | |
| 994 | int total_data_size; |
| 995 | int16_t *total_data_ptr; |
| 996 | |
| 997 | int i, j; |
| 998 | int ret; |
| 999 | |
| 1000 | while (data_process_ptr < data + data_size) { |
| 1001 | md_pcm_record_header = (MD_PCM_RECORD_HEADER *)data_process_ptr; |
| 1002 | syncWord = md_pcm_record_header->u16SyncWord; |
| 1003 | rawPcmDir = md_pcm_record_header->u16RawPcmDir; |
| 1004 | freq = md_pcm_record_header->u16Freq; |
| 1005 | pcmLength = md_pcm_record_header->u16Length; |
| 1006 | channel = md_pcm_record_header->u16Channel; |
| 1007 | bitFormat = md_pcm_record_header->u16BitFormat; |
| 1008 | pcm_data_ptr = data_process_ptr + sizeof(MD_PCM_RECORD_HEADER); |
| 1009 | |
| 1010 | if (syncWord != MD_PCM_RECORD_HEADER_SYNC) { |
| 1011 | ALOGE("%s(), Invalid packet, sync word: 0x%x", __func__, syncWord); |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | if (rawPcmDir == RECORD_TYPE_UL) { |
| 1016 | assert(!ul_data_ptr); |
| 1017 | ul_data_ptr = (uint16_t *)pcm_data_ptr; |
| 1018 | ul_data_ptr_size = pcmLength; |
| 1019 | ul_data_rate = freq; |
| 1020 | assert(channel == 1); |
| 1021 | } else { |
| 1022 | assert(!dl_data_ptr); |
| 1023 | dl_data_ptr = (uint16_t *)pcm_data_ptr; |
| 1024 | dl_data_ptr_size = pcmLength; |
| 1025 | dl_data_rate = freq; |
| 1026 | assert(channel == 1); |
| 1027 | } |
| 1028 | |
| 1029 | data_process_ptr += sizeof(MD_PCM_RECORD_HEADER) + pcmLength; |
| 1030 | } |
| 1031 | |
| 1032 | assert(ul_data_ptr); |
| 1033 | assert(dl_data_ptr); |
| 1034 | //only UL can be 8k or 16k, DL must be 16k |
| 1035 | total_data_size = dl_data_ptr_size; |
| 1036 | assert(!(total_data_size % ul_data_ptr_size)); |
| 1037 | |
| 1038 | total_data_ptr = malloc(total_data_size); |
| 1039 | |
| 1040 | if (ul_data_ptr_size < total_data_size) { |
| 1041 | ul_data_ptr = do_record_8k_to_16k_src(ul_data_ptr, ul_data_ptr_size); |
| 1042 | if (!ul_data_ptr) { |
| 1043 | return -EINVAL; |
| 1044 | } |
| 1045 | ul_data_ptr_size *= 2; |
| 1046 | } |
| 1047 | |
| 1048 | assert(total_data_size == ul_data_ptr_size); |
| 1049 | //MIX sound |
| 1050 | for (i = 0; i < ul_data_ptr_size / 2; ++i) { |
| 1051 | total_data_ptr[i] = ul_data_ptr[i] >> 1; |
| 1052 | } |
| 1053 | |
| 1054 | for (i = 0; i < dl_data_ptr_size / 2; ++i) { |
| 1055 | total_data_ptr[i] += dl_data_ptr[i] >> 1; |
| 1056 | } |
| 1057 | |
| 1058 | //write data |
| 1059 | ret = audio_ctrl_service_inCall_record_data_cb_pcm(total_data_size, total_data_ptr); |
| 1060 | free(total_data_ptr); |
| 1061 | return ret; |
| 1062 | } |
| 1063 | #endif |
| 1064 | |
| 1065 | #if defined(MTK_SPEECH_BGS_SUPPORT) |
| 1066 | int audio_ctrl_service_inCall_playback_start_inner() { |
| 1067 | #ifndef PLAYBACK_USE_UT_SINK |
| 1068 | int ret; |
| 1069 | AUDIO_V_LOG("%s()", __func__); |
| 1070 | if (!g_is_bgs_on) { |
| 1071 | ret = speechdrv_bgs_open(PHONECALL_SAMPLE_RATE); |
| 1072 | g_is_bgs_on = 1; |
| 1073 | } else { |
| 1074 | ALOGE("%s(), already start!", __func__); |
| 1075 | return 0; |
| 1076 | } |
| 1077 | return 0; |
| 1078 | #else |
| 1079 | return UT_BGSOn(); |
| 1080 | #endif |
| 1081 | } |
| 1082 | |
| 1083 | int audio_ctrl_service_inCall_playback_stop_inner() { |
| 1084 | #ifndef PLAYBACK_USE_UT_SINK |
| 1085 | AUDIO_V_LOG("%s()", __func__); |
| 1086 | speechdrv_bgs_close(); |
| 1087 | g_is_bgs_on = 0; |
| 1088 | return 0; |
| 1089 | #else |
| 1090 | return UT_BGSOff(); |
| 1091 | #endif |
| 1092 | } |
| 1093 | static const uint16_t table_1k_tone_16000_hz[] = { |
| 1094 | 0x0000, 0x30FC, 0x5A82, 0x7641, |
| 1095 | 0x7FFF, 0x7641, 0x5A82, 0x30FB, |
| 1096 | 0x0001, 0xCF05, 0xA57E, 0x89C0, |
| 1097 | 0x8001, 0x89BF, 0xA57E, 0xCF05 |
| 1098 | }; |
| 1099 | static const uint32_t kSizeSinewaveTable = sizeof(table_1k_tone_16000_hz); |
| 1100 | |
| 1101 | int audio_ctrl_service_inCall_playback_send_data_inner(int data_size, void *data_buf) { |
| 1102 | #ifndef PLAYBACK_USE_UT_SINK |
| 1103 | int ret; |
| 1104 | int write_size; |
| 1105 | int written_size = 0; |
| 1106 | int try_cnt = 20; |
| 1107 | if (!g_is_bgs_on) { |
| 1108 | ALOGE("%s(), not running!", __func__); |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | while (data_size > 0) { |
| 1113 | write_size = (data_size > PLAYBACK_BUF_SIZE) ? PLAYBACK_BUF_SIZE : data_size; |
| 1114 | AUDIO_V_LOG("%s(), sample rate: %d, data_size: %d, write_size: %d", |
| 1115 | __func__, PHONECALL_SAMPLE_RATE, data_size, write_size); |
| 1116 | ret = speechdrv_bgs_write(data_buf, write_size); |
| 1117 | if (ret < 0) { |
| 1118 | ALOGE("%s(), write to BGS error! ret: %d", __func__, ret); |
| 1119 | break; |
| 1120 | } |
| 1121 | data_size -= ret; |
| 1122 | data_buf += ret; |
| 1123 | written_size += ret; |
| 1124 | --try_cnt; |
| 1125 | if (try_cnt <= 0) { |
| 1126 | break; |
| 1127 | } |
| 1128 | usleep(SPEECH_DRV_BGS_INTERVAL_US); |
| 1129 | } |
| 1130 | return written_size; |
| 1131 | |
| 1132 | #else |
| 1133 | return UT_BGSSendData(data_buf, data_size); |
| 1134 | #endif |
| 1135 | } |
| 1136 | |
| 1137 | int audio_ctrl_service_inCall_playback_simulation(int data_ms) { |
| 1138 | uint32_t total_size = PHONECALL_SAMPLE_RATE * 2 * data_ms / 1000; |
| 1139 | uint32_t buffer_size = PLAYBACK_PERIOD_SIZE; |
| 1140 | uint32_t total_left = total_size, buffer_count = buffer_size, consume_byte = 0; |
| 1141 | char buffer[PLAYBACK_PERIOD_SIZE] = {0}; |
| 1142 | |
| 1143 | ALOGD("+%s(), data_ms = %d, total_size = %d, buffer_size = %d", |
| 1144 | __func__, data_ms, total_size, buffer_size); |
| 1145 | audio_ctrl_service_inCall_playback_start_inner(); |
| 1146 | while (total_left > 0) { |
| 1147 | uint32_t count = 0; |
| 1148 | if (total_left > buffer_size) { |
| 1149 | buffer_count = buffer_size; |
| 1150 | } else { |
| 1151 | buffer_count = total_left; |
| 1152 | } |
| 1153 | while (buffer_count > kSizeSinewaveTable) { |
| 1154 | memcpy(buffer + count, table_1k_tone_16000_hz, kSizeSinewaveTable); |
| 1155 | count += kSizeSinewaveTable; |
| 1156 | buffer_count -= kSizeSinewaveTable; |
| 1157 | } |
| 1158 | if (buffer_count > 0) { |
| 1159 | memcpy(buffer + count, table_1k_tone_16000_hz, buffer_count); |
| 1160 | count += buffer_count; |
| 1161 | } |
| 1162 | consume_byte = audio_ctrl_service_inCall_playback_send_data_inner(buffer_size, buffer); |
| 1163 | total_left -= count; |
| 1164 | } |
| 1165 | audio_ctrl_service_inCall_playback_stop_inner(); |
| 1166 | ALOGD("-%s(), data_ms = %d, total_size = %d, total_left = %d", |
| 1167 | __func__, data_ms, total_size, total_left); |
| 1168 | return total_size; |
| 1169 | } |
| 1170 | #endif |
| 1171 | |
| 1172 | int audio_ctrl_service_is_speech_on_inner(int output_device) { |
| 1173 | int result = 0; |
| 1174 | if (!g_is_speech_on) { |
| 1175 | result = 0; |
| 1176 | return result; |
| 1177 | } |
| 1178 | if (output_device == g_current_output_device) { |
| 1179 | result = 1; |
| 1180 | } else { |
| 1181 | result = 0; |
| 1182 | } |
| 1183 | AUDIO_V_LOG("%s(), result = %d, output_device=0x%x", __func__, result, output_device); |
| 1184 | return result; |
| 1185 | } |
| 1186 | |
| 1187 | int audio_ctrl_service_get_record_period_size_inner() { |
| 1188 | AUDIO_V_LOG("%s()", __func__); |
| 1189 | return RECORD_PERIOD_SIZE; |
| 1190 | } |
| 1191 | |
| 1192 | int audio_ctrl_service_get_playback_period_size_inner() { |
| 1193 | AUDIO_V_LOG("%s()", __func__); |
| 1194 | return PLAYBACK_PERIOD_SIZE; |
| 1195 | } |
| 1196 | |
| 1197 | int audio_ctrl_service_get_record_max_buffer_size_inner() { |
| 1198 | AUDIO_V_LOG("%s()", __func__); |
| 1199 | return RECORD_MAX_BUFFER_SIZE; |
| 1200 | } |
| 1201 | |
| 1202 | int audio_ctrl_service_get_playback_max_buffer_size_inner() { |
| 1203 | ALOGE("%s()", __func__); |
| 1204 | return PLAYBACK_MAX_BUFFER_SIZE; |
| 1205 | } |
| 1206 | |
| 1207 | #if defined(MTK_SPEECH_VM_SUPPORT) |
| 1208 | /* for vmlog, need to record if vmlog on */ |
| 1209 | int g_vmlog_on; |
| 1210 | int audio_ctrl_service_vmlog_on_inner(int vmlog_on) { |
| 1211 | g_vmlog_on = vmlog_on; |
| 1212 | return speechdrv_set_vm_on(vmlog_on); |
| 1213 | } |
| 1214 | |
| 1215 | int audio_ctrl_service_get_vmlog_on_inner() { |
| 1216 | return g_vmlog_on; |
| 1217 | } |
| 1218 | #endif |
| 1219 | |
| 1220 | /* for BT setting */ |
| 1221 | int audio_ctrl_service_set_bt_wbs_inner(int bt_wbs_on) { |
| 1222 | AUDIO_V_LOG("%s()", __func__); |
| 1223 | g_bt_wbs_on = !!bt_wbs_on; |
| 1224 | set_BT_WB(g_bt_wbs_on); |
| 1225 | speechdrv_set_bt_wbs(g_bt_wbs_on); |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | int audio_ctrl_service_get_bt_wbs_inner() { |
| 1230 | AUDIO_V_LOG("%s()", __func__); |
| 1231 | return g_bt_wbs_on; |
| 1232 | } |
| 1233 | |
| 1234 | int audio_ctrl_service_set_bt_client_has_ecnr_inner(int bt_client_ecnr_on) { |
| 1235 | if (g_bt_client_has_ecnr == bt_client_ecnr_on) { |
| 1236 | AUDIO_V_LOG("%s(), the same bt_nrec(0x%x) during voice call.", __func__, g_current_output_device); |
| 1237 | return 0; |
| 1238 | } |
| 1239 | AUDIO_V_LOG("%s(), bt_client_ecnr_on(%d->%d)", __func__, g_bt_client_has_ecnr, bt_client_ecnr_on); |
| 1240 | g_bt_client_has_ecnr = bt_client_ecnr_on; |
| 1241 | |
| 1242 | return speechdrv_set_bt_nrec(bt_client_ecnr_on); |
| 1243 | } |
| 1244 | |
| 1245 | int audio_ctrl_service_get_bt_client_has_ecnr_inner() { |
| 1246 | AUDIO_V_LOG("%s()", __func__); |
| 1247 | return g_bt_client_has_ecnr; |
| 1248 | } |
| 1249 | |
| 1250 | #if !defined(MTK_AUDIO_BRINGUP) |
| 1251 | int audio_ctrl_service_set_use_bt_in_call_inner(int turn_on_bt_in_call) { |
| 1252 | AUDIO_V_LOG("%s()", __func__); |
| 1253 | int new_output_device = AUDIO_DEVICE_NONE; |
| 1254 | int input_device = AUDIO_DEVICE_NONE; |
| 1255 | |
| 1256 | //if not in call, do nothing |
| 1257 | if (!g_is_speech_on) { return 0; } |
| 1258 | if (turn_on_bt_in_call) { |
| 1259 | new_output_device = AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 1260 | } else { |
| 1261 | new_output_device = AUDIO_DEVICE_OUT_SPEAKER; |
| 1262 | } |
| 1263 | if (new_output_device == g_current_output_device) { |
| 1264 | AUDIO_V_LOG("%s(), the same output device(0x%x) during voice call.", |
| 1265 | __func__, g_current_output_device); |
| 1266 | return 0; |
| 1267 | } |
| 1268 | g_current_output_device = new_output_device; |
| 1269 | if (g_current_output_device == AUDIO_DEVICE_OUT_SPEAKER) { |
| 1270 | input_device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 1271 | } else { |
| 1272 | input_device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 1273 | } |
| 1274 | |
| 1275 | AUDIO_V_LOG("%s(), output device(0x%x), input_device(0x%x).", |
| 1276 | __func__, g_current_output_device, input_device); |
| 1277 | if (g_current_output_device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) { |
| 1278 | dynamic_switch_phone_call_path(DEV_BT); |
| 1279 | } else { |
| 1280 | dynamic_switch_phone_call_path(get_default_device()); |
| 1281 | } |
| 1282 | speechdrv_change_speech_device(input_device, g_current_output_device); |
| 1283 | updateVolume(XML_SPEECH_VOLUME); |
| 1284 | |
| 1285 | return 0; |
| 1286 | } |
| 1287 | #endif |
| 1288 | |
| 1289 | int audio_ctrl_service_get_use_bt_in_call_inner() { |
| 1290 | AUDIO_V_LOG("%s()", __func__); |
| 1291 | if (!g_is_speech_on) { return 0; } |
| 1292 | return (g_current_output_device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET); |
| 1293 | } |
| 1294 | |
| 1295 | int audio_ctrl_service_reset_inner() { |
| 1296 | AUDIO_V_LOG("%s()", __func__); |
| 1297 | g_want_to_speech_off_after_record_off = 0; |
| 1298 | if (g_is_speech_on) { |
| 1299 | disable_phone_call_AFE_path(); |
| 1300 | } |
| 1301 | g_is_speech_on = 0; |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | /* DL:0 UL:1 */ |
| 1306 | int g_dl_mute_on; |
| 1307 | int g_ul_mute_on; |
| 1308 | int audio_ctrl_service_set_mute_inner(int path, int is_mute) { |
| 1309 | AUDIO_V_LOG("%s()", __func__); |
| 1310 | if (path == SPEECH_DL) { |
| 1311 | g_dl_mute_on = is_mute; |
| 1312 | speechdrv_set_dl_mute(g_dl_mute_on); |
| 1313 | } else { |
| 1314 | g_ul_mute_on = is_mute; |
| 1315 | speechdrv_set_ul_mute(g_ul_mute_on); |
| 1316 | } |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | int audio_ctrl_service_get_mute_inner(int path) { |
| 1321 | AUDIO_V_LOG("%s()", __func__); |
| 1322 | if (path == SPEECH_DL) { |
| 1323 | return g_dl_mute_on; |
| 1324 | } else { |
| 1325 | return g_ul_mute_on; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | #if defined(MTK_SPEECH_BGS_SUPPORT) |
| 1330 | /* for BGS UT */ |
| 1331 | |
| 1332 | static const char const *g_BGS_file_path = "/tmp/bgs_UT.pcm"; |
| 1333 | FILE *g_UT_file; |
| 1334 | |
| 1335 | int UT_BGSOn() { |
| 1336 | g_UT_file = fopen(g_BGS_file_path, "w"); |
| 1337 | if (!g_UT_file) { |
| 1338 | return -EIO; |
| 1339 | } |
| 1340 | return 0; |
| 1341 | } |
| 1342 | int UT_BGSOff() { |
| 1343 | if (g_UT_file) { |
| 1344 | fclose(g_UT_file); |
| 1345 | } |
| 1346 | g_UT_file = 0; |
| 1347 | return 0; |
| 1348 | } |
| 1349 | int UT_BGSSendData(void *buf, int buf_size) { |
| 1350 | int sleep_time_ms = buf_size * 1000 / PHONECALL_SAMPLE_RATE / 2; |
| 1351 | if (!g_UT_file) { |
| 1352 | ALOGE("%s(), plz send data after turn on", __func__); |
| 1353 | return 0; |
| 1354 | } |
| 1355 | fwrite(buf, 1, buf_size, g_UT_file); |
| 1356 | return buf_size; |
| 1357 | } |
| 1358 | #endif |