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_terminated = false;
59 /* Function definitions */
60 static int __stop_and_send_ready_state(unsigned int uid)
62 int ret = ttsd_server_stop(uid);
63 if (TTSD_ERROR_NONE != ret) {
64 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_stop() : uid(%u). ret(%d)", uid, ret);
68 int pid = ttsd_data_get_pid(uid);
70 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
71 return TTSD_ERROR_OPERATION_FAILED;
73 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
79 static void __synthesis(unsigned int uid);
81 static Eina_Bool __wait_synthesis(void *data)
83 /* get current play */
84 unsigned int uid = ttsd_data_get_current_playing();
87 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
88 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
91 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
93 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
94 /* Start next synthesis */
105 static void __synthesis(unsigned int uid)
107 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
109 speak_data_s* speak_data = NULL;
110 int ret = ttsd_data_get_speak_data(uid, &speak_data);
111 if (TTSD_ERROR_NONE != ret || NULL == speak_data) {
112 SLOG(LOG_DEBUG, tts_tag(), "@@@ Fail to get speak data. ret(%d)", ret);
113 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
117 int pid = ttsd_data_get_pid(uid);
119 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
120 ttsd_data_destroy_speak_data(speak_data);
125 char appid[1024] = {0, };
126 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
127 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
130 if (NULL == speak_data->lang || NULL == speak_data->text) {
131 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
132 __stop_and_send_ready_state(uid);
133 ttsd_data_destroy_speak_data(speak_data);
139 g_utt.uttid = speak_data->utt_id;
141 char* credential = ttsd_data_get_credential(uid);
143 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
144 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
145 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
146 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
147 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
148 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
150 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
151 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
152 if (TTSD_ERROR_NONE != ret) {
153 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
155 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
156 __stop_and_send_ready_state(uid);
158 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
163 ttsd_data_destroy_speak_data(speak_data);
165 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
171 * TTS Server Callback Functions
173 int ttsd_send_error(ttse_error_e error, const char* msg)
175 unsigned int uid = g_utt.uid;
176 int uttid = g_utt.uttid;
177 int tmp_pid = ttsd_data_get_pid(uid);
179 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));
181 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
183 if (0 != ttsd_data_clear_data(uid))
184 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
187 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
188 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
191 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
192 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
195 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
196 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
201 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)
203 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
205 unsigned int uid = g_utt.uid;
206 int uttid = g_utt.uttid;
208 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
209 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
210 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
211 SLOG(LOG_DEBUG, tts_tag(), "@@@");
212 return TTSD_ERROR_INVALID_PARAMETER;
215 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
216 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
217 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
218 return TTSD_ERROR_INVALID_PARAMETER;
221 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
222 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
223 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
224 SLOG(LOG_DEBUG, tts_tag(), "@@@");
225 return TTSD_ERROR_INVALID_PARAMETER;
228 /* check the current state */
229 app_tts_state_e state = ttsd_data_get_client_state(uid);
230 if (APP_STATE_NONE == state) {
231 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
232 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
233 return TTSD_ERROR_INVALID_PARAMETER;
236 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
237 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
238 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
239 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
240 return TTSD_ERROR_NONE;
243 /* Synthesis is success */
244 if (TTSE_RESULT_EVENT_START == event) {
245 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
246 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
247 uid, uttid, data, data_size, audio_type, rate);
249 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
250 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
251 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
252 double d_latency = (double)latency;
253 g_avg_latency += d_latency;
254 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
255 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
257 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);
258 } else if (TTSE_RESULT_EVENT_FINISH == event) {
259 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
260 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
261 uid, uttid, data, data_size, audio_type, rate);
263 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
267 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
268 if (NULL == sound_data) {
269 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
270 return TTSD_ERROR_OUT_OF_MEMORY;
273 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
274 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
275 ttsd_data_destroy_sound_data(sound_data);
277 return TTSD_ERROR_OPERATION_FAILED;
280 if (event == TTSE_RESULT_EVENT_FINISH) {
281 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
284 return TTSD_ERROR_NONE;
287 static bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
289 if (0 > ttsd_data_is_client(uid)) {
290 SLOG(LOG_ERROR, tts_tag(), "[Server] Client(%d) is not valid.", uid);
291 ttsd_data_delete_client(uid);
295 /* clear client data */
296 ttsd_data_clear_data(uid);
298 if (APP_STATE_READY != state) {
299 ttsd_data_set_client_state(uid, APP_STATE_READY);
300 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
306 static void __stop_all_client()
308 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
310 ttsd_engine_cancel_synthesis();
311 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
313 /* stop all player */
314 ttsd_player_all_stop();
316 /* send interrupt message to all clients */
317 ttsd_data_foreach_clients(__get_client_cb, NULL);
320 static void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
323 case TTS_CONFIG_TYPE_ENGINE:
325 /* TODO - Determine the policy when engine process get engine changed cb */
326 if (NULL == str_param) {
327 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
332 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
333 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
334 ret = ttsd_engine_agent_unload_current_engine();
336 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
339 ret = ttsd_engine_agent_load_current_engine();
341 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
350 case TTS_CONFIG_TYPE_VOICE:
352 if (NULL == str_param) {
353 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
357 char* out_lang = NULL;
361 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
362 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
363 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
365 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
367 /* Current language is not available */
368 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
370 if (NULL != out_lang) {
377 case TTS_CONFIG_TYPE_SPEED:
379 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
380 /* set default speed */
382 ret = ttsd_engine_agent_set_default_speed(int_param);
384 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
390 case TTS_CONFIG_TYPE_PITCH:
392 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
393 /* set default speed */
395 ret = ttsd_engine_agent_set_default_pitch(int_param);
397 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
403 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
405 if (0.0 <= double_param && double_param <= 1.0) {
406 /* set default bg volume ratio */
408 ret = ttsd_player_set_background_volume_ratio(double_param);
410 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
420 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
422 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
423 ttsd_server_finalize(uid);
427 static int __terminate_server()
429 if (g_is_terminated) {
430 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
431 return TTSD_ERROR_NONE;
434 int ret = TTSD_ERROR_NONE;
435 ret = ttsd_ipc_close_connection();
436 if (TTSD_ERROR_NONE != ret) {
437 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_ipc_close_connection(). ret(%d)", ret);
438 return TTSD_ERROR_OPERATION_FAILED;
441 ret = ttsd_finalize();
442 if (TTSD_ERROR_NONE != ret) {
443 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_finalize(). ret(%d)", ret);
444 return TTSD_ERROR_OPERATION_FAILED;
447 g_is_terminated = true;
448 return TTSD_ERROR_NONE;
451 static Eina_Bool __quit_ecore_loop(void *data)
453 __terminate_server();
454 ecore_main_loop_quit();
459 void __screen_reader_changed_cb(bool value)
461 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
465 static void __read_proc()
468 struct dirent *dirp = NULL;
472 if (0 < g_list_length(g_proc_list)) {
473 iter = g_list_first(g_proc_list);
474 while (NULL != iter) {
475 g_proc_list = g_list_remove_link(g_proc_list, iter);
477 iter = g_list_first(g_proc_list);
481 dp = opendir("/proc");
483 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
489 tmp = atoi(dirp->d_name);
490 if (0 >= tmp) continue;
491 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
493 } while (NULL != dirp);
499 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
505 for (i = 0; i < g_list_length(g_proc_list); i++) {
506 iter = g_list_nth(g_proc_list, i);
508 if (pid == GPOINTER_TO_INT(iter->data)) {
509 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
516 if (false == exist) {
517 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
518 ttsd_server_finalize(uid);
524 static Eina_Bool __cleanup_client(void *data)
526 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
529 if (0 < ttsd_data_get_client_count()) {
530 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
532 if (NULL == g_quit_loop_timer) {
533 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
534 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
536 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
537 g_check_client_timer = NULL;
541 SLOG(LOG_DEBUG, tts_tag(), "@@@");
549 int ttsd_initialize(ttse_request_callback_s *callback)
551 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
552 g_is_terminated = false;
554 if (ttsd_config_initialize(__config_changed_cb)) {
555 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
559 if (ttsd_player_init()) {
560 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
561 return TTSD_ERROR_OPERATION_FAILED;
565 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
567 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
568 int ret = ttsd_player_set_background_volume_ratio(ratio);
570 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
572 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
575 /* Engine Agent initialize */
576 if (TTSD_ERROR_NONE != ttsd_engine_agent_init(callback)) {
577 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
578 return TTSD_ERROR_OPERATION_FAILED;
581 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
582 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
584 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, __cleanup_client, NULL);
585 if (NULL == g_check_client_timer) {
586 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
589 return TTSD_ERROR_NONE;
594 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
597 if (0 < g_list_length(g_proc_list)) {
598 iter = g_list_first(g_proc_list);
599 while (NULL != iter) {
600 g_proc_list = g_list_remove_link(g_proc_list, iter);
602 iter = g_list_first(g_proc_list);
607 ecore_timer_del(g_wait_timer);
609 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting 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 ttsd_config_finalize();
626 int ret = TTSD_ERROR_NONE;
627 ret = ttsd_player_release();
628 if (TTSD_ERROR_NONE != ret) {
629 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_player_release(). ret(%d)", ret);
630 return TTSD_ERROR_OPERATION_FAILED;
633 ret = ttsd_engine_agent_release();
634 if (TTSD_ERROR_NONE != ret) {
635 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_engine_agent_release(). ret(%d)", ret);
636 return TTSD_ERROR_OPERATION_FAILED;
639 return TTSD_ERROR_NONE;
644 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
645 int ret = ttsd_data_foreach_clients(__terminate_client, NULL);
646 if (TTSD_ERROR_NONE != ret) {
647 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to terminate all clients. ret(%d)", ret);
648 return TTSD_ERROR_OPERATION_FAILED;
651 if (g_quit_loop_timer) {
652 ecore_timer_del(g_quit_loop_timer);
653 g_quit_loop_timer = NULL;
654 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
657 return __terminate_server();
661 * TTS Server Functions for Client
664 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
666 if (-1 != ttsd_data_is_client(uid))
667 *is_initialized = true;
669 *is_initialized = false;
671 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
672 return TTSD_ERROR_NONE;
675 int ttsd_server_initialize(int pid, unsigned int uid, ttsd_mode_e mode, tts_ipc_method_e method, bool* credential_needed)
677 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
679 if (-1 != ttsd_data_is_client(uid)) {
680 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
681 return TTSD_ERROR_NONE;
684 if (0 != ttsd_data_new_client(pid, uid, mode, method)) {
685 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
686 return TTSD_ERROR_OPERATION_FAILED;
689 if (TTSD_ERROR_NONE != ttsd_engine_agent_load_current_engine()) {
690 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
691 ttsd_data_delete_client(uid);
692 return TTSD_ERROR_OPERATION_FAILED;
695 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
696 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
697 ttsd_data_delete_client(uid);
698 return TTSD_ERROR_OPERATION_FAILED;
701 if (true == *credential_needed) {
703 if (0 != app_manager_get_app_id(pid, &appid)) {
704 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
706 bool is_agreed = false;
707 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
708 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
711 ttsd_data_delete_client(uid);
712 return TTSD_ERROR_OPERATION_FAILED;
717 if (false == is_agreed) {
718 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
719 ttsd_data_delete_client(uid);
720 return TTSD_ERROR_PERMISSION_DENIED;
724 return TTSD_ERROR_NONE;
727 void __used_voice_cb(const char* lang, int type)
729 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
730 if (0 != ttsd_engine_unload_voice(lang, type)) {
731 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
735 int ttsd_server_finalize(unsigned int uid)
737 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
738 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);
740 if (0 > ttsd_data_is_client(uid)) {
741 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
744 int ret = ttsd_server_stop(uid);
745 if (TTSD_ERROR_NONE != ret) {
746 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS server top(%u) fail (%d/%s) <<<<<", uid, ret, get_error_message(ret));
749 /* Need to unload voice when used voice is unregistered */
750 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
751 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
752 return TTSD_ERROR_OPERATION_FAILED;
755 ttsd_data_delete_client(uid);
757 /* unload engine, if ref count of client is 0 */
758 if (0 == ttsd_data_get_client_count()) {
759 if (NULL == g_quit_loop_timer) {
760 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
761 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
763 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
767 return TTSD_ERROR_NONE;
770 static bool __is_connected_to_network()
773 int network_status = 0;
774 int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
776 SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
780 if (network_status == VCONFKEY_NETWORK_OFF) {
781 SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
785 SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
790 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)
792 app_tts_state_e state = ttsd_data_get_client_state(uid);
793 if (APP_STATE_NONE == state) {
794 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_text : uid is not valid");
795 return TTSD_ERROR_INVALID_PARAMETER;
798 /* check valid voice */
799 char* temp_lang = NULL;
801 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
802 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
803 if (NULL != temp_lang) {
807 return TTSD_ERROR_INVALID_VOICE;
810 if (NULL == temp_lang) {
811 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
812 return TTSD_ERROR_INVALID_VOICE;
815 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
816 if (NULL == speak_data) {
817 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
818 if (NULL != temp_lang) {
823 return TTSD_ERROR_OPERATION_FAILED;
826 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);
828 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
830 ret = ttsd_data_add_speak_data(uid, speak_data);
832 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
833 if (NULL != temp_lang) {
838 ttsd_data_destroy_speak_data(speak_data);
844 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
845 /* Request load voice */
846 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
847 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
848 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
852 if (NULL != temp_lang) {
857 if (APP_STATE_PLAYING == state) {
858 /* check if engine use network */
859 if (ttsd_engine_agent_need_network()) {
860 if (false == __is_connected_to_network()) {
861 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
862 return TTSD_ERROR_OPERATION_FAILED;
866 ttsd_data_set_credential(uid, credential);
868 /* Check whether tts-engine is running or not */
869 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
870 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
871 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
872 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
878 return TTSD_ERROR_NONE;
881 static int __interrupt_player_by_policy(unsigned int request_app_uid)
883 unsigned int playing_app_uid = ttsd_data_get_current_playing();
884 if (0 > ttsd_data_is_client(playing_app_uid)) {
885 SLOG(LOG_INFO, tts_tag(), "[Server] There is no app which is in 'Play' state");
886 return TTSD_ERROR_NONE;
889 if (request_app_uid == playing_app_uid) {
890 SLOG(LOG_WARN, tts_tag(), "[Server] This is played uid. Skip policy behavior");
891 return TTSD_ERROR_NONE;
894 ttsd_mode_e request_app_mode = ttsd_data_get_mode(request_app_uid);
895 ttsd_mode_e playing_app_mode = ttsd_data_get_mode(playing_app_uid);
896 switch (playing_app_mode) {
897 case TTSD_MODE_DEFAULT:
898 case TTSD_MODE_NOTIFICATION:
899 case TTSD_MODE_SCREEN_READER:
900 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
901 __stop_and_send_ready_state(playing_app_uid);
904 case TTSD_MODE_INTERRUPT:
905 if (TTSD_MODE_INTERRUPT != request_app_mode) {
906 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Default, Screen reader, and Noti are prohibitted.");
907 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
910 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
911 __stop_and_send_ready_state(playing_app_uid);
915 return TTSD_ERROR_NONE;
918 int ttsd_server_play(unsigned int uid, const char* credential)
920 app_tts_state_e state = ttsd_data_get_client_state(uid);
921 if (APP_STATE_NONE == state) {
922 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
923 return TTSD_ERROR_INVALID_PARAMETER;
926 if (APP_STATE_PLAYING == state) {
927 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
928 return TTSD_ERROR_NONE;
931 /* check if engine use network */
932 if (ttsd_engine_agent_need_network()) {
933 if (false == __is_connected_to_network()) {
934 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
935 return TTSD_ERROR_OUT_OF_NETWORK;
939 int ret = __interrupt_player_by_policy(uid);
940 if (TTSD_ERROR_NONE != ret) {
941 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
945 /* Change current play */
946 if (0 != ttsd_player_wait_to_play(uid)) {
947 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
950 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
951 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
952 return TTSD_ERROR_OPERATION_FAILED;
955 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
956 ret = ttsd_player_play(uid);
957 if (TTSD_ERROR_NONE != ret) {
958 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
959 ttsd_data_set_client_state(uid, state);
960 return TTSD_ERROR_OPERATION_FAILED;
963 ttsd_data_set_credential(uid, credential);
965 /* Check whether tts-engine is running or not */
966 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
967 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
968 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
969 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
970 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
974 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_SYNTH);
976 return TTSD_ERROR_NONE;
979 static void __stop_engine_synthesis(unsigned int uid)
981 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
982 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
984 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
985 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
987 int ret = ttsd_engine_cancel_synthesis();
988 if (TTSD_ERROR_NONE != ret)
989 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
992 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
995 int ttsd_server_stop(unsigned int uid)
997 app_tts_state_e state = ttsd_data_get_client_state(uid);
998 if (APP_STATE_NONE == state) {
999 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1000 return TTSD_ERROR_INVALID_PARAMETER;
1003 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1005 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1006 if (TTS_APP_PLAY_TYPE_SYNTH == ttsd_data_get_play_type(uid)) {
1007 __stop_engine_synthesis(uid);
1011 ttsd_data_set_client_state(uid, APP_STATE_READY);
1012 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1013 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1017 /* Reset all data */
1018 ttsd_data_clear_data(uid);
1020 return TTSD_ERROR_NONE;
1023 int ttsd_server_pause(unsigned int uid)
1025 app_tts_state_e state = ttsd_data_get_client_state(uid);
1026 if (APP_STATE_NONE == state) {
1027 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1028 return TTSD_ERROR_INVALID_PARAMETER;
1031 if (APP_STATE_PLAYING != state) {
1032 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1033 return TTSD_ERROR_INVALID_STATE;
1036 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1037 if (TTSD_ERROR_NONE != ret) {
1038 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
1039 return TTSD_ERROR_OPERATION_FAILED;
1042 ret = ttsd_player_pause(uid);
1043 if (TTSD_ERROR_NONE != ret) {
1044 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1045 return TTSD_ERROR_OPERATION_FAILED;
1048 return TTSD_ERROR_NONE;
1051 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1053 if (0 > ttsd_data_is_client(uid)) {
1054 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1055 return TTSD_ERROR_INVALID_PARAMETER;
1059 int ret = ttsd_engine_get_voice_list(voice_list);
1061 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1065 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1067 return TTSD_ERROR_NONE;
1070 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1072 if (0 > ttsd_data_is_client(uid)) {
1073 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1074 return TTSD_ERROR_INVALID_PARAMETER;
1077 /* get current voice */
1078 int ret = ttsd_engine_get_default_voice(language, voice_type);
1080 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1084 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1086 return TTSD_ERROR_NONE;
1089 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1091 app_tts_state_e state = ttsd_data_get_client_state(uid);
1092 if (APP_STATE_NONE == state) {
1093 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1094 return TTSD_ERROR_INVALID_PARAMETER;
1097 if (APP_STATE_READY != state) {
1098 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1099 return TTSD_ERROR_INVALID_STATE;
1102 int ret = ttsd_engine_set_private_data(key, data);
1104 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1106 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1112 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1114 app_tts_state_e state = ttsd_data_get_client_state(uid);
1115 if (APP_STATE_NONE == state) {
1116 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1117 return TTSD_ERROR_INVALID_PARAMETER;
1120 if (APP_STATE_READY != state) {
1121 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1122 return TTSD_ERROR_INVALID_STATE;
1125 char* temp_data = NULL;
1126 int ret = ttsd_engine_get_private_data(key, &temp_data);
1128 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1130 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1133 if (NULL == temp_data) {
1134 temp_data = strdup("NULL");
1141 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1143 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1146 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1148 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1154 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1156 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1159 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1161 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1167 int ttsd_server_play_pcm(unsigned int uid)
1169 app_tts_state_e state = ttsd_data_get_client_state(uid);
1170 if (APP_STATE_NONE == state) {
1171 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1172 return TTSD_ERROR_INVALID_PARAMETER;
1175 if (APP_STATE_PLAYING == state) {
1176 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1177 return TTSD_ERROR_NONE;
1180 int ret = __interrupt_player_by_policy(uid);
1181 if (TTSD_ERROR_NONE != ret) {
1182 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1186 /* Change current play */
1187 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1188 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1189 return TTSD_ERROR_OPERATION_FAILED;
1192 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1193 ret = ttsd_player_play(uid);
1194 if (TTSD_ERROR_NONE != ret) {
1195 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1196 ttsd_data_set_client_state(uid, state);
1197 return TTSD_ERROR_OPERATION_FAILED;
1200 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_PCM);
1202 return TTSD_ERROR_NONE;
1205 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1207 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1211 /* Synthesis is success */
1212 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1213 if (TTSE_RESULT_EVENT_START == event) {
1214 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1215 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1216 uid, uttid, data, data_size, audio_type, rate);
1217 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1218 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1219 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1220 uid, uttid, data, data_size, audio_type, rate);
1222 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1225 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1226 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1227 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1228 return TTSD_ERROR_INVALID_PARAMETER;
1231 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1232 if (NULL == sound_data) {
1233 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1234 return TTSD_ERROR_OUT_OF_MEMORY;
1237 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1238 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1239 ttsd_data_destroy_sound_data(sound_data);
1242 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1245 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1247 return TTSD_ERROR_NONE;