X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=server%2Fsttd_recorder.c;h=b83cc47aba5e49ee809b8fc05fb47ac728041c53;hb=2e73c26e5b7baac7c43593e770cddc5bf9225af2;hp=5c4687b4ab107cbd6eccf4465db387864b23b2d2;hpb=b6aed48b1a283015e0948fa4b37578c80bd921fe;p=platform%2Fcore%2Fuifw%2Fstt.git diff --git a/server/sttd_recorder.c b/server/sttd_recorder.c index 5c4687b..b83cc47 100644 --- a/server/sttd_recorder.c +++ b/server/sttd_recorder.c @@ -21,7 +21,7 @@ #include #include #ifdef TV_BT_MODE -#include +#include #endif #include "stt_defs.h" @@ -59,6 +59,8 @@ static sttd_recorder_state g_recorder_state = STTD_RECORDER_STATE_NONE; static int g_buffer_count; +static int g_stream_focus_id; + /* Sound buf save for test */ /* #define BUF_SAVE_MODE @@ -156,26 +158,12 @@ const char* __stt_get_focus_changed_reason_code(sound_stream_focus_change_reason } } -void __recorder_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason, const char *extra_info, void *user_data) +void __recorder_focus_state_watch_cb(int id, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state, + sound_stream_focus_change_reason_e reason, const char *extra_info, void *user_data) { - SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Focus state changed cb"); - - if (stream_info != g_stream_info_h) { - SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid stream info handle"); - return; - } - - int ret; - sound_stream_focus_state_e state_for_recording; - ret = sound_manager_get_focus_state(g_stream_info_h, NULL, &state_for_recording); - if (SOUND_MANAGER_ERROR_NONE != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to get focus state"); - return; - } - - SLOG(LOG_WARN, TAG_STTD, "[Recorder] focus state changed to (%d) with reason(%s)", (int)state_for_recording, __stt_get_focus_changed_reason_code(reason)); + SLOG(LOG_WARN, TAG_STTD, "[Recorder] focus_state_watch_cb is called, focus_state(%d), reason(%s)", (int)focus_state, __stt_get_focus_changed_reason_code(reason)); - if (STTD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_RELEASED == state_for_recording) { + if (STTD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_ACQUIRED == focus_state) { SLOG(LOG_WARN, TAG_STTD, "[Recorder] Focus released as interrupt"); if (NULL != g_interrupt_cb) { g_interrupt_cb(); @@ -183,7 +171,6 @@ void __recorder_focus_state_cb(sound_stream_info_h stream_info, sound_stream_foc } } - int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interrupt_cb interrupt_cb) { if (NULL == audio_cb || NULL == interrupt_cb) { @@ -205,12 +192,16 @@ int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interr g_recorder_state = STTD_RECORDER_STATE_NONE; g_recorder = NULL; - if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, __recorder_focus_state_cb, NULL, &g_stream_info_h)) { + if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, NULL, NULL, &g_stream_info_h)) { SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create stream info"); } + if (0 != sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_BOTH, __recorder_focus_state_watch_cb, NULL, &g_stream_focus_id)) { + SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to add focus state watch callback"); + } + #ifdef TV_BT_MODE - if (BT_ERROR_NONE != bt_initialize()) { + if (BT_ERROR_NONE != bt_product_init()) { SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to init bt"); return STTD_ERROR_OPERATION_FAILED; } @@ -230,6 +221,10 @@ int sttd_recorder_deinitialize() SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy audio in handle mutex."); } + if (0 != sound_manager_remove_focus_state_watch_cb(g_stream_focus_id)) { + SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to remove focus state watch callback"); + } + if (0 != sound_manager_destroy_stream_information(g_stream_info_h)) { SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy stream info"); } @@ -267,7 +262,7 @@ int sttd_recorder_deinitialize() #ifdef TV_BT_MODE bt_hid_host_deinitialize(); - bt_deinitialize(); + bt_product_deinit(); #endif g_recorder_state = STTD_RECORDER_STATE_NONE; @@ -438,7 +433,7 @@ static float get_volume_decibel(char* data, int size, stte_audio_type_e type) if (0 == count) rms = 0.0; else - rms = sqrt(square_sum/count); + rms = sqrt((double)square_sum/count); if (depthByte == 2) db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16); @@ -517,14 +512,9 @@ int sttd_recorder_start(int uid) return STTD_ERROR_INVALID_PARAMETER; } - ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL); - if (SOUND_MANAGER_ERROR_NONE != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to acquire focus : %d", ret); - } else { - ret = audio_in_set_stream_info(g_recorder->audio_h, g_stream_info_h); - if (AUDIO_IO_ERROR_NONE != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set stream info"); - } + ret = audio_in_set_sound_stream_info(g_recorder->audio_h, g_stream_info_h); + if (AUDIO_IO_ERROR_NONE != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set stream info"); } ret = audio_in_prepare(g_recorder->audio_h); @@ -568,11 +558,27 @@ int sttd_recorder_start(int uid) #ifdef BUF_SAVE_MODE g_count++; - snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count); + while (1) { + snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count); + ret = access(g_temp_file_name, 0); + + if (0 == ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File is already exist"); + if (0 == remove(g_temp_file_name)) { + SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Remove file"); + break; + } else { + g_count++; + } + } else { + break; + } + } + SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Temp file name=[%s]", g_temp_file_name); /* open test file */ - g_pFile = fopen(g_temp_file_name, "wb+"); + g_pFile = fopen(g_temp_file_name, "wb+x"); if (!g_pFile) { SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File not found!"); return -1; @@ -587,15 +593,20 @@ int sttd_recorder_stop() if (STTD_RECORDER_STATE_READY == g_recorder_state) return 0; + // critical section required because this function can be called from stt engine thread context + SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section"); + pthread_mutex_lock(&sttd_audio_in_handle_mutex); + /* Check engine id is valid */ if (NULL == g_recorder) { SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid"); + pthread_mutex_unlock(&sttd_audio_in_handle_mutex); return STTD_ERROR_INVALID_PARAMETER; } g_recorder->uid = -1; - int ret; + int ret = -1; #ifndef TV_BT_MODE ret = audio_in_unprepare(g_recorder->audio_h); if (AUDIO_IO_ERROR_NONE != ret) { @@ -615,25 +626,90 @@ int sttd_recorder_stop() usleep(50000); bt_retry++; } else { + SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] bt_hid_rc_stop_sending_voice(): ret(%d)", ret); break; } } if (false == stopped) { - SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop bt audio"); - return STTD_ERROR_OPERATION_FAILED; + SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop bt audio(%d)", ret); +// return STTD_ERROR_OPERATION_FAILED; } #endif g_recorder_state = STTD_RECORDER_STATE_READY; - ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL); - if (SOUND_MANAGER_ERROR_NONE != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to release focus :%d", ret); - } - #ifdef BUF_SAVE_MODE fclose(g_pFile); #endif + pthread_mutex_unlock(&sttd_audio_in_handle_mutex); + SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section"); + + return 0; +} + +int sttd_recorder_start_file(int uid, const char *filepath) +{ + if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) + return 0; + + /* Check engine id is valid */ + if (NULL == g_recorder) { + SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid"); + return STTD_ERROR_INVALID_PARAMETER; + } + g_recorder_state = STTD_RECORDER_STATE_RECORDING; + g_recorder->uid = uid; + + int cnt = 0; + int totalReadBytes = 0; + + FILE *infile = fopen(filepath, "rb"); + + //process the file + if (infile != NULL) { + while (!feof(infile)) { + static char pcm_buff[BUFFER_LENGTH]; + int read_byte = fread(pcm_buff, 1, BUFFER_LENGTH, infile); + totalReadBytes += read_byte; + if (0 != read_byte) { + if (0 != g_audio_cb(pcm_buff, read_byte)) { + SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to call audio callback"); + fclose(infile); + return -1; + } + if (0 == cnt % 30) { + float vol_db = get_volume_decibel(pcm_buff, BUFFER_LENGTH, g_recorder->audio_type); + if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) { + SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db); + } + } + + /* Audio read log */ + if (0 == cnt % 50) + SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", cnt, read_byte); + cnt++; + } + } + fclose(infile); + } + + SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] total bytes(%d)", cnt, totalReadBytes); + return 0; +} + +int sttd_recorder_stop_file() +{ + if (STTD_RECORDER_STATE_READY == g_recorder_state) + return 0; + + /* Check engine id is valid */ + if (NULL == g_recorder) { + SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid"); + return STTD_ERROR_INVALID_PARAMETER; + } + + g_recorder->uid = -1; + g_recorder_state = STTD_RECORDER_STATE_READY; return 0; }