From 5aff1a7a4c2f12e8d64739376659d28969d404db Mon Sep 17 00:00:00 2001 From: Suyeon Hwang Date: Wed, 12 Jul 2023 14:38:29 +0900 Subject: [PATCH] Refactor sttd_server module according to review - Contents: This patch includes these changes: - Use play_sound() in sttd_server_start() and sttd_server_stop() - Change from strcmp() to strncmp() for secure access - Remove play_sound() from the functions related about streaming mode Change-Id: I0eef55f3034d6d75dcc33f806a83c4dba863f24d Signed-off-by: Suyeon Hwang --- common/stt_defs.h | 2 +- server/sttd_server.c | 204 ++++++++++++++++++--------------------------------- 2 files changed, 71 insertions(+), 135 deletions(-) diff --git a/common/stt_defs.h b/common/stt_defs.h index 02b3bc7..15c8175 100644 --- a/common/stt_defs.h +++ b/common/stt_defs.h @@ -112,7 +112,7 @@ extern "C" { #define STT_PRIVILEGE_APPLAUNCH "http://tizen.org/privilege/appmanager.launch" #define STT_INVALID_UID 0 -#define STT_MAX_TYPE_LENGTH 32 +#define STT_MAX_TYPE_LENGTH 64 /****************************************************************************************** * Defines for log tag diff --git a/server/sttd_server.c b/server/sttd_server.c index cf08e41..b8a4adc 100644 --- a/server/sttd_server.c +++ b/server/sttd_server.c @@ -1116,6 +1116,49 @@ static int __sttd_server_check_precondition_to_start(unsigned int uid, const cha return ret; } +static bool play_sound(const char *path, wav_player_playback_completed_cb callback, void *user_data) +{ + sound_stream_info_h wav_stream_info_h = NULL; + if (SOUND_MANAGER_ERROR_NONE != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav"); + return false; + } + + int ret = wav_player_start_new(path, wav_stream_info_h, callback, user_data, NULL); + if (SOUND_MANAGER_ERROR_NONE != sound_manager_destroy_stream_information(wav_stream_info_h)) { + SLOG(LOG_WARN, TAG_STTD, "[Server WARN] Fail to destroy stream info for playing wav"); + } + + if (WAV_PLAYER_ERROR_NONE != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav"); + return false; + } + + return true; +} + +static bool play_start_sound_for_uid(unsigned int uid) +{ + char* sound = NULL; + if (STTD_ERROR_NONE != sttd_client_get_start_sound(uid, &sound)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start sound"); + return false; + } + + if (NULL == sound) { + SLOG(LOG_INFO, TAG_STTD, "[Server] There is no start sound"); + return false; + } + + SLOG(LOG_INFO, TAG_STTD, "[Server] start sound : %s", sound); + + uintptr_t puid = (uintptr_t)uid; + bool ret = play_sound(sound, __sttd_start_sound_completed_cb, (void*)puid); + free(sound); + + return ret; +} + int sttd_server_start(unsigned int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential, const char* audio_id) { if (NULL == lang || NULL == recognition_type) { @@ -1136,61 +1179,26 @@ int sttd_server_start(unsigned int uid, const char* lang, const char* recognitio delete_timers_related_to_recording_session(); - char* sound = NULL; - ret = sttd_client_get_start_sound(uid, &sound); - if (0 != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound"); - return ret; - } - ret = sttd_client_set_audio_id(uid, audio_id); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set audio_id(%s)", audio_id); - if (NULL != sound) free(sound); return ret; } /* engine start recognition */ SLOG(LOG_INFO, TAG_STTD, "[Server] start : uid(%u), lang(%s), recog_type(%s)", uid, lang, recognition_type); - if (NULL != sound) - SLOG(LOG_INFO, TAG_STTD, "[Server] start sound : %s", sound); /* 1. Set audio session */ ret = sttd_recorder_set_audio_session(); if (0 != ret) { stt_client_unset_current_recognition(); SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret); - if (NULL != sound) free(sound); return ret; } - bool is_sound_done = false; - /* 2. Request wav play */ - if (NULL != sound) { - int id = 0; - uintptr_t puid = (uintptr_t)uid; - sound_stream_info_h wav_stream_info_h; - if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav"); - is_sound_done = true; - } else { - ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_start_sound_completed_cb, (void*)puid, &id); - if (WAV_PLAYER_ERROR_NONE != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav"); - is_sound_done = true; - } - - if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav"); - } - } - free(sound); - sound = NULL; - } else { - is_sound_done = true; - } + bool is_sound_done = play_start_sound_for_uid(uid); /* 3. Create recorder & engine initialize */ ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL); @@ -1201,7 +1209,7 @@ int sttd_server_start(unsigned int uid, const char* lang, const char* recognitio return ret; } - if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) { + if (STTD_ERROR_NONE != strncmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type, STT_MAX_TYPE_LENGTH)) { if (NULL == g_processing_timer) { g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL); } @@ -1327,6 +1335,28 @@ static void __sttd_stop_sound_completed_cb(int id, void *user_data) return; } +static bool play_stop_sound_for_uid(unsigned int uid) +{ + char* sound = NULL; + if (STTD_ERROR_NONE != sttd_client_get_stop_sound(uid, &sound)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start sound"); + return false; + } + + if (NULL == sound) { + SLOG(LOG_INFO, TAG_STTD, "[Server] There is no start sound"); + return false; + } + + SLOG(LOG_INFO, TAG_STTD, "[Server] stop sound : %s", sound); + + uintptr_t puid = (uintptr_t)uid; + bool ret = play_sound(sound, __sttd_stop_sound_completed_cb, (void*)puid); + free(sound); + + return ret; +} + int sttd_server_stop(unsigned int uid) { /* check if uid is valid */ @@ -1354,14 +1384,6 @@ int sttd_server_stop(unsigned int uid) delete_timers_related_to_recording_session(); - char* sound = NULL; - if (0 != sttd_client_get_stop_sound(uid, &sound)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound"); - return STTD_ERROR_OPERATION_FAILED; - } - - SLOG(LOG_INFO, TAG_STTD, "[Server] stop sound path : %s", sound); - int ret; /* 1. Stop recorder */ ret = sttd_engine_agent_recognize_stop_recorder(); @@ -1370,32 +1392,12 @@ int sttd_server_stop(unsigned int uid) if (0 != sttd_engine_agent_recognize_cancel()) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize"); } - if (NULL != sound) free(sound); return ret; } /* 2. Request wav play */ - if (NULL != sound) { - int id = 0; - uintptr_t puid = (uintptr_t)uid; - sound_stream_info_h wav_stream_info_h; - if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav"); - } else { - ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_stop_sound_completed_cb, (void*)puid, &id); - if (WAV_PLAYER_ERROR_NONE != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav"); - } else { - SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound); - } - - if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav"); - } - } - free(sound); - } else { - SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play"); + if (false == play_stop_sound_for_uid(uid)) { + SLOG(LOG_INFO, TAG_STTD, "[Server] No stop sound play"); /* Unset audio session */ ret = sttd_recorder_unset_audio_session(); @@ -1429,47 +1431,6 @@ int sttd_server_stop(unsigned int uid) return STTD_ERROR_NONE; } -static bool play_sound(const char *path, wav_player_playback_completed_cb callback, void *user_data) -{ - sound_stream_info_h wav_stream_info_h = NULL; - if (SOUND_MANAGER_ERROR_NONE != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav"); - return false; - } - - int ret = wav_player_start_new(path, wav_stream_info_h, callback, user_data, NULL); - if (SOUND_MANAGER_ERROR_NONE != sound_manager_destroy_stream_information(wav_stream_info_h)) { - SLOG(LOG_WARN, TAG_STTD, "[Server WARN] Fail to destroy stream info for playing wav"); - } - - if (WAV_PLAYER_ERROR_NONE != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav"); - return false; - } - - return true; -} - -static bool play_start_sound_for_uid(unsigned int uid) -{ - char* sound = NULL; - if (STTD_ERROR_NONE != sttd_client_get_start_sound(uid, &sound)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start sound"); - return false; - } - - if (NULL == sound) { - SLOG(LOG_INFO, TAG_STTD, "[Server] There is no start sound"); - return false; - } - - SLOG(LOG_INFO, TAG_STTD, "[Server] start sound : %s", sound); - bool ret = play_sound(sound, NULL, NULL); - free(sound); - - return ret; -} - int sttd_server_start_audio_streaming(unsigned int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential, const char* audio_id) { if (NULL == lang || NULL == recognition_type) { @@ -1499,8 +1460,6 @@ int sttd_server_start_audio_streaming(unsigned int uid, const char* lang, const /* engine start recognition */ SLOG(LOG_INFO, TAG_STTD, "[Server] start : uid(%u), lang(%s), recog_type(%s)", uid, lang, recognition_type); - play_start_sound_for_uid(uid); - /* 3. Create recorder & engine initialize */ ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL); if (STTD_ERROR_NONE != ret) { @@ -1510,7 +1469,7 @@ int sttd_server_start_audio_streaming(unsigned int uid, const char* lang, const return ret; } - if (STTD_ERROR_NONE != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) { + if (STTD_ERROR_NONE != strncmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type, STT_MAX_TYPE_LENGTH)) { if (NULL == g_processing_timer) { g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL); } @@ -1571,26 +1530,6 @@ int sttd_server_send_audio_streaming(unsigned int uid, const char *data, size_t return STTD_ERROR_NONE; } -static bool play_stop_sound_for_uid(unsigned int uid) -{ - char* sound = NULL; - if (STTD_ERROR_NONE != sttd_client_get_stop_sound(uid, &sound)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start sound"); - return false; - } - - if (NULL == sound) { - SLOG(LOG_INFO, TAG_STTD, "[Server] There is no start sound"); - return false; - } - - SLOG(LOG_INFO, TAG_STTD, "[Server] stop sound : %s", sound); - bool ret = play_sound(sound, NULL, NULL); - free(sound); - - return ret; -} - int sttd_server_stop_audio_streaming(unsigned int uid) { /* check if uid is valid */ @@ -1618,9 +1557,6 @@ int sttd_server_stop_audio_streaming(unsigned int uid) delete_timers_related_to_recording_session(); - /* 2. Request wav play */ - play_stop_sound_for_uid(uid); - /* Stop engine */ int ret = sttd_engine_agent_recognize_stop_engine(); if (0 != ret) { -- 2.7.4