Send synthesized PCM data from TTS engine to client
[platform/core/uifw/tts.git] / server / ttsd_server.c
index 72cee8f..d87b4ce 100644 (file)
 #include <app_manager.h>
 #include <aul.h>
 #include <Ecore.h>
+#include <sys/time.h>
 
 #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 CLIENT_CLEAN_UP_TIME 500
 
+/* for checking time */
+#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, 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 {
-       int uid;
+       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_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_terminated = false;
+
+static int g_activated_mode = 0;
+
 /* Function definitions */
-static int __synthesis(int uid, const char* credential);
+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)
 {
        /* get current play */
-       char* credential = (char*)data;
-       int uid = ttsd_data_get_current_playing();
+       unsigned int uid = ttsd_data_get_current_playing();
 
        if (uid > 0) {
-               if (TTSD_SYNTHESIS_CONTROL_DOING == 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 {
+                       SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
                        g_wait_timer = NULL;
-                       if (TTSD_SYNTHESIS_CONTROL_DONE == ttsd_get_synth_control()) {
+                       if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
                                /* Start next synthesis */
-                               __synthesis(uid, credential);
+                               __synthesis(uid);
                        }
                }
        } else {
@@ -69,68 +111,76 @@ static Eina_Bool __wait_synthesis(void *data)
        return EINA_FALSE;
 }
 
-static int __synthesis(int uid, const char* credential)
+static void __synthesis(unsigned int uid)
 {
        SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  START");
 
        speak_data_s* speak_data = NULL;
-       if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
-               if (NULL == speak_data) {
-                       SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
-                       return 0;
-               }
-
-               int pid = ttsd_data_get_pid(uid);
-               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");
-               }
-
-               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);
+       int ret = ttsd_data_get_speak_data(uid, &speak_data);
+       if (TTSD_ERROR_NONE != ret || NULL == speak_data) {
+               SLOG(LOG_DEBUG, tts_tag(), "@@@ Fail to get speak data. ret(%d)", ret);
+               SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  END");
+               return;
+       }
 
-                       ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
+       int pid = ttsd_data_get_pid(uid);
+       if (pid <= 0) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
+               ttsd_data_destroy_speak_data(speak_data);
+               speak_data = NULL;
+               return;
+       }
 
-                       ttsd_data_clear_speak_data(&speak_data);
+       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");
+       }
 
-                       return 0;
-               }
+       if (NULL == speak_data->lang || NULL == speak_data->text) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
+               __stop_and_send_ready_state(uid);
+               ttsd_data_destroy_speak_data(speak_data);
+               speak_data = NULL;
+               return;
+       }
 
-               g_utt.uid = uid;
-               g_utt.uttid = speak_data->utt_id;
+       g_utt.uid = uid;
+       g_utt.uttid = speak_data->utt_id;
 
-               SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
-               SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
-               SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
-               SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
-               SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
-               SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
+       char* credential = ttsd_data_get_credential(uid);
 
-               int ret = 0;
-               ttsd_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 (0 != ret) {
-                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
+       SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
+       SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
+       SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
+       SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
+       SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
+       SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
 
-                       ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+       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_server_stop(uid);
+               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);
+       }
 
-                       int pid = ttsd_data_get_pid(uid);
-                       ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
-               } 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_data_clear_speak_data(&speak_data);
-       } else {
-               ttsd_data_clear_speak_data(&speak_data);
+               ttsd_state_set_state(TTSD_STATE_SYNTHESIZING);
        }
 
+       free(credential);
+       credential = NULL;
+       ttsd_data_destroy_speak_data(speak_data);
+       speak_data = NULL;
        SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  END");
 
-       return 0;
+       return;
 }
 
 /*
@@ -138,145 +188,193 @@ static int __synthesis(int uid, const char* credential)
 */
 int ttsd_send_error(ttse_error_e error, const char* msg)
 {
-       int uid = g_utt.uid;
+       unsigned int uid = g_utt.uid;
        int uttid = g_utt.uttid;
        int tmp_pid = ttsd_data_get_pid(uid);
 
-       SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%d), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, (NULL == msg ? "NULL" : msg));
-
-       ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+       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));
 
-       if (0 != ttsd_player_clear(uid))
-               SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
+       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()");
 
-       if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
-               SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
+       if (tmp_pid > 0) {
+               if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
+                       SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
+       }
 
        if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
                SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
 
-       if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
-               SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
-
+       if (tmp_pid > 0) {
+               if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
+                       SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
+       }
        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");
 
-       int uid = g_utt.uid;
+       unsigned int uid = g_utt.uid;
        int uttid = g_utt.uttid;
 
-       /* Synthesis is success */
-       if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
-               if (TTSE_RESULT_EVENT_START == event) {
-                       SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
-                       SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
-                               uid, uttid, data, data_size, audio_type, rate);
-               } 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(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
-                               uid, uttid, data, data_size, audio_type, rate);
-               } else {
-                       /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
-               }
+       if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
+               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;
+       }
 
+       if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
+               SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
+               ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+               return TTSD_ERROR_INVALID_PARAMETER;
+       }
 
-               if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
-                       ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
-                       SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
-                       SLOG(LOG_DEBUG, tts_tag(), "@@@");
-                       return TTSD_ERROR_OPERATION_FAILED;
-               }
+       if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
+               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;
+       }
 
-               if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
-                       ttsd_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 = 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_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;
+       }
+
+       /* Synthesis is success */
+       if (TTSE_RESULT_EVENT_START == event) {
+               SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
+               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);
+
+               clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
+               long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
+               SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
+               double d_latency = (double)latency;
+               g_avg_latency += d_latency;
+               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;
+       }
 
+       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* temp_sound_data = NULL;
-               temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
-               if (NULL == temp_sound_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;
                }
 
-               temp_sound_data->data = NULL;
-               temp_sound_data->rate = 0;
-               temp_sound_data->data_size = 0;
-
-               if (0 < data_size) {
-                       temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
-                       if (NULL != temp_sound_data->data) {
-                               memcpy(temp_sound_data->data, data, data_size);
-                               temp_sound_data->data_size = data_size;
-                               SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)", 
-                                       uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
-                       } else {
-                               SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
-                       }
-               } else {
-                       SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
-               }
+               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);
 
-               temp_sound_data->utt_id = uttid;
-               temp_sound_data->event = event;
-               temp_sound_data->audio_type = audio_type;
-               temp_sound_data->rate = rate;
-
-               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 (event == TTSE_RESULT_EVENT_FINISH) {
-                       ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+                       return TTSD_ERROR_OPERATION_FAILED;
                }
-
-               if (0 != ttsd_player_play(uid)) {
-                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
-
-                       /* Change ready state */
-                       ttsd_server_stop(uid);
-
-                       int tmp_pid;
-                       tmp_pid = ttsd_data_get_pid(uid);
-                       ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
+       } 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;
                }
-       } else {
-               SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
-               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
        }
 
-
-       /*SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS RESULT CALLBACK END");
-       SLOG(LOG_DEBUG, tts_tag(), "  ");*/
+       if (event == TTSE_RESULT_EVENT_FINISH) {
+               ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+       }
 
        return TTSD_ERROR_NONE;
 }
 
-bool __get_client_cb(int pid, 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);
-       ttsd_data_set_client_state(uid, APP_STATE_READY);
 
-       /* send message */
-       if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
-               /* remove client */
-               ttsd_data_delete_client(uid);
+       if (APP_STATE_READY != state) {
+               ttsd_data_set_client_state(uid, APP_STATE_READY);
+               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)
+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:
@@ -302,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;
        }
 
@@ -365,81 +456,242 @@ void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_
                }
                break;
        }
+
+       case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
+       {
+               if (0.0 <= double_param && double_param <= 1.0) {
+                       /* set default bg volume ratio */
+                       int ret = 0;
+                       ret = ttsd_player_set_background_volume_ratio(double_param);
+                       if (0 != ret) {
+                               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
+                       }
+               }
+               break;
+       }
        }
 
        return;
 }
 
-bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
+bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
 {
        SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
        ttsd_server_finalize(uid);
        return true;
 }
 
-Eina_Bool ttsd_terminate_daemon(void *data)
+static int __terminate_server()
+{
+       if (g_is_terminated) {
+               SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
+               return TTSD_ERROR_NONE;
+       }
+
+       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;
+       }
+
+       g_is_terminated = true;
+       return TTSD_ERROR_NONE;
+}
+
+static Eina_Bool __quit_ecore_loop(void *data)
 {
-       ttsd_data_foreach_clients(__terminate_client, NULL);
+       __terminate_server();
+       ecore_main_loop_quit();
+
        return EINA_FALSE;
 }
 
 void __screen_reader_changed_cb(bool value)
 {
-       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");
-               ecore_timer_add(1, ttsd_terminate_daemon, NULL);
+       SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
+       return;
+}
+
+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);
+               }
+       }
+
+       dp = opendir("/proc");
+       if (NULL == dp) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
        } else {
-               SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
+               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);
        }
        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;
+                       }
+               }
+       }
+
+       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 {
+               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;
+}
+
+static bool __notify_state_changed_event(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
+{
+       state_changed_args_t* args = (state_changed_args_t*)user_data;
+       if (NULL == args) {
+               return false;
+       }
+
+       if (ttsd_data_is_service_state_changed_cb_set(uid)) {
+               ttsdc_ipc_send_set_service_state_message(pid, uid, args->before, args->current);
+       }
+
+       return true;
+}
+
+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);
+}
+
 /*
 * Server APIs
 */
 int ttsd_initialize(ttse_request_callback_s *callback)
 {
        SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
+       g_is_terminated = false;
 
        if (ttsd_config_initialize(__config_changed_cb)) {
                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.");
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       /* Engine Agent initialize */
-       if (0 != ttsd_engine_agent_init()) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
-               return TTSD_ERROR_OPERATION_FAILED;
+       double ratio;
+       if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
+       {
+               SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
+               int ret = ttsd_player_set_background_volume_ratio(ratio);
+               if (0 != ret)
+                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
+       } else {
+               SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
        }
 
-       /* 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");
+       /* Engine Agent initialize */
+       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;
        }
 
-       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);
-       }
+       ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+       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;
 }
 
@@ -457,182 +709,176 @@ int ttsd_finalize()
                }
        }
 
-       ttsd_config_finalize();
+       if (g_notify_state_timer) {
+               void* data = ecore_timer_del(g_notify_state_timer);
+               g_notify_state_timer = NULL;
 
-       ttsd_player_release();
+               free(data);
+               SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore notify state timer handle");
+       }
 
-       ttsd_engine_agent_release();
+       if (g_wait_timer) {
+               ecore_timer_del(g_wait_timer);
+               g_wait_timer = NULL;
+               SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
+       }
 
-       if (NULL != g_check_client_timer) {
+       if (g_check_client_timer) {
                ecore_timer_del(g_check_client_timer);
                g_check_client_timer = NULL;
-
-               SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore timer handle");
+               SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
        }
 
-       return TTSD_ERROR_NONE;
-}
-
-/*
-* TTS Server Functions for Client
-*/
+       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");
+       }
 
-int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
-{
-       SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
+       ttsd_config_finalize();
+       ttsd_state_finalize();
 
-       if (-1 != ttsd_data_is_client(uid)) {
-               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
-               return TTSD_ERROR_NONE;
+       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;
        }
 
-       if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
+       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;
        }
 
-       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);
+       return TTSD_ERROR_NONE;
+}
 
-               if (false == is_agreed) {
-                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
-                       return TTSD_ERROR_PERMISSION_DENIED;
-               }
+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;
        }
 
-       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;
+       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");
        }
 
-       if (0 != ttsd_player_create_instance(uid)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
-               return TTSD_ERROR_OPERATION_FAILED;
-       }
+       return __terminate_server();
+}
+
+/*
+* TTS Server Functions for Client
+*/
+
+int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
+{
+       if (-1 != ttsd_data_is_client(uid))
+               *is_initialized = true;
+       else
+               *is_initialized = false;
 
+       SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
        return TTSD_ERROR_NONE;
 }
 
-static Eina_Bool __quit_ecore_loop(void *data)
+static void __set_and_notify_activated_mode()
 {
-       ttsd_dbus_close_connection();
-       ttsd_network_finalize();
-       ttsd_finalize();
-       ecore_main_loop_quit();
+       int activated_mode = ttsd_data_get_activated_mode();
+       if (g_activated_mode == activated_mode) {
+               return;
+       }
 
-       return EINA_FALSE;
-}
+       SLOG(LOG_INFO, tts_tag(), "[Server] Activated mode is changed from(%d) to (%d)", g_activated_mode, activated_mode);
 
+       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);
+       }
+}
 
-static void __read_proc()
+static int __check_app_agreed(int pid, unsigned int uid, bool credential_needed)
 {
-       DIR *dp = NULL;
-       struct dirent *dirp = NULL;
-       int tmp;
+       if (false == credential_needed) {
+               return TTSD_ERROR_NONE;
+       }
 
-       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);
-               }
+       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);
        }
 
-       dp = opendir("/proc");
-       if (NULL == dp) {
-               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
-       } else {
-               do {
-                       dirp = readdir(dp);
+       bool is_agreed = false;
+       int ret = ttsd_engine_check_app_agreed(appid, &is_agreed);
+       free(appid);
+       appid = NULL;
 
-                       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 != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
+               return TTSD_ERROR_OPERATION_FAILED;
        }
-       return;
+
+       if (false == is_agreed) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
+               return TTSD_ERROR_PERMISSION_DENIED;
+       }
+
+       return TTSD_ERROR_NONE;
 }
 
-bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
+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)
 {
-       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 (%d) is running", uid);
-                               exist = true;
-                               break;
-                       }
-               }
-       }
+       SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
 
-       if (false == exist) {
-               SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
-               ttsd_server_finalize(uid);
+       if (-1 != ttsd_data_is_client(uid)) {
+               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
+               return TTSD_ERROR_NONE;
        }
 
-       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");
+       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;
        }
 
-       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);
+       __set_and_notify_activated_mode();
 
-               if (0 == result) {
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
-                       ttsd_server_finalize(uid);
-               } else if (-1 == result) {
-                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
-               }
+       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;
-#endif
-}
 
+       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;
+       }
 
-Eina_Bool ttsd_cleanup_client(void *data)
-{
-       SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
-       __read_proc();
+       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;
+       }
 
-       if (0 < ttsd_data_get_client_count()) {
-               ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
-       } else {
-               ecore_timer_add(0, __quit_ecore_loop, NULL);
+       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)
@@ -643,19 +889,19 @@ void __used_voice_cb(const char* lang, int type)
        }
 }
 
-int ttsd_server_finalize(int uid)
+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_stop(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)) {
@@ -664,21 +910,68 @@ int ttsd_server_finalize(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()) {
-               SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
-               ecore_timer_add(0, __quit_ecore_loop, NULL);
+               if (NULL == g_quit_loop_timer) {
+                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
+                       g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
+               } else {
+                       SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
+               }
+       }
+
+       return TTSD_ERROR_NONE;
+}
+
+int ttsd_server_update_instant_reprepare_client()
+{
+       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;
        }
 
+       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 ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
+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_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
+               return false;
+       }
+
+       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_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;
        }
 
@@ -699,25 +992,17 @@ int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice
                return TTSD_ERROR_INVALID_VOICE;
        }
 
-       speak_data_s* speak_data = NULL;
-       speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
+       speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
        if (NULL == speak_data) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
                if (NULL != temp_lang) {
                        free(temp_lang);
                        temp_lang = NULL;
                }
+
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       speak_data->lang = strdup(lang);
-       speak_data->vctype = voice_type;
-
-       speak_data->speed = speed;
-       speak_data->utt_id = utt_id;
-
-       speak_data->text = strdup(text);
-
        SLOG(LOG_INFO, tts_tag(), "[Server] Add queue, lang(%s), vctype(%d), speed(%d), uttid(%d), credential(%s)", lang, voice_type, speed, utt_id, credential);
 
        /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
@@ -729,16 +1014,9 @@ int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice
                        free(temp_lang);
                        temp_lang = NULL;
                }
-               if (NULL != speak_data) {
-                       if (NULL != speak_data->lang)   free(speak_data->lang);
-                       if (NULL != speak_data->text)   free(speak_data->text);
 
-                       speak_data->lang = NULL;
-                       speak_data->text = NULL;
-
-                       free(speak_data);
-                       speak_data = NULL;
-               }
+               ttsd_data_destroy_speak_data(speak_data);
+               speak_data = NULL;
 
                return ret;
        }
@@ -759,162 +1037,161 @@ int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice
        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;
                        }
                }
 
+               ttsd_data_set_credential(uid, credential);
+
                /* Check whether tts-engine is running or not */
-               if (TTSD_SYNTHESIS_CONTROL_DOING == 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, credential);
+                       __synthesis(uid);
                }
        }
 
        return TTSD_ERROR_NONE;
 }
 
-Eina_Bool __send_interrupt_client(void *data)
+static int __interrupt_player_by_policy(unsigned int request_app_uid)
 {
-       intptr_t puid = (intptr_t)data;
-       int uid = (int)puid;
+       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;
+       }
 
-       int pid = ttsd_data_get_pid(uid);
+       if (request_app_uid == playing_app_uid) {
+               SLOG(LOG_WARN, tts_tag(), "[Server] This is played uid. Skip policy behavior");
+               return TTSD_ERROR_NONE;
+       }
 
-       if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
-               /* send message to client about changing state */
-               ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
-       } else {
-               ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
+       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;
+
+       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;
+               }
+
+               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;
        }
 
-       return EINA_FALSE;
+       return TTSD_ERROR_NONE;
 }
 
-int ttsd_server_play(int uid, const char* credential)
+int ttsd_server_play(unsigned int uid, const char* credential)
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
+       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;
        }
 
        if (APP_STATE_PLAYING == state) {
-               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
+               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
                return TTSD_ERROR_NONE;
        }
 
        /* 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()) {
-               bool is_current_interrupt = false;
-               if (0 != ttsd_player_check_current_playback_focus(&is_current_interrupt)) {
-                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to check the current playback focus");
-               } else {
-                       if (true == is_current_interrupt) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
-                               return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
-                       }
-               }
+       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;
        }
 
-       int current_uid = ttsd_data_get_current_playing();
-       SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
-
-       if (uid != current_uid && -1 != current_uid) {
-               if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
-
-                       /* pause player */
-                       if (0 != ttsd_server_stop(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
-                       }
-                       if (0 != ttsd_player_stop(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
-                       }
-
-                       intptr_t pcurrent_uid = (intptr_t)current_uid;
-                       ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
-               } else {
-                       /* Default mode policy of interrupt is "Pause" */
-
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
-
-                       /* pause player */
-                       if (0 != ttsd_player_pause(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
-                       }
-
-                       /* change state */
-                       ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
-
-                       intptr_t pcurrent_uid = (intptr_t)current_uid;
-                       ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
-               }
+       /* Change current play */
+       if (0 != ttsd_player_wait_to_play(uid)) {
+               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
        }
 
-       /* Change current play */
        if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       if (APP_STATE_PAUSED == state) {
-               SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
-
-               /* Resume player */
-               if (0 != ttsd_player_resume(uid)) {
-                       SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
-               }
+       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;
        }
 
+       ttsd_data_set_credential(uid, credential);
+
        /* Check whether tts-engine is running or not */
-       if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
+       clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
+       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, credential);
+               __synthesis(uid);
        }
+       ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_SYNTH);
 
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_server_stop(int uid)
+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;
        }
 
-       SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
+       SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
 
        if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
-               if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
-
-                       int ret = 0;
-                       ret = ttsd_engine_cancel_synthesis();
-                       if (0 != 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);
-
-               if (0 != ttsd_player_clear(uid))
-                       SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
-
+               /* stop player */
                ttsd_data_set_client_state(uid, APP_STATE_READY);
+               if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
+                       SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
+               }
        }
 
        /* Reset all data */
@@ -923,10 +1200,10 @@ int ttsd_server_stop(int uid)
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_server_pause(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;
        }
@@ -936,25 +1213,24 @@ int ttsd_server_pause(int uid, int* utt_id)
                return TTSD_ERROR_INVALID_STATE;
        }
 
-       *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);
+       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 = 0;
        ret = ttsd_player_pause(uid);
-       if (0 != ret) {
+       if (TTSD_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
-
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_server_get_support_voices(int uid, GList** voice_list)
+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;
        }
@@ -971,10 +1247,9 @@ int ttsd_server_get_support_voices(int uid, GList** voice_list)
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
+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;
        }
@@ -991,16 +1266,16 @@ int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_server_set_private_data(int uid, const char* key, const char* data)
+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)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
+       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;
        }
 
        if (APP_STATE_READY != state) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
                return TTSD_ERROR_INVALID_STATE;
        }
 
@@ -1014,26 +1289,32 @@ int ttsd_server_set_private_data(int uid, const char* key, const char* data)
        return ret;
 }
 
-int ttsd_server_get_private_data(int uid, const char* key, char** data)
+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)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
+       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;
        }
 
        if (APP_STATE_READY != state) {
-               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
+               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
                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;
 }
 
@@ -1063,109 +1344,65 @@ int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
        return ret;
 }
 
-int ttsd_server_play_pcm(int uid)
+int ttsd_get_activated_mode(int* activated_mode)
 {
-       app_tts_state_e state;
-       if (0 > ttsd_data_get_client_state(uid, &state)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
+       if (NULL == activated_mode) {
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
-       if (APP_STATE_PLAYING == state) {
-               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
-               return TTSD_ERROR_NONE;
-       }
-
-       int current_uid = ttsd_data_get_current_playing();
-       SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
-
-       if (uid != current_uid && -1 != current_uid) {
-               if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
-
-                       /* pause player */
-                       if (0 != ttsd_server_stop(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
-                       }
-                       if (0 != ttsd_player_stop(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
-                       }
-
-                       intptr_t pcurrent_uid = (intptr_t)current_uid;
-                       ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
-               } else {
-                       /* Default mode policy of interrupt is "Pause" */
-
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
-
-                       /* pause player */
-                       if (0 != ttsd_player_pause(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
-                       }
-
-                       /* change state */
-                       ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
-
-                       intptr_t pcurrent_uid = (intptr_t)current_uid;
-                       ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
-               }
-       }
-
-       /* Change current play */
-       if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
-               return TTSD_ERROR_OPERATION_FAILED;
-       }
-
-       if (APP_STATE_PAUSED == state) {
-               SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
-
-               /* Resume player */
-               if (0 != ttsd_player_resume(uid)) {
-                       SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
-               }
-       } else {
-               if (0 != ttsd_player_play_pcm(uid)) {
-                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
-
-                       // Change ready state
-                       ttsd_server_stop_pcm(uid);
+       *activated_mode = ttsd_data_get_activated_mode();
+       return TTSD_ERROR_NONE;
+}
 
-                       int tmp_pid;
-                       tmp_pid = ttsd_data_get_pid(uid);
-                       ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
-               }
+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 TTSD_ERROR_NONE;
+       return ret;
 }
 
-int ttsd_server_stop_pcm(int uid)
+int ttsd_server_play_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");
+       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;
        }
 
-       SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
+       if (APP_STATE_PLAYING == state) {
+               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
+               return TTSD_ERROR_NONE;
+       }
 
-       if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
-               ttsd_data_set_client_state(uid, APP_STATE_READY);
+       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;
        }
 
-       /* Reset all data */
-       ttsd_data_clear_data(uid);
+       /* Change current play */
+       if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
 
-       ttsd_player_stop(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;
+       }
 
+       ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_PCM);
 
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
+int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
 {
        SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
 
@@ -1175,11 +1412,11 @@ int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio
        if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
                if (TTSE_RESULT_EVENT_START == event) {
                        SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
-                       SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
+                       SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
                                uid, uttid, data, data_size, audio_type, rate);
                } 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] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
+                       SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
                                uid, uttid, data, data_size, audio_type, rate);
                } else {
                        /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
@@ -1191,60 +1428,16 @@ int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio
                        return TTSD_ERROR_INVALID_PARAMETER;
                }
 
-               /* add wav data */
-               sound_data_s* temp_sound_data = NULL;
-               temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
-               if (NULL == temp_sound_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;
                }
 
-               temp_sound_data->data = NULL;
-               temp_sound_data->rate = 0;
-               temp_sound_data->data_size = 0;
-
-               if (0 < data_size) {
-                       temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
-                       if (NULL != temp_sound_data->data) {
-                               memcpy(temp_sound_data->data, data, data_size);
-                               temp_sound_data->data_size = data_size;
-                               SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)", 
-                                       uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
-                       } else {
-                               SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
-                       }
-               } else {
-                       SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
-               }
-
-               temp_sound_data->utt_id = uttid;
-               temp_sound_data->event = event;
-               temp_sound_data->audio_type = audio_type;
-               temp_sound_data->rate = rate;
-
-               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;
-               }
-
-/*             if (0 != ttsd_player_play(uid)) {
-                       SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
-
-                       // Change ready state
-                       ttsd_server_stop(uid);
-
-                       int tmp_pid;
-                       tmp_pid = ttsd_data_get_pid(uid);
-                       ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
+               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);
                }
-*/
        } else {
                SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
        }
@@ -1253,3 +1446,16 @@ int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio
 
        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;
+}