2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
14 #include <app_manager.h>
19 #include "ttsd_config.h"
20 #include "ttsd_data.h"
21 #include "ttsd_dbus.h"
22 #include "ttsd_dbus_server.h"
24 #include "ttsd_engine_agent.h"
25 #include "ttsd_main.h"
26 #include "ttsd_network.h"
27 #include "ttsd_player.h"
28 #include "ttsd_server.h"
31 #define CLIENT_CLEAN_UP_TIME 500
33 /* for checking time */
34 #define TIME_DIFF(start, end) \
35 ((uint64_t)((end).tv_sec - (start).tv_sec) * 1000000 + (((end).tv_nsec - (start).tv_nsec) / 1000)) / 1000
37 static struct timespec g_request_playing, g_start_playing;
38 static double g_avg_latency;
39 static double g_min_latency;
40 static double g_max_latency;
41 static int g_file_num;
48 /* If current engine exist */
49 //static bool g_is_engine;
51 static Ecore_Timer* g_check_client_timer = NULL;
52 static Ecore_Timer* g_wait_timer = NULL;
53 static Ecore_Timer* g_terminate_timer = NULL;
55 static utterance_t g_utt;
57 static GList *g_proc_list = NULL;
59 static bool g_is_paused;
62 /* Function definitions */
63 static int __synthesis(int uid, const char* credential);
65 static Eina_Bool __wait_synthesis(void *data)
67 /* get current play */
68 char* credential = (char*)data;
69 int uid = ttsd_data_get_current_playing();
72 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
76 if (TTSD_SYNTHESIS_CONTROL_DONE == ttsd_get_synth_control()) {
77 /* Start next synthesis */
78 __synthesis(uid, credential);
88 static int __synthesis(int uid, const char* credential)
90 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
92 speak_data_s* speak_data = NULL;
93 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
94 if (NULL == speak_data) {
95 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
99 int pid = ttsd_data_get_pid(uid);
101 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%d)", uid);
105 char appid[1024] = {0, };
106 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
107 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
110 if (NULL == speak_data->lang || NULL == speak_data->text) {
111 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
112 ttsd_server_stop(uid);
114 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
116 ttsd_data_clear_speak_data(&speak_data);
122 g_utt.uttid = speak_data->utt_id;
124 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
125 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
126 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
127 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
128 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
129 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
132 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
133 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
135 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
137 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
139 ttsd_server_stop(uid);
141 int pid = ttsd_data_get_pid(uid);
143 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%d)", uid);
145 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
148 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
151 ttsd_data_clear_speak_data(&speak_data);
153 ttsd_data_clear_speak_data(&speak_data);
156 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
162 * TTS Server Callback Functions
164 int ttsd_send_error(ttse_error_e error, const char* msg)
167 int uttid = g_utt.uttid;
168 int tmp_pid = ttsd_data_get_pid(uid);
170 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));
172 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
174 if (0 != ttsd_player_clear(uid))
175 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
177 if (0 != ttsd_data_clear_data(uid))
178 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
181 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
182 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
185 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
186 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
189 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
190 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
195 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)
197 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
200 int uttid = g_utt.uttid;
202 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
203 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
204 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
205 SLOG(LOG_DEBUG, tts_tag(), "@@@");
206 return TTSD_ERROR_INVALID_PARAMETER;
209 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
210 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
211 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
212 return TTSD_ERROR_INVALID_PARAMETER;
215 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
216 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
217 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
218 SLOG(LOG_DEBUG, tts_tag(), "@@@");
219 return TTSD_ERROR_INVALID_PARAMETER;
222 /* Synthesis is success */
223 if (TTSE_RESULT_EVENT_START == event) {
224 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
225 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
226 uid, uttid, data, data_size, audio_type, rate);
228 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
229 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
230 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
231 double d_latency = (double)latency;
232 g_avg_latency += d_latency;
233 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
234 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
236 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);
237 } else if (TTSE_RESULT_EVENT_FINISH == event) {
238 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
239 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
240 uid, uttid, data, data_size, audio_type, rate);
242 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
246 sound_data_s* temp_sound_data = NULL;
247 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
248 if (NULL == temp_sound_data) {
249 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
250 return TTSD_ERROR_OUT_OF_MEMORY;
253 temp_sound_data->data = NULL;
254 temp_sound_data->rate = 0;
255 temp_sound_data->data_size = 0;
257 if (NULL != data && 0 < data_size) {
258 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
259 if (NULL != temp_sound_data->data) {
260 memcpy(temp_sound_data->data, data, data_size);
261 temp_sound_data->data_size = data_size;
262 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
263 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
265 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
268 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
271 temp_sound_data->utt_id = uttid;
272 temp_sound_data->event = event;
273 temp_sound_data->audio_type = audio_type;
274 temp_sound_data->rate = rate;
276 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
277 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
278 if (NULL != temp_sound_data->data) {
279 free(temp_sound_data->data);
280 temp_sound_data->data = NULL;
283 free(temp_sound_data);
284 temp_sound_data = NULL;
286 return TTSD_ERROR_OPERATION_FAILED;
289 if (event == TTSE_RESULT_EVENT_FINISH) {
290 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
293 /* If the app state is paused, do not result to play */
294 if (true == g_is_paused) {
295 SLOG(LOG_DEBUG, tts_tag(), "[Server DEBUG] tts_pause is called. Do not request to play");
296 return TTSD_ERROR_NONE;
299 app_tts_state_e state = APP_STATE_CREATED;
300 if (0 != ttsd_data_get_client_state(uid, &state)) {
301 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
302 return TTSD_ERROR_INVALID_PARAMETER;
305 if (APP_STATE_PLAYING != state) {
306 SLOG(LOG_INFO, tts_tag(), "[Server] client state is not playing");
307 return TTSD_ERROR_NONE;
310 if (0 != ttsd_player_play(uid)) {
311 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
313 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
315 int tmp_pid = ttsd_data_get_pid(uid);
317 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] uid(%d) is already destroyed or invalid", uid);
319 /* Change ready state */
320 ttsd_server_stop(uid);
322 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
326 return TTSD_ERROR_NONE;
329 bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
331 /* clear client data */
332 ttsd_data_clear_data(uid);
334 if (APP_STATE_READY != state) {
335 ttsd_data_set_client_state(uid, APP_STATE_READY);
338 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
340 ttsd_data_delete_client(uid);
347 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
350 case TTS_CONFIG_TYPE_ENGINE:
352 /* TODO - Determine the policy when engine process get engine changed cb */
353 if (NULL == str_param) {
354 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
359 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
360 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
361 ret = ttsd_engine_agent_unload_current_engine();
363 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
366 ret = ttsd_engine_agent_load_current_engine();
368 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
373 /* stop all player */
374 ttsd_player_all_stop();
376 /* send interrupt message to all clients */
377 ttsd_data_foreach_clients(__get_client_cb, NULL);
379 ttsd_engine_cancel_synthesis();
384 case TTS_CONFIG_TYPE_VOICE:
386 if (NULL == str_param) {
387 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
391 char* out_lang = NULL;
395 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
396 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
397 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
399 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
401 /* Current language is not available */
402 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
404 if (NULL != out_lang) {
411 case TTS_CONFIG_TYPE_SPEED:
413 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
414 /* set default speed */
416 ret = ttsd_engine_agent_set_default_speed(int_param);
418 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
424 case TTS_CONFIG_TYPE_PITCH:
426 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
427 /* set default speed */
429 ret = ttsd_engine_agent_set_default_pitch(int_param);
431 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
437 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
439 if (0.0 <= double_param && double_param <= 1.0) {
440 /* set default bg volume ratio */
442 ret = ttsd_player_set_background_volume_ratio(double_param);
444 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
454 bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
456 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
457 ttsd_server_finalize(uid);
461 Eina_Bool ttsd_terminate_daemon(void *data)
463 ttsd_data_foreach_clients(__terminate_client, NULL);
464 g_terminate_timer = NULL;
468 void __screen_reader_changed_cb(bool value)
470 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
471 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
472 if (g_terminate_timer) {
473 ecore_timer_del(g_terminate_timer);
474 g_terminate_timer = NULL;
476 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
478 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
483 int ttsd_send_all_stop()
485 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
487 /* stop all player */
488 ttsd_player_all_stop();
490 /* send interrupt message to all clients */
491 ttsd_data_foreach_clients(__get_client_cb, NULL);
493 return TTSD_ERROR_NONE;
499 int ttsd_initialize(ttse_request_callback_s *callback)
501 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
503 if (ttsd_config_initialize(__config_changed_cb)) {
504 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
508 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
510 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
511 int ret = ttsd_player_set_background_volume_ratio(ratio);
513 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
516 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
519 if (ttsd_player_init()) {
520 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
521 return TTSD_ERROR_OPERATION_FAILED;
524 /* Engine Agent initialize */
525 if (0 != ttsd_engine_agent_init()) {
526 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
527 return TTSD_ERROR_OPERATION_FAILED;
530 /* set current engine */
531 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
532 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
533 // g_is_engine = false;
535 // g_is_engine = true;
537 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
538 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
539 return TTSD_ERROR_OPERATION_FAILED;
542 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
544 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
545 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
548 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
549 if (NULL == g_check_client_timer) {
550 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
553 return TTSD_ERROR_NONE;
558 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
561 if (0 < g_list_length(g_proc_list)) {
562 iter = g_list_first(g_proc_list);
563 while (NULL != iter) {
564 g_proc_list = g_list_remove_link(g_proc_list, iter);
566 iter = g_list_first(g_proc_list);
570 ttsd_config_finalize();
572 ttsd_player_release();
574 ttsd_engine_agent_release();
578 ecore_timer_del(g_wait_timer);
580 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
583 if (g_terminate_timer) {
584 ecore_timer_del(g_terminate_timer);
585 g_terminate_timer = NULL;
586 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
589 if (NULL != g_check_client_timer) {
590 ecore_timer_del(g_check_client_timer);
591 g_check_client_timer = NULL;
592 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
595 return TTSD_ERROR_NONE;
600 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
602 ttsd_terminate_daemon(NULL);
604 ttsd_ipc_close_connection();
605 ttsd_network_finalize();
608 return TTSD_ERROR_NONE;
612 * TTS Server Functions for Client
615 int ttsd_server_is_already_initialized(int pid, int uid, bool* is_initialized)
617 if (-1 != ttsd_data_is_client(uid))
618 *is_initialized = true;
620 *is_initialized = false;
622 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), Uid(%d) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
623 return TTSD_ERROR_NONE;
626 int ttsd_server_initialize(int pid, int uid, tts_ipc_method_e method, bool* credential_needed)
628 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
630 if (-1 != ttsd_data_is_client(uid)) {
631 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
632 return TTSD_ERROR_NONE;
635 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
636 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
637 return TTSD_ERROR_OPERATION_FAILED;
640 if (true == *credential_needed) {
642 if (0 != app_manager_get_app_id(pid, &appid)) {
643 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
645 bool is_agreed = false;
646 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
647 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
650 return TTSD_ERROR_OPERATION_FAILED;
655 if (false == is_agreed) {
656 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
657 return TTSD_ERROR_PERMISSION_DENIED;
661 if (0 != ttsd_data_new_client(pid, uid)) {
662 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
663 return TTSD_ERROR_OPERATION_FAILED;
666 if (0 != ttsd_data_set_ipc_method(uid, method)) {
667 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
668 return TTSD_ERROR_OPERATION_FAILED;
671 if (0 != ttsd_player_create_instance(uid)) {
672 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
673 return TTSD_ERROR_OPERATION_FAILED;
677 g_min_latency = 10000.0;
682 return TTSD_ERROR_NONE;
685 static Eina_Bool __quit_ecore_loop(void *data)
687 ttsd_ipc_close_connection();
688 ttsd_network_finalize();
690 ecore_main_loop_quit();
696 static void __read_proc()
699 struct dirent *dirp = NULL;
703 if (0 < g_list_length(g_proc_list)) {
704 iter = g_list_first(g_proc_list);
705 while (NULL != iter) {
706 g_proc_list = g_list_remove_link(g_proc_list, iter);
708 iter = g_list_first(g_proc_list);
712 dp = opendir("/proc");
714 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
720 tmp = atoi(dirp->d_name);
721 if (0 >= tmp) continue;
722 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
724 } while (NULL != dirp);
730 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
736 for (i = 0; i < g_list_length(g_proc_list); i++) {
737 iter = g_list_nth(g_proc_list, i);
739 if (pid == GPOINTER_TO_INT(iter->data)) {
740 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
747 if (false == exist) {
748 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
749 ttsd_server_finalize(uid);
754 char appid[1024] = {0, };
755 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
756 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
759 if (0 < strlen(appid)) {
760 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
762 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
765 result = ttsdc_send_hello(pid, uid);
768 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
769 ttsd_server_finalize(uid);
770 } else if (-1 == result) {
771 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
779 Eina_Bool ttsd_cleanup_client(void *data)
781 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
784 if (0 < ttsd_data_get_client_count()) {
785 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
787 ecore_timer_add(0, __quit_ecore_loop, NULL);
788 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
789 g_check_client_timer = NULL;
793 SLOG(LOG_DEBUG, tts_tag(), "@@@");
798 void __used_voice_cb(const char* lang, int type)
800 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
801 if (0 != ttsd_engine_unload_voice(lang, type)) {
802 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
806 int ttsd_server_finalize(int uid)
808 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
809 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);
811 app_tts_state_e state;
812 if (0 > ttsd_data_get_client_state(uid, &state)) {
813 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
816 ttsd_server_stop(uid);
817 ttsd_player_clear(uid);
819 ttsd_player_destroy_instance(uid);
821 /* Need to unload voice when used voice is unregistered */
822 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
823 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
824 return TTSD_ERROR_OPERATION_FAILED;
827 ttsd_data_delete_client(uid);
829 /* unload engine, if ref count of client is 0 */
830 if (0 == ttsd_data_get_client_count()) {
831 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
832 ecore_timer_add(0, __quit_ecore_loop, NULL);
835 return TTSD_ERROR_NONE;
838 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
840 app_tts_state_e state;
841 if (0 > ttsd_data_get_client_state(uid, &state)) {
842 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
843 return TTSD_ERROR_INVALID_PARAMETER;
846 /* check valid voice */
847 char* temp_lang = NULL;
849 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
850 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
851 if (NULL != temp_lang) {
855 return TTSD_ERROR_INVALID_VOICE;
858 if (NULL == temp_lang) {
859 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
860 return TTSD_ERROR_INVALID_VOICE;
863 speak_data_s* speak_data = NULL;
864 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
865 if (NULL == speak_data) {
866 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
867 if (NULL != temp_lang) {
871 return TTSD_ERROR_OPERATION_FAILED;
874 speak_data->lang = strdup(lang);
875 speak_data->vctype = voice_type;
877 speak_data->speed = speed;
878 speak_data->utt_id = utt_id;
880 speak_data->text = strdup(text);
882 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);
884 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
886 ret = ttsd_data_add_speak_data(uid, speak_data);
888 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
889 if (NULL != temp_lang) {
893 if (NULL != speak_data) {
894 if (NULL != speak_data->lang) free(speak_data->lang);
895 if (NULL != speak_data->text) free(speak_data->text);
897 speak_data->lang = NULL;
898 speak_data->text = NULL;
907 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
908 /* Request load voice */
909 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
910 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
911 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
915 if (NULL != temp_lang) {
920 if (APP_STATE_PLAYING == state) {
921 /* check if engine use network */
922 if (ttsd_engine_agent_need_network()) {
923 if (false == ttsd_network_is_connected()) {
924 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
925 return TTSD_ERROR_OPERATION_FAILED;
929 /* Check whether tts-engine is running or not */
930 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
931 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
933 __synthesis(uid, credential);
937 return TTSD_ERROR_NONE;
940 Eina_Bool __send_interrupt_client(void *data)
942 intptr_t puid = (intptr_t)data;
945 int pid = ttsd_data_get_pid(uid);
947 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%d), pid(%d)", uid, pid);
951 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
952 /* send message to client about changing state */
953 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
955 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
961 int ttsd_server_play(int uid, const char* credential)
963 app_tts_state_e state;
964 if (0 > ttsd_data_get_client_state(uid, &state)) {
965 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
966 return TTSD_ERROR_INVALID_PARAMETER;
969 if (APP_STATE_PLAYING == state) {
970 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
971 return TTSD_ERROR_NONE;
974 /* check if engine use network */
975 if (ttsd_engine_agent_need_network()) {
976 if (false == ttsd_network_is_connected()) {
977 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
978 return TTSD_ERROR_OUT_OF_NETWORK;
982 /* check the current playback focus */
983 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
984 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
985 ttsd_data_clear_data(uid);
986 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
989 int current_uid = ttsd_data_get_current_playing();
990 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
992 if (uid != current_uid && -1 != current_uid) {
993 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
994 /* Send interrupt message */
995 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
998 if (0 != ttsd_server_stop(current_uid)) {
999 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
1002 intptr_t pcurrent_uid = (intptr_t)current_uid;
1003 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1005 /* Default mode policy of interrupt is "Pause" */
1007 /* Send interrupt message */
1008 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1011 if (0 != ttsd_player_pause(current_uid)) {
1012 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1016 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1018 intptr_t pcurrent_uid = (intptr_t)current_uid;
1019 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1023 /* Change current play */
1024 if (0 != ttsd_player_wait_to_play(uid)) {
1025 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%d)", uid);
1028 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1029 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1030 return TTSD_ERROR_OPERATION_FAILED;
1033 if (APP_STATE_PAUSED == state) {
1034 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1036 g_is_paused = false;
1039 if (0 != ttsd_player_resume(uid)) {
1040 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1044 /* Check whether tts-engine is running or not */
1045 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1046 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
1047 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1049 __synthesis(uid, credential);
1052 return TTSD_ERROR_NONE;
1055 int ttsd_server_stop(int uid)
1057 app_tts_state_e state;
1058 if (0 > ttsd_data_get_client_state(uid, &state)) {
1059 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1060 return TTSD_ERROR_INVALID_PARAMETER;
1063 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1065 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1066 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
1067 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1070 ret = ttsd_engine_cancel_synthesis();
1072 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1074 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1077 if (0 != ttsd_player_stop(uid)) {
1078 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1081 ttsd_data_set_client_state(uid, APP_STATE_READY);
1084 /* Reset all data */
1085 ttsd_data_clear_data(uid);
1086 g_is_paused = false;
1088 return TTSD_ERROR_NONE;
1091 int ttsd_server_pause(int uid, int* utt_id)
1093 app_tts_state_e state;
1094 if (0 > ttsd_data_get_client_state(uid, &state)) {
1095 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1096 return TTSD_ERROR_INVALID_PARAMETER;
1099 if (APP_STATE_PLAYING != state) {
1100 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1101 return TTSD_ERROR_INVALID_STATE;
1106 *utt_id = g_utt.uttid;
1107 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
1110 ret = ttsd_player_pause(uid);
1112 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1113 return TTSD_ERROR_OPERATION_FAILED;
1116 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1118 return TTSD_ERROR_NONE;
1121 int ttsd_server_get_support_voices(int uid, GList** voice_list)
1123 app_tts_state_e state;
1124 if (0 > ttsd_data_get_client_state(uid, &state)) {
1125 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1126 return TTSD_ERROR_INVALID_PARAMETER;
1130 int ret = ttsd_engine_get_voice_list(voice_list);
1132 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1136 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1138 return TTSD_ERROR_NONE;
1141 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
1143 app_tts_state_e state;
1144 if (0 > ttsd_data_get_client_state(uid, &state)) {
1145 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1146 return TTSD_ERROR_INVALID_PARAMETER;
1149 /* get current voice */
1150 int ret = ttsd_engine_get_default_voice(language, voice_type);
1152 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1156 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1158 return TTSD_ERROR_NONE;
1161 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1163 app_tts_state_e state;
1164 if (0 > ttsd_data_get_client_state(uid, &state)) {
1165 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1166 return TTSD_ERROR_INVALID_PARAMETER;
1169 if (APP_STATE_READY != state) {
1170 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1171 return TTSD_ERROR_INVALID_STATE;
1174 int ret = ttsd_engine_set_private_data(key, data);
1176 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1178 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1184 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1186 app_tts_state_e state;
1187 if (0 > ttsd_data_get_client_state(uid, &state)) {
1188 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1189 return TTSD_ERROR_INVALID_PARAMETER;
1192 if (APP_STATE_READY != state) {
1193 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1194 return TTSD_ERROR_INVALID_STATE;
1197 int ret = ttsd_engine_get_private_data(key, data);
1199 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1201 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1207 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1209 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1212 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1214 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1220 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1222 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1225 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1227 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1233 int ttsd_server_play_pcm(int uid)
1235 app_tts_state_e state;
1236 if (0 > ttsd_data_get_client_state(uid, &state)) {
1237 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1238 return TTSD_ERROR_INVALID_PARAMETER;
1241 if (APP_STATE_PLAYING == state) {
1242 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1243 return TTSD_ERROR_NONE;
1246 int current_uid = ttsd_data_get_current_playing();
1247 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1249 if (uid != current_uid && -1 != current_uid) {
1250 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1251 /* Send interrupt message */
1252 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1255 if (0 != ttsd_server_stop(current_uid)) {
1256 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
1259 intptr_t pcurrent_uid = (intptr_t)current_uid;
1260 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1262 /* Default mode policy of interrupt is "Pause" */
1264 /* Send interrupt message */
1265 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1268 if (0 != ttsd_player_pause(current_uid)) {
1269 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1273 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1275 intptr_t pcurrent_uid = (intptr_t)current_uid;
1276 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1280 /* Change current play */
1281 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1282 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1283 return TTSD_ERROR_OPERATION_FAILED;
1286 if (APP_STATE_PAUSED == state) {
1287 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1290 if (0 != ttsd_player_resume(uid)) {
1291 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1294 if (0 != ttsd_player_play_pcm(uid)) {
1295 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1297 // Change ready state
1298 ttsd_server_stop_pcm(uid);
1300 int tmp_pid = ttsd_data_get_pid(uid);
1302 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%d)", uid);
1304 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1309 return TTSD_ERROR_NONE;
1312 int ttsd_server_stop_pcm(int uid)
1314 app_tts_state_e state;
1315 if (0 > ttsd_data_get_client_state(uid, &state)) {
1316 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1317 return TTSD_ERROR_INVALID_PARAMETER;
1320 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1322 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1323 ttsd_data_set_client_state(uid, APP_STATE_READY);
1326 /* Reset all data */
1327 ttsd_data_clear_data(uid);
1329 ttsd_player_stop(uid);
1332 return TTSD_ERROR_NONE;
1335 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1337 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1341 /* Synthesis is success */
1342 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1343 if (TTSE_RESULT_EVENT_START == event) {
1344 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1345 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1346 uid, uttid, data, data_size, audio_type, rate);
1347 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1348 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1349 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1350 uid, uttid, data, data_size, audio_type, rate);
1352 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1355 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1356 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1357 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1358 return TTSD_ERROR_INVALID_PARAMETER;
1362 sound_data_s* temp_sound_data = NULL;
1363 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1364 if (NULL == temp_sound_data) {
1365 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1366 return TTSD_ERROR_OUT_OF_MEMORY;
1369 temp_sound_data->data = NULL;
1370 temp_sound_data->rate = 0;
1371 temp_sound_data->data_size = 0;
1373 if (0 < data_size) {
1374 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1375 if (NULL != temp_sound_data->data) {
1376 memcpy(temp_sound_data->data, data, data_size);
1377 temp_sound_data->data_size = data_size;
1378 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1379 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1381 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1384 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1387 temp_sound_data->utt_id = uttid;
1388 temp_sound_data->event = event;
1389 temp_sound_data->audio_type = audio_type;
1390 temp_sound_data->rate = rate;
1392 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1393 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1395 if (NULL != temp_sound_data->data) {
1396 free(temp_sound_data->data);
1397 temp_sound_data->data = NULL;
1400 free(temp_sound_data);
1401 temp_sound_data = NULL;
1404 /* if (0 != ttsd_player_play(uid)) {
1405 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1407 // Change ready state
1408 ttsd_server_stop(uid);
1411 tmp_pid = ttsd_data_get_pid(uid);
1412 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1416 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1419 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1421 return TTSD_ERROR_NONE;