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"
22 #include "ttsd_engine_agent.h"
23 #include "ttsd_main.h"
24 #include "ttsd_player.h"
25 #include "ttsd_server.h"
28 #define CLIENT_CLEAN_UP_TIME 500
30 /* for checking time */
31 #define TIME_DIFF(start, end) \
32 ((uint64_t)((end).tv_sec - (start).tv_sec) * 1000000 + (((end).tv_nsec - (start).tv_nsec) / 1000)) / 1000
34 static struct timespec g_request_playing, g_start_playing;
35 static double g_avg_latency;
36 static double g_min_latency;
37 static double g_max_latency;
38 static int g_file_num;
45 /* If current engine exist */
46 //static bool g_is_engine;
48 static Ecore_Timer* g_check_client_timer = NULL;
49 static Ecore_Timer* g_wait_timer = NULL;
50 static Ecore_Timer* g_quit_loop_timer = NULL;
52 static utterance_t g_utt;
54 static GList *g_proc_list = NULL;
56 static bool g_is_paused;
58 static bool g_is_terminated = false;
61 /* Function definitions */
62 static void __synthesis(unsigned int uid);
64 static Eina_Bool __wait_synthesis(void *data)
66 /* get current play */
67 unsigned int uid = ttsd_data_get_current_playing();
70 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
71 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
74 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
76 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
77 /* Start next synthesis */
88 static void __synthesis(unsigned int uid)
90 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
92 speak_data_s* speak_data = NULL;
93 int ret = ttsd_data_get_speak_data(uid, &speak_data);
94 if (TTSD_ERROR_NONE != ret || NULL == speak_data) {
95 SLOG(LOG_DEBUG, tts_tag(), "@@@ Fail to get speak data. ret(%d)", ret);
96 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
100 int pid = ttsd_data_get_pid(uid);
102 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
103 ttsd_data_destroy_speak_data(speak_data);
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);
116 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
117 ttsd_data_destroy_speak_data(speak_data);
123 g_utt.uttid = speak_data->utt_id;
125 char* credential = ttsd_data_get_credential(uid);
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(), "-----------------------------------------------------------");
134 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
135 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
136 if (TTSD_ERROR_NONE != ret) {
137 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
139 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
140 ttsd_server_stop(uid);
142 int pid = ttsd_data_get_pid(uid);
144 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
146 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
149 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
154 ttsd_data_destroy_speak_data(speak_data);
156 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
162 * TTS Server Callback Functions
164 int ttsd_send_error(ttse_error_e error, const char* msg)
166 unsigned int uid = g_utt.uid;
167 int uttid = g_utt.uttid;
168 int tmp_pid = ttsd_data_get_pid(uid);
170 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%u), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, (NULL == msg ? "NULL" : msg));
172 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
174 if (0 != ttsd_data_clear_data(uid))
175 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
178 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
179 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
182 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
183 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
186 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
187 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
192 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)
194 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
196 unsigned int uid = g_utt.uid;
197 int uttid = g_utt.uttid;
199 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
200 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
201 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
202 SLOG(LOG_DEBUG, tts_tag(), "@@@");
203 return TTSD_ERROR_INVALID_PARAMETER;
206 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
207 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
208 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
209 return TTSD_ERROR_INVALID_PARAMETER;
212 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
213 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
214 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
215 SLOG(LOG_DEBUG, tts_tag(), "@@@");
216 return TTSD_ERROR_INVALID_PARAMETER;
219 /* check the current state */
220 app_tts_state_e state = ttsd_data_get_client_state(uid);
221 if (APP_STATE_NONE == state) {
222 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
223 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
224 return TTSD_ERROR_INVALID_PARAMETER;
227 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
228 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
229 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
230 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
231 return TTSD_ERROR_NONE;
234 /* Synthesis is success */
235 if (TTSE_RESULT_EVENT_START == event) {
236 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
237 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
238 uid, uttid, data, data_size, audio_type, rate);
240 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
241 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
242 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
243 double d_latency = (double)latency;
244 g_avg_latency += d_latency;
245 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
246 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
248 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);
249 } else if (TTSE_RESULT_EVENT_FINISH == event) {
250 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
251 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
252 uid, uttid, data, data_size, audio_type, rate);
254 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
258 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
259 if (NULL == sound_data) {
260 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
261 return TTSD_ERROR_OUT_OF_MEMORY;
264 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
265 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
266 ttsd_data_destroy_sound_data(sound_data);
268 return TTSD_ERROR_OPERATION_FAILED;
271 if (event == TTSE_RESULT_EVENT_FINISH) {
272 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
275 return TTSD_ERROR_NONE;
278 bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
280 /* clear client data */
281 ttsd_data_clear_data(uid);
283 if (APP_STATE_READY != state) {
284 ttsd_data_set_client_state(uid, APP_STATE_READY);
287 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
289 ttsd_data_delete_client(uid);
296 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
299 case TTS_CONFIG_TYPE_ENGINE:
301 /* TODO - Determine the policy when engine process get engine changed cb */
302 if (NULL == str_param) {
303 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
308 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
309 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
310 ret = ttsd_engine_agent_unload_current_engine();
312 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
315 ret = ttsd_engine_agent_load_current_engine();
317 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
322 /* stop all player */
323 ttsd_player_all_stop();
325 /* send interrupt message to all clients */
326 ttsd_data_foreach_clients(__get_client_cb, NULL);
328 ttsd_engine_cancel_synthesis();
333 case TTS_CONFIG_TYPE_VOICE:
335 if (NULL == str_param) {
336 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
340 char* out_lang = NULL;
344 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
345 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
346 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
348 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
350 /* Current language is not available */
351 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
353 if (NULL != out_lang) {
360 case TTS_CONFIG_TYPE_SPEED:
362 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
363 /* set default speed */
365 ret = ttsd_engine_agent_set_default_speed(int_param);
367 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
373 case TTS_CONFIG_TYPE_PITCH:
375 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
376 /* set default speed */
378 ret = ttsd_engine_agent_set_default_pitch(int_param);
380 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
386 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
388 if (0.0 <= double_param && double_param <= 1.0) {
389 /* set default bg volume ratio */
391 ret = ttsd_player_set_background_volume_ratio(double_param);
393 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
403 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
405 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
406 ttsd_server_finalize(uid);
410 static int __terminate_server()
412 if (g_is_terminated) {
413 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
414 return TTSD_ERROR_NONE;
417 int ret = TTSD_ERROR_NONE;
418 ret = ttsd_ipc_close_connection();
419 if (TTSD_ERROR_NONE != ret) {
420 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_ipc_close_connection(). ret(%d)", ret);
421 return TTSD_ERROR_OPERATION_FAILED;
424 ret = ttsd_finalize();
425 if (TTSD_ERROR_NONE != ret) {
426 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_finalize(). ret(%d)", ret);
427 return TTSD_ERROR_OPERATION_FAILED;
430 g_is_terminated = true;
431 return TTSD_ERROR_NONE;
434 static Eina_Bool __quit_ecore_loop(void *data)
436 __terminate_server();
437 ecore_main_loop_quit();
442 void __screen_reader_changed_cb(bool value)
444 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
448 int ttsd_send_all_stop()
450 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
452 /* stop all player */
453 ttsd_player_all_stop();
455 /* send interrupt message to all clients */
456 ttsd_data_foreach_clients(__get_client_cb, NULL);
458 return TTSD_ERROR_NONE;
464 int ttsd_initialize(ttse_request_callback_s *callback)
466 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
467 g_is_terminated = false;
469 if (ttsd_config_initialize(__config_changed_cb)) {
470 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
474 if (ttsd_player_init()) {
475 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
476 return TTSD_ERROR_OPERATION_FAILED;
480 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
482 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
483 int ret = ttsd_player_set_background_volume_ratio(ratio);
485 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
487 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
490 /* Engine Agent initialize */
491 if (0 != ttsd_engine_agent_init()) {
492 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
493 return TTSD_ERROR_OPERATION_FAILED;
496 /* set current engine */
497 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
498 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
499 // g_is_engine = false;
501 // g_is_engine = true;
503 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
504 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
505 return TTSD_ERROR_OPERATION_FAILED;
508 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
510 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
511 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
514 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
515 if (NULL == g_check_client_timer) {
516 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
519 return TTSD_ERROR_NONE;
524 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
527 if (0 < g_list_length(g_proc_list)) {
528 iter = g_list_first(g_proc_list);
529 while (NULL != iter) {
530 g_proc_list = g_list_remove_link(g_proc_list, iter);
532 iter = g_list_first(g_proc_list);
537 ecore_timer_del(g_wait_timer);
539 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
542 if (g_check_client_timer) {
543 ecore_timer_del(g_check_client_timer);
544 g_check_client_timer = NULL;
545 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
548 if (g_quit_loop_timer) {
549 ecore_timer_del(g_quit_loop_timer);
550 g_quit_loop_timer = NULL;
551 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
554 ttsd_config_finalize();
556 int ret = TTSD_ERROR_NONE;
557 ret = ttsd_player_release();
558 if (TTSD_ERROR_NONE != ret) {
559 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_player_release(). ret(%d)", ret);
560 return TTSD_ERROR_OPERATION_FAILED;
563 ret = ttsd_engine_agent_release();
564 if (TTSD_ERROR_NONE != ret) {
565 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_engine_agent_release(). ret(%d)", ret);
566 return TTSD_ERROR_OPERATION_FAILED;
569 return TTSD_ERROR_NONE;
574 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
575 int ret = ttsd_data_foreach_clients(__terminate_client, NULL);
576 if (TTSD_ERROR_NONE != ret) {
577 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to terminate all clients. ret(%d)", ret);
578 return TTSD_ERROR_OPERATION_FAILED;
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 __terminate_server();
591 * TTS Server Functions for Client
594 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
596 if (-1 != ttsd_data_is_client(uid))
597 *is_initialized = true;
599 *is_initialized = false;
601 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
602 return TTSD_ERROR_NONE;
605 int ttsd_server_initialize(int pid, unsigned int uid, tts_ipc_method_e method, bool* credential_needed)
607 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
609 if (-1 != ttsd_data_is_client(uid)) {
610 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
611 return TTSD_ERROR_NONE;
614 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
615 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
616 return TTSD_ERROR_OPERATION_FAILED;
619 if (true == *credential_needed) {
621 if (0 != app_manager_get_app_id(pid, &appid)) {
622 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
624 bool is_agreed = false;
625 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
626 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
629 return TTSD_ERROR_OPERATION_FAILED;
634 if (false == is_agreed) {
635 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
636 return TTSD_ERROR_PERMISSION_DENIED;
640 if (0 != ttsd_data_new_client(pid, uid)) {
641 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
642 return TTSD_ERROR_OPERATION_FAILED;
645 if (0 != ttsd_data_set_ipc_method(uid, method)) {
646 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
647 return TTSD_ERROR_OPERATION_FAILED;
650 return TTSD_ERROR_NONE;
653 static void __read_proc()
656 struct dirent *dirp = NULL;
660 if (0 < g_list_length(g_proc_list)) {
661 iter = g_list_first(g_proc_list);
662 while (NULL != iter) {
663 g_proc_list = g_list_remove_link(g_proc_list, iter);
665 iter = g_list_first(g_proc_list);
669 dp = opendir("/proc");
671 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
677 tmp = atoi(dirp->d_name);
678 if (0 >= tmp) continue;
679 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
681 } while (NULL != dirp);
687 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
693 for (i = 0; i < g_list_length(g_proc_list); i++) {
694 iter = g_list_nth(g_proc_list, i);
696 if (pid == GPOINTER_TO_INT(iter->data)) {
697 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
704 if (false == exist) {
705 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
706 ttsd_server_finalize(uid);
711 char appid[1024] = {0, };
712 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
713 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
716 if (0 < strlen(appid)) {
717 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
719 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
722 result = ttsdc_send_hello(pid, uid);
725 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) should be removed.", uid);
726 ttsd_server_finalize(uid);
727 } else if (-1 == result) {
728 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
736 Eina_Bool ttsd_cleanup_client(void *data)
738 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
741 if (0 < ttsd_data_get_client_count()) {
742 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
744 if (NULL == g_quit_loop_timer) {
745 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
746 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
748 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
749 g_check_client_timer = NULL;
753 SLOG(LOG_DEBUG, tts_tag(), "@@@");
758 void __used_voice_cb(const char* lang, int type)
760 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
761 if (0 != ttsd_engine_unload_voice(lang, type)) {
762 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
766 int ttsd_server_finalize(unsigned int uid)
768 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
769 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);
771 if (0 > ttsd_data_is_client(uid)) {
772 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
775 ttsd_server_stop(uid);
777 /* Need to unload voice when used voice is unregistered */
778 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
779 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
780 return TTSD_ERROR_OPERATION_FAILED;
783 ttsd_data_delete_client(uid);
785 /* unload engine, if ref count of client is 0 */
786 if (0 == ttsd_data_get_client_count()) {
787 if (NULL == g_quit_loop_timer) {
788 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
789 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
791 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
795 return TTSD_ERROR_NONE;
798 int ttsd_server_set_mode(unsigned int uid, ttsd_mode_e mode)
800 app_tts_state_e state = ttsd_data_get_client_state(uid);
801 if (APP_STATE_NONE == state) {
802 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
803 return TTSD_ERROR_INVALID_PARAMETER;
806 if (APP_STATE_READY != state) {
807 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
808 return TTSD_ERROR_INVALID_STATE;
811 int ret = ttsd_data_set_mode(uid, mode);
813 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
815 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%u), mode(%d)", uid, mode);
821 static bool __is_connected_to_network()
824 int network_status = 0;
825 int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
827 SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
831 if (network_status == VCONFKEY_NETWORK_OFF) {
832 SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
836 SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
841 int ttsd_server_add_text(unsigned int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
843 app_tts_state_e state = ttsd_data_get_client_state(uid);
844 if (APP_STATE_NONE == state) {
845 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_text : uid is not valid");
846 return TTSD_ERROR_INVALID_PARAMETER;
849 /* check valid voice */
850 char* temp_lang = NULL;
852 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
853 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
854 if (NULL != temp_lang) {
858 return TTSD_ERROR_INVALID_VOICE;
861 if (NULL == temp_lang) {
862 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
863 return TTSD_ERROR_INVALID_VOICE;
866 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
867 if (NULL == speak_data) {
868 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
869 if (NULL != temp_lang) {
874 return TTSD_ERROR_OPERATION_FAILED;
877 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);
879 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
881 ret = ttsd_data_add_speak_data(uid, speak_data);
883 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
884 if (NULL != temp_lang) {
889 ttsd_data_destroy_speak_data(speak_data);
895 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
896 /* Request load voice */
897 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
898 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
899 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
903 if (NULL != temp_lang) {
908 if (APP_STATE_PLAYING == state) {
909 /* check if engine use network */
910 if (ttsd_engine_agent_need_network()) {
911 if (false == __is_connected_to_network()) {
912 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
913 return TTSD_ERROR_OPERATION_FAILED;
917 ttsd_data_set_credential(uid, credential);
919 /* Check whether tts-engine is running or not */
920 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
921 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
922 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
923 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
929 return TTSD_ERROR_NONE;
932 void __send_interrupt_client(unsigned int uid)
934 SLOG(LOG_INFO, tts_tag(), "[Server] Send new state by policy. uid(%u)", uid);
935 int pid = ttsd_data_get_pid(uid);
937 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
941 app_tts_state_e state = ttsd_data_get_client_state(uid);
942 if (APP_STATE_NONE == state) {
943 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state. uid(%u), pid(%d)", uid, pid);
947 ttsdc_ipc_send_set_state_message(pid, uid, state);
948 SLOG(LOG_INFO, tts_tag(), "[Server] Finish to send new state by policy. uid(%u)", uid);
952 int ttsd_server_play(unsigned int uid, const char* credential)
954 app_tts_state_e state = ttsd_data_get_client_state(uid);
955 if (APP_STATE_NONE == state) {
956 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
957 return TTSD_ERROR_INVALID_PARAMETER;
960 if (APP_STATE_PLAYING == state) {
961 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
962 return TTSD_ERROR_NONE;
965 /* check if engine use network */
966 if (ttsd_engine_agent_need_network()) {
967 if (false == __is_connected_to_network()) {
968 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
969 return TTSD_ERROR_OUT_OF_NETWORK;
973 /* check the current playback focus */
974 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
975 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
976 ttsd_data_clear_data(uid);
977 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
980 unsigned int current_uid = ttsd_data_get_current_playing();
981 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
983 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
984 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
985 /* Send interrupt message */
986 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
989 if (0 != ttsd_server_stop(current_uid)) {
990 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
993 /* Default mode policy of interrupt is "Pause" */
995 /* Send interrupt message */
996 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
999 if (0 != ttsd_server_pause(current_uid)) {
1000 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1004 __send_interrupt_client(current_uid);
1007 /* Change current play */
1008 if (0 != ttsd_player_wait_to_play(uid)) {
1009 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1012 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1013 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1014 return TTSD_ERROR_OPERATION_FAILED;
1017 int ret = TTSD_ERROR_NONE;
1018 if (APP_STATE_PAUSED == state) {
1019 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1020 g_is_paused = false;
1021 ret = ttsd_player_resume(uid);
1023 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1024 ret = ttsd_player_play(uid);
1027 if (TTSD_ERROR_NONE != ret) {
1028 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1029 ttsd_data_set_client_state(uid, state);
1030 return TTSD_ERROR_OPERATION_FAILED;
1033 ttsd_data_set_credential(uid, credential);
1035 /* Check whether tts-engine is running or not */
1036 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1037 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1038 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1039 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1040 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1045 return TTSD_ERROR_NONE;
1048 int ttsd_server_stop(unsigned int uid)
1050 app_tts_state_e state = ttsd_data_get_client_state(uid);
1051 if (APP_STATE_NONE == state) {
1052 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1053 return TTSD_ERROR_INVALID_PARAMETER;
1056 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1058 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1059 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1060 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1061 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1062 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1064 int ret = ttsd_engine_cancel_synthesis();
1065 if (TTSD_ERROR_NONE != ret)
1066 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1068 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1071 ttsd_data_set_client_state(uid, APP_STATE_READY);
1072 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1073 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1077 /* Reset all data */
1078 ttsd_data_clear_data(uid);
1079 g_is_paused = false;
1081 return TTSD_ERROR_NONE;
1084 int ttsd_server_pause(unsigned int uid)
1086 app_tts_state_e state = ttsd_data_get_client_state(uid);
1087 if (APP_STATE_NONE == state) {
1088 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1089 return TTSD_ERROR_INVALID_PARAMETER;
1092 if (APP_STATE_PLAYING != state) {
1093 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1094 return TTSD_ERROR_INVALID_STATE;
1097 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1098 if (TTSD_ERROR_NONE != ret) {
1099 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
1100 return TTSD_ERROR_OPERATION_FAILED;
1103 ret = ttsd_player_pause(uid);
1104 if (TTSD_ERROR_NONE != ret) {
1105 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1106 return TTSD_ERROR_OPERATION_FAILED;
1111 return TTSD_ERROR_NONE;
1114 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1116 if (0 > ttsd_data_is_client(uid)) {
1117 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1118 return TTSD_ERROR_INVALID_PARAMETER;
1122 int ret = ttsd_engine_get_voice_list(voice_list);
1124 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1128 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1130 return TTSD_ERROR_NONE;
1133 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1135 if (0 > ttsd_data_is_client(uid)) {
1136 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1137 return TTSD_ERROR_INVALID_PARAMETER;
1140 /* get current voice */
1141 int ret = ttsd_engine_get_default_voice(language, voice_type);
1143 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1147 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1149 return TTSD_ERROR_NONE;
1152 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1154 app_tts_state_e state = ttsd_data_get_client_state(uid);
1155 if (APP_STATE_NONE == state) {
1156 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1157 return TTSD_ERROR_INVALID_PARAMETER;
1160 if (APP_STATE_READY != state) {
1161 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1162 return TTSD_ERROR_INVALID_STATE;
1165 int ret = ttsd_engine_set_private_data(key, data);
1167 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1169 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1175 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1177 app_tts_state_e state = ttsd_data_get_client_state(uid);
1178 if (APP_STATE_NONE == state) {
1179 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1180 return TTSD_ERROR_INVALID_PARAMETER;
1183 if (APP_STATE_READY != state) {
1184 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1185 return TTSD_ERROR_INVALID_STATE;
1188 int ret = ttsd_engine_get_private_data(key, data);
1190 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1192 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1198 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1200 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1203 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1205 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1211 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1213 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1216 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1218 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1224 int ttsd_server_play_pcm(unsigned int uid)
1226 app_tts_state_e state = ttsd_data_get_client_state(uid);
1227 if (APP_STATE_NONE == state) {
1228 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1229 return TTSD_ERROR_INVALID_PARAMETER;
1232 if (APP_STATE_PLAYING == state) {
1233 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1234 return TTSD_ERROR_NONE;
1237 unsigned int current_uid = ttsd_data_get_current_playing();
1238 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1240 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1241 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1242 /* Send interrupt message */
1243 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1246 if (0 != ttsd_server_stop(current_uid)) {
1247 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1250 /* Default mode policy of interrupt is "Pause" */
1252 /* Send interrupt message */
1253 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1256 if (0 != ttsd_server_pause(current_uid)) {
1257 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1261 __send_interrupt_client(current_uid);
1264 /* Change current play */
1265 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1266 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1267 return TTSD_ERROR_OPERATION_FAILED;
1270 int ret = TTSD_ERROR_NONE;
1271 if (APP_STATE_PAUSED == state) {
1272 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1273 g_is_paused = false;
1274 ret = ttsd_player_resume(uid);
1276 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1277 ret = ttsd_player_play(uid);
1280 if (TTSD_ERROR_NONE != ret) {
1281 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1282 ttsd_data_set_client_state(uid, state);
1283 return TTSD_ERROR_OPERATION_FAILED;
1286 return TTSD_ERROR_NONE;
1289 int ttsd_server_stop_pcm(unsigned int uid)
1291 app_tts_state_e state = ttsd_data_get_client_state(uid);
1292 if (APP_STATE_NONE == state) {
1293 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1294 return TTSD_ERROR_INVALID_PARAMETER;
1297 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1299 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1300 ttsd_data_set_client_state(uid, APP_STATE_READY);
1303 /* Reset all data */
1304 ttsd_data_clear_data(uid);
1306 ttsd_player_stop(uid);
1308 return TTSD_ERROR_NONE;
1311 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1313 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1317 /* Synthesis is success */
1318 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1319 if (TTSE_RESULT_EVENT_START == event) {
1320 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1321 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1322 uid, uttid, data, data_size, audio_type, rate);
1323 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1324 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1325 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1326 uid, uttid, data, data_size, audio_type, rate);
1328 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1331 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1332 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1333 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1334 return TTSD_ERROR_INVALID_PARAMETER;
1337 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1338 if (NULL == sound_data) {
1339 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1340 return TTSD_ERROR_OUT_OF_MEMORY;
1343 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1344 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1345 ttsd_data_destroy_sound_data(sound_data);
1348 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1351 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1353 return TTSD_ERROR_NONE;