Fix race condition when destroy recorder 61/70661/1 accepted/tizen/common/20160520.171419 accepted/tizen/ivi/20160520.135222 accepted/tizen/mobile/20160520.135027 accepted/tizen/tv/20160520.135111 accepted/tizen/wearable/20160520.135147 submit/tizen/20160520.061945
authorWonnam Jang <wn.jang@samsung.com>
Fri, 20 May 2016 04:57:13 +0000 (13:57 +0900)
committerWonnam Jang <wn.jang@samsung.com>
Fri, 20 May 2016 04:57:13 +0000 (13:57 +0900)
Change-Id: I942d17b0bf40b4b2fe2b856aacea4b53f5cfe532
Signed-off-by: Wonnam Jang <wn.jang@samsung.com>
server/sttd_engine_agent.c
server/sttd_recorder.c

index 74deec6..8d91eb1 100644 (file)
@@ -1476,6 +1476,22 @@ int sttd_engine_agent_check_app_agreed(int uid, const char* appid, bool* result)
        return 0;
 }
 
+static void __recorder_destroy(void* data)
+{
+       sttp_result_event_e event = (sttp_result_event_e)data;
+
+       SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Destroy recorder by ecore_thread_safe func");
+
+       if (0 != sttd_recorder_destroy(g_recording_engine_id)) {
+               SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to destroy recorder(%d)", g_recording_engine_id);
+       }
+       
+       if (event == STTP_RESULT_EVENT_FINAL_RESULT || event == STTP_RESULT_EVENT_ERROR) {
+               g_recording_engine_id = -1;
+       }
+}
+
+
 /*
 * STT Engine Callback Functions                                                                                        `                                 *
 */
@@ -1504,16 +1520,21 @@ void __result_cb(sttp_result_event_e event, const char* type, const char** data,
 
 #ifdef AUDIO_CREATE_ON_START
        if (event == STTP_RESULT_EVENT_ERROR) {
+#if 1
+               ecore_main_loop_thread_safe_call_async(__recorder_destroy, (void*)event);
+#else
                SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Destroy recorder");
                if (0 != sttd_recorder_destroy(g_recording_engine_id))
                        SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to destroy recorder(%d)", g_recording_engine_id);
+#endif
        }
 #endif
 
+#ifndef AUDIO_CREATE_ON_START
        if (event == STTP_RESULT_EVENT_FINAL_RESULT || event == STTP_RESULT_EVENT_ERROR) {
                g_recording_engine_id = -1;
        }
-
+#endif
        return;
 }
 
index c29c46d..629fb35 100644 (file)
@@ -251,8 +251,10 @@ int sttd_recorder_deinitialize()
 
                if (NULL != recorder) {
                        g_recorder_list = g_slist_remove(g_recorder_list, recorder);
-                       audio_in_destroy(recorder->audio_h);
-
+                       if(recorder->audio_h) {
+                               audio_in_destroy(recorder->audio_h);
+                               recorder->audio_h = NULL;
+                       }
                        free(recorder);
                }
 
@@ -349,7 +351,10 @@ int sttd_recorder_create(int engine_id, int uid, sttp_audio_type_e type, int cha
        recorder = (stt_recorder_s*)calloc(1, sizeof(stt_recorder_s));
        if (NULL == recorder) {
 #ifndef TV_BT_MODE
-               audio_in_destroy(temp_in_h);
+               if (temp_in_h) {
+                       audio_in_destroy(temp_in_h);
+                       temp_in_h = NULL;
+               }
 #endif
                SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to allocate memory");
                return STTD_ERROR_OUT_OF_MEMORY;
@@ -385,17 +390,22 @@ int sttd_recorder_destroy(int engine_id)
 #ifndef TV_BT_MODE
        int ret;
        if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
-               ret = audio_in_unprepare(recorder->audio_h);
-               if (AUDIO_IO_ERROR_NONE != ret) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
+               if (recorder->audio_h) {
+                       ret = audio_in_unprepare(recorder->audio_h);
+                       if (AUDIO_IO_ERROR_NONE != ret) {
+                               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
+                       }
                }
 
                g_recorder_state = STTD_RECORDER_STATE_READY;
        }
 
-       ret = audio_in_destroy(recorder->audio_h);
-       if (AUDIO_IO_ERROR_NONE != ret) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy audioin : %d", ret);
+       if (recorder->audio_h) {
+               ret = audio_in_destroy(recorder->audio_h);
+               if (AUDIO_IO_ERROR_NONE != ret) {
+                       SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy audioin : %d", ret);
+               }
+               recorder->audio_h = NULL;
        }
 #else 
        if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {