Fix thread unsafety issue in ttsd_player_pause
[platform/core/uifw/tts.git] / server / ttsd_player.c
index 0c7679d..b111ef4 100644 (file)
@@ -70,6 +70,8 @@ static audio_out_h g_audio_h;
 static sound_stream_info_h g_stream_info_h;
 
 static int g_focus_watch_id;
+
+
 /*
 * Internal Interfaces
 */
@@ -351,7 +353,7 @@ int ttsd_player_check_current_playback_focus(bool *is_current_interrupt)
 
                ret = sound_manager_get_current_playback_focus(&reason, &sound_behavior, &extra_info);
 
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] current playback focus: extra_info(%s), reason(%d), sound_behavior(%d)", extra_info, reason, sound_behavior, extra_info);
+               SLOG(LOG_DEBUG, tts_tag(), "[Player] current playback focus: extra_info(%s), reason(%d), sound_behavior(%d)", extra_info, reason, sound_behavior);
 
                if (SOUND_MANAGER_ERROR_NONE == ret && NULL != extra_info && 0 < strlen(extra_info)) {
                        if (SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
@@ -444,10 +446,9 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                                                SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
                                                        }
                                                        g_audio_state = AUDIO_STATE_READY;
-
-                                                       /* unset volume policy, volume will be 100% */
-                                                       __unset_policy_for_playing();
                                                }
+                                               /* unset volume policy, volume will be 100% */
+                                               __unset_policy_for_playing();
                                                return;
                                        } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
                                                /* new audio data come */
@@ -499,7 +500,6 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        /* unset volume policy, volume will be 100% */
                                        __unset_policy_for_playing();
 
-                                       ttsd_data_clear_sound_data(&sound_data);
                                        return;
                                }
 
@@ -524,7 +524,6 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                                SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
                                                /* unset volume policy, volume will be 100% */
                                                __unset_policy_for_playing();
-                                               ttsd_data_clear_sound_data(&sound_data);
                                                return;
                                        }
                                        if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
@@ -533,13 +532,13 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        }
                                }
                                SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
-                               ttsd_data_clear_sound_data(&sound_data);
+                               ttsd_data_clear_sound_data(player->uid, &sound_data);
                                continue;
                        }
                }
 
                if (g_sampling_rate != sound_data->rate || g_audio_type != sound_data->audio_type) {
-                       SLOG(LOG_DEBUG, tts_tag(), "[Player] Change audio handle : org type(%d) org rate(%d)", g_audio_type, g_sampling_rate);
+                       SLOG(LOG_INFO, tts_tag(), "[Player] Change audio handle : org type(%d) org rate(%d)", g_audio_type, g_sampling_rate);
                        if (NULL != g_audio_h) {
                                __destroy_audio_out();
                        }
@@ -549,11 +548,12 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                /* unset volume policy, volume will be 100% */
                                __unset_policy_for_playing();
 
-                               ttsd_data_clear_sound_data(&sound_data);
+                               ttsd_data_clear_sound_data(player->uid, &sound_data);
 
                                return;
                        }
 
+                       SLOG(LOG_INFO, tts_tag(), "[Player INFO] Success to destroy and recreate audio out");
                        __set_policy_for_playing(40);
                }
 
@@ -576,7 +576,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        /* unset volume policy, volume will be 100% */
                                        __unset_policy_for_playing();
 
-                                       ttsd_data_clear_sound_data(&sound_data);
+                                       ttsd_data_clear_sound_data(player->uid, &sound_data);
 
                                        return;
                                }
@@ -585,11 +585,14 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                        }
 
                        char* temp_data = sound_data->data;
+                       SLOG(LOG_INFO, tts_tag(), "[Player INFO] Before audio_out_write. data(%p), data[%d](%p), uid(%d), utt_id(%d), len(%d)",
+                                       temp_data, idx, &temp_data[idx], player->uid, sound_data->utt_id, len);
                        ret = audio_out_write(g_audio_h, &temp_data[idx], len);
                        if (0 > ret) {
                                SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
                        } else {
                                idx += len;
+                               SLOG(LOG_INFO, tts_tag(), "[Player INFO] After audio_out_write");
                        }
 
                        if (NULL == g_playing_info && APP_STATE_PAUSED != player->state) {
@@ -602,19 +605,21 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                /* unset volume policy, volume will be 100% */
                                __unset_policy_for_playing();
 
-                               ttsd_data_clear_sound_data(&sound_data);
+                               ttsd_data_clear_sound_data(player->uid, &sound_data);
 
                                return;
                        }
 
                        if (APP_STATE_PAUSED == player->state) {
                                /* Save data */
+                               SLOG(LOG_DEBUG, tts_tag(), "[Player] player(%p)", player);
                                player->paused_data = sound_data;
 
                                player->is_paused_data = true;
                                player->idx = idx;
 
                                g_audio_state = AUDIO_STATE_READY;
+
                                SLOG(LOG_INFO, tts_tag(), "[Player] Stop player thread by pause");
 
                                /* Request prepare */
@@ -643,7 +648,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
                        }
 
-                       ttsd_data_clear_sound_data(&sound_data);
+                       ttsd_data_clear_sound_data(player->uid, &sound_data);
                        /* unset volume policy, volume will be 100% */
                        __unset_policy_for_playing();
                        return;
@@ -659,7 +664,6 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                /* unset volume policy, volume will be 100% */
                                __unset_policy_for_playing();
 
-                               ttsd_data_clear_sound_data(&sound_data);
                                return;
                        }
 
@@ -669,14 +673,14 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                /* unset volume policy, volume will be 100% */
                                __unset_policy_for_playing();
 
-                               ttsd_data_clear_sound_data(&sound_data);
+                               ttsd_data_clear_sound_data(player->uid, &sound_data);
                                return;
                        }
 
                        SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
                }
 
-               ttsd_data_clear_sound_data(&sound_data);
+               ttsd_data_clear_sound_data(player->uid, &sound_data);
 
                if (NULL == g_playing_info) {
                        SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
@@ -1067,36 +1071,44 @@ int ttsd_player_pause(int uid)
        if (NULL != g_playing_info) {
                if (uid == g_playing_info->uid) {
                        /* release current playing info */
+                       SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release current playing info (%d)", uid);
                        g_playing_info = NULL;
                } else {
                        /* error case */
                }
        }
 
+       SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current player (%p), g_playing_info(%p)", current, g_playing_info);
+
        current->state = APP_STATE_PAUSED;
 
-       SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
-       SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
-       SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
+       if (NULL == g_playing_info) {
+               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
+               SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
+               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
 
-       /* The thread should be released */
-       int thread_count = ecore_thread_active_get();
-       int count = 0;
-       while (0 < thread_count) {
-               usleep(10000);
+               /* The thread should be released */
+               int thread_count = ecore_thread_active_get();
+               int count = 0;
+               while (0 < thread_count) {
+                       usleep(10000);
 
-               count++;
-               if (30 == count) {
-                       SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
-                       break;
+                       count++;
+                       SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current(%p), state(%d)", current, current->state);
+
+                       if (30 == count) {
+                               SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue. current(%p) current state(%d)", current, current->state);
+                               break;
+                       }
+
+                       thread_count = ecore_thread_active_get();
                }
 
-               thread_count = ecore_thread_active_get();
+               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
+               SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
+               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
        }
 
-       SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
-       SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
-       SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
 
        SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);