Send synthesized PCM data from TTS engine to client
[platform/core/uifw/tts.git] / server / ttsd_server.c
index 04d9a70..d87b4ce 100644 (file)
 
 #include "ttsd_config.h"
 #include "ttsd_data.h"
-#include "ttsd_dbus.h"
-#include "ttsd_dbus_server.h"
 #include "ttsd_ipc.h"
 #include "ttsd_engine_agent.h"
 #include "ttsd_main.h"
-#include "ttsd_network.h"
 #include "ttsd_player.h"
+#include "ttsd_state.h"
 #include "ttsd_server.h"
 
 
 #define TIME_DIFF(start, end) \
        ((uint64_t)((end).tv_sec - (start).tv_sec) * 1000000 + (((end).tv_nsec - (start).tv_nsec) / 1000)) / 1000
 
-static struct timespec g_request_playing, g_start_playing;
+static struct timespec g_request_playing, g_start_playing, g_finish_playing;
 static double g_avg_latency;
 static double g_min_latency;
 static double g_max_latency;
 static int g_file_num;
+static int g_total_data_size;
 
 typedef struct {
        unsigned int uid;
        int uttid;
 } utterance_t;
 
+typedef struct {
+       ttsd_state_e before;
+       ttsd_state_e current;
+} state_changed_args_t;
+
 /* If current engine exist */
 //static bool  g_is_engine;
 
 static Ecore_Timer* g_check_client_timer = NULL;
 static Ecore_Timer* g_wait_timer = NULL;
-static Ecore_Timer* g_terminate_timer = NULL;
 static Ecore_Timer* g_quit_loop_timer = NULL;
+static Ecore_Timer* g_notify_state_timer = NULL;
 
 static utterance_t g_utt;
 
 static GList *g_proc_list = NULL;
 
-static bool g_is_paused;
-
 static bool g_is_terminated = false;
 
+static int g_activated_mode = 0;
 
 /* Function definitions */
+static int __stop_and_send_ready_state(unsigned int uid)
+{
+       int ret = ttsd_server_stop(uid);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_stop() : uid(%u). ret(%d)", uid, ret);
+               return ret;
+       }
+
+       int pid = ttsd_data_get_pid(uid);
+       if (pid <= 0) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
+               return TTSD_ERROR_OPERATION_FAILED;
+       } else {
+               ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
+       }
+
+       return ret;
+}
+
 static void __synthesis(unsigned int uid);
 
 static Eina_Bool __wait_synthesis(void *data)
@@ -71,7 +93,7 @@ static Eina_Bool __wait_synthesis(void *data)
        unsigned int uid = ttsd_data_get_current_playing();
 
        if (uid > 0) {
-               ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
+               ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
                if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
                        return EINA_TRUE;
                } else {
@@ -116,8 +138,7 @@ static void __synthesis(unsigned int uid)
 
        if (NULL == speak_data->lang || NULL == speak_data->text) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
-               ttsd_server_stop(uid);
-               ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
+               __stop_and_send_ready_state(uid);
                ttsd_data_destroy_speak_data(speak_data);
                speak_data = NULL;
                return;
@@ -135,24 +156,24 @@ static void __synthesis(unsigned int uid)
        SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
        SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
 
-       ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
+       ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
        ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
        if (TTSD_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
 
-               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
-               ttsd_server_stop(uid);
-
-               int pid = ttsd_data_get_pid(uid);
-               if (pid <= 0) {
-                       SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
-               } else {
-                       ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
-               }
+               ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+               __stop_and_send_ready_state(uid);
        } else {
                g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
        }
 
+       if (ttsd_data_get_synth_control() == TTSD_SYNTHESIS_CONTROL_DOING && ttsd_state_get_state() == TTSD_STATE_READY) {
+               int ret = vconf_set_bool(TTS_PLAYING_STATUS_KEY, 1);
+               SLOG(LOG_INFO, tts_tag(), "[Server] Synthesis starting. Set playing status (True). ret(%d)", ret);
+
+               ttsd_state_set_state(TTSD_STATE_SYNTHESIZING);
+       }
+
        free(credential);
        credential = NULL;
        ttsd_data_destroy_speak_data(speak_data);
@@ -173,10 +194,7 @@ int ttsd_send_error(ttse_error_e error, const char* msg)
 
        SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%u), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, (NULL == msg ? "NULL" : msg));
 
-       ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
-
-       if (0 != ttsd_player_clear(uid))
-               SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
+       ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
 
        if (0 != ttsd_data_clear_data(uid))
                SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
@@ -196,6 +214,23 @@ int ttsd_send_error(ttse_error_e error, const char* msg)
        return 0;
 }
 
+static int ttsd_convert_audio_type_to_bytes(ttse_audio_type_e audio_type)
+{
+       int ret = 0;
+       switch (audio_type) {
+       case TTSE_AUDIO_TYPE_RAW_S16:
+               ret = 2; // 16bit
+               break;
+       case TTSE_AUDIO_TYPE_RAW_U8:
+               ret = 1; // 8bit
+               break;
+       default:
+               ret = 2; // Default 16bit
+               break;
+       }
+       return ret;
+}
+
 int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int data_size, ttse_audio_type_e audio_type, int rate, void* user_data)
 {
        SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
@@ -204,7 +239,7 @@ int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int d
        int uttid = g_utt.uttid;
 
        if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
-               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+               ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
                SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
                SLOG(LOG_DEBUG, tts_tag(), "@@@");
                return TTSD_ERROR_INVALID_PARAMETER;
@@ -212,28 +247,28 @@ int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int d
 
        if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
                SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
-               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+               ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
        if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
-               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+               ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
                SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
                SLOG(LOG_DEBUG, tts_tag(), "@@@");
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
        /* check the current state */
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
-               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+       app_tts_state_e state = ttsd_data_get_client_state(uid);
+       if (APP_STATE_NONE == state) {
+               ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
        SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
        if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
-               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+               ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
                SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
                return TTSD_ERROR_NONE;
        }
@@ -252,55 +287,94 @@ int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int d
                g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
                g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
                g_file_num++;
+               g_total_data_size = 0;
+               g_total_data_size += data_size;
                SLOG(LOG_INFO, tts_tag(), "[Server] File num(%d), Avg Latency(%lf), Min(%lf), Max(%lf)", g_file_num, (g_avg_latency / (double)g_file_num), g_min_latency, g_max_latency);
        } else if (TTSE_RESULT_EVENT_FINISH == event) {
                SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
                SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
                        uid, uttid, data, data_size, audio_type, rate);
+
+               g_total_data_size += data_size;
+               int audio_type_bytes = ttsd_convert_audio_type_to_bytes(audio_type);
+               /* Calculate xRT */
+               clock_gettime(CLOCK_MONOTONIC_RAW, &g_finish_playing);
+               long long int time_processing = (uint64_t)TIME_DIFF(g_request_playing, g_finish_playing);
+               long long int time_speech = (long long int)(1000 * g_total_data_size * sizeof(short) / (rate * audio_type_bytes));
+               double xRT = (double)(time_processing) / (double)time_speech;
+               SLOG(LOG_INFO, tts_tag(), "[SERVER] time_processing : %lld, time_speech : %lld, total data size: %d", time_processing, time_speech, g_total_data_size);
+               SLOG(LOG_INFO, tts_tag(), "[SERVER] xRT : %lf", xRT);
        } else {
                /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
+               g_total_data_size += data_size;
        }
 
-       /* add wav data */
-       sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
-       if (NULL == sound_data) {
-               SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
-               return TTSD_ERROR_OUT_OF_MEMORY;
-       }
+       ttsd_playing_mode_e playing_mode = ttsd_data_get_playing_mode(uid);
+       SLOG(LOG_DEBUG, tts_tag(), "[SERVER] uid(%d), playing mode(%d)", uid, playing_mode);
+       if (TTSD_PLAYING_MODE_BY_SERVICE == playing_mode) {
+               /* add wav data */
+               sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
+               if (NULL == sound_data) {
+                       SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
+                       return TTSD_ERROR_OUT_OF_MEMORY;
+               }
 
-       if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
-               SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
-               ttsd_data_destroy_sound_data(sound_data);
+               if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
+                       SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
+                       ttsd_data_destroy_sound_data(sound_data);
 
-               return TTSD_ERROR_OPERATION_FAILED;
+                       return TTSD_ERROR_OPERATION_FAILED;
+               }
+       } else { /* TTSD_PLAYING_MODE_BY_CLIENT */
+               /* send pcm data to client */
+               int tmp_pid = ttsd_data_get_pid(uid);
+               if (0 != ttsdc_ipc_send_pcm(tmp_pid, uid, uttid, event, data, data_size, audio_type, rate)) {
+                       SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to send pcm data to client");
+                       return TTSD_ERROR_OPERATION_FAILED;
+               }
        }
 
        if (event == TTSE_RESULT_EVENT_FINISH) {
-               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+               ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
        }
 
        return TTSD_ERROR_NONE;
 }
 
-bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
+static bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
 {
+       if (0 > ttsd_data_is_client(uid)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server] Client(%d) is not valid.", uid);
+               ttsd_data_delete_client(uid);
+               return true;
+       }
+
        /* clear client data */
        ttsd_data_clear_data(uid);
 
        if (APP_STATE_READY != state) {
                ttsd_data_set_client_state(uid, APP_STATE_READY);
-
-               /* send message */
-               if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
-                       /* remove client */
-                       ttsd_data_delete_client(uid);
-               }
+               ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
        }
 
        return true;
 }
 
-void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
+static void __stop_all_client()
+{
+       SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
+
+       ttsd_engine_cancel_synthesis();
+       ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+
+       /* stop all player */
+       ttsd_player_all_stop();
+
+       /* send interrupt message to  all clients */
+       ttsd_data_foreach_clients(__get_client_cb, NULL);
+}
+
+static void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
 {
        switch (type) {
        case TTS_CONFIG_TYPE_ENGINE:
@@ -326,14 +400,7 @@ void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_
                        return;
                }
 
-               /* stop all player */
-               ttsd_player_all_stop();
-
-               /* send interrupt message to  all clients */
-               ttsd_data_foreach_clients(__get_client_cb, NULL);
-
-               ttsd_engine_cancel_synthesis();
-
+               __stop_all_client();
                break;
        }
 
@@ -414,18 +481,28 @@ bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void*
        return true;
 }
 
-static void __terminate_server()
+static int __terminate_server()
 {
        if (g_is_terminated) {
                SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
-               return;
+               return TTSD_ERROR_NONE;
        }
 
-       g_is_terminated = true;
+       int ret = TTSD_ERROR_NONE;
+       ret = ttsd_ipc_close_connection();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_ipc_close_connection(). ret(%d)", ret);
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
+
+       ret = ttsd_finalize();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_finalize(). ret(%d)", ret);
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
 
-       ttsd_ipc_close_connection();
-       ttsd_network_finalize();
-       ttsd_finalize();
+       g_is_terminated = true;
+       return TTSD_ERROR_NONE;
 }
 
 static Eina_Bool __quit_ecore_loop(void *data)
@@ -436,49 +513,134 @@ static Eina_Bool __quit_ecore_loop(void *data)
        return EINA_FALSE;
 }
 
-Eina_Bool ttsd_terminate_daemon(void *data)
+void __screen_reader_changed_cb(bool value)
 {
-       ttsd_data_foreach_clients(__terminate_client, NULL);
+       SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
+       return;
+}
 
-       if (g_quit_loop_timer) {
-               ecore_timer_del(g_quit_loop_timer);
-               g_quit_loop_timer = NULL;
-               SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
+static void __read_proc()
+{
+       DIR *dp = NULL;
+       struct dirent *dirp = NULL;
+       int tmp;
+
+       GList *iter = NULL;
+       if (0 < g_list_length(g_proc_list)) {
+               iter = g_list_first(g_proc_list);
+               while (NULL != iter) {
+                       g_proc_list = g_list_remove_link(g_proc_list, iter);
+                       g_list_free(iter);
+                       iter = g_list_first(g_proc_list);
+               }
        }
 
-       __terminate_server();
+       dp = opendir("/proc");
+       if (NULL == dp) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
+       } else {
+               do {
+                       dirp = readdir(dp);
 
-       g_terminate_timer = NULL;
-       return EINA_FALSE;
+                       if (NULL != dirp) {
+                               tmp = atoi(dirp->d_name);
+                               if (0 >= tmp)   continue;
+                               g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
+                       }
+               } while (NULL != dirp);
+               closedir(dp);
+       }
+       return;
 }
 
-void __screen_reader_changed_cb(bool value)
+bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
 {
-/*     if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
-               SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
-               if (g_terminate_timer) {
-                       ecore_timer_del(g_terminate_timer);
-                       g_terminate_timer = NULL;
+       bool exist = false;
+       int i = 0;
+
+       GList *iter = NULL;
+       for (i = 0; i < g_list_length(g_proc_list); i++) {
+               iter = g_list_nth(g_proc_list, i);
+               if (NULL != iter) {
+                       if (pid == GPOINTER_TO_INT(iter->data)) {
+                               SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
+                               exist = true;
+                               break;
+                       }
                }
-               g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
+       }
+
+       if (false == exist) {
+               SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
+               ttsd_server_finalize(uid);
+       }
+
+       return true;
+}
+
+static Eina_Bool __cleanup_client(void *data)
+{
+       SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
+       __read_proc();
+
+       if (0 < ttsd_data_get_client_count()) {
+               ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
        } else {
-*/
-               SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
-//     }
-       return;
+               if (NULL == g_quit_loop_timer) {
+                       SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
+                       g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
+               }
+               SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
+               g_check_client_timer = NULL;
+               return EINA_FALSE;
+       }
+
+       SLOG(LOG_DEBUG, tts_tag(), "@@@");
+
+       return EINA_TRUE;
 }
 
-int ttsd_send_all_stop()
+static bool __notify_state_changed_event(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
 {
-       SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
+       state_changed_args_t* args = (state_changed_args_t*)user_data;
+       if (NULL == args) {
+               return false;
+       }
 
-       /* stop all player */
-       ttsd_player_all_stop();
+       if (ttsd_data_is_service_state_changed_cb_set(uid)) {
+               ttsdc_ipc_send_set_service_state_message(pid, uid, args->before, args->current);
+       }
 
-       /* send interrupt message to  all clients */
-       ttsd_data_foreach_clients(__get_client_cb, NULL);
+       return true;
+}
 
-       return TTSD_ERROR_NONE;
+static Eina_Bool __state_changed_timer_cb(void *data)
+{
+       if (NULL == data) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server] Invalid data passed.");
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       int ret = ttsd_data_foreach_clients(__notify_state_changed_event, data);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server] Fail to notify state change.");
+       }
+
+       free(data);
+       g_notify_state_timer = NULL;
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static void __state_changed_cb(ttsd_state_e before, ttsd_state_e current)
+{
+       state_changed_args_t* args = (state_changed_args_t*)calloc(1, sizeof(state_changed_args_t));
+       if (NULL != args) {
+               args->before = before;
+               args->current = current;
+       }
+
+       SLOG(LOG_INFO, tts_tag(), "[Server] Notify state chanaged from (%d) to (%d).", before, current);
+       g_notify_state_timer = ecore_timer_add(0.0, __state_changed_timer_cb, args);
 }
 
 /*
@@ -493,6 +655,10 @@ int ttsd_initialize(ttse_request_callback_s *callback)
                SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
        }
 
+       if (TTSD_ERROR_NONE != ttsd_state_initialize(__state_changed_cb)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize state.");
+       }
+
        /* player init */
        if (ttsd_player_init()) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
@@ -511,34 +677,21 @@ int ttsd_initialize(ttse_request_callback_s *callback)
        }
 
        /* Engine Agent initialize */
-       if (0 != ttsd_engine_agent_init()) {
+       if (TTSD_ERROR_NONE != ttsd_engine_agent_init(callback)) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       /* set current engine */
-       //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
-       //      SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
-       //      g_is_engine = false;
-       //} else
-       //      g_is_engine = true;
-
-       if (0 != ttsd_engine_agent_load_current_engine(callback)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
-               return TTSD_ERROR_OPERATION_FAILED;
-       }
+       ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+       ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
 
-       ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
-
-//     if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
-               ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
-//     }
-
-       g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
+       g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, __cleanup_client, NULL);
        if (NULL == g_check_client_timer) {
                SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
        }
 
+       g_activated_mode = 0;
+
        return TTSD_ERROR_NONE;
 }
 
@@ -556,12 +709,13 @@ int ttsd_finalize()
                }
        }
 
-       ttsd_config_finalize();
-
-       ttsd_player_release();
-
-       ttsd_engine_agent_release();
+       if (g_notify_state_timer) {
+               void* data = ecore_timer_del(g_notify_state_timer);
+               g_notify_state_timer = NULL;
 
+               free(data);
+               SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore notify state timer handle");
+       }
 
        if (g_wait_timer) {
                ecore_timer_del(g_wait_timer);
@@ -569,12 +723,6 @@ int ttsd_finalize()
                SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
        }
 
-       if (g_terminate_timer) {
-               ecore_timer_del(g_terminate_timer);
-               g_terminate_timer = NULL;
-               SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
-       }
-
        if (g_check_client_timer) {
                ecore_timer_del(g_check_client_timer);
                g_check_client_timer = NULL;
@@ -587,16 +735,41 @@ int ttsd_finalize()
                SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
        }
 
+       ttsd_config_finalize();
+       ttsd_state_finalize();
+
+       int ret = TTSD_ERROR_NONE;
+       ret = ttsd_player_release();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_player_release(). ret(%d)", ret);
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
+
+       ret = ttsd_engine_agent_release();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_engine_agent_release(). ret(%d)", ret);
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
+
        return TTSD_ERROR_NONE;
 }
 
 int ttsd_terminate()
 {
        SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
+       int ret = ttsd_data_foreach_clients(__terminate_client, NULL);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to terminate all clients. ret(%d)", ret);
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
 
-       ttsd_terminate_daemon(NULL);
+       if (g_quit_loop_timer) {
+               ecore_timer_del(g_quit_loop_timer);
+               g_quit_loop_timer = NULL;
+               SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
+       }
 
-       return TTSD_ERROR_NONE;
+       return __terminate_server();
 }
 
 /*
@@ -614,168 +787,98 @@ int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initi
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_server_initialize(int pid, unsigned int uid, tts_ipc_method_e method, bool* credential_needed)
+static void __set_and_notify_activated_mode()
 {
-       SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
-
-       if (-1 != ttsd_data_is_client(uid)) {
-               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
-               return TTSD_ERROR_NONE;
-       }
-
-       if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
-               return TTSD_ERROR_OPERATION_FAILED;
+       int activated_mode = ttsd_data_get_activated_mode();
+       if (g_activated_mode == activated_mode) {
+               return;
        }
 
-       if (true == *credential_needed) {
-               char* appid = NULL;
-               if (0 != app_manager_get_app_id(pid, &appid)) {
-                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
-               }
-               bool is_agreed = false;
-               if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
-                       SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
-                       if (!appid)
-                               free(appid);
-                       return TTSD_ERROR_OPERATION_FAILED;
-               }
-               if (!appid)
-                       free(appid);
+       SLOG(LOG_INFO, tts_tag(), "[Server] Activated mode is changed from(%d) to (%d)", g_activated_mode, activated_mode);
 
-               if (false == is_agreed) {
-                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
-                       return TTSD_ERROR_PERMISSION_DENIED;
-               }
+       g_activated_mode = activated_mode;
+       int ret = ttsd_engine_notify_activated_mode_changed(g_activated_mode);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to notify activated mode changed event.(%d)", ret);
        }
+}
 
-       if (0 != ttsd_data_new_client(pid, uid)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
-               return TTSD_ERROR_OPERATION_FAILED;
+static int __check_app_agreed(int pid, unsigned int uid, bool credential_needed)
+{
+       if (false == credential_needed) {
+               return TTSD_ERROR_NONE;
        }
 
-       if (0 != ttsd_data_set_ipc_method(uid, method)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
-               return TTSD_ERROR_OPERATION_FAILED;
+       char* appid = NULL;
+       if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
        }
 
-       if (0 != ttsd_player_create_instance(uid)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
+       bool is_agreed = false;
+       int ret = ttsd_engine_check_app_agreed(appid, &is_agreed);
+       free(appid);
+       appid = NULL;
+
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       g_file_num = 0;
-       g_min_latency = 10000.0;
-       g_max_latency = 0.0;
-       g_avg_latency = 0.0;
-
+       if (false == is_agreed) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
+               return TTSD_ERROR_PERMISSION_DENIED;
+       }
 
        return TTSD_ERROR_NONE;
 }
 
-static void __read_proc()
+int ttsd_server_initialize(int pid, unsigned int uid, ttsd_mode_e mode, ttsd_playing_mode_e playing_mode, int registered_event_mask, tts_ipc_method_e method, ttsd_state_e* service_state, bool* credential_needed)
 {
-       DIR *dp = NULL;
-       struct dirent *dirp = NULL;
-       int tmp;
+       SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
 
-       GList *iter = NULL;
-       if (0 < g_list_length(g_proc_list)) {
-               iter = g_list_first(g_proc_list);
-               while (NULL != iter) {
-                       g_proc_list = g_list_remove_link(g_proc_list, iter);
-                       g_list_free(iter);
-                       iter = g_list_first(g_proc_list);
-               }
+       if (-1 != ttsd_data_is_client(uid)) {
+               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
+               return TTSD_ERROR_NONE;
        }
 
-       dp = opendir("/proc");
-       if (NULL == dp) {
-               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
-       } else {
-               do {
-                       dirp = readdir(dp);
-
-                       if (NULL != dirp) {
-                               tmp = atoi(dirp->d_name);
-                               if (0 >= tmp)   continue;
-                               g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
-                       }
-               } while (NULL != dirp);
-               closedir(dp);
+       if (TTSD_ERROR_NONE != ttsd_data_new_client(pid, uid, mode, playing_mode, registered_event_mask, method)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
+               return TTSD_ERROR_OPERATION_FAILED;
        }
-       return;
-}
 
-bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
-{
-       bool exist = false;
-       int i = 0;
-
-       GList *iter = NULL;
-       for (i = 0; i < g_list_length(g_proc_list); i++) {
-               iter = g_list_nth(g_proc_list, i);
-               if (NULL != iter) {
-                       if (pid == GPOINTER_TO_INT(iter->data)) {
-                               SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
-                               exist = true;
-                               break;
-                       }
-               }
-       }
+       __set_and_notify_activated_mode();
 
-       if (false == exist) {
-               SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
-               ttsd_server_finalize(uid);
+       if (TTSD_ERROR_NONE != ttsd_engine_agent_load_current_engine()) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
+               ttsd_data_delete_client(uid);
+               return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       return true;
-#if 0
-       char appid[1024] = {0, };
-       if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
+       bool is_needed = false;
+       if (TTSD_ERROR_NONE != ttsd_engine_agent_is_credential_needed(uid, &is_needed)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
+               ttsd_data_delete_client(uid);
+               return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       if (0 < strlen(appid)) {
-               SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
-       } else {
-               SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
-
-               int result = 1;
-               result = ttsdc_send_hello(pid, uid);
-
-               if (0 == result) {
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) should be removed.", uid);
-                       ttsd_server_finalize(uid);
-               } else if (-1 == result) {
-                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
-               }
+       int ret = __check_app_agreed(pid, uid, is_needed);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Checking credential is finished(%d)", ret);
+               ttsd_data_delete_client(uid);
+               return ret;
        }
-       return true;
-#endif
-}
 
-
-Eina_Bool ttsd_cleanup_client(void *data)
-{
-       SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
-       __read_proc();
-
-       if (0 < ttsd_data_get_client_count()) {
-               ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
-       } else {
-               if (NULL == g_quit_loop_timer) {
-                       SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
-                       g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
-               }
-               SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
-               g_check_client_timer = NULL;
-               return EINA_FALSE;
+       ttsd_state_e tmp_service_state = ttsd_state_get_state();
+       if (TTSD_STATE_INVALID == tmp_service_state) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state");
+               ttsd_data_delete_client(uid);
+               return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       SLOG(LOG_DEBUG, tts_tag(), "@@@");
+       *credential_needed = is_needed;
+       *service_state = tmp_service_state;
 
-       return EINA_TRUE;
+       return TTSD_ERROR_NONE;
 }
 
 void __used_voice_cb(const char* lang, int type)
@@ -791,15 +894,14 @@ int ttsd_server_finalize(unsigned int uid)
        SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
        SLOG(LOG_INFO, tts_tag(), "[Server] File num(%d), Avg Latency(%lf), Min(%lf), Max(%lf)", g_file_num, (g_avg_latency / (double)g_file_num), g_min_latency, g_max_latency);
 
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
+       if (0 > ttsd_data_is_client(uid)) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
        }
 
-       ttsd_server_stop(uid);
-       ttsd_player_clear(uid);
-
-       ttsd_player_destroy_instance(uid);
+       int ret = ttsd_server_stop(uid);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS server top(%u) fail (%d/%s) <<<<<", uid, ret, get_error_message(ret));
+       }
 
        /* Need to unload voice when used voice is unregistered */
        if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
@@ -808,6 +910,7 @@ int ttsd_server_finalize(unsigned int uid)
        }
 
        ttsd_data_delete_client(uid);
+       __set_and_notify_activated_mode();
 
        /* unload engine, if ref count of client is 0 */
        if (0 == ttsd_data_get_client_count()) {
@@ -822,34 +925,53 @@ int ttsd_server_finalize(unsigned int uid)
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_server_set_mode(unsigned int uid, ttsd_mode_e mode)
+int ttsd_server_update_instant_reprepare_client()
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
-               return TTSD_ERROR_INVALID_PARAMETER;
+       SLOG(LOG_INFO, tts_tag(), "[Server] Update instant reprepare client");
+       unsigned int instant_reprepare_uid = TTS_INVALID_UID;
+       ttsd_config_get_instant_reprepare_client(&instant_reprepare_uid);
+
+       if (0 <= ttsd_data_is_client(instant_reprepare_uid)) {
+               SLOG(LOG_INFO, tts_tag(), "[Server] Uid(%u) is valid. Still no need to changee the value", instant_reprepare_uid);
+               return TTSD_ERROR_NONE;
        }
 
-       if (APP_STATE_READY != state) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
-               return TTSD_ERROR_INVALID_STATE;
+       unsigned int new_instant_reprepare_uid = ttsd_data_get_first_client_uid();
+       int ret = ttsd_config_set_instant_reprepare_client(new_instant_reprepare_uid);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server] Fail to set UID(%u) of instant reprepare client", instant_reprepare_uid);
+               return TTSD_ERROR_OPERATION_FAILED;
        }
+       SLOG(LOG_INFO, tts_tag(), "[Server] Update UID(%u) of instant reprepare client", instant_reprepare_uid);
+
+       return TTSD_ERROR_NONE;
+}
 
-       int ret = ttsd_data_set_mode(uid, mode);
+static bool __is_connected_to_network()
+{
+       /* Check network */
+       int network_status = 0;
+       int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
        if (0 != ret) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
-       } else {
-               SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%u), mode(%d)", uid, mode);
+               SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
+               return false;
        }
 
-       return ret;
+       if (network_status == VCONFKEY_NETWORK_OFF) {
+               SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
+               return false;
+       }
+
+       SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
+
+       return true;
 }
 
-int ttsd_server_add_queue(unsigned int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
+int ttsd_server_add_text(unsigned int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
+       app_tts_state_e state = ttsd_data_get_client_state(uid);
+       if (APP_STATE_NONE == state) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_text : uid is not valid");
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
@@ -915,7 +1037,7 @@ int ttsd_server_add_queue(unsigned int uid, const char* text, const char* lang,
        if (APP_STATE_PLAYING == state) {
                /* check if engine use network */
                if (ttsd_engine_agent_need_network()) {
-                       if (false == ttsd_network_is_connected()) {
+                       if (false == __is_connected_to_network()) {
                                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
                                return TTSD_ERROR_OPERATION_FAILED;
                        }
@@ -924,7 +1046,7 @@ int ttsd_server_add_queue(unsigned int uid, const char* text, const char* lang,
                ttsd_data_set_credential(uid, credential);
 
                /* Check whether tts-engine is running or not */
-               ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
+               ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
                SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
                if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
                        SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
@@ -936,48 +1058,47 @@ int ttsd_server_add_queue(unsigned int uid, const char* text, const char* lang,
        return TTSD_ERROR_NONE;
 }
 
-static int __pause_client(unsigned int uid)
+static int __interrupt_player_by_policy(unsigned int request_app_uid)
 {
-       int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
-       if (TTSD_ERROR_NONE != ret) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
-               return TTSD_ERROR_OPERATION_FAILED;
+       unsigned int playing_app_uid = ttsd_data_get_current_playing();
+       if (0 > ttsd_data_is_client(playing_app_uid)) {
+               SLOG(LOG_INFO, tts_tag(), "[Server] There is no app which is in 'Play' state");
+               return TTSD_ERROR_NONE;
        }
 
-       ret = ttsd_player_pause(uid);
-       if (TTSD_ERROR_NONE != ret) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
-               return TTSD_ERROR_OPERATION_FAILED;
+       if (request_app_uid == playing_app_uid) {
+               SLOG(LOG_WARN, tts_tag(), "[Server] This is played uid. Skip policy behavior");
+               return TTSD_ERROR_NONE;
        }
 
-       return TTSD_ERROR_NONE;
-}
-
-void __send_interrupt_client(unsigned int uid)
-{
-       SLOG(LOG_INFO, tts_tag(), "[Server] Send new state by policy. uid(%u)", uid);
-       int pid = ttsd_data_get_pid(uid);
-       if (pid <= 0) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
-               return;
-       }
+       ttsd_mode_e request_app_mode = ttsd_data_get_mode(request_app_uid);
+       ttsd_mode_e playing_app_mode = ttsd_data_get_mode(playing_app_uid);
+       switch (playing_app_mode) {
+       case TTSD_MODE_DEFAULT:
+       case TTSD_MODE_NOTIFICATION:
+       case TTSD_MODE_SCREEN_READER:
+               SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
+               __stop_and_send_ready_state(playing_app_uid);
+               break;
 
-       app_tts_state_e state = APP_STATE_CREATED;
+       case TTSD_MODE_INTERRUPT:
+               if (TTSD_MODE_INTERRUPT != request_app_mode) {
+                       SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Default, Screen reader, and Noti are prohibitted.");
+                       return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
+               }
 
-       if (0 != ttsd_data_get_client_state(uid, &state)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state. uid(%u), pid(%d)", uid, pid);
-               return;
+               SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
+               __stop_and_send_ready_state(playing_app_uid);
+               break;
        }
 
-       ttsdc_ipc_send_set_state_message(pid, uid, state);
-       SLOG(LOG_INFO, tts_tag(), "[Server] Finish to send new state by policy. uid(%u)", uid);
-       return;
+       return TTSD_ERROR_NONE;
 }
 
 int ttsd_server_play(unsigned int uid, const char* credential)
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
+       app_tts_state_e state = ttsd_data_get_client_state(uid);
+       if (APP_STATE_NONE == state) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
                return TTSD_ERROR_INVALID_PARAMETER;
        }
@@ -989,44 +1110,16 @@ int ttsd_server_play(unsigned int uid, const char* credential)
 
        /* check if engine use network */
        if (ttsd_engine_agent_need_network()) {
-               if (false == ttsd_network_is_connected()) {
+               if (false == __is_connected_to_network()) {
                        SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
                        return TTSD_ERROR_OUT_OF_NETWORK;
                }
        }
 
-       /* check the current playback focus */
-       if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
-               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
-               ttsd_data_clear_data(uid);
-               return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
-       }
-
-       unsigned int current_uid = ttsd_data_get_current_playing();
-       SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
-
-       if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
-               if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
-
-                       /* stop player */
-                       if (0 != ttsd_server_stop(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
-                       }
-               } else {
-                       /* Default mode policy of interrupt is "Pause" */
-
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
-
-                       /* pause player */
-                       if (0 != __pause_client(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
-                       }
-               }
-
-               __send_interrupt_client(current_uid);
+       int ret = __interrupt_player_by_policy(uid);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
+               return ret;
        }
 
        /* Change current play */
@@ -1039,16 +1132,8 @@ int ttsd_server_play(unsigned int uid, const char* credential)
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       int ret = TTSD_ERROR_NONE;
-       if (APP_STATE_PAUSED == state) {
-               SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
-               g_is_paused = false;
-               ret = ttsd_player_resume(uid);
-       } else {
-               SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
-               ret = ttsd_player_play(uid);
-       }
-
+       SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
+       ret = ttsd_player_play(uid);
        if (TTSD_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
                ttsd_data_set_client_state(uid, state);
@@ -1059,21 +1144,38 @@ int ttsd_server_play(unsigned int uid, const char* credential)
 
        /* Check whether tts-engine is running or not */
        clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
-       ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
+       ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
        SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
        if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
                SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
        } else {
                __synthesis(uid);
        }
+       ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_SYNTH);
 
        return TTSD_ERROR_NONE;
 }
 
+static void __stop_engine_synthesis(unsigned int uid)
+{
+       ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
+       SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
+
+       if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
+               SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
+
+               int ret = ttsd_engine_cancel_synthesis();
+               if (TTSD_ERROR_NONE != ret)
+                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
+       }
+
+       ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+}
+
 int ttsd_server_stop(unsigned int uid)
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
+       app_tts_state_e state = ttsd_data_get_client_state(uid);
+       if (APP_STATE_NONE == state) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
                return TTSD_ERROR_INVALID_PARAMETER;
        }
@@ -1081,16 +1183,9 @@ int ttsd_server_stop(unsigned int uid)
        SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
 
        if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
-               ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
-               SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
-               if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
-
-                       int ret = ttsd_engine_cancel_synthesis();
-                       if (TTSD_ERROR_NONE != ret)
-                               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
+               if (TTS_APP_PLAY_TYPE_SYNTH == ttsd_data_get_play_type(uid)) {
+                       __stop_engine_synthesis(uid);
                }
-               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
 
                /* stop player */
                ttsd_data_set_client_state(uid, APP_STATE_READY);
@@ -1101,15 +1196,14 @@ int ttsd_server_stop(unsigned int uid)
 
        /* Reset all data */
        ttsd_data_clear_data(uid);
-       g_is_paused = false;
 
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_server_pause(unsigned int uid, int* utt_id)
+int ttsd_server_pause(unsigned int uid)
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
+       app_tts_state_e state = ttsd_data_get_client_state(uid);
+       if (APP_STATE_NONE == state) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
                return TTSD_ERROR_INVALID_PARAMETER;
        }
@@ -1119,14 +1213,15 @@ int ttsd_server_pause(unsigned 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(%u), g_uid(%u), utt_id(%d)", uid, g_utt.uid, *utt_id);
+       int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
 
-       int ret = __pause_client(uid);
+       ret = ttsd_player_pause(uid);
        if (TTSD_ERROR_NONE != ret) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to pause player : ret(%d)", ret);
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
@@ -1135,8 +1230,7 @@ int ttsd_server_pause(unsigned int uid, int* utt_id)
 
 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
+       if (0 > ttsd_data_is_client(uid)) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
                return TTSD_ERROR_INVALID_PARAMETER;
        }
@@ -1155,8 +1249,7 @@ int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
 
 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
+       if (0 > ttsd_data_is_client(uid)) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
                return TTSD_ERROR_INVALID_PARAMETER;
        }
@@ -1175,8 +1268,8 @@ int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_
 
 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
+       app_tts_state_e state = ttsd_data_get_client_state(uid);
+       if (APP_STATE_NONE == state) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
                return TTSD_ERROR_INVALID_PARAMETER;
        }
@@ -1198,8 +1291,8 @@ int ttsd_server_set_private_data(unsigned int uid, const char* key, const char*
 
 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
+       app_tts_state_e state = ttsd_data_get_client_state(uid);
+       if (APP_STATE_NONE == state) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
                return TTSD_ERROR_INVALID_PARAMETER;
        }
@@ -1209,13 +1302,19 @@ int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
                return TTSD_ERROR_INVALID_STATE;
        }
 
-       int ret = ttsd_engine_get_private_data(key, data);
+       char* temp_data = NULL;
+       int ret = ttsd_engine_get_private_data(key, &temp_data);
        if (0 != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
        } else {
                SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
        }
 
+       if (NULL == temp_data) {
+               temp_data = strdup("NULL");
+       }
+       *data = temp_data;
+
        return ret;
 }
 
@@ -1245,10 +1344,30 @@ int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
        return ret;
 }
 
+int ttsd_get_activated_mode(int* activated_mode)
+{
+       if (NULL == activated_mode) {
+               return TTSD_ERROR_INVALID_PARAMETER;
+       }
+
+       *activated_mode = ttsd_data_get_activated_mode();
+       return TTSD_ERROR_NONE;
+}
+
+int ttsd_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
+{
+       int ret = ttsd_engine_agent_set_activated_mode_changed_cb(callback);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
+       }
+
+       return ret;
+}
+
 int ttsd_server_play_pcm(unsigned int uid)
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
+       app_tts_state_e state = ttsd_data_get_client_state(uid);
+       if (APP_STATE_NONE == state) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
                return TTSD_ERROR_INVALID_PARAMETER;
        }
@@ -1258,31 +1377,10 @@ int ttsd_server_play_pcm(unsigned int uid)
                return TTSD_ERROR_NONE;
        }
 
-       unsigned int current_uid = ttsd_data_get_current_playing();
-       SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
-
-       if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
-               if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
-
-                       /* stop player */
-                       if (0 != ttsd_server_stop(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
-                       }
-               } else {
-                       /* Default mode policy of interrupt is "Pause" */
-
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
-
-                       /* pause player */
-                       if (0 != __pause_client(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
-                       }
-               }
-
-               __send_interrupt_client(current_uid);
+       int ret = __interrupt_player_by_policy(uid);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
+               return ret;
        }
 
        /* Change current play */
@@ -1291,44 +1389,15 @@ int ttsd_server_play_pcm(unsigned int uid)
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       int ret = TTSD_ERROR_NONE;
-       if (APP_STATE_PAUSED == state) {
-               SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
-               g_is_paused = false;
-               ret = ttsd_player_resume(uid);
-       } else {
-               SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
-               ret = ttsd_player_play(uid);
-       }
-
+       SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
+       ret = ttsd_player_play(uid);
        if (TTSD_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
                ttsd_data_set_client_state(uid, state);
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       return TTSD_ERROR_NONE;
-}
-
-int ttsd_server_stop_pcm(unsigned int uid)
-{
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
-               return TTSD_ERROR_INVALID_PARAMETER;
-       }
-
-       SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
-
-       if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
-               ttsd_data_set_client_state(uid, APP_STATE_READY);
-       }
-
-       /* Reset all data */
-       ttsd_data_clear_data(uid);
-
-       ttsd_player_stop(uid);
-
+       ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_PCM);
 
        return TTSD_ERROR_NONE;
 }
@@ -1377,3 +1446,16 @@ int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size,
 
        return TTSD_ERROR_NONE;
 }
+
+int ttsd_server_get_service_state(unsigned int uid, int* service_state)
+{
+       if (0 > ttsd_data_is_client(uid)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_service_state : uid is not valid");
+               return TTSD_ERROR_INVALID_PARAMETER;
+       }
+
+       *service_state = (int)ttsd_state_get_state();
+       SLOG(LOG_DEBUG, tts_tag(), "[Server] Get current service state. service state(%d) ", *service_state);
+
+       return TTSD_ERROR_NONE;
+}