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;
63 /* Function definitions */
64 static int __synthesis(unsigned int uid, const char* credential);
66 static Eina_Bool __wait_synthesis(void *data)
68 /* get current play */
69 char* credential = (char*)data;
70 unsigned int uid = ttsd_data_get_current_playing();
73 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
74 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
77 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
79 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
80 /* Start next synthesis */
81 __synthesis(uid, credential);
91 static int __synthesis(unsigned int uid, const char* credential)
93 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
95 speak_data_s* speak_data = NULL;
96 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
97 if (NULL == speak_data) {
98 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
102 int pid = ttsd_data_get_pid(uid);
104 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
108 char appid[1024] = {0, };
109 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
110 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
113 if (NULL == speak_data->lang || NULL == speak_data->text) {
114 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
115 ttsd_server_stop(uid);
117 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
119 ttsd_data_clear_speak_data(&speak_data);
125 g_utt.uttid = speak_data->utt_id;
127 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
128 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
129 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
130 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
131 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
132 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
135 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
136 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
138 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
140 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
142 ttsd_server_stop(uid);
144 int pid = ttsd_data_get_pid(uid);
146 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
148 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
151 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
154 ttsd_data_clear_speak_data(&speak_data);
156 ttsd_data_clear_speak_data(&speak_data);
159 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
165 * TTS Server Callback Functions
167 int ttsd_send_error(ttse_error_e error, const char* msg)
169 unsigned int uid = g_utt.uid;
170 int uttid = g_utt.uttid;
171 int tmp_pid = ttsd_data_get_pid(uid);
173 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));
175 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
177 if (0 != ttsd_player_clear(uid))
178 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
180 if (0 != ttsd_data_clear_data(uid))
181 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
184 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
185 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
188 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
189 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
192 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
193 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
198 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)
200 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
202 unsigned int uid = g_utt.uid;
203 int uttid = g_utt.uttid;
205 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
206 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
207 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
208 SLOG(LOG_DEBUG, tts_tag(), "@@@");
209 return TTSD_ERROR_INVALID_PARAMETER;
212 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
213 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
214 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
215 return TTSD_ERROR_INVALID_PARAMETER;
218 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
219 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
220 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
221 SLOG(LOG_DEBUG, tts_tag(), "@@@");
222 return TTSD_ERROR_INVALID_PARAMETER;
225 /* check the current state */
226 app_tts_state_e state;
227 if (0 > ttsd_data_get_client_state(uid, &state)) {
228 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
229 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
230 return TTSD_ERROR_INVALID_PARAMETER;
233 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
234 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
235 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
236 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
237 return TTSD_ERROR_NONE;
240 /* Synthesis is success */
241 if (TTSE_RESULT_EVENT_START == event) {
242 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
243 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
244 uid, uttid, data, data_size, audio_type, rate);
246 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
247 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
248 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
249 double d_latency = (double)latency;
250 g_avg_latency += d_latency;
251 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
252 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
254 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);
255 } else if (TTSE_RESULT_EVENT_FINISH == event) {
256 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
257 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
258 uid, uttid, data, data_size, audio_type, rate);
260 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
264 sound_data_s* temp_sound_data = NULL;
265 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
266 if (NULL == temp_sound_data) {
267 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
268 return TTSD_ERROR_OUT_OF_MEMORY;
271 temp_sound_data->data = NULL;
272 temp_sound_data->rate = 0;
273 temp_sound_data->data_size = 0;
275 if (NULL != data && 0 < data_size) {
276 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
277 if (NULL != temp_sound_data->data) {
278 memcpy(temp_sound_data->data, data, data_size);
279 temp_sound_data->data_size = data_size;
280 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%u), event(%d) sound_data(%p) data(%p) size(%d)",
281 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
283 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
286 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
289 temp_sound_data->utt_id = uttid;
290 temp_sound_data->event = event;
291 temp_sound_data->audio_type = audio_type;
292 temp_sound_data->rate = rate;
294 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
295 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
296 if (NULL != temp_sound_data->data) {
297 free(temp_sound_data->data);
298 temp_sound_data->data = NULL;
301 free(temp_sound_data);
302 temp_sound_data = NULL;
304 return TTSD_ERROR_OPERATION_FAILED;
307 if (event == TTSE_RESULT_EVENT_FINISH) {
308 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
311 return TTSD_ERROR_NONE;
314 bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
316 /* clear client data */
317 ttsd_data_clear_data(uid);
319 if (APP_STATE_READY != state) {
320 ttsd_data_set_client_state(uid, APP_STATE_READY);
323 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
325 ttsd_data_delete_client(uid);
332 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
335 case TTS_CONFIG_TYPE_ENGINE:
337 /* TODO - Determine the policy when engine process get engine changed cb */
338 if (NULL == str_param) {
339 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
344 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
345 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
346 ret = ttsd_engine_agent_unload_current_engine();
348 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
351 ret = ttsd_engine_agent_load_current_engine();
353 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
358 /* stop all player */
359 ttsd_player_all_stop();
361 /* send interrupt message to all clients */
362 ttsd_data_foreach_clients(__get_client_cb, NULL);
364 ttsd_engine_cancel_synthesis();
369 case TTS_CONFIG_TYPE_VOICE:
371 if (NULL == str_param) {
372 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
376 char* out_lang = NULL;
380 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
381 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
382 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
384 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
386 /* Current language is not available */
387 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
389 if (NULL != out_lang) {
396 case TTS_CONFIG_TYPE_SPEED:
398 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
399 /* set default speed */
401 ret = ttsd_engine_agent_set_default_speed(int_param);
403 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
409 case TTS_CONFIG_TYPE_PITCH:
411 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
412 /* set default speed */
414 ret = ttsd_engine_agent_set_default_pitch(int_param);
416 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
422 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
424 if (0.0 <= double_param && double_param <= 1.0) {
425 /* set default bg volume ratio */
427 ret = ttsd_player_set_background_volume_ratio(double_param);
429 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
439 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
441 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
442 ttsd_server_finalize(uid);
446 Eina_Bool ttsd_terminate_daemon(void *data)
448 ttsd_data_foreach_clients(__terminate_client, NULL);
449 g_terminate_timer = NULL;
453 void __screen_reader_changed_cb(bool value)
455 /* if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
456 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
457 if (g_terminate_timer) {
458 ecore_timer_del(g_terminate_timer);
459 g_terminate_timer = NULL;
461 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
464 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
469 int ttsd_send_all_stop()
471 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
473 /* stop all player */
474 ttsd_player_all_stop();
476 /* send interrupt message to all clients */
477 ttsd_data_foreach_clients(__get_client_cb, NULL);
479 return TTSD_ERROR_NONE;
485 int ttsd_initialize(ttse_request_callback_s *callback)
487 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
489 if (ttsd_config_initialize(__config_changed_cb)) {
490 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
494 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
496 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
497 int ret = ttsd_player_set_background_volume_ratio(ratio);
499 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
502 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
505 if (ttsd_player_init()) {
506 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
507 return TTSD_ERROR_OPERATION_FAILED;
510 /* Engine Agent initialize */
511 if (0 != ttsd_engine_agent_init()) {
512 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
513 return TTSD_ERROR_OPERATION_FAILED;
516 /* set current engine */
517 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
518 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
519 // g_is_engine = false;
521 // g_is_engine = true;
523 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
524 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
525 return TTSD_ERROR_OPERATION_FAILED;
528 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
530 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
531 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
534 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
535 if (NULL == g_check_client_timer) {
536 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
539 return TTSD_ERROR_NONE;
544 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
547 if (0 < g_list_length(g_proc_list)) {
548 iter = g_list_first(g_proc_list);
549 while (NULL != iter) {
550 g_proc_list = g_list_remove_link(g_proc_list, iter);
552 iter = g_list_first(g_proc_list);
556 ttsd_config_finalize();
558 ttsd_player_release();
560 ttsd_engine_agent_release();
564 ecore_timer_del(g_wait_timer);
566 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
569 if (g_terminate_timer) {
570 ecore_timer_del(g_terminate_timer);
571 g_terminate_timer = NULL;
572 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
575 if (g_check_client_timer) {
576 ecore_timer_del(g_check_client_timer);
577 g_check_client_timer = NULL;
578 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
581 if (g_quit_loop_timer) {
582 ecore_timer_del(g_quit_loop_timer);
583 g_quit_loop_timer = NULL;
584 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
587 return TTSD_ERROR_NONE;
592 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
594 ttsd_terminate_daemon(NULL);
596 return TTSD_ERROR_NONE;
600 * TTS Server Functions for Client
603 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
605 if (-1 != ttsd_data_is_client(uid))
606 *is_initialized = true;
608 *is_initialized = false;
610 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
611 return TTSD_ERROR_NONE;
614 int ttsd_server_initialize(int pid, unsigned int uid, tts_ipc_method_e method, bool* credential_needed)
616 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
618 if (-1 != ttsd_data_is_client(uid)) {
619 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
620 return TTSD_ERROR_NONE;
623 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
624 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
625 return TTSD_ERROR_OPERATION_FAILED;
628 if (true == *credential_needed) {
630 if (0 != app_manager_get_app_id(pid, &appid)) {
631 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
633 bool is_agreed = false;
634 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
635 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
638 return TTSD_ERROR_OPERATION_FAILED;
643 if (false == is_agreed) {
644 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
645 return TTSD_ERROR_PERMISSION_DENIED;
649 if (0 != ttsd_data_new_client(pid, uid)) {
650 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
651 return TTSD_ERROR_OPERATION_FAILED;
654 if (0 != ttsd_data_set_ipc_method(uid, method)) {
655 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
656 return TTSD_ERROR_OPERATION_FAILED;
659 if (0 != ttsd_player_create_instance(uid)) {
660 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
661 return TTSD_ERROR_OPERATION_FAILED;
665 g_min_latency = 10000.0;
670 return TTSD_ERROR_NONE;
673 static Eina_Bool __quit_ecore_loop(void *data)
675 ttsd_ipc_close_connection();
676 ttsd_network_finalize();
678 ecore_main_loop_quit();
684 static void __read_proc()
687 struct dirent *dirp = NULL;
691 if (0 < g_list_length(g_proc_list)) {
692 iter = g_list_first(g_proc_list);
693 while (NULL != iter) {
694 g_proc_list = g_list_remove_link(g_proc_list, iter);
696 iter = g_list_first(g_proc_list);
700 dp = opendir("/proc");
702 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
708 tmp = atoi(dirp->d_name);
709 if (0 >= tmp) continue;
710 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
712 } while (NULL != dirp);
718 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
724 for (i = 0; i < g_list_length(g_proc_list); i++) {
725 iter = g_list_nth(g_proc_list, i);
727 if (pid == GPOINTER_TO_INT(iter->data)) {
728 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
735 if (false == exist) {
736 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
737 ttsd_server_finalize(uid);
742 char appid[1024] = {0, };
743 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
744 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
747 if (0 < strlen(appid)) {
748 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
750 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
753 result = ttsdc_send_hello(pid, uid);
756 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) should be removed.", uid);
757 ttsd_server_finalize(uid);
758 } else if (-1 == result) {
759 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
767 Eina_Bool ttsd_cleanup_client(void *data)
769 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
772 if (0 < ttsd_data_get_client_count()) {
773 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
775 if (NULL == g_quit_loop_timer) {
776 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
777 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
779 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
780 g_check_client_timer = NULL;
784 SLOG(LOG_DEBUG, tts_tag(), "@@@");
789 void __used_voice_cb(const char* lang, int type)
791 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
792 if (0 != ttsd_engine_unload_voice(lang, type)) {
793 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
797 int ttsd_server_finalize(unsigned int uid)
799 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
800 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);
802 app_tts_state_e state;
803 if (0 > ttsd_data_get_client_state(uid, &state)) {
804 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
807 ttsd_server_stop(uid);
808 ttsd_player_clear(uid);
810 ttsd_player_destroy_instance(uid);
812 /* Need to unload voice when used voice is unregistered */
813 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
814 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
815 return TTSD_ERROR_OPERATION_FAILED;
818 ttsd_data_delete_client(uid);
820 /* unload engine, if ref count of client is 0 */
821 if (0 == ttsd_data_get_client_count()) {
822 if (NULL == g_quit_loop_timer) {
823 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
824 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
826 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
830 return TTSD_ERROR_NONE;
833 int ttsd_server_set_mode(unsigned int uid, ttsd_mode_e mode)
835 app_tts_state_e state;
836 if (0 > ttsd_data_get_client_state(uid, &state)) {
837 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
838 return TTSD_ERROR_INVALID_PARAMETER;
841 if (APP_STATE_READY != state) {
842 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
843 return TTSD_ERROR_INVALID_STATE;
846 int ret = ttsd_data_set_mode(uid, mode);
848 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
850 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%u), mode(%d)", uid, mode);
856 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)
858 app_tts_state_e state;
859 if (0 > ttsd_data_get_client_state(uid, &state)) {
860 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
861 return TTSD_ERROR_INVALID_PARAMETER;
864 /* check valid voice */
865 char* temp_lang = NULL;
867 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
868 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
869 if (NULL != temp_lang) {
873 return TTSD_ERROR_INVALID_VOICE;
876 if (NULL == temp_lang) {
877 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
878 return TTSD_ERROR_INVALID_VOICE;
881 speak_data_s* speak_data = NULL;
882 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
883 if (NULL == speak_data) {
884 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
885 if (NULL != temp_lang) {
889 return TTSD_ERROR_OPERATION_FAILED;
892 speak_data->lang = strdup(lang);
893 speak_data->vctype = voice_type;
895 speak_data->speed = speed;
896 speak_data->utt_id = utt_id;
898 speak_data->text = strdup(text);
900 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);
902 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
904 ret = ttsd_data_add_speak_data(uid, speak_data);
906 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
907 if (NULL != temp_lang) {
911 if (NULL != speak_data) {
912 if (NULL != speak_data->lang) free(speak_data->lang);
913 if (NULL != speak_data->text) free(speak_data->text);
915 speak_data->lang = NULL;
916 speak_data->text = NULL;
925 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
926 /* Request load voice */
927 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
928 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
929 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
933 if (NULL != temp_lang) {
938 if (APP_STATE_PLAYING == state) {
939 /* check if engine use network */
940 if (ttsd_engine_agent_need_network()) {
941 if (false == ttsd_network_is_connected()) {
942 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
943 return TTSD_ERROR_OPERATION_FAILED;
947 /* Check whether tts-engine is running or not */
948 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
949 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
950 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
951 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
953 __synthesis(uid, credential);
957 return TTSD_ERROR_NONE;
960 Eina_Bool __send_interrupt_client(void *data)
962 unsigned int uid = (uintptr_t)data;
963 int pid = ttsd_data_get_pid(uid);
965 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
969 ttsd_mode_e mode = ttsd_data_get_mode(uid);
970 if (TTSD_MODE_DEFAULT != mode) {
971 /* send message to client about changing state */
972 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
974 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
980 int ttsd_server_play(unsigned int uid, const char* credential)
982 app_tts_state_e state;
983 if (0 > ttsd_data_get_client_state(uid, &state)) {
984 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
985 return TTSD_ERROR_INVALID_PARAMETER;
988 if (APP_STATE_PLAYING == state) {
989 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
990 return TTSD_ERROR_NONE;
993 /* check if engine use network */
994 if (ttsd_engine_agent_need_network()) {
995 if (false == ttsd_network_is_connected()) {
996 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
997 return TTSD_ERROR_OUT_OF_NETWORK;
1001 /* check the current playback focus */
1002 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
1003 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
1004 ttsd_data_clear_data(uid);
1005 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1008 unsigned int current_uid = ttsd_data_get_current_playing();
1009 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1011 if (uid != current_uid && -1 != current_uid) {
1012 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1013 /* Send interrupt message */
1014 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1017 if (0 != ttsd_server_stop(current_uid)) {
1018 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1021 uintptr_t p_current_uid = (uintptr_t)current_uid;
1022 ecore_timer_add(0, __send_interrupt_client, (void*)p_current_uid);
1024 /* Default mode policy of interrupt is "Pause" */
1026 /* Send interrupt message */
1027 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1030 if (0 != ttsd_player_pause(current_uid)) {
1031 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%u)", current_uid);
1035 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1037 uintptr_t p_current_uid = (uintptr_t)current_uid;
1038 ecore_timer_add(0, __send_interrupt_client, (void*)p_current_uid);
1042 /* Change current play */
1043 if (0 != ttsd_player_wait_to_play(uid)) {
1044 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1047 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1048 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1049 return TTSD_ERROR_OPERATION_FAILED;
1052 if (APP_STATE_PAUSED == state) {
1053 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1055 g_is_paused = false;
1058 if (0 != ttsd_player_resume(uid)) {
1059 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1062 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1064 if (0 != ttsd_player_play(uid)) {
1065 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_play()");
1069 /* Check whether tts-engine is running or not */
1070 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1071 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1072 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1073 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1074 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1076 __synthesis(uid, credential);
1079 return TTSD_ERROR_NONE;
1082 int ttsd_server_stop(unsigned int uid)
1084 app_tts_state_e state;
1085 if (0 > ttsd_data_get_client_state(uid, &state)) {
1086 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1087 return TTSD_ERROR_INVALID_PARAMETER;
1090 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1092 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1093 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1094 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1095 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1096 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1099 ret = ttsd_engine_cancel_synthesis();
1101 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1103 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1106 if (0 != ttsd_player_stop(uid)) {
1107 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1110 ttsd_data_set_client_state(uid, APP_STATE_READY);
1113 /* Reset all data */
1114 ttsd_data_clear_data(uid);
1115 g_is_paused = false;
1117 return TTSD_ERROR_NONE;
1120 int ttsd_server_pause(unsigned int uid, int* utt_id)
1122 app_tts_state_e state;
1123 if (0 > ttsd_data_get_client_state(uid, &state)) {
1124 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1125 return TTSD_ERROR_INVALID_PARAMETER;
1128 if (APP_STATE_PLAYING != state) {
1129 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1130 return TTSD_ERROR_INVALID_STATE;
1135 *utt_id = g_utt.uttid;
1136 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%u), g_uid(%u), utt_id(%d)", uid, g_utt.uid, *utt_id);
1139 ret = ttsd_player_pause(uid);
1141 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1142 return TTSD_ERROR_OPERATION_FAILED;
1145 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1147 return TTSD_ERROR_NONE;
1150 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1152 app_tts_state_e state;
1153 if (0 > ttsd_data_get_client_state(uid, &state)) {
1154 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1155 return TTSD_ERROR_INVALID_PARAMETER;
1159 int ret = ttsd_engine_get_voice_list(voice_list);
1161 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1165 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1167 return TTSD_ERROR_NONE;
1170 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1172 app_tts_state_e state;
1173 if (0 > ttsd_data_get_client_state(uid, &state)) {
1174 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1175 return TTSD_ERROR_INVALID_PARAMETER;
1178 /* get current voice */
1179 int ret = ttsd_engine_get_default_voice(language, voice_type);
1181 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1185 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1187 return TTSD_ERROR_NONE;
1190 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1192 app_tts_state_e state;
1193 if (0 > ttsd_data_get_client_state(uid, &state)) {
1194 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1195 return TTSD_ERROR_INVALID_PARAMETER;
1198 if (APP_STATE_READY != state) {
1199 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1200 return TTSD_ERROR_INVALID_STATE;
1203 int ret = ttsd_engine_set_private_data(key, data);
1205 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1207 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1213 int ttsd_server_get_private_data(unsigned int uid, const char* key, 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_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1223 return TTSD_ERROR_INVALID_STATE;
1226 int ret = ttsd_engine_get_private_data(key, data);
1228 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1230 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1236 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1238 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1241 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1243 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1249 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1251 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1254 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1256 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1262 int ttsd_server_play_pcm(unsigned int uid)
1264 app_tts_state_e state;
1265 if (0 > ttsd_data_get_client_state(uid, &state)) {
1266 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1267 return TTSD_ERROR_INVALID_PARAMETER;
1270 if (APP_STATE_PLAYING == state) {
1271 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1272 return TTSD_ERROR_NONE;
1275 unsigned int current_uid = ttsd_data_get_current_playing();
1276 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1278 if (uid != current_uid && -1 != current_uid) {
1279 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1280 /* Send interrupt message */
1281 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1284 if (0 != ttsd_server_stop(current_uid)) {
1285 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1288 uintptr_t p_current_uid = (uintptr_t)current_uid;
1289 ecore_timer_add(0, __send_interrupt_client, (void*)p_current_uid);
1291 /* Default mode policy of interrupt is "Pause" */
1293 /* Send interrupt message */
1294 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1297 if (0 != ttsd_player_pause(current_uid)) {
1298 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%u)", current_uid);
1302 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1304 uintptr_t p_current_uid = (uintptr_t)current_uid;
1305 ecore_timer_add(0, __send_interrupt_client, (void*)p_current_uid);
1309 /* Change current play */
1310 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1311 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1312 return TTSD_ERROR_OPERATION_FAILED;
1315 if (APP_STATE_PAUSED == state) {
1316 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1319 if (0 != ttsd_player_resume(uid)) {
1320 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1323 if (0 != ttsd_player_play(uid)) {
1324 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%u)", uid);
1326 // Change ready state
1327 ttsd_server_stop_pcm(uid);
1329 int tmp_pid = ttsd_data_get_pid(uid);
1331 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
1333 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1338 return TTSD_ERROR_NONE;
1341 int ttsd_server_stop_pcm(unsigned int uid)
1343 app_tts_state_e state;
1344 if (0 > ttsd_data_get_client_state(uid, &state)) {
1345 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1346 return TTSD_ERROR_INVALID_PARAMETER;
1349 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1351 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1352 ttsd_data_set_client_state(uid, APP_STATE_READY);
1355 /* Reset all data */
1356 ttsd_data_clear_data(uid);
1358 ttsd_player_stop(uid);
1361 return TTSD_ERROR_NONE;
1364 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1366 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1370 /* Synthesis is success */
1371 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1372 if (TTSE_RESULT_EVENT_START == event) {
1373 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1374 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1375 uid, uttid, data, data_size, audio_type, rate);
1376 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1377 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1378 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1379 uid, uttid, data, data_size, audio_type, rate);
1381 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1384 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1385 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1386 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1387 return TTSD_ERROR_INVALID_PARAMETER;
1391 sound_data_s* temp_sound_data = NULL;
1392 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1393 if (NULL == temp_sound_data) {
1394 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1395 return TTSD_ERROR_OUT_OF_MEMORY;
1398 temp_sound_data->data = NULL;
1399 temp_sound_data->rate = 0;
1400 temp_sound_data->data_size = 0;
1402 if (0 < data_size) {
1403 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1404 if (NULL != temp_sound_data->data) {
1405 memcpy(temp_sound_data->data, data, data_size);
1406 temp_sound_data->data_size = data_size;
1407 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%u), event(%d) sound_data(%p) data(%p) size(%d)",
1408 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1410 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1413 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1416 temp_sound_data->utt_id = uttid;
1417 temp_sound_data->event = event;
1418 temp_sound_data->audio_type = audio_type;
1419 temp_sound_data->rate = rate;
1421 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1422 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1424 if (NULL != temp_sound_data->data) {
1425 free(temp_sound_data->data);
1426 temp_sound_data->data = NULL;
1429 free(temp_sound_data);
1430 temp_sound_data = NULL;
1433 /* if (0 != ttsd_player_play(uid)) {
1434 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1436 // Change ready state
1437 ttsd_server_stop(uid);
1440 tmp_pid = ttsd_data_get_pid(uid);
1441 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1445 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1448 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1450 return TTSD_ERROR_NONE;