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;
54 static Ecore_Timer* g_quit_loop_timer = NULL;
56 static utterance_t g_utt;
58 static GList *g_proc_list = NULL;
60 static bool g_is_paused;
62 static bool g_is_terminated = false;
65 /* Function definitions */
66 static int __synthesis(unsigned int uid, const char* credential);
68 static Eina_Bool __wait_synthesis(void *data)
70 /* get current play */
71 char* credential = (char*)data;
72 unsigned int uid = ttsd_data_get_current_playing();
75 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
76 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
79 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
81 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
82 /* Start next synthesis */
83 __synthesis(uid, credential);
93 static int __synthesis(unsigned int uid, const char* credential)
95 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
97 speak_data_s* speak_data = NULL;
98 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
99 if (NULL == speak_data) {
100 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
104 int pid = ttsd_data_get_pid(uid);
106 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
110 char appid[1024] = {0, };
111 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
112 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
115 if (NULL == speak_data->lang || NULL == speak_data->text) {
116 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
117 ttsd_server_stop(uid);
119 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
121 ttsd_data_clear_speak_data(&speak_data);
127 g_utt.uttid = speak_data->utt_id;
129 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
130 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
131 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
132 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
133 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
134 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
137 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
138 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
140 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
142 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
144 ttsd_server_stop(uid);
146 int pid = ttsd_data_get_pid(uid);
148 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
150 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
153 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
156 ttsd_data_clear_speak_data(&speak_data);
158 ttsd_data_clear_speak_data(&speak_data);
161 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
167 * TTS Server Callback Functions
169 int ttsd_send_error(ttse_error_e error, const char* msg)
171 unsigned int uid = g_utt.uid;
172 int uttid = g_utt.uttid;
173 int tmp_pid = ttsd_data_get_pid(uid);
175 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));
177 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
179 if (0 != ttsd_player_clear(uid))
180 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
182 if (0 != ttsd_data_clear_data(uid))
183 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
186 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
187 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
190 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
191 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
194 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
195 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
200 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)
202 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
204 unsigned int uid = g_utt.uid;
205 int uttid = g_utt.uttid;
207 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
208 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
209 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
210 SLOG(LOG_DEBUG, tts_tag(), "@@@");
211 return TTSD_ERROR_INVALID_PARAMETER;
214 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
215 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
216 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
217 return TTSD_ERROR_INVALID_PARAMETER;
220 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
221 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
222 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
223 SLOG(LOG_DEBUG, tts_tag(), "@@@");
224 return TTSD_ERROR_INVALID_PARAMETER;
227 /* check the current state */
228 app_tts_state_e state;
229 if (0 > ttsd_data_get_client_state(uid, &state)) {
230 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
231 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
232 return TTSD_ERROR_INVALID_PARAMETER;
235 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
236 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
237 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
238 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
239 return TTSD_ERROR_NONE;
242 /* Synthesis is success */
243 if (TTSE_RESULT_EVENT_START == event) {
244 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
245 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
246 uid, uttid, data, data_size, audio_type, rate);
248 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
249 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
250 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
251 double d_latency = (double)latency;
252 g_avg_latency += d_latency;
253 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
254 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
256 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);
257 } else if (TTSE_RESULT_EVENT_FINISH == event) {
258 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
259 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
260 uid, uttid, data, data_size, audio_type, rate);
262 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
266 sound_data_s* temp_sound_data = NULL;
267 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
268 if (NULL == temp_sound_data) {
269 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
270 return TTSD_ERROR_OUT_OF_MEMORY;
273 temp_sound_data->data = NULL;
274 temp_sound_data->rate = 0;
275 temp_sound_data->data_size = 0;
277 if (NULL != data && 0 < data_size) {
278 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
279 if (NULL != temp_sound_data->data) {
280 memcpy(temp_sound_data->data, data, data_size);
281 temp_sound_data->data_size = data_size;
282 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%u), event(%d) sound_data(%p) data(%p) size(%d)",
283 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
285 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
288 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
291 temp_sound_data->utt_id = uttid;
292 temp_sound_data->event = event;
293 temp_sound_data->audio_type = audio_type;
294 temp_sound_data->rate = rate;
296 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
297 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
298 if (NULL != temp_sound_data->data) {
299 free(temp_sound_data->data);
300 temp_sound_data->data = NULL;
303 free(temp_sound_data);
304 temp_sound_data = NULL;
306 return TTSD_ERROR_OPERATION_FAILED;
309 if (event == TTSE_RESULT_EVENT_FINISH) {
310 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
313 return TTSD_ERROR_NONE;
316 bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
318 /* clear client data */
319 ttsd_data_clear_data(uid);
321 if (APP_STATE_READY != state) {
322 ttsd_data_set_client_state(uid, APP_STATE_READY);
325 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
327 ttsd_data_delete_client(uid);
334 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
337 case TTS_CONFIG_TYPE_ENGINE:
339 /* TODO - Determine the policy when engine process get engine changed cb */
340 if (NULL == str_param) {
341 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
346 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
347 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
348 ret = ttsd_engine_agent_unload_current_engine();
350 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
353 ret = ttsd_engine_agent_load_current_engine();
355 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
360 /* stop all player */
361 ttsd_player_all_stop();
363 /* send interrupt message to all clients */
364 ttsd_data_foreach_clients(__get_client_cb, NULL);
366 ttsd_engine_cancel_synthesis();
371 case TTS_CONFIG_TYPE_VOICE:
373 if (NULL == str_param) {
374 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
378 char* out_lang = NULL;
382 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
383 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
384 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
386 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
388 /* Current language is not available */
389 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
391 if (NULL != out_lang) {
398 case TTS_CONFIG_TYPE_SPEED:
400 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
401 /* set default speed */
403 ret = ttsd_engine_agent_set_default_speed(int_param);
405 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
411 case TTS_CONFIG_TYPE_PITCH:
413 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
414 /* set default speed */
416 ret = ttsd_engine_agent_set_default_pitch(int_param);
418 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
424 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
426 if (0.0 <= double_param && double_param <= 1.0) {
427 /* set default bg volume ratio */
429 ret = ttsd_player_set_background_volume_ratio(double_param);
431 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
441 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
443 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
444 ttsd_server_finalize(uid);
448 static void __terminate_server()
450 if (g_is_terminated) {
451 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
455 g_is_terminated = true;
457 ttsd_ipc_close_connection();
458 ttsd_network_finalize();
462 static Eina_Bool __quit_ecore_loop(void *data)
464 __terminate_server();
465 ecore_main_loop_quit();
470 Eina_Bool ttsd_terminate_daemon(void *data)
472 ttsd_data_foreach_clients(__terminate_client, NULL);
474 if (g_quit_loop_timer) {
475 ecore_timer_del(g_quit_loop_timer);
476 g_quit_loop_timer = NULL;
477 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
480 __terminate_server();
482 g_terminate_timer = NULL;
486 void __screen_reader_changed_cb(bool value)
488 /* if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
489 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
490 if (g_terminate_timer) {
491 ecore_timer_del(g_terminate_timer);
492 g_terminate_timer = NULL;
494 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
497 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
502 int ttsd_send_all_stop()
504 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
506 /* stop all player */
507 ttsd_player_all_stop();
509 /* send interrupt message to all clients */
510 ttsd_data_foreach_clients(__get_client_cb, NULL);
512 return TTSD_ERROR_NONE;
518 int ttsd_initialize(ttse_request_callback_s *callback)
520 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
521 g_is_terminated = false;
523 if (ttsd_config_initialize(__config_changed_cb)) {
524 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
528 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
530 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
531 int ret = ttsd_player_set_background_volume_ratio(ratio);
533 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
536 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
539 if (ttsd_player_init()) {
540 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
541 return TTSD_ERROR_OPERATION_FAILED;
544 /* Engine Agent initialize */
545 if (0 != ttsd_engine_agent_init()) {
546 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
547 return TTSD_ERROR_OPERATION_FAILED;
550 /* set current engine */
551 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
552 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
553 // g_is_engine = false;
555 // g_is_engine = true;
557 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
558 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
559 return TTSD_ERROR_OPERATION_FAILED;
562 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
564 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
565 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
568 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
569 if (NULL == g_check_client_timer) {
570 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
573 return TTSD_ERROR_NONE;
578 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
581 if (0 < g_list_length(g_proc_list)) {
582 iter = g_list_first(g_proc_list);
583 while (NULL != iter) {
584 g_proc_list = g_list_remove_link(g_proc_list, iter);
586 iter = g_list_first(g_proc_list);
590 ttsd_config_finalize();
592 ttsd_player_release();
594 ttsd_engine_agent_release();
598 ecore_timer_del(g_wait_timer);
600 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
603 if (g_terminate_timer) {
604 ecore_timer_del(g_terminate_timer);
605 g_terminate_timer = NULL;
606 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
609 if (g_check_client_timer) {
610 ecore_timer_del(g_check_client_timer);
611 g_check_client_timer = NULL;
612 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
615 if (g_quit_loop_timer) {
616 ecore_timer_del(g_quit_loop_timer);
617 g_quit_loop_timer = NULL;
618 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
621 return TTSD_ERROR_NONE;
626 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
628 ttsd_terminate_daemon(NULL);
630 return TTSD_ERROR_NONE;
634 * TTS Server Functions for Client
637 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
639 if (-1 != ttsd_data_is_client(uid))
640 *is_initialized = true;
642 *is_initialized = false;
644 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
645 return TTSD_ERROR_NONE;
648 int ttsd_server_initialize(int pid, unsigned int uid, tts_ipc_method_e method, bool* credential_needed)
650 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
652 if (-1 != ttsd_data_is_client(uid)) {
653 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
654 return TTSD_ERROR_NONE;
657 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
658 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
659 return TTSD_ERROR_OPERATION_FAILED;
662 if (true == *credential_needed) {
664 if (0 != app_manager_get_app_id(pid, &appid)) {
665 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
667 bool is_agreed = false;
668 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
669 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
672 return TTSD_ERROR_OPERATION_FAILED;
677 if (false == is_agreed) {
678 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
679 return TTSD_ERROR_PERMISSION_DENIED;
683 if (0 != ttsd_data_new_client(pid, uid)) {
684 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
685 return TTSD_ERROR_OPERATION_FAILED;
688 if (0 != ttsd_data_set_ipc_method(uid, method)) {
689 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
690 return TTSD_ERROR_OPERATION_FAILED;
693 if (0 != ttsd_player_create_instance(uid)) {
694 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
695 return TTSD_ERROR_OPERATION_FAILED;
699 g_min_latency = 10000.0;
704 return TTSD_ERROR_NONE;
707 static void __read_proc()
710 struct dirent *dirp = NULL;
714 if (0 < g_list_length(g_proc_list)) {
715 iter = g_list_first(g_proc_list);
716 while (NULL != iter) {
717 g_proc_list = g_list_remove_link(g_proc_list, iter);
719 iter = g_list_first(g_proc_list);
723 dp = opendir("/proc");
725 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
731 tmp = atoi(dirp->d_name);
732 if (0 >= tmp) continue;
733 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
735 } while (NULL != dirp);
741 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
747 for (i = 0; i < g_list_length(g_proc_list); i++) {
748 iter = g_list_nth(g_proc_list, i);
750 if (pid == GPOINTER_TO_INT(iter->data)) {
751 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
758 if (false == exist) {
759 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
760 ttsd_server_finalize(uid);
765 char appid[1024] = {0, };
766 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
767 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
770 if (0 < strlen(appid)) {
771 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
773 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
776 result = ttsdc_send_hello(pid, uid);
779 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) should be removed.", uid);
780 ttsd_server_finalize(uid);
781 } else if (-1 == result) {
782 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
790 Eina_Bool ttsd_cleanup_client(void *data)
792 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
795 if (0 < ttsd_data_get_client_count()) {
796 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
798 if (NULL == g_quit_loop_timer) {
799 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
800 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
802 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
803 g_check_client_timer = NULL;
807 SLOG(LOG_DEBUG, tts_tag(), "@@@");
812 void __used_voice_cb(const char* lang, int type)
814 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
815 if (0 != ttsd_engine_unload_voice(lang, type)) {
816 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
820 int ttsd_server_finalize(unsigned int uid)
822 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
823 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);
825 app_tts_state_e state;
826 if (0 > ttsd_data_get_client_state(uid, &state)) {
827 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
830 ttsd_server_stop(uid);
831 ttsd_player_clear(uid);
833 ttsd_player_destroy_instance(uid);
835 /* Need to unload voice when used voice is unregistered */
836 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
837 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
838 return TTSD_ERROR_OPERATION_FAILED;
841 ttsd_data_delete_client(uid);
843 /* unload engine, if ref count of client is 0 */
844 if (0 == ttsd_data_get_client_count()) {
845 if (NULL == g_quit_loop_timer) {
846 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
847 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
849 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
853 return TTSD_ERROR_NONE;
856 int ttsd_server_set_mode(unsigned int uid, ttsd_mode_e mode)
858 app_tts_state_e state;
859 if (0 > ttsd_data_get_client_state(uid, &state)) {
860 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
861 return TTSD_ERROR_INVALID_PARAMETER;
864 if (APP_STATE_READY != state) {
865 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
866 return TTSD_ERROR_INVALID_STATE;
869 int ret = ttsd_data_set_mode(uid, mode);
871 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
873 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%u), mode(%d)", uid, mode);
879 int ttsd_server_add_queue(unsigned int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
881 app_tts_state_e state;
882 if (0 > ttsd_data_get_client_state(uid, &state)) {
883 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
884 return TTSD_ERROR_INVALID_PARAMETER;
887 /* check valid voice */
888 char* temp_lang = NULL;
890 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
891 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
892 if (NULL != temp_lang) {
896 return TTSD_ERROR_INVALID_VOICE;
899 if (NULL == temp_lang) {
900 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
901 return TTSD_ERROR_INVALID_VOICE;
904 speak_data_s* speak_data = NULL;
905 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
906 if (NULL == speak_data) {
907 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
908 if (NULL != temp_lang) {
912 return TTSD_ERROR_OPERATION_FAILED;
915 speak_data->lang = strdup(lang);
916 speak_data->vctype = voice_type;
918 speak_data->speed = speed;
919 speak_data->utt_id = utt_id;
921 speak_data->text = strdup(text);
923 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);
925 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
927 ret = ttsd_data_add_speak_data(uid, speak_data);
929 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
930 if (NULL != temp_lang) {
934 if (NULL != speak_data) {
935 if (NULL != speak_data->lang) free(speak_data->lang);
936 if (NULL != speak_data->text) free(speak_data->text);
938 speak_data->lang = NULL;
939 speak_data->text = NULL;
948 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
949 /* Request load voice */
950 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
951 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
952 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
956 if (NULL != temp_lang) {
961 if (APP_STATE_PLAYING == state) {
962 /* check if engine use network */
963 if (ttsd_engine_agent_need_network()) {
964 if (false == ttsd_network_is_connected()) {
965 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
966 return TTSD_ERROR_OPERATION_FAILED;
970 /* Check whether tts-engine is running or not */
971 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
972 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
973 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
974 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
976 __synthesis(uid, credential);
980 return TTSD_ERROR_NONE;
983 Eina_Bool __send_interrupt_client(void *data)
985 unsigned int uid = (uintptr_t)data;
986 int pid = ttsd_data_get_pid(uid);
988 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
992 ttsd_mode_e mode = ttsd_data_get_mode(uid);
993 if (TTSD_MODE_DEFAULT != mode) {
994 /* send message to client about changing state */
995 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
997 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
1003 int ttsd_server_play(unsigned int uid, const char* credential)
1005 app_tts_state_e state;
1006 if (0 > ttsd_data_get_client_state(uid, &state)) {
1007 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1008 return TTSD_ERROR_INVALID_PARAMETER;
1011 if (APP_STATE_PLAYING == state) {
1012 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1013 return TTSD_ERROR_NONE;
1016 /* check if engine use network */
1017 if (ttsd_engine_agent_need_network()) {
1018 if (false == ttsd_network_is_connected()) {
1019 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
1020 return TTSD_ERROR_OUT_OF_NETWORK;
1024 /* check the current playback focus */
1025 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
1026 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
1027 ttsd_data_clear_data(uid);
1028 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1031 unsigned int current_uid = ttsd_data_get_current_playing();
1032 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1034 if (uid != current_uid && -1 != current_uid) {
1035 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1036 /* Send interrupt message */
1037 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1040 if (0 != ttsd_server_stop(current_uid)) {
1041 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1044 uintptr_t p_current_uid = (uintptr_t)current_uid;
1045 ecore_timer_add(0, __send_interrupt_client, (void*)p_current_uid);
1047 /* Default mode policy of interrupt is "Pause" */
1049 /* Send interrupt message */
1050 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1053 if (0 != ttsd_player_pause(current_uid)) {
1054 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%u)", current_uid);
1058 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1060 uintptr_t p_current_uid = (uintptr_t)current_uid;
1061 ecore_timer_add(0, __send_interrupt_client, (void*)p_current_uid);
1065 /* Change current play */
1066 if (0 != ttsd_player_wait_to_play(uid)) {
1067 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1070 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1071 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1072 return TTSD_ERROR_OPERATION_FAILED;
1075 if (APP_STATE_PAUSED == state) {
1076 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1078 g_is_paused = false;
1081 if (0 != ttsd_player_resume(uid)) {
1082 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1085 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1087 if (0 != ttsd_player_play(uid)) {
1088 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_play()");
1092 /* Check whether tts-engine is running or not */
1093 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1094 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1095 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1096 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1097 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1099 __synthesis(uid, credential);
1102 return TTSD_ERROR_NONE;
1105 int ttsd_server_stop(unsigned int uid)
1107 app_tts_state_e state;
1108 if (0 > ttsd_data_get_client_state(uid, &state)) {
1109 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1110 return TTSD_ERROR_INVALID_PARAMETER;
1113 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1115 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1116 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1117 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1118 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1119 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1122 ret = ttsd_engine_cancel_synthesis();
1124 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1126 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1129 if (0 != ttsd_player_stop(uid)) {
1130 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1133 ttsd_data_set_client_state(uid, APP_STATE_READY);
1136 /* Reset all data */
1137 ttsd_data_clear_data(uid);
1138 g_is_paused = false;
1140 return TTSD_ERROR_NONE;
1143 int ttsd_server_pause(unsigned int uid, int* utt_id)
1145 app_tts_state_e state;
1146 if (0 > ttsd_data_get_client_state(uid, &state)) {
1147 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1148 return TTSD_ERROR_INVALID_PARAMETER;
1151 if (APP_STATE_PLAYING != state) {
1152 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1153 return TTSD_ERROR_INVALID_STATE;
1158 *utt_id = g_utt.uttid;
1159 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%u), g_uid(%u), utt_id(%d)", uid, g_utt.uid, *utt_id);
1162 ret = ttsd_player_pause(uid);
1164 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1165 return TTSD_ERROR_OPERATION_FAILED;
1168 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1170 return TTSD_ERROR_NONE;
1173 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1175 app_tts_state_e state;
1176 if (0 > ttsd_data_get_client_state(uid, &state)) {
1177 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1178 return TTSD_ERROR_INVALID_PARAMETER;
1182 int ret = ttsd_engine_get_voice_list(voice_list);
1184 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1188 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1190 return TTSD_ERROR_NONE;
1193 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1195 app_tts_state_e state;
1196 if (0 > ttsd_data_get_client_state(uid, &state)) {
1197 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1198 return TTSD_ERROR_INVALID_PARAMETER;
1201 /* get current voice */
1202 int ret = ttsd_engine_get_default_voice(language, voice_type);
1204 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1208 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1210 return TTSD_ERROR_NONE;
1213 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1215 app_tts_state_e state;
1216 if (0 > ttsd_data_get_client_state(uid, &state)) {
1217 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1218 return TTSD_ERROR_INVALID_PARAMETER;
1221 if (APP_STATE_READY != state) {
1222 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1223 return TTSD_ERROR_INVALID_STATE;
1226 int ret = ttsd_engine_set_private_data(key, data);
1228 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1230 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1236 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1238 app_tts_state_e state;
1239 if (0 > ttsd_data_get_client_state(uid, &state)) {
1240 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1241 return TTSD_ERROR_INVALID_PARAMETER;
1244 if (APP_STATE_READY != state) {
1245 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1246 return TTSD_ERROR_INVALID_STATE;
1249 int ret = ttsd_engine_get_private_data(key, data);
1251 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1253 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1259 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1261 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1264 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1266 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1272 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1274 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1277 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1279 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1285 int ttsd_server_play_pcm(unsigned int uid)
1287 app_tts_state_e state;
1288 if (0 > ttsd_data_get_client_state(uid, &state)) {
1289 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1290 return TTSD_ERROR_INVALID_PARAMETER;
1293 if (APP_STATE_PLAYING == state) {
1294 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1295 return TTSD_ERROR_NONE;
1298 unsigned int current_uid = ttsd_data_get_current_playing();
1299 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1301 if (uid != current_uid && -1 != current_uid) {
1302 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1303 /* Send interrupt message */
1304 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1307 if (0 != ttsd_server_stop(current_uid)) {
1308 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1311 uintptr_t p_current_uid = (uintptr_t)current_uid;
1312 ecore_timer_add(0, __send_interrupt_client, (void*)p_current_uid);
1314 /* Default mode policy of interrupt is "Pause" */
1316 /* Send interrupt message */
1317 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1320 if (0 != ttsd_player_pause(current_uid)) {
1321 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%u)", current_uid);
1325 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1327 uintptr_t p_current_uid = (uintptr_t)current_uid;
1328 ecore_timer_add(0, __send_interrupt_client, (void*)p_current_uid);
1332 /* Change current play */
1333 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1334 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1335 return TTSD_ERROR_OPERATION_FAILED;
1338 if (APP_STATE_PAUSED == state) {
1339 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1342 if (0 != ttsd_player_resume(uid)) {
1343 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1346 if (0 != ttsd_player_play(uid)) {
1347 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%u)", uid);
1349 // Change ready state
1350 ttsd_server_stop_pcm(uid);
1352 int tmp_pid = ttsd_data_get_pid(uid);
1354 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
1356 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1361 return TTSD_ERROR_NONE;
1364 int ttsd_server_stop_pcm(unsigned int uid)
1366 app_tts_state_e state;
1367 if (0 > ttsd_data_get_client_state(uid, &state)) {
1368 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1369 return TTSD_ERROR_INVALID_PARAMETER;
1372 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1374 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1375 ttsd_data_set_client_state(uid, APP_STATE_READY);
1378 /* Reset all data */
1379 ttsd_data_clear_data(uid);
1381 ttsd_player_stop(uid);
1384 return TTSD_ERROR_NONE;
1387 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1389 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1393 /* Synthesis is success */
1394 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1395 if (TTSE_RESULT_EVENT_START == event) {
1396 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1397 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1398 uid, uttid, data, data_size, audio_type, rate);
1399 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1400 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1401 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1402 uid, uttid, data, data_size, audio_type, rate);
1404 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1407 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1408 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1409 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1410 return TTSD_ERROR_INVALID_PARAMETER;
1414 sound_data_s* temp_sound_data = NULL;
1415 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1416 if (NULL == temp_sound_data) {
1417 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1418 return TTSD_ERROR_OUT_OF_MEMORY;
1421 temp_sound_data->data = NULL;
1422 temp_sound_data->rate = 0;
1423 temp_sound_data->data_size = 0;
1425 if (0 < data_size) {
1426 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1427 if (NULL != temp_sound_data->data) {
1428 memcpy(temp_sound_data->data, data, data_size);
1429 temp_sound_data->data_size = data_size;
1430 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%u), event(%d) sound_data(%p) data(%p) size(%d)",
1431 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1433 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1436 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1439 temp_sound_data->utt_id = uttid;
1440 temp_sound_data->event = event;
1441 temp_sound_data->audio_type = audio_type;
1442 temp_sound_data->rate = rate;
1444 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1445 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1447 if (NULL != temp_sound_data->data) {
1448 free(temp_sound_data->data);
1449 temp_sound_data->data = NULL;
1452 free(temp_sound_data);
1453 temp_sound_data = NULL;
1456 /* if (0 != ttsd_player_play(uid)) {
1457 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1459 // Change ready state
1460 ttsd_server_stop(uid);
1463 tmp_pid = ttsd_data_get_pid(uid);
1464 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1468 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1471 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1473 return TTSD_ERROR_NONE;