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);
68 static Eina_Bool __wait_synthesis(void *data)
70 /* get current play */
71 unsigned int uid = ttsd_data_get_current_playing();
74 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
75 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
78 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
80 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
81 /* Start next synthesis */
92 static int __synthesis(unsigned int uid)
94 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
96 speak_data_s* speak_data = NULL;
97 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
98 if (NULL == speak_data) {
99 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
103 int pid = ttsd_data_get_pid(uid);
105 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
109 char appid[1024] = {0, };
110 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
111 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
114 if (NULL == speak_data->lang || NULL == speak_data->text) {
115 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
116 ttsd_server_stop(uid);
118 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
120 ttsd_data_clear_speak_data(&speak_data);
126 g_utt.uttid = speak_data->utt_id;
128 char* credential = ttsd_data_get_credential(uid);
130 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
131 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
132 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
133 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
134 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
135 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
138 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
139 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
141 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
143 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
145 ttsd_server_stop(uid);
147 int pid = ttsd_data_get_pid(uid);
149 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
151 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
154 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, NULL);
159 ttsd_data_clear_speak_data(&speak_data);
161 ttsd_data_clear_speak_data(&speak_data);
164 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
170 * TTS Server Callback Functions
172 int ttsd_send_error(ttse_error_e error, const char* msg)
174 unsigned int uid = g_utt.uid;
175 int uttid = g_utt.uttid;
176 int tmp_pid = ttsd_data_get_pid(uid);
178 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));
180 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
182 if (0 != ttsd_player_clear(uid))
183 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
185 if (0 != ttsd_data_clear_data(uid))
186 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
189 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
190 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
193 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
194 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
197 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
198 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
203 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)
205 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
207 unsigned int uid = g_utt.uid;
208 int uttid = g_utt.uttid;
210 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
211 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
212 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
213 SLOG(LOG_DEBUG, tts_tag(), "@@@");
214 return TTSD_ERROR_INVALID_PARAMETER;
217 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
218 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
219 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
220 return TTSD_ERROR_INVALID_PARAMETER;
223 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
224 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
225 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
226 SLOG(LOG_DEBUG, tts_tag(), "@@@");
227 return TTSD_ERROR_INVALID_PARAMETER;
230 /* check the current state */
231 app_tts_state_e state;
232 if (0 > ttsd_data_get_client_state(uid, &state)) {
233 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
234 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
235 return TTSD_ERROR_INVALID_PARAMETER;
238 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
239 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
240 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
241 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
242 return TTSD_ERROR_NONE;
245 /* Synthesis is success */
246 if (TTSE_RESULT_EVENT_START == event) {
247 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
248 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
249 uid, uttid, data, data_size, audio_type, rate);
251 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
252 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
253 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
254 double d_latency = (double)latency;
255 g_avg_latency += d_latency;
256 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
257 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
259 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);
260 } else if (TTSE_RESULT_EVENT_FINISH == event) {
261 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
262 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
263 uid, uttid, data, data_size, audio_type, rate);
265 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
269 sound_data_s* temp_sound_data = NULL;
270 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
271 if (NULL == temp_sound_data) {
272 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
273 return TTSD_ERROR_OUT_OF_MEMORY;
276 temp_sound_data->data = NULL;
277 temp_sound_data->rate = 0;
278 temp_sound_data->data_size = 0;
280 if (NULL != data && 0 < data_size) {
281 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
282 if (NULL != temp_sound_data->data) {
283 memcpy(temp_sound_data->data, data, data_size);
284 temp_sound_data->data_size = data_size;
285 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%u), event(%d) sound_data(%p) data(%p) size(%d)",
286 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
288 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
291 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
294 temp_sound_data->utt_id = uttid;
295 temp_sound_data->event = event;
296 temp_sound_data->audio_type = audio_type;
297 temp_sound_data->rate = rate;
299 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
300 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
301 if (NULL != temp_sound_data->data) {
302 free(temp_sound_data->data);
303 temp_sound_data->data = NULL;
306 free(temp_sound_data);
307 temp_sound_data = NULL;
309 return TTSD_ERROR_OPERATION_FAILED;
312 if (event == TTSE_RESULT_EVENT_FINISH) {
313 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
316 return TTSD_ERROR_NONE;
319 bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
321 /* clear client data */
322 ttsd_data_clear_data(uid);
324 if (APP_STATE_READY != state) {
325 ttsd_data_set_client_state(uid, APP_STATE_READY);
328 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
330 ttsd_data_delete_client(uid);
337 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
340 case TTS_CONFIG_TYPE_ENGINE:
342 /* TODO - Determine the policy when engine process get engine changed cb */
343 if (NULL == str_param) {
344 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
349 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
350 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
351 ret = ttsd_engine_agent_unload_current_engine();
353 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
356 ret = ttsd_engine_agent_load_current_engine();
358 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
363 /* stop all player */
364 ttsd_player_all_stop();
366 /* send interrupt message to all clients */
367 ttsd_data_foreach_clients(__get_client_cb, NULL);
369 ttsd_engine_cancel_synthesis();
374 case TTS_CONFIG_TYPE_VOICE:
376 if (NULL == str_param) {
377 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
381 char* out_lang = NULL;
385 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
386 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
387 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
389 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
391 /* Current language is not available */
392 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
394 if (NULL != out_lang) {
401 case TTS_CONFIG_TYPE_SPEED:
403 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
404 /* set default speed */
406 ret = ttsd_engine_agent_set_default_speed(int_param);
408 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
414 case TTS_CONFIG_TYPE_PITCH:
416 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
417 /* set default speed */
419 ret = ttsd_engine_agent_set_default_pitch(int_param);
421 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
427 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
429 if (0.0 <= double_param && double_param <= 1.0) {
430 /* set default bg volume ratio */
432 ret = ttsd_player_set_background_volume_ratio(double_param);
434 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
444 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
446 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
447 ttsd_server_finalize(uid);
451 static void __terminate_server()
453 if (g_is_terminated) {
454 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
458 g_is_terminated = true;
460 ttsd_ipc_close_connection();
461 ttsd_network_finalize();
465 static Eina_Bool __quit_ecore_loop(void *data)
467 __terminate_server();
468 ecore_main_loop_quit();
473 Eina_Bool ttsd_terminate_daemon(void *data)
475 ttsd_data_foreach_clients(__terminate_client, NULL);
477 if (g_quit_loop_timer) {
478 ecore_timer_del(g_quit_loop_timer);
479 g_quit_loop_timer = NULL;
480 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
483 __terminate_server();
485 g_terminate_timer = NULL;
489 void __screen_reader_changed_cb(bool value)
491 /* if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
492 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
493 if (g_terminate_timer) {
494 ecore_timer_del(g_terminate_timer);
495 g_terminate_timer = NULL;
497 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
500 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
505 int ttsd_send_all_stop()
507 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
509 /* stop all player */
510 ttsd_player_all_stop();
512 /* send interrupt message to all clients */
513 ttsd_data_foreach_clients(__get_client_cb, NULL);
515 return TTSD_ERROR_NONE;
521 int ttsd_initialize(ttse_request_callback_s *callback)
523 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
524 g_is_terminated = false;
526 if (ttsd_config_initialize(__config_changed_cb)) {
527 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
531 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
533 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
534 int ret = ttsd_player_set_background_volume_ratio(ratio);
536 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
539 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
542 if (ttsd_player_init()) {
543 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
544 return TTSD_ERROR_OPERATION_FAILED;
547 /* Engine Agent initialize */
548 if (0 != ttsd_engine_agent_init()) {
549 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
550 return TTSD_ERROR_OPERATION_FAILED;
553 /* set current engine */
554 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
555 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
556 // g_is_engine = false;
558 // g_is_engine = true;
560 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
561 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
562 return TTSD_ERROR_OPERATION_FAILED;
565 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
567 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
568 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
571 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
572 if (NULL == g_check_client_timer) {
573 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
576 return TTSD_ERROR_NONE;
581 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
584 if (0 < g_list_length(g_proc_list)) {
585 iter = g_list_first(g_proc_list);
586 while (NULL != iter) {
587 g_proc_list = g_list_remove_link(g_proc_list, iter);
589 iter = g_list_first(g_proc_list);
593 ttsd_config_finalize();
595 ttsd_player_release();
597 ttsd_engine_agent_release();
601 ecore_timer_del(g_wait_timer);
603 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
606 if (g_terminate_timer) {
607 ecore_timer_del(g_terminate_timer);
608 g_terminate_timer = NULL;
609 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
612 if (g_check_client_timer) {
613 ecore_timer_del(g_check_client_timer);
614 g_check_client_timer = NULL;
615 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
618 if (g_quit_loop_timer) {
619 ecore_timer_del(g_quit_loop_timer);
620 g_quit_loop_timer = NULL;
621 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
624 return TTSD_ERROR_NONE;
629 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
631 ttsd_terminate_daemon(NULL);
633 return TTSD_ERROR_NONE;
637 * TTS Server Functions for Client
640 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
642 if (-1 != ttsd_data_is_client(uid))
643 *is_initialized = true;
645 *is_initialized = false;
647 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
648 return TTSD_ERROR_NONE;
651 int ttsd_server_initialize(int pid, unsigned int uid, tts_ipc_method_e method, bool* credential_needed)
653 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
655 if (-1 != ttsd_data_is_client(uid)) {
656 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
657 return TTSD_ERROR_NONE;
660 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
661 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
662 return TTSD_ERROR_OPERATION_FAILED;
665 if (true == *credential_needed) {
667 if (0 != app_manager_get_app_id(pid, &appid)) {
668 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
670 bool is_agreed = false;
671 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
672 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
675 return TTSD_ERROR_OPERATION_FAILED;
680 if (false == is_agreed) {
681 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
682 return TTSD_ERROR_PERMISSION_DENIED;
686 if (0 != ttsd_data_new_client(pid, uid)) {
687 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
688 return TTSD_ERROR_OPERATION_FAILED;
691 if (0 != ttsd_data_set_ipc_method(uid, method)) {
692 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
693 return TTSD_ERROR_OPERATION_FAILED;
696 if (0 != ttsd_player_create_instance(uid)) {
697 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
698 return TTSD_ERROR_OPERATION_FAILED;
702 g_min_latency = 10000.0;
707 return TTSD_ERROR_NONE;
710 static void __read_proc()
713 struct dirent *dirp = NULL;
717 if (0 < g_list_length(g_proc_list)) {
718 iter = g_list_first(g_proc_list);
719 while (NULL != iter) {
720 g_proc_list = g_list_remove_link(g_proc_list, iter);
722 iter = g_list_first(g_proc_list);
726 dp = opendir("/proc");
728 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
734 tmp = atoi(dirp->d_name);
735 if (0 >= tmp) continue;
736 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
738 } while (NULL != dirp);
744 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
750 for (i = 0; i < g_list_length(g_proc_list); i++) {
751 iter = g_list_nth(g_proc_list, i);
753 if (pid == GPOINTER_TO_INT(iter->data)) {
754 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
761 if (false == exist) {
762 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
763 ttsd_server_finalize(uid);
768 char appid[1024] = {0, };
769 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
770 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
773 if (0 < strlen(appid)) {
774 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
776 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
779 result = ttsdc_send_hello(pid, uid);
782 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) should be removed.", uid);
783 ttsd_server_finalize(uid);
784 } else if (-1 == result) {
785 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
793 Eina_Bool ttsd_cleanup_client(void *data)
795 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
798 if (0 < ttsd_data_get_client_count()) {
799 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
801 if (NULL == g_quit_loop_timer) {
802 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
803 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
805 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
806 g_check_client_timer = NULL;
810 SLOG(LOG_DEBUG, tts_tag(), "@@@");
815 void __used_voice_cb(const char* lang, int type)
817 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
818 if (0 != ttsd_engine_unload_voice(lang, type)) {
819 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
823 int ttsd_server_finalize(unsigned int uid)
825 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
826 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);
828 app_tts_state_e state;
829 if (0 > ttsd_data_get_client_state(uid, &state)) {
830 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
833 ttsd_server_stop(uid);
834 ttsd_player_clear(uid);
836 ttsd_player_destroy_instance(uid);
838 /* Need to unload voice when used voice is unregistered */
839 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
840 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
841 return TTSD_ERROR_OPERATION_FAILED;
844 ttsd_data_delete_client(uid);
846 /* unload engine, if ref count of client is 0 */
847 if (0 == ttsd_data_get_client_count()) {
848 if (NULL == g_quit_loop_timer) {
849 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
850 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
852 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
856 return TTSD_ERROR_NONE;
859 int ttsd_server_set_mode(unsigned int uid, ttsd_mode_e mode)
861 app_tts_state_e state;
862 if (0 > ttsd_data_get_client_state(uid, &state)) {
863 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
864 return TTSD_ERROR_INVALID_PARAMETER;
867 if (APP_STATE_READY != state) {
868 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
869 return TTSD_ERROR_INVALID_STATE;
872 int ret = ttsd_data_set_mode(uid, mode);
874 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
876 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%u), mode(%d)", uid, mode);
882 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)
884 app_tts_state_e state;
885 if (0 > ttsd_data_get_client_state(uid, &state)) {
886 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
887 return TTSD_ERROR_INVALID_PARAMETER;
890 /* check valid voice */
891 char* temp_lang = NULL;
893 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
894 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
895 if (NULL != temp_lang) {
899 return TTSD_ERROR_INVALID_VOICE;
902 if (NULL == temp_lang) {
903 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
904 return TTSD_ERROR_INVALID_VOICE;
907 speak_data_s* speak_data = NULL;
908 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
909 if (NULL == speak_data) {
910 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
911 if (NULL != temp_lang) {
915 return TTSD_ERROR_OPERATION_FAILED;
918 speak_data->lang = strdup(lang);
919 speak_data->vctype = voice_type;
921 speak_data->speed = speed;
922 speak_data->utt_id = utt_id;
924 speak_data->text = strdup(text);
926 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);
928 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
930 ret = ttsd_data_add_speak_data(uid, speak_data);
932 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
933 if (NULL != temp_lang) {
937 if (NULL != speak_data) {
938 if (NULL != speak_data->lang) free(speak_data->lang);
939 if (NULL != speak_data->text) free(speak_data->text);
941 speak_data->lang = NULL;
942 speak_data->text = NULL;
951 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
952 /* Request load voice */
953 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
954 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
955 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
959 if (NULL != temp_lang) {
964 if (APP_STATE_PLAYING == state) {
965 /* check if engine use network */
966 if (ttsd_engine_agent_need_network()) {
967 if (false == ttsd_network_is_connected()) {
968 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
969 return TTSD_ERROR_OPERATION_FAILED;
973 ttsd_data_set_credential(uid, credential);
975 /* Check whether tts-engine is running or not */
976 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
977 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
978 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
979 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
985 return TTSD_ERROR_NONE;
988 void __send_interrupt_client(unsigned int uid)
990 SLOG(LOG_INFO, tts_tag(), "[Server] Send new state by policy. uid(%u)", uid);
991 int pid = ttsd_data_get_pid(uid);
993 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
997 ttsd_mode_e mode = ttsd_data_get_mode(uid);
998 if (TTSD_MODE_DEFAULT != mode) {
999 /* send message to client about changing state */
1000 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
1002 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
1005 SLOG(LOG_INFO, tts_tag(), "[Server] Finish to send new state by policy. uid(%u)", uid);
1009 int ttsd_server_play(unsigned int uid, const char* credential)
1011 app_tts_state_e state;
1012 if (0 > ttsd_data_get_client_state(uid, &state)) {
1013 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1014 return TTSD_ERROR_INVALID_PARAMETER;
1017 if (APP_STATE_PLAYING == state) {
1018 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1019 return TTSD_ERROR_NONE;
1022 /* check if engine use network */
1023 if (ttsd_engine_agent_need_network()) {
1024 if (false == ttsd_network_is_connected()) {
1025 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
1026 return TTSD_ERROR_OUT_OF_NETWORK;
1030 /* check the current playback focus */
1031 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
1032 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
1033 ttsd_data_clear_data(uid);
1034 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1037 unsigned int current_uid = ttsd_data_get_current_playing();
1038 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1040 if (uid != current_uid && -1 != current_uid) {
1041 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1042 /* Send interrupt message */
1043 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1046 if (0 != ttsd_server_stop(current_uid)) {
1047 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1050 __send_interrupt_client(current_uid);
1052 /* Default mode policy of interrupt is "Pause" */
1054 /* Send interrupt message */
1055 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1058 if (0 != ttsd_player_pause(current_uid)) {
1059 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%u)", current_uid);
1063 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1065 __send_interrupt_client(current_uid);
1069 /* Change current play */
1070 if (0 != ttsd_player_wait_to_play(uid)) {
1071 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1074 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1075 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1076 return TTSD_ERROR_OPERATION_FAILED;
1079 if (APP_STATE_PAUSED == state) {
1080 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1082 g_is_paused = false;
1085 if (0 != ttsd_player_resume(uid)) {
1086 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1089 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1091 if (0 != ttsd_player_play(uid)) {
1092 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_play()");
1096 ttsd_data_set_credential(uid, credential);
1098 /* Check whether tts-engine is running or not */
1099 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1100 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1101 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1102 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1103 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1108 return TTSD_ERROR_NONE;
1111 int ttsd_server_stop(unsigned int uid)
1113 app_tts_state_e state;
1114 if (0 > ttsd_data_get_client_state(uid, &state)) {
1115 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1116 return TTSD_ERROR_INVALID_PARAMETER;
1119 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1121 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1122 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1123 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1124 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1125 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1128 ret = ttsd_engine_cancel_synthesis();
1130 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1132 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1135 if (0 != ttsd_player_stop(uid)) {
1136 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1139 ttsd_data_set_client_state(uid, APP_STATE_READY);
1142 /* Reset all data */
1143 ttsd_data_clear_data(uid);
1144 g_is_paused = false;
1146 return TTSD_ERROR_NONE;
1149 int ttsd_server_pause(unsigned int uid, int* utt_id)
1151 app_tts_state_e state;
1152 if (0 > ttsd_data_get_client_state(uid, &state)) {
1153 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1154 return TTSD_ERROR_INVALID_PARAMETER;
1157 if (APP_STATE_PLAYING != state) {
1158 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1159 return TTSD_ERROR_INVALID_STATE;
1164 *utt_id = g_utt.uttid;
1165 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%u), g_uid(%u), utt_id(%d)", uid, g_utt.uid, *utt_id);
1168 ret = ttsd_player_pause(uid);
1170 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1171 return TTSD_ERROR_OPERATION_FAILED;
1174 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1176 return TTSD_ERROR_NONE;
1179 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1181 app_tts_state_e state;
1182 if (0 > ttsd_data_get_client_state(uid, &state)) {
1183 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1184 return TTSD_ERROR_INVALID_PARAMETER;
1188 int ret = ttsd_engine_get_voice_list(voice_list);
1190 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1194 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1196 return TTSD_ERROR_NONE;
1199 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1201 app_tts_state_e state;
1202 if (0 > ttsd_data_get_client_state(uid, &state)) {
1203 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1204 return TTSD_ERROR_INVALID_PARAMETER;
1207 /* get current voice */
1208 int ret = ttsd_engine_get_default_voice(language, voice_type);
1210 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1214 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1216 return TTSD_ERROR_NONE;
1219 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1221 app_tts_state_e state;
1222 if (0 > ttsd_data_get_client_state(uid, &state)) {
1223 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1224 return TTSD_ERROR_INVALID_PARAMETER;
1227 if (APP_STATE_READY != state) {
1228 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1229 return TTSD_ERROR_INVALID_STATE;
1232 int ret = ttsd_engine_set_private_data(key, data);
1234 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1236 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1242 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1244 app_tts_state_e state;
1245 if (0 > ttsd_data_get_client_state(uid, &state)) {
1246 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1247 return TTSD_ERROR_INVALID_PARAMETER;
1250 if (APP_STATE_READY != state) {
1251 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1252 return TTSD_ERROR_INVALID_STATE;
1255 int ret = ttsd_engine_get_private_data(key, data);
1257 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1259 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1265 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1267 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1270 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1272 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1278 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1280 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1283 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1285 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1291 int ttsd_server_play_pcm(unsigned int uid)
1293 app_tts_state_e state;
1294 if (0 > ttsd_data_get_client_state(uid, &state)) {
1295 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1296 return TTSD_ERROR_INVALID_PARAMETER;
1299 if (APP_STATE_PLAYING == state) {
1300 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1301 return TTSD_ERROR_NONE;
1304 unsigned int current_uid = ttsd_data_get_current_playing();
1305 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1307 if (uid != current_uid && -1 != current_uid) {
1308 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1309 /* Send interrupt message */
1310 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1313 if (0 != ttsd_server_stop(current_uid)) {
1314 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1317 __send_interrupt_client(current_uid);
1319 /* Default mode policy of interrupt is "Pause" */
1321 /* Send interrupt message */
1322 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1325 if (0 != ttsd_player_pause(current_uid)) {
1326 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%u)", current_uid);
1330 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1332 __send_interrupt_client(current_uid);
1336 /* Change current play */
1337 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1338 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1339 return TTSD_ERROR_OPERATION_FAILED;
1342 if (APP_STATE_PAUSED == state) {
1343 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1346 if (0 != ttsd_player_resume(uid)) {
1347 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1350 if (0 != ttsd_player_play(uid)) {
1351 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%u)", uid);
1353 // Change ready state
1354 ttsd_server_stop_pcm(uid);
1356 int tmp_pid = ttsd_data_get_pid(uid);
1358 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
1360 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1365 return TTSD_ERROR_NONE;
1368 int ttsd_server_stop_pcm(unsigned int uid)
1370 app_tts_state_e state;
1371 if (0 > ttsd_data_get_client_state(uid, &state)) {
1372 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1373 return TTSD_ERROR_INVALID_PARAMETER;
1376 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1378 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1379 ttsd_data_set_client_state(uid, APP_STATE_READY);
1382 /* Reset all data */
1383 ttsd_data_clear_data(uid);
1385 ttsd_player_stop(uid);
1388 return TTSD_ERROR_NONE;
1391 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1393 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1397 /* Synthesis is success */
1398 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1399 if (TTSE_RESULT_EVENT_START == event) {
1400 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
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);
1403 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1404 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1405 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1406 uid, uttid, data, data_size, audio_type, rate);
1408 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1411 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1412 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1413 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1414 return TTSD_ERROR_INVALID_PARAMETER;
1418 sound_data_s* temp_sound_data = NULL;
1419 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1420 if (NULL == temp_sound_data) {
1421 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1422 return TTSD_ERROR_OUT_OF_MEMORY;
1425 temp_sound_data->data = NULL;
1426 temp_sound_data->rate = 0;
1427 temp_sound_data->data_size = 0;
1429 if (0 < data_size) {
1430 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1431 if (NULL != temp_sound_data->data) {
1432 memcpy(temp_sound_data->data, data, data_size);
1433 temp_sound_data->data_size = data_size;
1434 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%u), event(%d) sound_data(%p) data(%p) size(%d)",
1435 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1437 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1440 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1443 temp_sound_data->utt_id = uttid;
1444 temp_sound_data->event = event;
1445 temp_sound_data->audio_type = audio_type;
1446 temp_sound_data->rate = rate;
1448 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1449 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1451 if (NULL != temp_sound_data->data) {
1452 free(temp_sound_data->data);
1453 temp_sound_data->data = NULL;
1456 free(temp_sound_data);
1457 temp_sound_data = NULL;
1460 /* if (0 != ttsd_player_play(uid)) {
1461 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1463 // Change ready state
1464 ttsd_server_stop(uid);
1467 tmp_pid = ttsd_data_get_pid(uid);
1468 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1472 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1475 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1477 return TTSD_ERROR_NONE;