blob: 8674cc9378819a26b987fe8dd27510f9b5fa1ed0 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001#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
29//#define UT_LOG
30#ifdef UT_LOG
31#define AUDIO_V_LOG printf
32#else
33#define AUDIO_V_LOG
34#endif
35
36#define PATH_BUF_SIZE 1024
37#define SPEECH_DRV_RETRY_TIME 3
38#define SPEECH_DRV_RETRY_US 20000 //20ms
39enum
40{
41 SPEECH_DRV_NORMAL = 0,
42 SPEECH_DRV_EARPHONE = 1,
43 SPEECH_DRV_LSPEAKER = 2,
44 SPEECH_DRV_BT_EARPHONE = 3,
45 SPEECH_DRV_BT_CORDLESS = 4,
46 SPEECH_DRV_BT_CARKIT = 5,
47 SPEECH_DRV_MAGIC_CON_CALL = 6,
48 SPEECH_DRV_PRESERVED_2 = 7,
49 SPEECH_DRV_MODE_HAC = 8,
50 SPEECH_DRV_MODE_NO_CONNECT = 9
51};
52
53const char const *BAND_PATH_STR[BAND_NUM] = {"Band,NB","Band,WB"};
54const char const *PROFILE_PATH_STR[PROFILE_NUM] =
55 {"Profile,Normal","Profile,eCall",
56 "Profile,BT_ECNR"};
57const char const *VOLUME_PARAM_PATH = "VolumeParam,Common";
58
59#define MAX_REC_BUF_SIZE 3200//16000Hz 16bytes 100ms
60#define MD_PCM_RECORD_HEADER_SYNC 0x1234
61
62char *g_record_buf;
63int g_want_to_speech_off_after_record_off;
64
65int g_record_buf_size;
66int g_record_read_virt_idx;
67int g_record_write_virt_idx;
68int g_record_virt_boundry;
69
70#define AUDIO_FORMAT_PCM_16_BIT 0x1
71int g_BGSPlayer_init;
72int g_BGSPlayerBuffer_init;
73int g_BGSPlayerBuffer_idx;
74
75/* SRC for inCall record UL*/
76
77#define RECORD_SRC_BUF_SIZE 160 //in frame
78#define RECORD_DST_BUF_SIZE 320 //in frame
79#define RECORD_SRC_RATIO 2.0
80const int g_converter_type = SRC_SINC_MEDIUM_QUALITY;
81SRC_DATA g_incall_record_src_data;
82SRC_STATE *g_incall_record_src_state;
83float *g_incall_record_src_buffer;
84float *g_incall_record_dst_buffer;
85int16_t g_incall_record_dst_int_buffer[RECORD_DST_BUF_SIZE];
86
87pthread_mutex_t g_mutex_record = PTHREAD_MUTEX_INITIALIZER;
88pthread_mutex_t g_mutex_playback = PTHREAD_MUTEX_INITIALIZER;
89
90/* modem pcm record package header*/
91typedef struct md_pcm_record_header
92{
93 uint16_t u16SyncWord;
94 uint16_t u16RawPcmDir;
95 uint16_t u16Freq;
96 uint16_t u16Length;
97 uint16_t u16Channel;
98 uint16_t u16BitFormat;
99} MD_PCM_RECORD_HEADER;
100
101static AUDIO_SPEECH_MODE_NB_PARAM_STRUCT g_default_NB_SPEECH_PARA =
102{
103 DEFAULT_SPEECH_NORMAL_MODE_PARA,
104 DEFAULT_SPEECH_EARPHONE_MODE_PARA,
105 DEFAULT_SPEECH_BT_EARPHONE_MODE_PARA,
106 DEFAULT_SPEECH_LOUDSPK_MODE_PARA,
107 DEFAULT_SPEECH_CARKIT_MODE_PARA,
108 DEFAULT_SPEECH_BT_CORDLESS_MODE_PARA,
109 DEFAULT_SPEECH_AUX1_MODE_PARA,
110 DEFAULT_SPEECH_AUX2_MODE_PARA,
111};
112
113static AUDIO_SPEECH_MODE_WB_PARAM_STRUCT g_default_WB_SPEECH_PARA =
114{
115 DEFAULT_WB_SPEECH_NORMAL_MODE_PARA,
116 DEFAULT_WB_SPEECH_EARPHONE_MODE_PARA,
117 DEFAULT_WB_SPEECH_BT_EARPHONE_MODE_PARA,
118 DEFAULT_WB_SPEECH_LOUDSPK_MODE_PARA,
119 DEFAULT_WB_SPEECH_CARKIT_MODE_PARA,
120 DEFAULT_WB_SPEECH_BT_CORDLESS_MODE_PARA,
121 DEFAULT_WB_SPEECH_AUX1_MODE_PARA,
122 DEFAULT_WB_SPEECH_AUX2_MODE_PARA,
123};
124
125static AUDIO_FIR_NB_PARAM_STRUCT g_default_NB_FIR_PARA =
126{
127 SPEECH_INPUT_FIR_COEF,
128 SPEECH_OUTPUT_FIR_COEF,
129};
130
131static AUDIO_FIR_WB_PARAM_STRUCT g_default_WB_FIR_PARA =
132{
133 WB_SPEECH_INPUT_FIR_COEF,
134 WB_SPEECH_OUTPUT_FIR_COEF,
135};
136
137static AUDIO_SPEECH_MODE_NB_PARAM_STRUCT g_default_NB_SPEECH_PARA_ECALL =
138{
139 DEFAULT_SPEECH_NORMAL_MODE_PARA,
140 DEFAULT_SPEECH_EARPHONE_MODE_PARA,
141 DEFAULT_SPEECH_BT_EARPHONE_MODE_PARA,
142 DEFAULT_SPEECH_LOUDSPK_MODE_PARA,
143 DEFAULT_SPEECH_CARKIT_MODE_PARA,
144 DEFAULT_SPEECH_BT_CORDLESS_MODE_PARA,
145 DEFAULT_SPEECH_AUX1_MODE_PARA,
146 DEFAULT_SPEECH_AUX2_MODE_PARA,
147};
148
149static AUDIO_SPEECH_MODE_WB_PARAM_STRUCT g_default_WB_SPEECH_PARA_ECALL =
150{
151 DEFAULT_WB_SPEECH_NORMAL_MODE_PARA,
152 DEFAULT_WB_SPEECH_EARPHONE_MODE_PARA,
153 DEFAULT_WB_SPEECH_BT_EARPHONE_MODE_PARA,
154 DEFAULT_WB_SPEECH_LOUDSPK_MODE_PARA,
155 DEFAULT_WB_SPEECH_CARKIT_MODE_PARA,
156 DEFAULT_WB_SPEECH_BT_CORDLESS_MODE_PARA,
157 DEFAULT_WB_SPEECH_AUX1_MODE_PARA,
158 DEFAULT_WB_SPEECH_AUX2_MODE_PARA,
159};
160
161static AUDIO_FIR_NB_PARAM_STRUCT g_default_NB_FIR_PARA_ECALL =
162{
163 SPEECH_INPUT_FIR_COEF,
164 SPEECH_OUTPUT_FIR_COEF,
165};
166
167static AUDIO_FIR_WB_PARAM_STRUCT g_default_WB_FIR_PARA_ECALL =
168{
169 WB_SPEECH_INPUT_FIR_COEF,
170 WB_SPEECH_OUTPUT_FIR_COEF,
171};
172
173static Param *getParamValue(AppHandle *appHandle, char *type_name,
174 char *cat_path, char *param_name);
175
176static int getParamArraySize(Param *param) {return param->arraySize;}
177
178static unsigned short *getUShortArrayFromParam(Param *param,
179 unsigned short *ushortArrayDest);
180
181static short *getShortArrayFromParam(Param *param,
182 unsigned short *shortArrayDest);
183
184static void parseAndUpdateParameter(AppHandle *appHandle,
185 const char *audioTypeName);
186
187static char *getPathString(int band, int profile, char* dest);
188
189static void *speech_flow_upper_func_operator(void *arg);
190static void *speech_flow_lower_func_operator(void *arg);
191static void *speech_flow_dummy_sender(void *arg);
192
193static void updateSpeech48param(AppHandle *appHandle);
194static void updateFirInParam(AppHandle *appHandle);
195static void updateFirOutParam(AppHandle *appHandle);
196static void updateUlGainParam(AppHandle *appHandle);
197static void updateUlSwagcGainMapParam(AppHandle *appHandle);
198static void updateUlPgaGainMapParam(AppHandle *appHandle);
199static void updateDlAnalogGainMapParam(AppHandle *appHandle);
200static void updateDlDigitalGainMapParam(AppHandle *appHandle);
201
202static void doUpdateDlGain(int retry);
203static void doUpdateUlGain(void);
204
205
206const struct Param_data param_data_2635[PARAM_NUM] = {
207 {
208 .id = PARAM_SPEECH,
209 .xml_type_name = XML_TYPE_SPEECH_STR,
210 .param_name = SPEECH_PARA,
211 .param_type = PARAM_TYPE_USHORT_ARRAY,
212 .update_xml_callback = updateSpeech48param,
213 },
214 {
215 .id = PARAM_FIR_IN,
216 .xml_type_name = XML_TYPE_SPEECH_STR,
217 .param_name = FIR_IN_PARA,
218 .param_type = PARAM_TYPE_SHORT_ARRAY,
219 .update_xml_callback = updateFirInParam,
220 },
221 {
222 .id = PARAM_FIR_OUT,
223 .xml_type_name = XML_TYPE_SPEECH_STR,
224 .param_name = FIR_OUT_PARA,
225 .param_type = PARAM_TYPE_SHORT_ARRAY,
226 .update_xml_callback = updateFirOutParam,
227 },
228 {
229 .id = PARAM_UL_GAIN_IDX,
230 .xml_type_name = XML_TYPE_SPEECHVOL_STR,
231 .param_name = UL_GAIN_IDX_PARA,
232 .param_type = PARAM_TYPE_INT,
233 .update_xml_callback = updateUlGainParam,
234 },
235 {
236 .id = PARAM_UL_SWAGC_GAIN_MAP,
237 .xml_type_name = XML_TYPE_VOLUME_STR,
238 .param_name = SWAGC_GAIN_MAP_PARA,
239 .param_type = PARAM_TYPE_SHORT_ARRAY,
240 .update_xml_callback = updateUlSwagcGainMapParam,
241 },
242 {
243 .id = PARAM_UL_PGA_GAIN_MAP,
244 .xml_type_name = XML_TYPE_VOLUME_STR,
245 .param_name = UL_PGA_GAIN_MAP_PARA,
246 .param_type = PARAM_TYPE_SHORT_ARRAY,
247 .update_xml_callback = updateUlPgaGainMapParam,
248 },
249 {
250 .id = PARAM_DL_ANALOG_GAIN_MAP,
251 .xml_type_name = XML_TYPE_VOLUMEGAINMAP_STR,
252 .param_name = DL_ANALOG_GAIN_MAP_PARA,
253 .param_type = PARAM_TYPE_SHORT_ARRAY,
254 .update_xml_callback = updateDlAnalogGainMapParam,
255 },
256 {
257 .id = PARAM_DL_DIGITAL_GAIN_MAP,
258 .xml_type_name = XML_TYPE_VOLUMEGAINMAP_STR,
259 .param_name = DL_DIGITAL_GAIN_MAP_PARA,
260 .param_type = PARAM_TYPE_SHORT_ARRAY,
261 .update_xml_callback = updateDlDigitalGainMapParam,
262 },
263};
264
265#define UL_GAIN_MIN 0
266#define UL_GAIN_MAX 45
267#define DL_GAIN_MIN -23
268#define DL_GAIN_MAX 17
269#define BT_DL_GAIN_MIN 0
270#define BT_DL_GAIN_MAX 15
271
272/* table size = max - min + 1 */
273#define UL_GAIN_TABLE_SIZE 46
274#define DL_GAIN_TABLE_SIZE 41
275
276static short g_ul_analog_gain_table[UL_GAIN_TABLE_SIZE];
277static short g_ul_digital_gain_table[UL_GAIN_TABLE_SIZE];
278static int g_ul_gain_idx = INT_MAX; /* ul gain setting */
279
280static short g_dl_analog_gain_table[DL_GAIN_TABLE_SIZE];
281static short g_dl_digital_gain_table[DL_GAIN_TABLE_SIZE];
282static int g_dl_gain_idx = 0 - DL_GAIN_MIN; /* dl gain setting */
283static int g_bt_dl_gain = 10; /* BT DL gain, 0~15 */
284
285static int g_is_speech_on;
286static int g_speech_on_device;
287static int g_bt_wbs_on = FUNC_SET_BT_WB_default;
288static int g_bt_client_has_ecnr;
289
290/* record the BT ECNR setting, we might turn them off
291 and might need to restore them */
292
293enum BT_ECNR_SETTING {
294 BT_ECNR_AEC,
295 BT_ECNR_RX_NR,
296 BT_ECNR_TX_NR,
297 BT_ECNR_SETTING_NUM,
298};
299
300struct BT_ECNR_recorder {
301 int id;
302 int par_location;
303 int bit;
304 int inverse; //if inverse, 1 is off
305 int value_NB;
306 int value_WB;
307};
308
309struct BT_ECNR_recorder BT_ECNR_recorder_data[BT_ECNR_SETTING_NUM] = {
310 {
311 .id = BT_ECNR_AEC,
312 .par_location = 1,
313 .bit = 8,
314 .inverse = 1,
315 .value_NB = 0,
316 .value_WB = 0,
317 },
318 {
319 .id = BT_ECNR_RX_NR,
320 .par_location = 4,
321 .bit = 2,
322 .inverse = 0,
323 .value_NB = 1,
324 .value_WB = 1,
325 },
326 {
327 .id = BT_ECNR_TX_NR,
328 .par_location = 4,
329 .bit = 0,
330 .inverse = 0,
331 .value_NB = 1,
332 .value_WB = 1,
333 },
334};
335
336void update_BT_ECNR_recorder(int is_WB, unsigned short* para_arr)
337{
338 int par_location, bit;
339 for(int i=0;i<BT_ECNR_SETTING_NUM;++i) {
340 par_location = BT_ECNR_recorder_data[i].par_location;
341 bit = BT_ECNR_recorder_data[i].bit;
342 if (!is_WB) {
343 BT_ECNR_recorder_data[i].value_NB = (para_arr[par_location] >> bit) & 0x1;
344 } else {
345 BT_ECNR_recorder_data[i].value_WB = (para_arr[par_location] >> bit) & 0x1;
346 }
347 }
348}
349void close_all_BT_ECNR_param(int is_WB, unsigned short* para_arr)
350{
351 int par_location, bit, inverse;
352 unsigned short mask_bit;
353 for(int i=0;i<BT_ECNR_SETTING_NUM;++i) {
354 par_location = BT_ECNR_recorder_data[i].par_location;
355 bit = BT_ECNR_recorder_data[i].bit;
356 inverse = BT_ECNR_recorder_data[i].inverse;
357 mask_bit = 1 << bit;
358 para_arr[par_location] = para_arr[par_location] & (~mask_bit); //set the bit to 0
359 if (inverse) {
360 para_arr[par_location] = para_arr[par_location] | mask_bit; //set to 1
361 }
362 }
363}
364
365void restore_all_BT_ECNR_param(int is_WB, unsigned short* para_arr)
366{
367 int par_location, bit, value;
368 unsigned short mask_bit;
369 for(int i=0;i<BT_ECNR_SETTING_NUM;++i) {
370 par_location = BT_ECNR_recorder_data[i].par_location;
371 bit = BT_ECNR_recorder_data[i].bit;
372 if (!is_WB) {
373 value = BT_ECNR_recorder_data[i].value_NB;
374 } else {
375 value = BT_ECNR_recorder_data[i].value_WB;
376 }
377 mask_bit = 1 << bit;
378 para_arr[par_location] = para_arr[par_location] & (~mask_bit); //set the bit to 0
379 if (value) {
380 para_arr[par_location] = para_arr[par_location] | mask_bit; //set to 1
381 }
382 }
383}
384
385/* IPC variable*/
386static pthread_t g_pthread_speech_flow_upper_rcv;
387static pthread_t g_pthread_speech_flow_lower_rcv;
388static pthread_t g_pthread_speech_flow_dummy_send;
389
390static int g_ipc_upper_rcv_handler;
391static int g_ipc_upper_send_handler;
392static int g_ipc_lower_rcv_handler;
393static int g_ipc_lower_send_handler;
394
395
396int main()
397{
398 int i;
399 AppHandle *appHandle = NULL;
400 struct stat st = {0};
401
402 /* Get AppHandle global instance, this API will parse xml automatically */
403 appHandle = appHandleGetInstance();
404
405 /* Wait for initial done */
406 usleep(100 * 1000);//100ms
407
408 /* Set the debug level to warn*/
409 appSetDebugLevel(ERR_LEVEL);
410
411 /* register callback func */
412 appHandleRegXmlChangedCb(appHandle, parseAndUpdateParameter);
413
414 if (stat("/tmp/audio_ctrl_service", &st) == -1)
415 mkdir("/tmp/audio_ctrl_service", 0700);
416
417 /* for service init */
418 mkfifo(ACS_IPC_FOR_UPPER_RCV, 0600);
419 mkfifo(ACS_IPC_FOR_UPPER_SEND, 0600);
420 mkfifo(ACS_IPC_FOR_LOWER_RCV, 0600);
421 mkfifo(ACS_IPC_FOR_LOWER_SEND, 0600);
422
423 pthread_create(&g_pthread_speech_flow_upper_rcv, NULL, speech_flow_upper_func_operator,
424 NULL);
425 pthread_create(&g_pthread_speech_flow_lower_rcv, NULL, speech_flow_lower_func_operator,
426 NULL);
427 pthread_create(&g_pthread_speech_flow_dummy_send, NULL, speech_flow_dummy_sender, NULL);
428
429 /* initial setting for all param */
430 for (i = 0 ; i < PARAM_NUM; ++i)
431 param_data_2635[i].update_xml_callback(appHandle);
432
433 while(1)
434 sleep(1000000);
435 /* Release appHandle resources */
436 appHandleUninit(appHandle);
437
438 return 0;
439}
440
441static void *speech_flow_upper_func_operator(void *arg)
442{
443 char buf[IPC_DATA_SIZE_MAX];
444 int size = 0, function_type = 0, first_param = 0;
445 int str_output_size = 0;
446 int func_result;
447 char *first_param_str;
448 char *second_param_str;
449 char buf_response[IPC_DATA_SIZE_MAX];
450 char *buf_for_get_data = buf; //use the buffer to get data out for ioplugin
451
452 g_ipc_upper_rcv_handler = open(ACS_IPC_FOR_UPPER_RCV, O_RDONLY);
453 g_ipc_upper_send_handler = open(ACS_IPC_FOR_UPPER_SEND, O_WRONLY);
454
455 while(1){
456 size = read(g_ipc_upper_rcv_handler, buf, IPC_DATA_SIZE_MAX);
457 if(!size) {
458 printf("reset fifo!\n");
459 close(g_ipc_upper_rcv_handler);
460 g_ipc_upper_rcv_handler = open(ACS_IPC_FOR_UPPER_RCV, O_RDONLY);
461 continue;
462 }
463 /* finish string */
464 buf[size] = '\0';
465
466 /* parse string */
467 strtok_r(buf, ",", &first_param_str);
468 function_type = atoi(buf);
469 switch(function_type) {
470 case FUNC_AUDIO_CTRL_SERVICE_SPEECH_ON:
471 first_param = atoi(first_param_str);
472 pthread_mutex_lock(&g_mutex_record); //for g_want_to_speech_off_after_record_off
473 func_result = audio_ctrl_service_speech_on_inner(first_param, 0);
474 pthread_mutex_unlock(&g_mutex_record);
475 str_output_size = sprintf(buf_response, "%d", func_result);
476 break;
477 case FUNC_AUDIO_CTRL_SERVICE_SET_DL_VOLUME:
478 first_param = atoi(first_param_str);
479 func_result = audio_ctrl_service_set_dl_volume_inner(first_param);
480 str_output_size = sprintf(buf_response, "%d", func_result);
481 break;
482
483 case FUNC_AUDIO_CTRL_INCALL_RECORD_START:
484 first_param = atoi(first_param_str);
485 pthread_mutex_lock(&g_mutex_record);
486 func_result = audio_ctrl_service_inCall_record_start_inner(first_param);
487 pthread_mutex_unlock(&g_mutex_record);
488 str_output_size = sprintf(buf_response, "%d", func_result);
489 break;
490 case FUNC_AUDIO_CTRL_INCALL_RECORD_STOP:
491 pthread_mutex_lock(&g_mutex_record);
492 func_result = audio_ctrl_service_inCall_record_stop_inner();
493 pthread_mutex_unlock(&g_mutex_record);
494 str_output_size = sprintf(buf_response, "%d", func_result);
495 break;
496 case FUNC_AUDIO_CTRL_INCALL_RECORD_POINTER:
497 pthread_mutex_lock(&g_mutex_record);
498 func_result = audio_ctrl_service_inCall_record_pointer_inner();
499 pthread_mutex_unlock(&g_mutex_record);
500 str_output_size = sprintf(buf_response, "%d", func_result);
501 break;
502 case FUNC_AUDIO_CTRL_INCALL_RECORD_GET_WATERMARK:
503 pthread_mutex_lock(&g_mutex_record);
504 func_result = audio_ctrl_service_inCall_record_get_watermark_inner();
505 pthread_mutex_unlock(&g_mutex_record);
506 str_output_size = sprintf(buf_response, "%d", func_result);
507 break;
508 case FUNC_AUDIO_CTRL_INCALL_RECORD_GET_DATA:
509 first_param = atoi(first_param_str);
510 //use the "buf" to get data out for ioplugin
511 pthread_mutex_lock(&g_mutex_record);
512 func_result = audio_ctrl_service_inCall_record_get_data_inner
513 (first_param, buf_for_get_data);
514 pthread_mutex_unlock(&g_mutex_record);
515 str_output_size = sprintf(buf_response, "%d,", func_result);
516 if (func_result > 0) {
517 memcpy(buf_response+str_output_size, buf_for_get_data,
518 func_result);
519 str_output_size += func_result;
520 }
521 break;
522 case FUNC_AUDIO_CTRL_INCALL_PLAYBACK_START:
523 pthread_mutex_lock(&g_mutex_playback);
524 func_result = audio_ctrl_service_inCall_playback_start_inner();
525 pthread_mutex_unlock(&g_mutex_playback);
526 str_output_size = sprintf(buf_response, "%d", func_result);
527 break;
528 case FUNC_AUDIO_CTRL_INCALL_PLAYBACK_STOP:
529 pthread_mutex_lock(&g_mutex_playback);
530 func_result = audio_ctrl_service_inCall_playback_stop_inner();
531 pthread_mutex_unlock(&g_mutex_playback);
532 str_output_size = sprintf(buf_response, "%d", func_result);
533 break;
534 case FUNC_AUDIO_CTRL_INCALL_PLAYBACK_SEND_DATA:
535 first_param = atoi(first_param_str);
536 strtok_r(first_param_str, ",", &second_param_str);
537 pthread_mutex_lock(&g_mutex_playback);
538 func_result = audio_ctrl_service_inCall_playback_send_data_inner
539 (first_param, second_param_str);
540 pthread_mutex_unlock(&g_mutex_playback);
541 str_output_size = sprintf(buf_response, "%d", func_result);
542 break;
543 case FUNC_AUDIO_CTRL_SERVICE_IS_SPEECH_ON:
544 func_result = audio_ctrl_service_is_speech_on_inner(0);
545 str_output_size = sprintf(buf_response, "%d", func_result);
546 break;
547 case FUNC_AUDIO_CTRL_SERVICE_GET_DL_GAIN:
548 func_result = audio_ctrl_service_get_dl_gain_inner();
549 str_output_size = sprintf(buf_response, "%d", func_result);
550 break;
551 case FUNC_AUDIO_CTRL_SERVICE_GET_RECORD_PERIOD_SIZE:
552 func_result = audio_ctrl_service_get_record_period_size_inner();
553 str_output_size = sprintf(buf_response, "%d", func_result);
554 break;
555 case FUNC_AUDIO_CTRL_SERVICE_GET_PLAYBACK_PERIOD_SIZE:
556 func_result = audio_ctrl_service_get_playback_period_size_inner();
557 str_output_size = sprintf(buf_response, "%d", func_result);
558 break;
559 case FUNC_AUDIO_CTRL_SERVICE_GET_RECORD_MAX_BUFFER_SIZE:
560 func_result = audio_ctrl_service_get_record_max_buffer_size_inner();
561 str_output_size = sprintf(buf_response, "%d", func_result);
562 break;
563 case FUNC_AUDIO_CTRL_SERVICE_GET_PLAYBACK_MAX_BUFFER_SIZE:
564 func_result = audio_ctrl_service_get_playback_max_buffer_size_inner();
565 str_output_size = sprintf(buf_response, "%d", func_result);
566 break;
567
568 case FUNC_AUDIO_CTRL_SERVICE_VMLOG_ON:
569 first_param = atoi(first_param_str);
570 func_result = audio_ctrl_service_vmlog_on_inner(first_param);
571 str_output_size = sprintf(buf_response, "%d", func_result);
572 break;
573 case FUNC_AUDIO_CTRL_SERVICE_GET_VMLOG_ON:
574 func_result = audio_ctrl_service_get_vmlog_on_inner();
575 str_output_size = sprintf(buf_response, "%d", func_result);
576 break;
577 case FUNC_AUDIO_CTRL_SERVICE_BT_SPEECH_ON:
578 first_param = atoi(first_param_str);
579 func_result = audio_ctrl_service_speech_on_inner(first_param, 1);
580 str_output_size = sprintf(buf_response, "%d", func_result);
581 break;
582 case FUNC_AUDIO_CTRL_SERVICE_IS_BT_SPEECH_ON:
583 func_result = audio_ctrl_service_is_speech_on_inner(1);
584 str_output_size = sprintf(buf_response, "%d", func_result);
585 break;
586 case FUNC_AUDIO_CTRL_SERVICE_SET_BT_WBS:
587 first_param = atoi(first_param_str);
588 func_result = audio_ctrl_service_set_bt_wbs_inner(first_param);
589 str_output_size = sprintf(buf_response, "%d", func_result);
590 break;
591 case FUNC_AUDIO_CTRL_SERVICE_GET_BT_WBS:
592 func_result = audio_ctrl_service_get_bt_wbs_inner();
593 str_output_size = sprintf(buf_response, "%d", func_result);
594 break;
595 case FUNC_AUDIO_CTRL_SERVICE_SET_BT_DL_GAIN:
596 first_param = atoi(first_param_str);
597 func_result = audio_ctrl_service_set_bt_dl_gain_inner(first_param);
598 str_output_size = sprintf(buf_response, "%d", func_result);
599 break;
600 case FUNC_AUDIO_CTRL_SERVICE_GET_BT_DL_GAIN:
601 func_result = audio_ctrl_service_get_bt_dl_gain_inner();
602 str_output_size = sprintf(buf_response, "%d", func_result);
603 break;
604 case FUNC_AUDIO_CTRL_SERVICE_SET_BT_CLIENT_HAS_ECNR:
605 first_param = atoi(first_param_str);
606 func_result = audio_ctrl_service_set_bt_client_has_ecnr_inner(first_param);
607 str_output_size = sprintf(buf_response, "%d", func_result);
608 break;
609 case FUNC_AUDIO_CTRL_SERVICE_GET_BT_CLIENT_HAS_ECNR:
610 func_result = audio_ctrl_service_get_bt_client_has_ecnr_inner();
611 str_output_size = sprintf(buf_response, "%d", func_result);
612 break;
613 case FUNC_AUDIO_CTRL_SERVICE_SET_USE_BT_IN_CALL:
614 first_param = atoi(first_param_str);
615 func_result = audio_ctrl_service_set_use_bt_in_call_inner(first_param);
616 str_output_size = sprintf(buf_response, "%d", func_result);
617 break;
618 case FUNC_AUDIO_CTRL_SERVICE_GET_USE_BT_IN_CALL:
619 func_result = audio_ctrl_service_get_use_bt_in_call_inner();
620 str_output_size = sprintf(buf_response, "%d", func_result);
621 break;
622 case FUNC_AUDIO_CTRL_SERVICE_RESET_INNER:
623 func_result = audio_ctrl_service_reset_inner();
624 str_output_size = sprintf(buf_response, "%d", func_result);
625 break;
626 default:
627 break;
628 }
629
630 /*send result*/
631 write(g_ipc_upper_send_handler, buf_response, str_output_size);
632
633 }
634 close(g_ipc_upper_send_handler);
635 close(g_ipc_upper_rcv_handler);
636}
637
638static void *speech_flow_lower_func_operator(void *arg)
639{
640 char buf[IPC_DATA_SIZE_MAX];
641 int size = 0, function_type = 0, first_param = 0;
642 int str_output_size = 0;
643 int func_result;
644 char *first_param_str;
645 char *second_param_str;
646 char buf_response[IPC_DATA_SIZE_MAX];
647 char *buf_for_get_data = buf; //use the buffer to get data out for ioplugin
648
649 g_ipc_lower_rcv_handler = open(ACS_IPC_FOR_LOWER_RCV, O_RDONLY);
650 g_ipc_lower_send_handler = open(ACS_IPC_FOR_LOWER_SEND, O_WRONLY);
651 while(1){
652 size = read(g_ipc_lower_rcv_handler, buf, IPC_DATA_SIZE_MAX);
653 if(!size) {
654 printf("reset fifo!\n");
655 close(g_ipc_lower_rcv_handler);
656 g_ipc_lower_rcv_handler = open(ACS_IPC_FOR_LOWER_RCV, O_RDONLY);
657 continue;
658 }
659 /* finish string */
660 buf[size] = '\0';
661
662 /* parse string */
663 strtok_r(buf, ",", &first_param_str);
664 function_type = atoi(buf);
665 switch(function_type) {
666 case FUNC_AUDIO_CTRL_INCALL_SERVICE_RECORD_DATA_CB:
667 first_param = atoi(first_param_str);
668 strtok_r(first_param_str, ",", &second_param_str);
669 pthread_mutex_lock(&g_mutex_record);
670 func_result = audio_ctrl_service_inCall_record_data_cb_inner
671 (first_param, second_param_str);
672 pthread_mutex_unlock(&g_mutex_record);
673 str_output_size = sprintf(buf_response, "%d", func_result);
674 break;
675 default:
676 break;
677 }
678
679 /*send result*/
680 write(g_ipc_lower_send_handler, buf_response, str_output_size);
681
682 }
683 close(g_ipc_lower_send_handler);
684 close(g_ipc_lower_rcv_handler);
685}
686
687
688static void *speech_flow_dummy_sender(void *arg)
689{
690 int dummy_handler = 0;
691 dummy_handler = open(ACS_IPC_FOR_UPPER_RCV, O_WRONLY);
692 dummy_handler = open(ACS_IPC_FOR_LOWER_RCV, O_WRONLY);
693 while(1) {
694 sleep(1000000);
695 }
696 close(dummy_handler);
697}
698
699static Param *getParamValue(AppHandle *appHandle, char *type_name, char *cat_path,
700 char *param_name)
701{
702 AudioType *audioType = NULL;
703 ParamUnit *paramUnit = NULL;
704 Param *param = NULL;
705
706 audioType = appHandleGetAudioTypeByName(appHandle, type_name);
707 if (!audioType)
708 {
709 printf("Error: no such audio type\n");
710 return NULL;
711 }
712
713 /* Read lock */
714 audioTypeReadLock(audioType, __FUNCTION__);
715
716 paramUnit = audioTypeGetParamUnit(audioType, cat_path);
717 if (!paramUnit)
718 {
719 printf("Error: Cannot find the param unit!\n");
720 audioTypeUnlock(audioType);
721 return NULL;
722 }
723
724 param = paramUnitGetParamByName(paramUnit, param_name);
725 if (!param)
726 {
727 printf("Error: Cannot find the param!\n");
728 audioTypeUnlock(audioType);
729 return NULL;
730 }
731
732 /* Unlock */
733 audioTypeUnlock(audioType);
734 return param;
735}
736
737static unsigned short *getUShortArrayFromParam(Param *param,
738 unsigned short *ushortArrayDest)
739{
740 unsigned short *ushortArraySrc = (unsigned short *)param->data;
741 int arraySize = param->arraySize;
742 int i = 0;
743
744 if (param->paramInfo->dataType != TYPE_USHORT_ARRAY) {
745 printf("%s Type not correct!\n", __func__);
746 return NULL;
747 }
748
749 for (i = 0; i < arraySize; i++)
750 ushortArrayDest[i] = ushortArraySrc[i];
751
752 return ushortArrayDest;
753}
754
755static short *getShortArrayFromParam(Param *param, unsigned short *shortArrayDest)
756{
757 short *shortArraySrc = (short *)param->data;
758 int arraySize = param->arraySize;
759 int i = 0;
760
761 if (param->paramInfo->dataType != TYPE_SHORT_ARRAY) {
762 printf("%s Type not correct!\n", __func__);
763 return NULL;
764 }
765
766 for (i = 0; i < arraySize; i++)
767 shortArrayDest[i] = shortArraySrc[i];
768
769 return shortArrayDest;
770}
771
772static void parseAndUpdateParameter(AppHandle *appHandle, const char *audioTypeName)
773{
774 int i;
775
776 if (appHandleReloadAudioType(appHandle, audioTypeName) == APP_ERROR)
777 {
778 printf("Reload xml fail! (audioType = %s)\n", audioTypeName);
779 return;
780 }
781
782 for (i = 0 ; i < PARAM_NUM; ++i)
783 if (!strcmp(audioTypeName, param_data_2635[i].xml_type_name)
784 && param_data_2635[i].update_xml_callback)
785 param_data_2635[i].update_xml_callback(appHandle);
786}
787
788static char *getPathString(int band, int profile, char* dest)
789{
790 strcpy(dest, BAND_PATH_STR[band]);
791 strcat(dest, ",");
792 strcat(dest, PROFILE_PATH_STR[profile]);
793 return dest;
794}
795
796static unsigned short *get_speech_param_arr(int band_iter, int profile_iter)
797{
798 if (band_iter==BAND_NB && profile_iter==PROFILE_NORMAL)
799 return g_default_NB_SPEECH_PARA.speech_mode_nb_para[SPEECH_DRV_LSPEAKER];
800 if (band_iter==BAND_WB && profile_iter==PROFILE_NORMAL)
801 return g_default_WB_SPEECH_PARA.speech_mode_wb_para[SPEECH_DRV_LSPEAKER];
802 if (band_iter==BAND_NB && profile_iter==PROFILE_ECALL)
803 return g_default_NB_SPEECH_PARA_ECALL.speech_mode_nb_para[SPEECH_DRV_LSPEAKER];
804 if (band_iter==BAND_WB && profile_iter==PROFILE_ECALL)
805 return g_default_WB_SPEECH_PARA_ECALL.speech_mode_wb_para[SPEECH_DRV_LSPEAKER];
806 if (band_iter==BAND_NB && profile_iter==PROFILE_BT)
807 return g_default_NB_SPEECH_PARA.speech_mode_nb_para[SPEECH_DRV_BT_EARPHONE];
808 if (band_iter==BAND_WB && profile_iter==PROFILE_BT)
809 return g_default_WB_SPEECH_PARA.speech_mode_wb_para[SPEECH_DRV_BT_EARPHONE];
810 return NULL;
811}
812
813static void copy_speaker_speech_para_to_hp()
814{
815 int i;
816 for (i=0; i<SPEECH_PARA_NUM; ++i) {
817 g_default_NB_SPEECH_PARA.speech_mode_nb_para[SPEECH_DRV_EARPHONE][i] =
818 g_default_NB_SPEECH_PARA.speech_mode_nb_para[SPEECH_DRV_LSPEAKER][i];
819
820 g_default_WB_SPEECH_PARA.speech_mode_wb_para[SPEECH_DRV_EARPHONE][i] =
821 g_default_WB_SPEECH_PARA.speech_mode_wb_para[SPEECH_DRV_LSPEAKER][i];
822
823 g_default_NB_SPEECH_PARA_ECALL.speech_mode_nb_para[SPEECH_DRV_EARPHONE][i] =
824 g_default_NB_SPEECH_PARA_ECALL.speech_mode_nb_para[SPEECH_DRV_LSPEAKER][i];
825
826 g_default_WB_SPEECH_PARA_ECALL.speech_mode_wb_para[SPEECH_DRV_EARPHONE][i] =
827 g_default_WB_SPEECH_PARA_ECALL.speech_mode_wb_para[SPEECH_DRV_LSPEAKER][i];
828 }
829}
830
831static void updateSpeech48param(AppHandle *appHandle)
832{
833 Param *param = NULL;
834 int arr_size, i, band_iter, profile_iter;
835 unsigned short *ushortArray;
836 char path_char[PATH_BUF_SIZE];
837 int retry_ite = 0;
838 int ret;
839
840 for (band_iter = 0; band_iter < BAND_NUM; ++band_iter) {
841 for (profile_iter = 0; profile_iter < PROFILE_NUM; ++profile_iter) {
842 getPathString(band_iter, profile_iter, path_char);
843 param = getParamValue(appHandle, XML_TYPE_SPEECH_STR, path_char,
844 SPEECH_PARA);
845 arr_size = getParamArraySize(param);
846 ushortArray = get_speech_param_arr(band_iter, profile_iter);
847 if (!ushortArray) {
848 printf("%s, get_speech_param_arr err, band: %d, profile: %d",
849 __func__, band_iter, profile_iter);
850 continue;
851 }
852 ushortArray = getUShortArrayFromParam(param, ushortArray);
853 }
854 }
855
856 copy_speaker_speech_para_to_hp();
857 update_BT_ECNR_recorder(BAND_NB, get_speech_param_arr(BAND_NB, PROFILE_BT));
858 update_BT_ECNR_recorder(BAND_WB, get_speech_param_arr(BAND_WB, PROFILE_BT));
859
860 if (g_bt_client_has_ecnr) {
861 close_all_BT_ECNR_param(BAND_NB, get_speech_param_arr(BAND_NB, PROFILE_BT));
862 close_all_BT_ECNR_param(BAND_WB, get_speech_param_arr(BAND_WB, PROFILE_BT));
863 }
864
865 do {
866 ret = speechdrv_set_speech_mode_nb_para(SPEECH_DRV_LSPEAKER, &g_default_NB_SPEECH_PARA);
867 ++retry_ite;
868 if (!ret)
869 break;
870 usleep(SPEECH_DRV_RETRY_US);
871 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
872
873 if (ret) {
874 printf("%s, speechdrv_set_speech_mode_nb_para err, errno: %d\n",
875 __func__, ret);
876 }
877
878 retry_ite = 0;
879 do {
880 ret = speechdrv_set_speech_mode_nb_para(SPEECH_DRV_EARPHONE, &g_default_NB_SPEECH_PARA);
881 ++retry_ite;
882 if (!ret)
883 break;
884 usleep(SPEECH_DRV_RETRY_US);
885 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
886
887 if (ret) {
888 printf("%s, speechdrv_set_speech_mode_nb_para err, errno: %d\n",
889 __func__, ret);
890 }
891
892 retry_ite = 0;
893 do {
894 ret = speechdrv_set_speech_mode_nb_para(SPEECH_DRV_BT_EARPHONE, &g_default_NB_SPEECH_PARA);
895 ++retry_ite;
896 if (!ret)
897 break;
898 usleep(SPEECH_DRV_RETRY_US);
899 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
900
901 if (ret) {
902 printf("%s, speechdrv_set_speech_mode_nb_para err, errno: %d\n",
903 __func__, ret);
904 }
905
906 retry_ite = 0;
907 do {
908 ret = speechdrv_set_speech_mode_wb_para(SPEECH_DRV_LSPEAKER, &g_default_WB_SPEECH_PARA);
909 ++retry_ite;
910 if (!ret)
911 break;
912 usleep(SPEECH_DRV_RETRY_US);
913 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
914
915 if (ret) {
916 printf("%s, speechdrv_set_speech_mode_wb_para err, errno: %d\n",
917 __func__, ret);
918 }
919
920 retry_ite = 0;
921 do {
922 ret = speechdrv_set_speech_mode_wb_para(SPEECH_DRV_EARPHONE, &g_default_WB_SPEECH_PARA);
923 ++retry_ite;
924 if (!ret)
925 break;
926 usleep(SPEECH_DRV_RETRY_US);
927 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
928
929 if (ret) {
930 printf("%s, speechdrv_set_speech_mode_wb_para err, errno: %d\n",
931 __func__, ret);
932 }
933
934 retry_ite = 0;
935 do {
936 ret = speechdrv_set_speech_mode_wb_para(SPEECH_DRV_BT_EARPHONE, &g_default_WB_SPEECH_PARA);
937 ++retry_ite;
938 if (!ret)
939 break;
940 usleep(SPEECH_DRV_RETRY_US);
941 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
942
943 if (ret) {
944 printf("%s, speechdrv_set_speech_mode_wb_para err, errno: %d\n",
945 __func__, ret);
946 }
947
948#if 0 //TODO: implement ecall para switch
949 speechdrv_set_ecall_speech_mode_nb_para(SPEECH_DRV_LSPEAKER, &g_default_NB_SPEECH_PARA_ECALL);
950 speechdrv_set_ecall_speech_mode_wb_para(SPEECH_DRV_LSPEAKER, &g_default_WB_SPEECH_PARA_ECALL);
951#endif
952}
953
954static unsigned short *get_fir_in_param_arr(int band_iter, int profile_iter)
955{
956 if (band_iter==BAND_NB && profile_iter==PROFILE_NORMAL)
957 return g_default_NB_FIR_PARA.sph_in_fir[SPEECH_DRV_LSPEAKER];
958 if (band_iter==BAND_WB && profile_iter==PROFILE_NORMAL)
959 return g_default_WB_FIR_PARA.sph_wb_in_fir[SPEECH_DRV_LSPEAKER];
960 if (band_iter==BAND_NB && profile_iter==PROFILE_ECALL)
961 return g_default_NB_FIR_PARA_ECALL.sph_in_fir[SPEECH_DRV_LSPEAKER];
962 if (band_iter==BAND_WB && profile_iter==PROFILE_ECALL)
963 return g_default_WB_FIR_PARA_ECALL.sph_wb_in_fir[SPEECH_DRV_LSPEAKER];
964 if (band_iter==BAND_NB && profile_iter==PROFILE_BT)
965 return g_default_NB_FIR_PARA.sph_in_fir[SPEECH_DRV_BT_EARPHONE];
966 if (band_iter==BAND_WB && profile_iter==PROFILE_BT)
967 return g_default_WB_FIR_PARA.sph_wb_in_fir[SPEECH_DRV_BT_EARPHONE];
968 return NULL;
969}
970
971static void copy_speaker_fir_in_para_to_hp()
972{
973 int i;
974 for (i=0; i<NB_FIR_NUM; ++i) {
975 g_default_NB_FIR_PARA.sph_in_fir[SPEECH_DRV_EARPHONE][i] =
976 g_default_NB_FIR_PARA.sph_in_fir[SPEECH_DRV_LSPEAKER][i];
977
978 g_default_NB_FIR_PARA_ECALL.sph_in_fir[SPEECH_DRV_EARPHONE][i] =
979 g_default_NB_FIR_PARA_ECALL.sph_in_fir[SPEECH_DRV_LSPEAKER][i];
980 }
981
982 for (i=0; i<WB_FIR_NUM; ++i) {
983 g_default_WB_FIR_PARA.sph_wb_in_fir[SPEECH_DRV_EARPHONE][i] =
984 g_default_WB_FIR_PARA.sph_wb_in_fir[SPEECH_DRV_LSPEAKER][i];
985
986 g_default_WB_FIR_PARA_ECALL.sph_wb_in_fir[SPEECH_DRV_EARPHONE][i] =
987 g_default_WB_FIR_PARA_ECALL.sph_wb_in_fir[SPEECH_DRV_LSPEAKER][i];
988 }
989}
990
991
992static void updateFirInParam(AppHandle *appHandle)
993{
994 Param *param = NULL;
995 int arr_size, i, band_iter, profile_iter;
996 short *shortArray;
997 char path_char[PATH_BUF_SIZE];
998 int ret;
999 int retry_ite = 0;
1000
1001 for (band_iter = 0; band_iter < BAND_NUM; ++band_iter) {
1002 for (profile_iter = 0; profile_iter < PROFILE_NUM; ++profile_iter) {
1003 getPathString(band_iter, profile_iter, path_char);
1004 param = getParamValue(appHandle, XML_TYPE_SPEECH_STR, path_char,
1005 FIR_IN_PARA);
1006 arr_size = getParamArraySize(param);
1007 shortArray = get_fir_in_param_arr(band_iter, profile_iter);
1008
1009 if (!shortArray) {
1010 printf("%s, get_fir_in_param_arr err, band: %d, profile: %d",
1011 __func__, band_iter, profile_iter);
1012 continue;
1013 }
1014
1015 shortArray = getShortArrayFromParam(param, shortArray);
1016 }
1017 }
1018
1019 copy_speaker_fir_in_para_to_hp();
1020
1021 do {
1022 ret = speechdrv_set_fir_nb_para(SPEECH_DRV_LSPEAKER, &g_default_NB_FIR_PARA);
1023 ++retry_ite;
1024 if (!ret)
1025 break;
1026 usleep(SPEECH_DRV_RETRY_US);
1027 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1028
1029 if (ret) {
1030 printf("%s, speechdrv_set_fir_nb_para err, errno: %d\n",
1031 __func__, ret);
1032 }
1033
1034 retry_ite = 0;
1035 do {
1036 ret = speechdrv_set_fir_nb_para(SPEECH_DRV_EARPHONE, &g_default_NB_FIR_PARA);
1037 ++retry_ite;
1038 if (!ret)
1039 break;
1040 usleep(SPEECH_DRV_RETRY_US);
1041 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1042
1043 if (ret) {
1044 printf("%s, speechdrv_set_fir_nb_para err, errno: %d\n",
1045 __func__, ret);
1046 }
1047
1048 retry_ite = 0;
1049 do {
1050 ret = speechdrv_set_fir_nb_para(SPEECH_DRV_BT_EARPHONE, &g_default_NB_FIR_PARA);
1051 ++retry_ite;
1052 if (!ret)
1053 break;
1054 usleep(SPEECH_DRV_RETRY_US);
1055 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1056
1057 if (ret) {
1058 printf("%s, speechdrv_set_fir_nb_para err, errno: %d\n",
1059 __func__, ret);
1060 }
1061
1062 retry_ite = 0;
1063 do {
1064 ret = speechdrv_set_fir_wb_para(SPEECH_DRV_LSPEAKER, &g_default_WB_FIR_PARA);
1065 ++retry_ite;
1066 if (!ret)
1067 break;
1068 usleep(SPEECH_DRV_RETRY_US);
1069 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1070
1071 if (ret) {
1072 printf("%s, speechdrv_set_fir_wb_para err, errno: %d\n",
1073 __func__, ret);
1074 }
1075
1076 retry_ite = 0;
1077 do {
1078 ret = speechdrv_set_fir_wb_para(SPEECH_DRV_EARPHONE, &g_default_WB_FIR_PARA);
1079 ++retry_ite;
1080 if (!ret)
1081 break;
1082 usleep(SPEECH_DRV_RETRY_US);
1083 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1084
1085 if (ret) {
1086 printf("%s, speechdrv_set_fir_wb_para err, errno: %d\n",
1087 __func__, ret);
1088 }
1089
1090 retry_ite = 0;
1091 do {
1092 ret = speechdrv_set_fir_wb_para(SPEECH_DRV_BT_EARPHONE, &g_default_WB_FIR_PARA);
1093 ++retry_ite;
1094 if (!ret)
1095 break;
1096 usleep(SPEECH_DRV_RETRY_US);
1097 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1098
1099 if (ret) {
1100 printf("%s, speechdrv_set_fir_wb_para err, errno: %d\n",
1101 __func__, ret);
1102 }
1103
1104#if 0 //TODO: implement ecall para switch
1105 speechdrv_set_ecall_fir_nb_para(SPEECH_DRV_LSPEAKER, &g_default_NB_FIR_PARA_ECALL);
1106 speechdrv_set_ecall_fir_wb_para(SPEECH_DRV_LSPEAKER, &g_default_WB_FIR_PARA_ECALL);
1107#endif
1108}
1109
1110static unsigned short *get_fir_out_param_arr(int band_iter, int profile_iter)
1111{
1112 if (band_iter==BAND_NB && profile_iter==PROFILE_NORMAL)
1113 return g_default_NB_FIR_PARA.sph_out_fir[SPEECH_DRV_LSPEAKER];
1114 if (band_iter==BAND_WB && profile_iter==PROFILE_NORMAL)
1115 return g_default_WB_FIR_PARA.sph_wb_out_fir[SPEECH_DRV_LSPEAKER];
1116 if (band_iter==BAND_NB && profile_iter==PROFILE_ECALL)
1117 return g_default_NB_FIR_PARA_ECALL.sph_out_fir[SPEECH_DRV_LSPEAKER];
1118 if (band_iter==BAND_WB && profile_iter==PROFILE_ECALL)
1119 return g_default_WB_FIR_PARA_ECALL.sph_wb_out_fir[SPEECH_DRV_LSPEAKER];
1120 if (band_iter==BAND_NB && profile_iter==PROFILE_BT)
1121 return g_default_NB_FIR_PARA.sph_out_fir[SPEECH_DRV_BT_EARPHONE];
1122 if (band_iter==BAND_WB && profile_iter==PROFILE_BT)
1123 return g_default_WB_FIR_PARA.sph_wb_out_fir[SPEECH_DRV_BT_EARPHONE];
1124 return NULL;
1125}
1126
1127static void copy_speaker_fir_out_para_to_hp()
1128{
1129 int i;
1130 for (i=0; i<NB_FIR_NUM; ++i) {
1131 g_default_NB_FIR_PARA.sph_out_fir[SPEECH_DRV_EARPHONE][i] =
1132 g_default_NB_FIR_PARA.sph_out_fir[SPEECH_DRV_LSPEAKER][i];
1133
1134 g_default_NB_FIR_PARA_ECALL.sph_out_fir[SPEECH_DRV_EARPHONE][i] =
1135 g_default_NB_FIR_PARA_ECALL.sph_out_fir[SPEECH_DRV_LSPEAKER][i];
1136 }
1137
1138 for (i=0; i<WB_FIR_NUM; ++i) {
1139 g_default_WB_FIR_PARA.sph_wb_out_fir[SPEECH_DRV_EARPHONE][i] =
1140 g_default_WB_FIR_PARA.sph_wb_out_fir[SPEECH_DRV_LSPEAKER][i];
1141
1142 g_default_WB_FIR_PARA_ECALL.sph_wb_out_fir[SPEECH_DRV_EARPHONE][i] =
1143 g_default_WB_FIR_PARA_ECALL.sph_wb_out_fir[SPEECH_DRV_LSPEAKER][i];
1144 }
1145}
1146
1147static void updateFirOutParam(AppHandle *appHandle)
1148{
1149 Param *param = NULL;
1150 int arr_size, band_iter, profile_iter;
1151 short *shortArray;
1152 char path_char[PATH_BUF_SIZE];
1153 int ret;
1154 int retry_ite = 0;
1155
1156 for (band_iter = 0; band_iter < BAND_NUM; ++band_iter) {
1157 for (profile_iter = 0; profile_iter < PROFILE_NUM; ++profile_iter) {
1158 getPathString(band_iter, profile_iter, path_char);
1159 param = getParamValue(appHandle, XML_TYPE_SPEECH_STR, path_char,
1160 FIR_OUT_PARA);
1161 arr_size = getParamArraySize(param);
1162 shortArray = get_fir_out_param_arr(band_iter, profile_iter);
1163
1164 if(!shortArray) {
1165 printf("%s, get_fir_in_param_arr err, band: %d, profile: %d",
1166 __func__, band_iter, profile_iter);
1167 continue;
1168 }
1169
1170 shortArray = getShortArrayFromParam(param, shortArray);
1171 }
1172 }
1173
1174 copy_speaker_fir_out_para_to_hp();
1175
1176 do {
1177 ret = speechdrv_set_fir_nb_para(SPEECH_DRV_LSPEAKER, &g_default_NB_FIR_PARA);
1178 ++retry_ite;
1179 if (!ret)
1180 break;
1181 usleep(SPEECH_DRV_RETRY_US);
1182 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1183
1184 if (ret) {
1185 printf("%s, speechdrv_set_fir_nb_para err, errno: %d\n",
1186 __func__, ret);
1187 }
1188
1189 retry_ite = 0;
1190 do {
1191 ret = speechdrv_set_fir_nb_para(SPEECH_DRV_EARPHONE, &g_default_NB_FIR_PARA);
1192 ++retry_ite;
1193 if (!ret)
1194 break;
1195 usleep(SPEECH_DRV_RETRY_US);
1196 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1197
1198 if (ret) {
1199 printf("%s, speechdrv_set_fir_nb_para err, errno: %d\n",
1200 __func__, ret);
1201 }
1202
1203 retry_ite = 0;
1204 do {
1205 ret = speechdrv_set_fir_nb_para(SPEECH_DRV_BT_EARPHONE, &g_default_NB_FIR_PARA);
1206 ++retry_ite;
1207 if (!ret)
1208 break;
1209 usleep(SPEECH_DRV_RETRY_US);
1210 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1211
1212 if (ret) {
1213 printf("%s, speechdrv_set_fir_nb_para err, errno: %d\n",
1214 __func__, ret);
1215 }
1216
1217 retry_ite = 0;
1218 do {
1219 ret = speechdrv_set_fir_wb_para(SPEECH_DRV_LSPEAKER, &g_default_WB_FIR_PARA);
1220 ++retry_ite;
1221 if (!ret)
1222 break;
1223 usleep(SPEECH_DRV_RETRY_US);
1224 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1225
1226 if (ret) {
1227 printf("%s, speechdrv_set_fir_wb_para err, errno: %d\n",
1228 __func__, ret);
1229 }
1230
1231 retry_ite = 0;
1232 do {
1233 ret = speechdrv_set_fir_wb_para(SPEECH_DRV_EARPHONE, &g_default_WB_FIR_PARA);
1234 ++retry_ite;
1235 if (!ret)
1236 break;
1237 usleep(SPEECH_DRV_RETRY_US);
1238 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1239
1240 if (ret) {
1241 printf("%s, speechdrv_set_fir_wb_para err, errno: %d\n",
1242 __func__, ret);
1243 }
1244
1245 retry_ite = 0;
1246 do {
1247 ret = speechdrv_set_fir_wb_para(SPEECH_DRV_BT_EARPHONE, &g_default_WB_FIR_PARA);
1248 ++retry_ite;
1249 if (!ret)
1250 break;
1251 usleep(SPEECH_DRV_RETRY_US);
1252 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1253
1254 if (ret) {
1255 printf("%s, speechdrv_set_fir_wb_para err, errno: %d\n",
1256 __func__, ret);
1257 }
1258
1259#if 0 //TODO: implement ecall para switch
1260 speechdrv_set_ecall_fir_nb_para(SPEECH_DRV_LSPEAKER, &g_default_NB_FIR_PARA_ECALL);
1261 speechdrv_set_ecall_fir_wb_para(SPEECH_DRV_LSPEAKER, &g_default_WB_FIR_PARA_ECALL);
1262#endif
1263
1264}
1265
1266static void updateUlGainParam(AppHandle *appHandle)
1267{
1268 Param *param = NULL;
1269 int arr_size, band_iter, profile_iter, ul_gain;
1270 char path_char[PATH_BUF_SIZE];
1271
1272 getPathString(0, 0, path_char);
1273 param = getParamValue(appHandle, XML_TYPE_SPEECHVOL_STR, path_char,
1274 UL_GAIN_IDX_PARA);
1275 g_ul_gain_idx = (*(int *) param->data) - UL_GAIN_MIN;
1276 doUpdateUlGain();
1277}
1278
1279static void updateUlSwagcGainMapParam(AppHandle *appHandle)
1280{
1281 Param *param = NULL;
1282 int arr_size, i;
1283 short *shortArray;
1284
1285 param = getParamValue(appHandle, XML_TYPE_VOLUME_STR, VOLUME_PARAM_PATH,
1286 SWAGC_GAIN_MAP_PARA);
1287 arr_size = getParamArraySize(param);
1288
1289 if (arr_size != UL_GAIN_TABLE_SIZE){
1290 printf("%s, Error! size not match!\n", __func__);
1291 printf("size from para: %d, size expected: %d\n", arr_size,
1292 UL_GAIN_TABLE_SIZE);
1293 printf("We are going to assert to prevent furthur error, plz fix it\n");
1294 assert(false);
1295 }
1296
1297 shortArray = g_ul_digital_gain_table;
1298 shortArray = getShortArrayFromParam(param, shortArray);
1299
1300 /* dB value in xml, but for modem driver, 4 = 1dB*/
1301 for (i = 0; i < arr_size; ++i)
1302 shortArray[i] = shortArray[i] << 2;
1303
1304 doUpdateUlGain();
1305}
1306
1307static void updateUlPgaGainMapParam(AppHandle *appHandle)
1308{
1309 Param *param = NULL;
1310 int arr_size;
1311 short *shortArray;
1312
1313 param = getParamValue(appHandle, XML_TYPE_VOLUME_STR, VOLUME_PARAM_PATH,
1314 UL_PGA_GAIN_MAP_PARA);
1315 arr_size = getParamArraySize(param);
1316
1317 if (arr_size != UL_GAIN_TABLE_SIZE){
1318 printf("%s, Error! size not match!\n", __func__);
1319 printf("size from para: %d, size expected: %d\n", arr_size,
1320 UL_GAIN_TABLE_SIZE);
1321 printf("We are going to assert to prevent furthur error, plz fix it\n");
1322 assert(false);
1323 }
1324
1325
1326 shortArray = g_ul_analog_gain_table;
1327 shortArray = getShortArrayFromParam(param, shortArray);
1328
1329 doUpdateUlGain();
1330}
1331
1332static void doUpdateUlGain(void)
1333{
1334 int ret;
1335 int retry_ite = 0;
1336
1337 if (g_ul_gain_idx >= UL_GAIN_TABLE_SIZE)
1338 return;
1339
1340 /* digital part */
1341 do {
1342 ret = speechdrv_set_ul_digit_volume(g_ul_digital_gain_table[g_ul_gain_idx]);
1343 ++retry_ite;
1344 if (!ret)
1345 break;
1346 usleep(SPEECH_DRV_RETRY_US);
1347 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1348
1349 if (ret < 0)
1350 printf("%s, set ul digit volume error, gain value: %d, errno: %d\n",
1351 __func__, g_ul_digital_gain_table[g_ul_gain_idx], ret);
1352
1353 /* analog part */
1354 ret = set_UL_analog_gain(g_ul_analog_gain_table[g_ul_gain_idx]);
1355 if (ret < 0)
1356 printf("%s, set ul analog volume error, gain value: %d, errno: %d\n",
1357 __func__, g_ul_analog_gain_table[g_ul_gain_idx], ret);
1358}
1359
1360
1361static void doUpdateDlGain(int retry)
1362{
1363 int ret = 0, retry_counter_analog = 5;
1364 int retry_speech_drv_ite = 0;
1365
1366 if (g_dl_gain_idx >= DL_GAIN_TABLE_SIZE)
1367 return;
1368
1369 /* digital part */
1370 do {
1371 if (g_speech_on_device!=DEV_BT) {
1372 ret = speechdrv_set_dl_digit_volume(g_dl_digital_gain_table[g_dl_gain_idx]);
1373 }
1374 ++retry_speech_drv_ite;
1375 if (!ret)
1376 break;
1377 usleep(SPEECH_DRV_RETRY_US);
1378 } while(ret && retry_speech_drv_ite < SPEECH_DRV_RETRY_TIME);
1379
1380 if (ret < 0)
1381 printf("%s, set dl digit volume error, gain value: %d, errno: %d\n",
1382 __func__, g_dl_digital_gain_table[g_dl_gain_idx], ret);
1383
1384 /* analog part */
1385
1386 do{
1387 ret = set_DL_analog_gain(g_dl_analog_gain_table[g_dl_gain_idx]);
1388 if (!ret)
1389 break;
1390 usleep(200 * 1000); //prevent write failed for libmodem-ctrl-service
1391 --retry_counter_analog;
1392 } while(retry && retry_counter_analog);
1393
1394 if (ret < 0)
1395 printf("%s, set dl analog volume error, gain value: %d, errno: %d\n",
1396 __func__, g_dl_analog_gain_table[g_dl_gain_idx], ret);
1397}
1398
1399static void updateDlAnalogGainMapParam(AppHandle *appHandle)
1400{
1401 Param *param = NULL;
1402 int arr_size;
1403 short *shortArray;
1404 char path_char[PATH_BUF_SIZE];
1405
1406 /* gain table are the same for all mode*/
1407 getPathString(0, 0, path_char);
1408 param = getParamValue(appHandle, XML_TYPE_VOLUMEGAINMAP_STR, path_char,
1409 DL_ANALOG_GAIN_MAP_PARA);
1410 arr_size = getParamArraySize(param);
1411 if (arr_size != DL_GAIN_TABLE_SIZE){
1412 printf("%s, Error! size not match!\n", __func__);
1413 printf("size from para: %d, size expected: %d\n", arr_size,
1414 DL_GAIN_TABLE_SIZE);
1415 printf("We are going to assert to prevent furthur error, plz fix it\n");
1416 assert(false);
1417 }
1418 shortArray = g_dl_analog_gain_table;
1419 shortArray = getShortArrayFromParam(param, shortArray);
1420
1421 doUpdateDlGain(0);
1422}
1423
1424static void updateDlDigitalGainMapParam(AppHandle *appHandle)
1425{
1426 Param *param = NULL;
1427 int arr_size, i;
1428 short *shortArray;
1429 char path_char[PATH_BUF_SIZE];
1430
1431 /* gain table are the same for all mode*/
1432 getPathString(0, 0, path_char);
1433 param = getParamValue(appHandle, XML_TYPE_VOLUMEGAINMAP_STR, path_char,
1434 DL_DIGITAL_GAIN_MAP_PARA);
1435 arr_size = getParamArraySize(param);
1436 if (arr_size != DL_GAIN_TABLE_SIZE){
1437 printf("%s, Error! size not match!\n", __func__);
1438 printf("size from para: %d, size expected: %d\n", arr_size,
1439 DL_GAIN_TABLE_SIZE);
1440 printf("We are going to assert to prevent furthur error, plz fix it\n");
1441 assert(false);
1442 }
1443
1444 shortArray = g_dl_digital_gain_table;
1445 shortArray = getShortArrayFromParam(param, shortArray);
1446
1447 /* dB value in xml, but for modem driver, -4 = -1dB*/
1448 for (i = 0; i < arr_size; ++i)
1449 shortArray[i] = shortArray[i] << 2;
1450
1451 doUpdateDlGain(0);
1452}
1453
1454int audio_ctrl_service_speech_on_inner(int speech_on, int is_BT)
1455{
1456 int speech_bool = !!speech_on;
1457 int ret;
1458 int retry_ite = 0;
1459 int device_mode;
1460 int speech_dev_mode;
1461
1462 if (speech_bool == g_is_speech_on) {
1463 printf("speech on status no change! g_is_speech_on: %d\n",
1464 g_is_speech_on);
1465 return 0;
1466 }
1467
1468 g_is_speech_on = speech_bool;
1469
1470 if (!is_BT) {
1471 speech_dev_mode = SPEECH_MODE_LOUD_SPEAKER;
1472 speechdrv_set_mdside_sample_rate(16000);
1473 } else {
1474 speech_dev_mode = SPEECH_MODE_BT_EARPHONE;
1475 speechdrv_set_mdside_sample_rate(g_bt_wbs_on? 16000: 8000);
1476 }
1477
1478 do {
1479 speechdrv_set_speech_mode(speech_dev_mode);
1480 if (speech_bool || !g_record_buf) {
1481 ret = speechdrv_set_speech_on(speech_bool, 0);
1482 ++retry_ite;
1483 if (!ret)
1484 break;
1485 } else { //recording!!
1486 printf("%s, record is still on\n", __func__);
1487 printf("Will auto sppech off after record off, don't worry!\n");
1488 g_want_to_speech_off_after_record_off = 1;
1489 ret = 0;
1490 break;
1491 }
1492 usleep(SPEECH_DRV_RETRY_US);
1493 } while(retry_ite < SPEECH_DRV_RETRY_TIME);
1494
1495 if (ret)
1496 return ret;
1497
1498 if (speech_bool) {
1499 if (!is_BT) {
1500 device_mode = get_default_device();
1501 g_speech_on_device = device_mode;
1502 enable_phone_call_AFE_path(device_mode);
1503 } else {
1504 device_mode = DEV_BT;
1505 g_speech_on_device = device_mode;
1506 enable_phone_call_AFE_path(device_mode);
1507 }
1508 } else {
1509 disable_phone_call_AFE_path();
1510 }
1511
1512 usleep(100 * 1000);//100ms
1513
1514 if (speech_bool) {
1515 if (g_speech_on_device!=DEV_BT) {
1516 doUpdateDlGain(1);
1517 doUpdateUlGain();
1518 } else {
1519 speechdrv_set_dl_digit_volume((BT_DL_GAIN_MAX-g_bt_dl_gain) * -256 / (BT_DL_GAIN_MAX-BT_DL_GAIN_MIN));
1520 }
1521 }
1522 return 0;
1523}
1524
1525int audio_ctrl_service_set_dl_volume_inner(int Db)
1526{
1527 if (Db > DL_GAIN_MAX || Db < DL_GAIN_MIN) {
1528 printf("Invalid DL gain value: %d\n", Db);
1529 return -1;
1530 }
1531 g_dl_gain_idx = Db - DL_GAIN_MIN;
1532 doUpdateDlGain(0);
1533 return 0;
1534}
1535
1536int audio_ctrl_service_inCall_record_start_inner(int buf_size)
1537{
1538 int ret;
1539
1540 AUDIO_V_LOG("%s\n", __func__);
1541 if (buf_size > MAX_REC_BUF_SIZE || buf_size <= 0 || buf_size % 2)
1542 return -EINVAL;
1543 if (g_record_buf) {
1544 printf("%s, record already open!", __func__);
1545 return -EBUSY;
1546 }
1547 g_record_buf_size = buf_size;
1548 g_record_buf = malloc(g_record_buf_size);
1549 g_record_read_virt_idx = 0;
1550 g_record_write_virt_idx = 0;
1551 g_record_virt_boundry = buf_size * 1000;
1552
1553#ifndef RECORD_USE_UT_SOURCE
1554 ret = speechdrv_set_RecordOn(RECORD_TYPE_MIX);
1555#else
1556 ret = tmp_recordOn();
1557#endif
1558
1559 if (ret < 0)
1560 free(g_record_buf);
1561
1562 return ret;
1563}
1564
1565int audio_ctrl_service_inCall_record_stop_inner()
1566{
1567 int ret;
1568
1569 AUDIO_V_LOG("%s\n", __func__);
1570 if (!g_record_buf) {
1571 printf("%s, record not start!\n", __func__);
1572 return -EINVAL;
1573 }
1574#ifndef RECORD_USE_UT_SOURCE
1575
1576 ret = speechdrv_set_RecordOff(RECORD_TYPE_MIX);
1577
1578 if (g_want_to_speech_off_after_record_off) {
1579 int ret_speech_off = speechdrv_set_speech_on(0, 0);
1580 if (ret_speech_off) {
1581 printf("%s, speechdrv_set_speech_on failed! err %d\n", __func__,
1582 ret_speech_off);
1583 }
1584 g_want_to_speech_off_after_record_off = 0;
1585 }
1586#else
1587 ret = tmp_recordOff();
1588#endif
1589 free(g_record_buf);
1590 g_record_buf = 0;
1591 return ret;
1592}
1593
1594
1595int audio_ctrl_service_inCall_record_get_watermark_inner()
1596{
1597 int watermark;
1598
1599 if (!g_record_buf) {
1600 printf("%s, record not start!\n", __func__);
1601 return -EINVAL;
1602 }
1603 watermark = (g_record_write_virt_idx - g_record_read_virt_idx + g_record_virt_boundry) % g_record_virt_boundry;
1604 if (watermark > g_record_buf_size) {//XRUN
1605 printf("XRUN reset ptr %s\n", __func__);
1606 g_record_write_virt_idx = g_record_read_virt_idx;
1607 return -EPIPE;
1608 }
1609
1610 return watermark;
1611}
1612
1613int audio_ctrl_service_inCall_record_pointer_inner()
1614{
1615 int watermark;
1616
1617 AUDIO_V_LOG("%s\n", __func__);
1618 if (!g_record_buf) {
1619 printf("%s, record not start!\n", __func__);
1620 return -EINVAL;
1621 }
1622
1623 watermark = audio_ctrl_service_inCall_record_get_watermark_inner();
1624 if (watermark < 0)
1625 return watermark;
1626 return (g_record_write_virt_idx % g_record_buf_size);
1627}
1628
1629int check_copy_twice(int virt_ptr, int buf_size, int size)
1630{
1631 if (virt_ptr % buf_size + size > buf_size)
1632 return 1;
1633 else
1634 return 0;
1635}
1636
1637int audio_ctrl_service_inCall_record_get_data_inner(int data_size, char* dest)
1638{
1639 int watermark;
1640 int size_to_take;
1641 int copy_twice = 0;
1642 int read_phy_idx = g_record_read_virt_idx % g_record_buf_size;
1643 int first_copy_size;
1644
1645 AUDIO_V_LOG("%s\n", __func__);
1646 if (!g_record_buf) {
1647 printf("%s, record not start!\n", __func__);
1648 return -EINVAL;
1649 }
1650
1651 watermark = audio_ctrl_service_inCall_record_get_watermark_inner();
1652 if (watermark < 0)
1653 return watermark;
1654
1655 size_to_take = (data_size < watermark)? data_size: watermark;
1656 copy_twice = check_copy_twice(g_record_read_virt_idx, g_record_buf_size, size_to_take);
1657
1658 if (!copy_twice) {
1659 memcpy(dest, g_record_buf + read_phy_idx, size_to_take);
1660 } else {
1661 first_copy_size = g_record_buf_size - read_phy_idx;
1662 memcpy(dest, g_record_buf + read_phy_idx, first_copy_size);
1663 dest += first_copy_size;
1664 memcpy(dest, g_record_buf, size_to_take - first_copy_size);
1665 }
1666 g_record_read_virt_idx += size_to_take;
1667 g_record_read_virt_idx %= g_record_virt_boundry;
1668 return size_to_take;
1669}
1670
1671int audio_ctrl_service_inCall_record_data_cb_pcm(int data_size, char* data)
1672{
1673 int copy_twice = 0;
1674 int write_phy_idx = g_record_write_virt_idx % g_record_buf_size;
1675 int first_copy_size;
1676
1677 if (!g_record_buf)
1678 return -EINVAL;
1679
1680 copy_twice = check_copy_twice(g_record_write_virt_idx, g_record_buf_size, data_size);
1681 if (!copy_twice) {
1682 memcpy(g_record_buf + write_phy_idx, data, data_size);
1683 } else {
1684 first_copy_size = g_record_buf_size - write_phy_idx;
1685 memcpy(g_record_buf + write_phy_idx, data, first_copy_size);
1686 data += first_copy_size;
1687 memcpy(g_record_buf, data, data_size - first_copy_size);
1688 }
1689
1690 g_record_write_virt_idx += data_size;
1691 g_record_write_virt_idx %= g_record_virt_boundry;
1692 return data_size;
1693}
1694
1695int16_t* do_record_8k_to_16k_src(int16_t *src, int size_byte)
1696{
1697 int offset, err;
1698 int16_t *dst_buf;
1699 assert(size_byte == RECORD_SRC_BUF_SIZE * 2);
1700 if (!g_incall_record_src_state){ //not init
1701 g_incall_record_src_state = src_new(g_converter_type, 1, &err);
1702 if (!g_incall_record_src_state) {
1703 printf("%s, src_new failed %d\n", __func__, err);
1704 return NULL;
1705 }
1706 assert(!g_incall_record_src_buffer);
1707 assert(!g_incall_record_dst_buffer);
1708 g_incall_record_src_buffer = calloc(1,sizeof(float) * RECORD_SRC_BUF_SIZE);
1709 g_incall_record_dst_buffer = calloc(1,sizeof(float) * RECORD_DST_BUF_SIZE);
1710
1711 g_incall_record_src_data.data_in = g_incall_record_src_buffer;
1712 g_incall_record_src_data.data_out = g_incall_record_dst_buffer;
1713 g_incall_record_src_data.src_ratio = RECORD_SRC_RATIO;
1714 g_incall_record_src_data.end_of_input = 0;
1715 }
1716
1717 /* do convert */
1718 g_incall_record_src_data.input_frames = RECORD_SRC_BUF_SIZE;
1719 g_incall_record_src_data.output_frames = RECORD_DST_BUF_SIZE;
1720 src_short_to_float_array(src, g_incall_record_src_buffer, RECORD_SRC_BUF_SIZE);
1721 err = src_process(g_incall_record_src_state, &g_incall_record_src_data);
1722 if (err) {
1723 printf("%s, src_process failed %d", __func__, err);
1724 return NULL;
1725 }
1726
1727 dst_buf = g_incall_record_dst_int_buffer;
1728 if (g_incall_record_src_data.output_frames > g_incall_record_src_data.output_frames_gen)
1729 offset = g_incall_record_src_data.output_frames - g_incall_record_src_data.output_frames_gen;
1730 else
1731 offset = 0;
1732
1733 src_float_to_short_array(g_incall_record_dst_buffer, dst_buf + offset,
1734 g_incall_record_src_data.output_frames_gen);
1735
1736 return dst_buf;
1737}
1738
1739int audio_ctrl_service_inCall_record_data_cb_inner(int data_size, char* data)
1740{
1741 MD_PCM_RECORD_HEADER* md_pcm_record_header;
1742 char *data_process_ptr = data;
1743 char *pcm_data_ptr;
1744 uint16_t syncWord;
1745 uint16_t rawPcmDir;
1746 uint16_t freq;
1747 uint16_t pcmLength;
1748 uint16_t channel;
1749 uint16_t bitFormat;
1750
1751 int16_t *ul_data_ptr = 0;
1752 int ul_data_ptr_size;
1753 int ul_data_rate;
1754 int16_t *dl_data_ptr = 0;
1755 int dl_data_ptr_size;
1756 int dl_data_rate;
1757
1758 int total_data_size;
1759 int16_t *total_data_ptr;
1760
1761 int i,j;
1762 int ret;
1763
1764 while (data_process_ptr < data + data_size)
1765 {
1766 md_pcm_record_header = (MD_PCM_RECORD_HEADER*)data_process_ptr;
1767 syncWord = md_pcm_record_header->u16SyncWord;
1768 rawPcmDir = md_pcm_record_header->u16RawPcmDir;
1769 freq = md_pcm_record_header->u16Freq;
1770 pcmLength = md_pcm_record_header->u16Length;
1771 channel = md_pcm_record_header->u16Channel;
1772 bitFormat = md_pcm_record_header->u16BitFormat;
1773 pcm_data_ptr = data_process_ptr + sizeof(MD_PCM_RECORD_HEADER);
1774
1775 if (syncWord != MD_PCM_RECORD_HEADER_SYNC)
1776 {
1777 printf("%s, Invalid packet, sync word: 0x%x\n", __func__, syncWord);
1778 return 0;
1779 }
1780
1781 if (rawPcmDir == RECORD_TYPE_UL)
1782 {
1783 assert(!ul_data_ptr);
1784 ul_data_ptr = (uint16_t*)pcm_data_ptr;
1785 ul_data_ptr_size = pcmLength;
1786 ul_data_rate = freq;
1787 assert(channel==1);
1788 } else {
1789 assert(!dl_data_ptr);
1790 dl_data_ptr = (uint16_t*)pcm_data_ptr;
1791 dl_data_ptr_size = pcmLength;
1792 dl_data_rate = freq;
1793 assert(channel==1);
1794 }
1795
1796 data_process_ptr += sizeof(MD_PCM_RECORD_HEADER) + pcmLength;
1797 }
1798
1799 assert(ul_data_ptr);
1800 assert(dl_data_ptr);
1801 //only UL can be 8k or 16k, DL must be 16k
1802 total_data_size = dl_data_ptr_size;
1803 assert(!(total_data_size % ul_data_ptr_size));
1804
1805 total_data_ptr = malloc(total_data_size);
1806
1807 if (ul_data_ptr_size < total_data_size) {
1808 ul_data_ptr = do_record_8k_to_16k_src(ul_data_ptr, ul_data_ptr_size);
1809 if (!ul_data_ptr)
1810 return -EINVAL;
1811 ul_data_ptr_size *= 2;
1812 }
1813
1814 assert(total_data_size == ul_data_ptr_size);
1815 //MIX sound
1816 for(i = 0; i < ul_data_ptr_size / 2; ++i) {
1817 total_data_ptr[i] = ul_data_ptr[i] >> 1;
1818 }
1819
1820 for(i = 0; i < dl_data_ptr_size / 2; ++i) {
1821 total_data_ptr[i] += dl_data_ptr[i] >> 1;
1822 }
1823
1824 //write data
1825 ret = audio_ctrl_service_inCall_record_data_cb_pcm(total_data_size, total_data_ptr);
1826 free(total_data_ptr);
1827 return ret;
1828}
1829
1830int audio_ctrl_service_inCall_playback_start_inner()
1831{
1832#ifndef PLAYBACK_USE_UT_SINK
1833 int ret;
1834
1835 AUDIO_V_LOG("%s\n", __func__);
1836 if (!g_BGSPlayer_init) {
1837 ret = speechdrv_BGSPlayer_Init();
1838 /* TODO: change it when libspeech_drv interface fix*/
1839 /*
1840 if (!ret) {
1841 printf("%s, BGSPlayer_Init failed\n", __func__);
1842 return ret;
1843 }
1844 */
1845 g_BGSPlayer_init = 1;
1846 }
1847
1848 if (!g_BGSPlayerBuffer_init) {
1849 g_BGSPlayerBuffer_idx = speechdrv_CreateBGSPlayBuffer(16000, 1, AUDIO_FORMAT_PCM_16_BIT);
1850 g_BGSPlayerBuffer_init = 1;
1851 } else {
1852 printf("%s, already start!\n", __func__);
1853 return 0;
1854 }
1855 /* TODO: change it when libspeech_drv interface fix*/
1856 ret = speechdrv_BGSPlayer_Open(0x0, 0xFF);
1857 /*
1858 if (ret)
1859 return 0;
1860 else
1861 return -EINVAL;
1862 */
1863 return 0;
1864#else
1865 return UT_BGSOn();
1866#endif
1867}
1868
1869int audio_ctrl_service_inCall_playback_stop_inner()
1870{
1871#ifndef PLAYBACK_USE_UT_SINK
1872 AUDIO_V_LOG("%s\n", __func__);
1873 if (!g_BGSPlayerBuffer_init) {
1874 printf("%s, already stop!\n", __func__);
1875 return 0;
1876 }
1877 speechdrv_BGSPlayer_Close();
1878
1879 g_BGSPlayerBuffer_init = 0;
1880 speechdrv_DestroyBGSPlayBuffer(g_BGSPlayerBuffer_idx);
1881 g_BGSPlayerBuffer_idx = 0;
1882
1883 return 0;
1884#else
1885 return UT_BGSOff();
1886#endif
1887}
1888
1889int audio_ctrl_service_inCall_playback_send_data_inner(int data_size, const char* data_buf)
1890{
1891#ifndef PLAYBACK_USE_UT_SINK
1892 int ret;
1893 int write_size;
1894 int written_size = 0;
1895 int try_cnt = 20;
1896 AUDIO_V_LOG("%s\n", __func__);
1897 if (!g_BGSPlayerBuffer_init) {
1898 printf("%s, not running!\n", __func__);
1899 return 0;
1900 }
1901
1902 while(data_size){
1903 write_size = (data_size>1024)? 1024:data_size;
1904 //ret = speechdrv_BGSPlayer_Write(g_BGSPlayerBuffer_idx, data_buf, data_size);
1905 ret = speechdrv_BGSPlayer_Write(g_BGSPlayerBuffer_idx, data_buf, write_size);
1906 if (ret < 0) {
1907 printf("%s, write to BGS error! ret: %d\n", __func__, ret);
1908 break;
1909 }
1910 data_size -= ret;
1911 data_buf += ret;
1912 written_size += ret;
1913
1914 --try_cnt;
1915 if(!try_cnt)
1916 break;
1917 }
1918 return written_size;
1919
1920#else
1921 return UT_BGSSendData(data_buf, data_size);
1922#endif
1923}
1924
1925int audio_ctrl_service_is_speech_on_inner(int is_BT)
1926{
1927 if (!g_is_speech_on)
1928 return false;
1929 if (!is_BT)
1930 return (g_speech_on_device!=DEV_BT);
1931 else
1932 return (g_speech_on_device==DEV_BT);
1933}
1934
1935int audio_ctrl_service_get_dl_gain_inner()
1936{
1937 return g_dl_gain_idx + DL_GAIN_MIN;
1938}
1939
1940int audio_ctrl_service_get_record_period_size_inner()
1941{
1942 return RECORD_PERIOD_SIZE;
1943}
1944
1945int audio_ctrl_service_get_playback_period_size_inner()
1946{
1947 return PLAYBACK_PERIOD_SIZE;
1948}
1949
1950int audio_ctrl_service_get_record_max_buffer_size_inner()
1951{
1952 return RECORD_MAX_BUFFER_SIZE;
1953}
1954
1955int audio_ctrl_service_get_playback_max_buffer_size_inner()
1956{
1957 return PLAYBACK_MAX_BUFFER_SIZE;
1958}
1959
1960/* for vmlog, need to record if vmlog on */
1961int g_vmlog_on;
1962int audio_ctrl_service_vmlog_on_inner(int vmlog_on)
1963{
1964 g_vmlog_on = vmlog_on;
1965 return speechdrv_vmlog_record(vmlog_on);
1966}
1967
1968int audio_ctrl_service_get_vmlog_on_inner()
1969{
1970 return g_vmlog_on;
1971}
1972
1973/* for BT setting */
1974int audio_ctrl_service_set_bt_wbs_inner(int bt_wbs_on)
1975{
1976 g_bt_wbs_on = !!bt_wbs_on;
1977 set_BT_WB(g_bt_wbs_on);
1978 return 0;
1979}
1980
1981int audio_ctrl_service_get_bt_wbs_inner()
1982{
1983 return g_bt_wbs_on;
1984}
1985
1986int audio_ctrl_service_set_bt_dl_gain_inner(int vol)
1987{
1988 if (vol > BT_DL_GAIN_MAX || vol < BT_DL_GAIN_MIN) {
1989 printf("Invalid BT DL gain value: %d\n", vol);
1990 return -1;
1991 }
1992 g_bt_dl_gain = vol;
1993
1994 if (g_speech_on_device == DEV_BT)
1995 speechdrv_set_dl_digit_volume((BT_DL_GAIN_MAX-vol) * -256 / (BT_DL_GAIN_MAX-BT_DL_GAIN_MIN));
1996
1997 return 0;
1998}
1999
2000int audio_ctrl_service_get_bt_dl_gain_inner()
2001{
2002 return g_bt_dl_gain;
2003}
2004
2005int audio_ctrl_service_set_bt_client_has_ecnr_inner(int bt_client_ecnr_on)
2006{
2007 if (g_bt_client_has_ecnr != (!!bt_client_ecnr_on)) {
2008 if (bt_client_ecnr_on) {
2009 close_all_BT_ECNR_param(BAND_NB, get_speech_param_arr(BAND_NB, PROFILE_BT));
2010 close_all_BT_ECNR_param(BAND_WB, get_speech_param_arr(BAND_WB, PROFILE_BT));
2011 } else {
2012 restore_all_BT_ECNR_param(BAND_NB, get_speech_param_arr(BAND_NB, PROFILE_BT));
2013 restore_all_BT_ECNR_param(BAND_WB, get_speech_param_arr(BAND_WB, PROFILE_BT));
2014 }
2015 g_bt_client_has_ecnr = !!bt_client_ecnr_on;
2016
2017 speechdrv_set_speech_mode_nb_para(SPEECH_DRV_BT_EARPHONE, &g_default_NB_SPEECH_PARA);
2018 speechdrv_set_speech_mode_wb_para(SPEECH_DRV_BT_EARPHONE, &g_default_WB_SPEECH_PARA);
2019 }
2020 return 0;
2021}
2022
2023int audio_ctrl_service_get_bt_client_has_ecnr_inner()
2024{
2025 return g_bt_client_has_ecnr;
2026}
2027
2028int audio_ctrl_service_set_use_bt_in_call_inner(int turn_on_bt_in_call)
2029{
2030 //if not in call, do nothing
2031 if (!g_is_speech_on) return 0;
2032 if (turn_on_bt_in_call && g_speech_on_device!=DEV_BT) {
2033 g_speech_on_device = DEV_BT;
2034 speechdrv_set_speech_mode(SPEECH_MODE_BT_EARPHONE);
2035 dynamic_switch_phone_call_path(DEV_BT);
2036 speechdrv_set_dl_digit_volume((BT_DL_GAIN_MAX-g_bt_dl_gain) * -256 / (BT_DL_GAIN_MAX-BT_DL_GAIN_MIN));
2037
2038 } else if ( !turn_on_bt_in_call && g_speech_on_device==DEV_BT) {
2039 speechdrv_set_speech_mode(SPEECH_MODE_LOUD_SPEAKER);
2040 g_speech_on_device = get_default_device();
2041 dynamic_switch_phone_call_path(g_speech_on_device);
2042 doUpdateDlGain(1);
2043 doUpdateUlGain();
2044 }
2045 return 0;
2046}
2047
2048int audio_ctrl_service_get_use_bt_in_call_inner()
2049{
2050 if (!g_is_speech_on) return 0;
2051 return (g_speech_on_device==DEV_BT);
2052}
2053
2054int audio_ctrl_service_reset_inner()
2055{
2056 g_want_to_speech_off_after_record_off = 0;
2057 if (g_is_speech_on) {
2058 disable_phone_call_AFE_path();
2059 }
2060 g_is_speech_on = 0;
2061 return 0;
2062}
2063
2064/* for BGS UT */
2065
2066static const char const *g_BGS_file_path="/tmp/bgs_UT.pcm";
2067FILE *g_UT_file;
2068
2069int UT_BGSOn()
2070{
2071 g_UT_file = fopen(g_BGS_file_path, "w");
2072 if (!g_UT_file) {
2073 return -EIO;
2074 }
2075 return 0;
2076}
2077int UT_BGSOff()
2078{
2079 if (g_UT_file) {
2080 fclose(g_UT_file);
2081 }
2082 g_UT_file = 0;
2083 return 0;
2084}
2085int UT_BGSSendData(void *buf, int buf_size)
2086{
2087 int sleep_time_ms = buf_size * 1000 / 16000 / 2;
2088 if (!g_UT_file) {
2089 printf("plz send data after turn on\n");
2090 return 0;
2091 }
2092 fwrite(buf, 1, buf_size, g_UT_file);
2093 return buf_size;
2094}
2095