Create play thread if the app state is still playing 42/255942/2
authorSuyeon Hwang <stom.hwang@samsung.com>
Thu, 25 Mar 2021 07:44:11 +0000 (16:44 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Fri, 26 Mar 2021 00:35:27 +0000 (09:35 +0900)
Change-Id: I68a5d0f676cd350063df4aa459bc5a5690b56e47
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
server/ttsd_data.cpp
server/ttsd_data.h
server/ttsd_server.c

index 271476f..7d2b8d4 100644 (file)
@@ -47,6 +47,7 @@ static vector<app_data_s> g_app_list;
 static pthread_mutex_t g_speak_data_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t g_sound_data_mutex = PTHREAD_MUTEX_INITIALIZER;
 
+static pthread_mutex_t g_app_state_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* If engine is running */
 static ttsd_synthesis_control_e        g_synth_control;
@@ -752,7 +753,10 @@ int ttsd_data_get_client_state(int uid, app_tts_state_e* state)
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
+       //TODO : lock more specific memory
+       pthread_mutex_lock(&g_app_state_mutex);
        *state = g_app_list[index].state;
+       pthread_mutex_unlock(&g_app_state_mutex);
 
        return TTSD_ERROR_NONE;
 }
@@ -767,18 +771,22 @@ int ttsd_data_set_client_state(int uid, app_tts_state_e state)
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
+       //TODO : lock more specific memory
+       pthread_mutex_lock(&g_app_state_mutex);
        /* The client of playing state of all clients is only one. need to check state. */
        if (APP_STATE_PLAYING == state) {
                int vsize = g_app_list.size();
                for (int i = 0; i < vsize; i++) {
                        if(g_app_list[i].state == APP_STATE_PLAYING) {
                                SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] A playing client has already existed.");
+                               pthread_mutex_unlock(&g_app_state_mutex);
                                return -1;
                        }
                }
        }
 
        g_app_list[index].state = state;
+       pthread_mutex_unlock(&g_app_state_mutex);
 
        return TTSD_ERROR_NONE;
 }
index 025caf7..e500869 100644 (file)
@@ -104,9 +104,9 @@ int ttsd_data_set_used_voice(int uid, const char* lang, int type);
 
 int ttsd_data_reset_used_voice(int uid, ttsd_used_voice_cb callback);
 
-int ttsd_data_get_client_state(int pid, app_tts_state_e* state);
+int ttsd_data_get_client_state(int uid, app_tts_state_e* state);
 
-int ttsd_data_set_client_state(int pid, app_tts_state_e state);
+int ttsd_data_set_client_state(int uid, app_tts_state_e state);
 
 
 int ttsd_data_get_current_playing();
index e39343c..35dd834 100644 (file)
@@ -188,112 +188,119 @@ int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int d
        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_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_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_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_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;
-               }
+       /* 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(%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");*/
+       }
 
-               /* 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) {
-                       SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
-                       return TTSD_ERROR_OUT_OF_MEMORY;
-               }
+       /* 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) {
+               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;
+       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");
-                       }
+       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(), "Sound data is NULL");
+                       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;
+       temp_sound_data->utt_id = uttid;
+       temp_sound_data->event = event;
+       temp_sound_data->audio_type = audio_type;
+       temp_sound_data->rate = rate;
 
-                       return TTSD_ERROR_OPERATION_FAILED;
+       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;
                }
 
-               if (event == TTSE_RESULT_EVENT_FINISH) {
-                       ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
-               }
+               free(temp_sound_data);
+               temp_sound_data = NULL;
 
-               /* 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;
-               }
+               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);
+       if (event == TTSE_RESULT_EVENT_FINISH) {
+               ttsd_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;
+       }
 
-                       int tmp_pid = ttsd_data_get_pid(uid);
-                       if (tmp_pid <= 0) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] uid(%d) is already destroyed or invalid", uid);
-                       } else {
-                               /* Change ready state */
-                               ttsd_server_stop(uid);
+       app_tts_state_e state = APP_STATE_CREATED;
+       if (0 != ttsd_data_get_client_state(uid, &state)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
+               return TTSD_ERROR_INVALID_PARAMETER;
+       }
 
-                               ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
-                       }
-               }
-       } else {
-               SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
-               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
+       if (APP_STATE_PLAYING != state) {
+               SLOG(LOG_INFO, tts_tag(), "[Server] client state is not playing");
+               return TTSD_ERROR_NONE;
        }
 
+       if (0 != ttsd_player_play(uid)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
+
+               ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
 
-       /*SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS RESULT CALLBACK END");
-       SLOG(LOG_DEBUG, tts_tag(), "  ");*/
+               int tmp_pid = ttsd_data_get_pid(uid);
+               if (tmp_pid <= 0) {
+                       SLOG(LOG_WARN, tts_tag(), "[Server WARNING] uid(%d) is already destroyed or invalid", uid);
+               } else {
+                       /* Change ready state */
+                       ttsd_server_stop(uid);
+
+                       ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
+               }
+       }
 
        return TTSD_ERROR_NONE;
 }