Fix thread unsafety issue in ttsd_player_pause
[platform/core/uifw/tts.git] / server / ttsd_server.c
index 14dd166..c1b5ab8 100644 (file)
@@ -43,6 +43,9 @@ static utterance_t g_utt;
 
 static GList *g_proc_list = NULL;
 
+static bool g_is_paused;
+
+
 /* Function definitions */
 static int __synthesis(int uid, const char* credential);
 
@@ -233,12 +236,27 @@ int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int d
 
                if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
                        SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
+                       if (NULL != temp_sound_data->data) {
+                               free(temp_sound_data->data);
+                               temp_sound_data->data = NULL;
+                       }
+
+                       free(temp_sound_data);
+                       temp_sound_data = NULL;
+
+                       return TTSD_ERROR_OPERATION_FAILED;
                }
 
                if (event == TTSE_RESULT_EVENT_FINISH) {
                        ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
                }
 
+               /* If the app state is paused, do not result to play */
+               if (true == g_is_paused) {
+                       SLOG(LOG_DEBUG, tts_tag(), "[Server DEBUG] tts_pause is called. Do not request to play");
+                       return TTSD_ERROR_NONE;
+               }
+
                if (0 != ttsd_player_play(uid)) {
                        SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
 
@@ -887,6 +905,8 @@ int ttsd_server_play(int uid, const char* credential)
        if (APP_STATE_PAUSED == state) {
                SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
 
+               g_is_paused = false;
+
                /* Resume player */
                if (0 != ttsd_player_resume(uid)) {
                        SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
@@ -933,6 +953,7 @@ int ttsd_server_stop(int uid)
 
        /* Reset all data */
        ttsd_data_clear_data(uid);
+       g_is_paused = false;
 
        return TTSD_ERROR_NONE;
 }
@@ -950,6 +971,8 @@ int ttsd_server_pause(int uid, int* utt_id)
                return TTSD_ERROR_INVALID_STATE;
        }
 
+       g_is_paused = true;
+
        *utt_id = g_utt.uttid;
        SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);