Add a checker into __send_interrupt_client
[platform/core/uifw/tts.git] / server / ttsd_server.c
index 0731597..8ba5724 100644 (file)
 
 #define CLIENT_CLEAN_UP_TIME 500
 
-
-typedef enum {
-       TTSD_SYNTHESIS_CONTROL_DOING    = 0,
-       TTSD_SYNTHESIS_CONTROL_DONE     = 1,
-       TTSD_SYNTHESIS_CONTROL_EXPIRED  = 2
-} ttsd_synthesis_control_e;
-
 typedef struct {
        int uid;
        int uttid;
@@ -43,29 +36,19 @@ typedef struct {
 /* If current engine exist */
 //static bool  g_is_engine;
 
-/* If engine is running */
-static ttsd_synthesis_control_e        g_synth_control;
-
 static Ecore_Timer* g_check_client_timer = NULL;
 static Ecore_Timer* g_wait_timer = NULL;
+static Ecore_Timer* g_terminate_timer = NULL;
 
 static utterance_t g_utt;
 
 static GList *g_proc_list = NULL;
 
-/* Function definitions */
-static int __synthesis(int uid, const char* credential);
+static bool g_is_paused;
 
-static int __server_set_synth_control(ttsd_synthesis_control_e control)
-{
-       g_synth_control = control;
-       return 0;
-}
 
-static ttsd_synthesis_control_e __server_get_synth_control()
-{
-       return g_synth_control;
-}
+/* Function definitions */
+static int __synthesis(int uid, const char* credential);
 
 static Eina_Bool __wait_synthesis(void *data)
 {
@@ -74,11 +57,11 @@ static Eina_Bool __wait_synthesis(void *data)
        int uid = ttsd_data_get_current_playing();
 
        if (uid > 0) {
-               if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
+               if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
                        return EINA_TRUE;
                } else {
                        g_wait_timer = NULL;
-                       if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
+                       if (TTSD_SYNTHESIS_CONTROL_DONE == ttsd_get_synth_control()) {
                                /* Start next synthesis */
                                __synthesis(uid, credential);
                        }
@@ -113,7 +96,7 @@ static int __synthesis(int uid, const char* credential)
 
                        ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
 
-                       ttsd_data_clear_speak_data(&speak_data);
+                       ttsd_data_clear_speak_data(uid, &speak_data);
 
                        return 0;
                }
@@ -129,12 +112,12 @@ static int __synthesis(int uid, const char* credential)
                SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
 
                int ret = 0;
-               __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
+               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 !!!! * ");
 
-                       __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+                       ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
 
                        ttsd_server_stop(uid);
 
@@ -144,9 +127,9 @@ static int __synthesis(int uid, const char* credential)
                        g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
                }
 
-               ttsd_data_clear_speak_data(&speak_data);
+               ttsd_data_clear_speak_data(uid, &speak_data);
        } else {
-               ttsd_data_clear_speak_data(&speak_data);
+               ttsd_data_clear_speak_data(uid, &speak_data);
        }
 
        SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  END");
@@ -165,7 +148,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(%d), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, (NULL == msg ? "NULL" : msg));
 
-       __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+       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()");
@@ -208,14 +191,14 @@ int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int d
 
 
                if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
-                       __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+                       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) {
-                       __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+                       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;
@@ -254,10 +237,25 @@ int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int d
 
                if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
                        SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
+                       if (NULL != temp_sound_data->data) {
+                               free(temp_sound_data->data);
+                               temp_sound_data->data = NULL;
+                       }
+
+                       free(temp_sound_data);
+                       temp_sound_data = NULL;
+
+                       return TTSD_ERROR_OPERATION_FAILED;
                }
 
                if (event == TTSE_RESULT_EVENT_FINISH) {
-                       __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+                       ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
+               }
+
+               /* If the app state is paused, do not result to play */
+               if (true == g_is_paused) {
+                       SLOG(LOG_DEBUG, tts_tag(), "[Server DEBUG] tts_pause is called. Do not request to play");
+                       return TTSD_ERROR_NONE;
                }
 
                if (0 != ttsd_player_play(uid)) {
@@ -272,7 +270,7 @@ int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int d
                }
        } else {
                SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
-               __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
        }
 
 
@@ -297,7 +295,7 @@ bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
        return true;
 }
 
-void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
+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:
@@ -386,6 +384,19 @@ 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;
@@ -401,6 +412,7 @@ bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data
 Eina_Bool ttsd_terminate_daemon(void *data)
 {
        ttsd_data_foreach_clients(__terminate_client, NULL);
+       g_terminate_timer = NULL;
        return EINA_FALSE;
 }
 
@@ -408,7 +420,11 @@ 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);
+               if (g_terminate_timer) {
+                       ecore_timer_del(g_terminate_timer);
+                       g_terminate_timer = NULL;
+               }
+               g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
        } else {
                SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
        }
@@ -426,6 +442,17 @@ int ttsd_initialize(ttse_request_callback_s *callback)
                SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
        }
 
+       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");
+
        /* player init */
        if (ttsd_player_init()) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
@@ -450,7 +477,7 @@ int ttsd_initialize(ttse_request_callback_s *callback)
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+       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);
@@ -484,20 +511,56 @@ int ttsd_finalize()
 
        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 (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 (NULL != 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;
 }
 
+int ttsd_terminate()
+{
+       SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
+
+       ttsd_terminate_daemon(NULL);
+
+       ttsd_dbus_close_connection();
+       ttsd_network_finalize();
+       ttsd_finalize();
+
+       return TTSD_ERROR_NONE;
+}
+
 /*
 * TTS Server Functions for Client
 */
 
+int ttsd_server_is_already_initialized(int pid, 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(%d) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
+       return TTSD_ERROR_NONE;
+}
+
 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
 {
        SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
@@ -649,6 +712,8 @@ Eina_Bool ttsd_cleanup_client(void *data)
                ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
        } else {
                ecore_timer_add(0, __quit_ecore_loop, NULL);
+               SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
+               return EINA_FALSE;
        }
 
        SLOG(LOG_DEBUG, tts_tag(), "@@@");
@@ -787,7 +852,7 @@ int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice
                }
 
                /* Check whether tts-engine is running or not */
-               if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
+               if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
                        SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
                } else {
                        __synthesis(uid, credential);
@@ -804,6 +869,11 @@ Eina_Bool __send_interrupt_client(void *data)
 
        int pid = ttsd_data_get_pid(uid);
 
+       if (pid <= 0) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%d), pid(%d)", uid, pid);
+               return EINA_FALSE;
+       }
+
        if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
                /* send message to client about changing state */
                ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
@@ -843,6 +913,7 @@ int ttsd_server_play(int uid, const char* credential)
                } 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.");
+                               ttsd_data_clear_data(uid);
                                return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
                        }
                }
@@ -894,6 +965,8 @@ int ttsd_server_play(int uid, const char* credential)
        if (APP_STATE_PAUSED == state) {
                SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
 
+               g_is_paused = false;
+
                /* Resume player */
                if (0 != ttsd_player_resume(uid)) {
                        SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
@@ -901,7 +974,7 @@ int ttsd_server_play(int uid, const char* credential)
        }
 
        /* Check whether tts-engine is running or not */
-       if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
+       if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
                SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
        } else {
                __synthesis(uid, credential);
@@ -921,7 +994,7 @@ int ttsd_server_stop(int uid)
        SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
 
        if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
-               if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control() && uid == ttsd_data_get_current_playing()) {
+               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;
@@ -930,7 +1003,7 @@ int ttsd_server_stop(int uid)
                                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
                }
 
-               __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+               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()");
@@ -940,6 +1013,7 @@ int ttsd_server_stop(int uid)
 
        /* Reset all data */
        ttsd_data_clear_data(uid);
+       g_is_paused = false;
 
        return TTSD_ERROR_NONE;
 }
@@ -957,6 +1031,8 @@ int ttsd_server_pause(int uid, int* utt_id)
                return TTSD_ERROR_INVALID_STATE;
        }
 
+       g_is_paused = true;
+
        *utt_id = g_utt.uttid;
        SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);