Clean up the sttd_server code 16/290316/2
authorSuyeon Hwang <stom.hwang@samsung.com>
Thu, 23 Mar 2023 04:40:57 +0000 (13:40 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Wed, 19 Apr 2023 08:55:04 +0000 (08:55 +0000)
This patch includes these changes to get more readbility:
- Add static keyword to every internal functions and variables
- Remove unvaluable comments and unused code
- Remove duplicated code

Change-Id: I1592ae524e6524d66bd6143af666434f52380d34
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
server/sttd_server.c

index 99df629..361dff5 100644 (file)
@@ -41,9 +41,9 @@ static double g_processing_timeout = 60;
 
 static double g_recording_timeout = 60;
 
-static Ecore_Timerg_check_client_timer = NULL;
-Ecore_Timer*   g_recording_timer = NULL;
-Ecore_Timer*   g_processing_timer = NULL;
+static Ecore_Timer *g_check_client_timer = NULL;
+static Ecore_Timer *g_recording_timer = NULL;
+static Ecore_Timer *g_processing_timer = NULL;
 static Ecore_Timer *g_stop_by_silence_timer = NULL;
 
 static int g_recording_log_count = 0;
@@ -55,7 +55,7 @@ static GList *g_proc_list = NULL;
 */
 static void __cancel_recognition_internal();
 
-Eina_Bool __stop_by_silence(void *data)
+static Eina_Bool __stop_by_silence(void *data)
 {
        SLOG(LOG_INFO, TAG_STTD, "===== Stop by silence detection");
 
@@ -120,7 +120,7 @@ static void __cancel_by_error(void *data)
        return;
 }
 
-int __server_audio_recorder_callback(const void* data, const unsigned int length)
+static int __server_audio_recorder_callback(const void* data, const unsigned int length)
 {
        int ret;
 
@@ -154,7 +154,7 @@ int __server_audio_recorder_callback(const void* data, const unsigned int length
        return 0;
 }
 
-void __server_audio_interrupt_callback()
+static void __server_audio_interrupt_callback()
 {
        SLOG(LOG_INFO, TAG_STTD, "===== Cancel by sound interrupt");
 
@@ -164,19 +164,7 @@ void __server_audio_interrupt_callback()
        SLOG(LOG_DEBUG, TAG_STTD, "  ");
 }
 
-void __cancel_by_no_record(void *data)
-{
-       SLOG(LOG_INFO, TAG_STTD, "===== Cancel by no record");
-
-       __cancel_recognition_internal();
-
-       SLOG(LOG_DEBUG, TAG_STTD, "=====");
-       SLOG(LOG_DEBUG, TAG_STTD, "  ");
-
-       return;
-}
-
-int __server_recognition_result_callback(stte_result_event_e event, const char* type,
+static int __server_recognition_result_callback(stte_result_event_e event, const char* type,
                                        const char** data, int data_count, const char* msg, void *user_data)
 {
        // critical section
@@ -206,13 +194,13 @@ int __server_recognition_result_callback(stte_result_event_e event, const char*
                SLOG(LOG_INFO, TAG_STTD, "[Server] the size of result from engine is '%d'", data_count);
 
                /* Delete timer for processing time out */
+               ecore_thread_main_loop_begin();
                if (NULL != g_processing_timer) {
-                       ecore_thread_main_loop_begin();
                        SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
                        ecore_timer_del(g_processing_timer);
                        g_processing_timer = NULL;
-                       ecore_thread_main_loop_end();
                }
+               ecore_thread_main_loop_end();
 
                sttd_config_time_save();
                sttd_config_time_reset();
@@ -242,10 +230,6 @@ int __server_recognition_result_callback(stte_result_event_e event, const char*
                        }
                }
 
-               /* change state of uid */
-//             sttd_client_set_state(uid, APP_STATE_READY);
-//             stt_client_unset_current_recognition();
-
        } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) {
                SLOG(LOG_INFO, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event,  data_count);
 
@@ -265,13 +249,13 @@ int __server_recognition_result_callback(stte_result_event_e event, const char*
                SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
 
                /* Delete timer for processing time out */
+               ecore_thread_main_loop_begin();
                if (NULL != g_processing_timer) {
-                       ecore_thread_main_loop_begin();
                        SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
                        ecore_timer_del(g_processing_timer);
                        g_processing_timer = NULL;
-                       ecore_thread_main_loop_end();
                }
+               ecore_thread_main_loop_end();
                sttd_config_time_reset();
 
                int ret = 0;
@@ -294,10 +278,6 @@ int __server_recognition_result_callback(stte_result_event_e event, const char*
                                SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
                        }
                }
-
-               /* change state of uid */
-//             sttd_client_set_state(uid, APP_STATE_READY);
-//             stt_client_unset_current_recognition();
        } else {
                /* nothing */
        }
@@ -309,7 +289,7 @@ int __server_recognition_result_callback(stte_result_event_e event, const char*
        return STTD_ERROR_NONE;
 }
 
-bool __server_result_time_callback(int index, stte_result_time_event_e event, const char* text, long start_time, long end_time, void* user_data)
+static bool __server_result_time_callback(int index, stte_result_time_event_e event, const char* text, long start_time, long end_time, void* user_data)
 {
        pthread_mutex_lock(&stte_result_time_mutex);
 
@@ -329,7 +309,7 @@ bool __server_result_time_callback(int index, stte_result_time_event_e event, co
        return true;
 }
 
-int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
+static int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
 {
        SLOG(LOG_INFO, TAG_STTD, "===== Speech status detected Callback");
 
@@ -367,7 +347,7 @@ int __server_speech_status_callback(stte_speech_status_e status, void *user_para
        return STTD_ERROR_NONE;
 }
 
-int __server_error_callback(stte_error_e error, const char* msg)
+static int __server_error_callback(stte_error_e error, const char* msg)
 {
        SLOG(LOG_ERROR, TAG_STTD, "[Server] Error Callback is called, error(%d), err_msg(%s)", error, msg);
        ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
@@ -375,7 +355,7 @@ int __server_error_callback(stte_error_e error, const char* msg)
        return STTD_ERROR_NONE;
 }
 
-void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
+static void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
 {
        if (NULL == engine_id) {
                SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
@@ -384,40 +364,10 @@ void __sttd_server_engine_changed_cb(const char* engine_id, const char* language
                SLOG(LOG_INFO, TAG_STTD, "[Server] New default engine : %s", engine_id);
        }
 
-#if 0
-       /* need to change state of app to ready */
-       unsigned int uid;
-       uid = stt_client_get_current_recognition();
-
-       if (0 != uid) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%u)", uid);
-
-               sttd_server_cancel(uid);
-               sttdc_send_set_state(uid, (int)APP_STATE_READY);
-
-               stt_client_unset_current_recognition();
-       }
-
-       /* set engine */
-       int ret = sttd_engine_agent_set_default_engine(engine_id);
-       if (0 != ret)
-               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
-
-       if (NULL != language) {
-               ret = sttd_engine_agent_set_default_language(language);
-               if (0 != ret)
-                       SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
-       }
-
-       ret = sttd_engine_agent_set_silence_detection(support_silence);
-       if (0 != ret)
-               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
-#endif
-
        return;
 }
 
-void __sttd_server_language_changed_cb(const char* language, void* user_data)
+static void __sttd_server_language_changed_cb(const char* language, void* user_data)
 {
        if (NULL == language) {
                SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
@@ -433,7 +383,7 @@ void __sttd_server_language_changed_cb(const char* language, void* user_data)
        return;
 }
 
-void __sttd_server_silence_changed_cb(bool value, void* user_data)
+static void __sttd_server_silence_changed_cb(bool value, void* user_data)
 {
        SLOG(LOG_INFO, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
 
@@ -610,16 +560,6 @@ Eina_Bool sttd_cleanup_client(void *data)
                                SLOG(LOG_ERROR, TAG_STTD, "uid (%u) should be removed", client_list[i]);
                                sttd_server_finalize(client_list[i]);
                        }
-#if 0
-                       result = sttdc_send_hello(client_list[i]);
-
-                       if (0 == result) {
-                               SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%u) should be removed.", client_list[i]);
-                               sttd_server_finalize(client_list[i]);
-                       } else if (-1 == result) {
-                               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
-                       }
-#endif
                }
 
                SLOG(LOG_INFO, TAG_STTD, "=====");
@@ -686,6 +626,25 @@ static Eina_Bool __quit_ecore_loop(void *data)
        return EINA_FALSE;
 }
 
+static void delete_timers_related_to_recording_session()
+{
+       if (NULL != g_recording_timer)  {
+               ecore_timer_del(g_recording_timer);
+               g_recording_timer = NULL;
+       }
+
+       if (NULL != g_processing_timer) {
+               SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
+               ecore_timer_del(g_processing_timer);
+               g_processing_timer = NULL;
+       }
+
+       if (NULL != g_stop_by_silence_timer) {
+               ecore_timer_del(g_stop_by_silence_timer);
+               g_stop_by_silence_timer = NULL;
+       }
+}
+
 int sttd_server_finalize(unsigned int uid)
 {
        SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter Finalize");
@@ -699,28 +658,9 @@ int sttd_server_finalize(unsigned int uid)
 
        /* release recorder */
        if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
-               if (APP_STATE_RECORDING == state) {
-                       if (NULL != g_recording_timer)  {
-                               ecore_timer_del(g_recording_timer);
-                               g_recording_timer = NULL;
-                       }
-               }
-
-               if (APP_STATE_PROCESSING == state) {
-                       if (NULL != g_processing_timer) {
-                               SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
-                               ecore_timer_del(g_processing_timer);
-                               g_processing_timer = NULL;
-                       }
-               }
-
-               if (NULL != g_stop_by_silence_timer) {
-                       ecore_timer_del(g_stop_by_silence_timer);
-                       g_stop_by_silence_timer = NULL;
-               }
+               delete_timers_related_to_recording_session();
 
                SLOG(LOG_INFO, TAG_STTD, "[Server INFO] stt_cancel is invoked while state is (%d)", state);
-
                if (0 != sttd_engine_agent_recognize_cancel(uid)) {
                        SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
                }
@@ -735,7 +675,6 @@ int sttd_server_finalize(unsigned int uid)
 
        /* unload engine, if ref count of client is 0 */
        if (0 == sttd_client_get_ref_count()) {
-//             sttd_dbus_close_connection();
                ecore_timer_add(0, __quit_ecore_loop, NULL);
        }
 
@@ -1040,49 +979,10 @@ int sttd_server_set_stop_sound(unsigned int uid, const char* file)
        return 0;
 }
 
-#if 0
-Eina_Bool __check_recording_state(void *data)
-{
-       /* current uid */
-       unsigned int uid = stt_client_get_current_recognition();
-       if (0 == uid)
-               return EINA_FALSE;
-
-       app_state_e state;
-       if (0 != sttdc_send_get_state(uid, (int*)&state)) {
-               /* client is removed */
-               SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%u) should be removed.", uid);
-               sttd_server_finalize(uid);
-               return EINA_FALSE;
-       }
-
-       if (APP_STATE_READY == state) {
-               /* Cancel stt */
-               SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%u) is 'Ready'. The STT service should cancel recording", uid);
-               sttd_server_cancel(uid);
-       } else if (APP_STATE_PROCESSING == state) {
-               /* Cancel stt and send change state */
-               SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%u) is 'Processing'. The STT service should cancel recording", uid);
-               sttd_server_cancel(uid);
-               sttdc_send_set_state(uid, (int)APP_STATE_READY);
-       } else {
-               /* Normal state */
-               SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
-               return EINA_TRUE;
-       }
-
-       return EINA_FALSE;
-}
-#endif
-
-Eina_Bool __stop_by_recording_timeout(void *data)
+static Eina_Bool __stop_by_recording_timeout(void *data)
 {
        SLOG(LOG_INFO, TAG_STTD, "===== Stop by timeout");
-
-       if (NULL != g_recording_timer)  {
-               ecore_timer_del(g_recording_timer);
-               g_recording_timer = NULL;
-       }
+       g_recording_timer = NULL;
 
        unsigned int uid = stt_client_get_current_recognition();
        if (STT_INVALID_UID != uid) {
@@ -1099,7 +999,7 @@ Eina_Bool __stop_by_recording_timeout(void *data)
        return EINA_FALSE;
 }
 
-void __sttd_server_recorder_start(void* data)
+static void __sttd_server_recorder_start(void* data)
 {
        uintptr_t puid = (uintptr_t)data;
        unsigned int uid = (unsigned int)puid;
@@ -1138,7 +1038,7 @@ void __sttd_server_recorder_start(void* data)
        }
 }
 
-void __sttd_start_sound_completed_cb(int id, void *user_data)
+static void __sttd_start_sound_completed_cb(int id, void *user_data)
 {
        SLOG(LOG_INFO, TAG_STTD, "===== Start sound completed");
 
@@ -1210,21 +1110,7 @@ int sttd_server_start(unsigned int uid, const char* lang, const char* recognitio
                return STTD_ERROR_RECORDER_BUSY;
        }
 
-       if (NULL != g_recording_timer) {
-               ecore_timer_del(g_recording_timer);
-               g_recording_timer = NULL;
-       }
-
-       if (NULL != g_processing_timer) {
-               SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
-               ecore_timer_del(g_processing_timer);
-               g_processing_timer = NULL;
-       }
-
-       if (NULL != g_stop_by_silence_timer) {
-               ecore_timer_del(g_stop_by_silence_timer);
-               g_stop_by_silence_timer = NULL;
-       }
+       delete_timers_related_to_recording_session();
 
        char* sound = NULL;
        ret = sttd_client_get_start_sound(uid, &sound);
@@ -1292,7 +1178,9 @@ int sttd_server_start(unsigned int uid, const char* lang, const char* recognitio
        }
 
        if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
-               g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
+               if (NULL == g_processing_timer) {
+                       g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
+               }
        }
 
        /* change uid state */
@@ -1338,14 +1226,9 @@ int sttd_server_start(unsigned int uid, const char* lang, const char* recognitio
        return STTD_ERROR_NONE;
 }
 
-Eina_Bool __time_out_for_processing(void *data)
+static Eina_Bool __time_out_for_processing(void *data)
 {
-       if (NULL != g_processing_timer) {
-               SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
-               ecore_timer_del(g_processing_timer);
-               g_processing_timer = NULL;
-       }
-
+       g_processing_timer = NULL;
        SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter __time_out_for_processing");
 
        /* current uid */
@@ -1380,7 +1263,7 @@ Eina_Bool __time_out_for_processing(void *data)
        return EINA_FALSE;
 }
 
-void __sttd_server_engine_stop(void* data)
+static void __sttd_server_engine_stop(void* data)
 {
        uintptr_t puid = (uintptr_t)data;
        unsigned int uid = (unsigned int)puid;
@@ -1407,7 +1290,7 @@ void __sttd_server_engine_stop(void* data)
        SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
 }
 
-void __sttd_stop_sound_completed_cb(int id, void *user_data)
+static void __sttd_stop_sound_completed_cb(int id, void *user_data)
 {
        SLOG(LOG_INFO, TAG_STTD, "===== Stop sound completed");
 
@@ -1439,21 +1322,7 @@ int sttd_server_stop(unsigned int uid)
                return STTD_ERROR_INVALID_STATE;
        }
 
-       if (NULL != g_recording_timer)  {
-               ecore_timer_del(g_recording_timer);
-               g_recording_timer = NULL;
-       }
-
-       if (NULL != g_processing_timer) {
-               SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
-               ecore_timer_del(g_processing_timer);
-               g_processing_timer = NULL;
-       }
-
-       if (NULL != g_stop_by_silence_timer) {
-               ecore_timer_del(g_stop_by_silence_timer);
-               g_stop_by_silence_timer = NULL;
-       }
+       delete_timers_related_to_recording_session();
 
        char* sound = NULL;
        if (0 != sttd_client_get_stop_sound(uid, &sound)) {
@@ -1495,9 +1364,6 @@ int sttd_server_stop(unsigned int uid)
                        }
                }
                free(sound);
-
-               SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
-               g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
        } else {
                SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
 
@@ -1523,8 +1389,10 @@ int sttd_server_stop(unsigned int uid)
                sttdc_send_set_state(uid, APP_STATE_PROCESSING);
 
                SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Stop recognition");
+       }
 
-               SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
+       SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
+       if (NULL == g_processing_timer) {
                g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
        }
 
@@ -1548,21 +1416,7 @@ static int __sttd_server_cancel(unsigned int uid)
 
        stt_client_unset_current_recognition();
 
-       if (NULL != g_recording_timer) {
-               ecore_timer_del(g_recording_timer);
-               g_recording_timer = NULL;
-       }
-
-       if (NULL != g_processing_timer) {
-               SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
-               ecore_timer_del(g_processing_timer);
-               g_processing_timer = NULL;
-       }
-
-       if (NULL != g_stop_by_silence_timer) {
-               ecore_timer_del(g_stop_by_silence_timer);
-               g_stop_by_silence_timer = NULL;
-       }
+       delete_timers_related_to_recording_session();
 
        if (APP_STATE_RECORDING == state) {
                /* Unset audio session */