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.
17 #include "ttsd_config.h"
18 #include "ttsd_data.h"
19 #include "ttsd_dbus.h"
20 #include "ttsd_dbus_server.h"
21 #include "ttsd_engine_agent.h"
22 #include "ttsd_main.h"
23 #include "ttsd_network.h"
24 #include "ttsd_player.h"
25 #include "ttsd_server.h"
28 #define CLIENT_CLEAN_UP_TIME 500
32 TTSD_SYNTHESIS_CONTROL_DOING = 0,
33 TTSD_SYNTHESIS_CONTROL_DONE = 1,
34 TTSD_SYNTHESIS_CONTROL_EXPIRED = 2
35 } ttsd_synthesis_control_e;
42 /* If current engine exist */
43 //static bool g_is_engine;
45 /* If engine is running */
46 static ttsd_synthesis_control_e g_synth_control;
48 static Ecore_Timer* g_check_client_timer = NULL;
49 static Ecore_Timer* g_wait_timer = NULL;
51 static utterance_t g_utt;
53 static GList *g_proc_list = NULL;
55 /* Function definitions */
56 static int __synthesis(int uid, const char* credential);
58 static int __server_set_synth_control(ttsd_synthesis_control_e control)
60 g_synth_control = control;
64 static ttsd_synthesis_control_e __server_get_synth_control()
66 return g_synth_control;
69 static Eina_Bool __wait_synthesis(void *data)
71 /* get current play */
72 char* credential = (char*)data;
73 int uid = ttsd_data_get_current_playing();
76 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
81 if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
82 /* Start next synthesis */
83 __synthesis(uid, credential);
93 static int __synthesis(int uid, const char* credential)
95 SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS START");
97 speak_data_s* speak_data = NULL;
98 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
99 if (NULL == speak_data) {
100 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
104 int pid = ttsd_data_get_pid(uid);
105 char appid[128] = {0, };
106 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
107 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
110 if (NULL == speak_data->lang || NULL == speak_data->text) {
111 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
112 ttsd_server_stop(uid);
114 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
116 if (NULL != speak_data) {
117 if (NULL != speak_data->lang) free(speak_data->lang);
118 if (NULL != speak_data->text) free(speak_data->text);
120 speak_data->lang = NULL;
121 speak_data->text = NULL;
131 g_utt.uttid = speak_data->utt_id;
133 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
134 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
135 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
136 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
137 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
138 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
141 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
142 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
144 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
146 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
148 ttsd_server_stop(uid);
150 int pid = ttsd_data_get_pid(uid);
151 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
153 g_wait_timer = ecore_timer_add(0, __wait_synthesis, (void*)credential);
156 if (NULL != speak_data) {
157 if (NULL != speak_data->lang) free(speak_data->lang);
158 if (NULL != speak_data->text) free(speak_data->text);
160 speak_data->lang = NULL;
161 speak_data->text = NULL;
168 SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS END");
169 SLOG(LOG_DEBUG, tts_tag(), " ");
175 * TTS Server Callback Functions
177 int ttsd_send_error(ttse_error_e error, const char* msg)
180 int uttid = g_utt.uttid;
181 int tmp_pid = ttsd_data_get_pid(uid);
183 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%d), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, msg);
185 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
187 if (0 != ttsd_player_clear(uid))
188 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
190 if (0 != ttsd_data_clear_data(uid))
191 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
193 if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
194 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
196 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
197 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
199 if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
200 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
205 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)
207 SLOG(LOG_DEBUG, tts_tag(), "===== SEND SYNTHESIS RESULT START");
210 int uttid = g_utt.uttid;
212 /* Synthesis is success */
213 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
214 if (TTSE_RESULT_EVENT_START == event) {
215 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
216 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
217 uid, uttid, data, data_size, audio_type, rate);
218 } else if (TTSE_RESULT_EVENT_FINISH == event) {
219 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
220 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
221 uid, uttid, data, data_size, audio_type, rate);
223 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
227 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
228 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
229 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
230 SLOG(LOG_DEBUG, tts_tag(), "=====");
231 SLOG(LOG_DEBUG, tts_tag(), " ");
232 return TTSD_ERROR_OPERATION_FAILED;
235 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
236 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
237 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
238 SLOG(LOG_DEBUG, tts_tag(), "=====");
239 SLOG(LOG_DEBUG, tts_tag(), " ");
240 return TTSD_ERROR_INVALID_PARAMETER;
244 sound_data_s* temp_sound_data = NULL;
245 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
246 if (NULL == temp_sound_data) {
247 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
248 return TTSD_ERROR_OUT_OF_MEMORY;
251 temp_sound_data->data = NULL;
252 temp_sound_data->rate = 0;
253 temp_sound_data->data_size = 0;
256 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
257 if (NULL != temp_sound_data->data) {
258 memcpy(temp_sound_data->data, data, data_size);
259 temp_sound_data->data_size = data_size;
260 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
261 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
263 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
266 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
269 temp_sound_data->utt_id = uttid;
270 temp_sound_data->event = event;
271 temp_sound_data->audio_type = audio_type;
272 temp_sound_data->rate = rate;
274 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
275 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
278 if (event == TTSE_RESULT_EVENT_FINISH) {
279 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
282 if (0 != ttsd_player_play(uid)) {
283 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
285 /* Change ready state */
286 ttsd_server_stop(uid);
289 tmp_pid = ttsd_data_get_pid(uid);
290 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
293 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
294 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
298 /*SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS RESULT CALLBACK END");
299 SLOG(LOG_DEBUG, tts_tag(), " ");*/
301 return TTSD_ERROR_NONE;
304 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
306 /* clear client data */
307 ttsd_data_clear_data(uid);
308 ttsd_data_set_client_state(uid, APP_STATE_READY);
311 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
313 ttsd_data_delete_client(uid);
319 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
322 case TTS_CONFIG_TYPE_ENGINE:
324 /* TODO - Determine the policy when engine process get engine changed cb */
325 if (NULL == str_param) {
326 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
331 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
332 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
333 ret = ttsd_engine_agent_unload_current_engine();
335 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
338 ret = ttsd_engine_agent_load_current_engine();
340 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
345 /* stop all player */
346 ttsd_player_all_stop();
348 /* send interrupt message to all clients */
349 ttsd_data_foreach_clients(__get_client_cb, NULL);
351 ttsd_engine_cancel_synthesis();
356 case TTS_CONFIG_TYPE_VOICE:
358 if (NULL == str_param) {
359 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
363 char* out_lang = NULL;
367 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
368 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
369 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
371 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
373 /* Current language is not available */
374 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
376 if (NULL != out_lang) free(out_lang);
380 case TTS_CONFIG_TYPE_SPEED:
382 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
383 /* set default speed */
385 ret = ttsd_engine_agent_set_default_speed(int_param);
387 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
393 case TTS_CONFIG_TYPE_PITCH:
395 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
396 /* set default speed */
398 ret = ttsd_engine_agent_set_default_pitch(int_param);
400 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
410 bool __terminate_client(int pid, int uid, app_state_e state, void* user_data)
412 SLOG(LOG_INFO, tts_tag(), "=== Start to terminate client [%d] ===", uid);
413 ttsd_server_finalize(uid);
417 Eina_Bool ttsd_terminate_daemon(void *data)
419 ttsd_data_foreach_clients(__terminate_client, NULL);
423 void __screen_reader_changed_cb(bool value)
425 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
426 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
427 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
429 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
437 int ttsd_initialize(ttse_request_callback_s *callback)
439 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
441 if (ttsd_config_initialize(__config_changed_cb)) {
442 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
446 if (ttsd_player_init()) {
447 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
448 return TTSD_ERROR_OPERATION_FAILED;
451 /* Engine Agent initialize */
452 if (0 != ttsd_engine_agent_init()) {
453 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
454 return TTSD_ERROR_OPERATION_FAILED;
457 /* set current engine */
458 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
459 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
460 // g_is_engine = false;
462 // g_is_engine = true;
464 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
465 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
466 return TTSD_ERROR_OPERATION_FAILED;
469 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
471 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
472 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
475 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
476 if (NULL == g_check_client_timer) {
477 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
480 return TTSD_ERROR_NONE;
485 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
488 if (0 < g_list_length(g_proc_list)) {
489 iter = g_list_first(g_proc_list);
490 while (NULL != iter) {
491 g_proc_list = g_list_remove_link(g_proc_list, iter);
493 iter = g_list_first(g_proc_list);
497 ttsd_config_finalize();
499 ttsd_player_release();
501 ttsd_engine_agent_release();
503 if (NULL != g_check_client_timer) {
504 ecore_timer_del(g_check_client_timer);
505 g_check_client_timer = NULL;
507 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore timer handle");
510 return TTSD_ERROR_NONE;
514 * TTS Server Functions for Client
517 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
519 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
521 if (-1 != ttsd_data_is_client(uid)) {
522 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
523 return TTSD_ERROR_NONE;
526 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
527 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to get credential necessity");
528 return TTSD_ERROR_OPERATION_FAILED;
530 if (0 != ttsd_data_new_client(pid, uid)) {
531 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
532 return TTSD_ERROR_OPERATION_FAILED;
535 if (0 != ttsd_player_create_instance(uid)) {
536 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
537 return TTSD_ERROR_OPERATION_FAILED;
540 return TTSD_ERROR_NONE;
543 static Eina_Bool __quit_ecore_loop(void *data)
545 ttsd_dbus_close_connection();
546 ttsd_network_finalize();
548 ecore_main_loop_quit();
554 static void __read_proc()
558 struct dirent *dirp = NULL;
563 if (0 < g_list_length(g_proc_list)) {
564 iter = g_list_first(g_proc_list);
565 while (NULL != iter) {
566 g_proc_list = g_list_remove_link(g_proc_list, iter);
568 iter = g_list_first(g_proc_list);
572 dp = opendir("/proc");
574 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
577 ret = readdir_r(dp, &entry, &dirp);
579 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to readdir");
584 tmp = atoi(dirp->d_name);
585 if (0 >= tmp) continue;
586 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
588 } while (NULL != dirp);
594 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
600 for (i = 0; i < g_list_length(g_proc_list); i++) {
601 iter = g_list_nth(g_proc_list, i);
603 if (pid == GPOINTER_TO_INT(iter->data)) {
604 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
611 if (false == exist) {
612 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
613 ttsd_server_finalize(uid);
618 char appid[128] = {0, };
619 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
620 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
623 if (0 < strlen(appid)) {
624 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
626 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
629 result = ttsdc_send_hello(pid, uid);
632 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
633 ttsd_server_finalize(uid);
634 } else if (-1 == result) {
635 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
643 Eina_Bool ttsd_cleanup_client(void *data)
645 SLOG(LOG_DEBUG, tts_tag(), "===== CLEAN UP CLIENT START");
648 if (0 < ttsd_data_get_client_count()) {
649 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
651 ecore_timer_add(0, __quit_ecore_loop, NULL);
654 SLOG(LOG_DEBUG, tts_tag(), "=====");
655 SLOG(LOG_DEBUG, tts_tag(), " ");
660 void __used_voice_cb(const char* lang, int type)
662 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
663 if (0 != ttsd_engine_unload_voice(lang, type)) {
664 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
668 int ttsd_server_finalize(int uid)
670 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
673 if (0 > ttsd_data_get_client_state(uid, &state)) {
674 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
677 ttsd_server_stop(uid);
678 ttsd_player_stop(uid);
680 ttsd_player_destroy_instance(uid);
682 /* Need to unload voice when used voice is unregistered */
683 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
684 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
685 return TTSD_ERROR_OPERATION_FAILED;
688 ttsd_data_delete_client(uid);
690 /* unload engine, if ref count of client is 0 */
691 if (0 == ttsd_data_get_client_count()) {
692 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
693 ecore_timer_add(0, __quit_ecore_loop, NULL);
696 return TTSD_ERROR_NONE;
699 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
702 if (0 > ttsd_data_get_client_state(uid, &state)) {
703 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
704 return TTSD_ERROR_INVALID_PARAMETER;
707 /* check valid voice */
708 char* temp_lang = NULL;
710 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
711 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
712 if (NULL != temp_lang) free(temp_lang);
713 return TTSD_ERROR_INVALID_VOICE;
716 if (NULL == temp_lang) {
717 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
718 return TTSD_ERROR_INVALID_VOICE;
721 speak_data_s* speak_data = NULL;
722 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
723 if (NULL == speak_data) {
724 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
725 if (NULL != temp_lang) free(temp_lang);
726 return TTSD_ERROR_OPERATION_FAILED;
729 speak_data->lang = strdup(lang);
730 speak_data->vctype = voice_type;
732 speak_data->speed = speed;
733 speak_data->utt_id = utt_id;
735 speak_data->text = strdup(text);
737 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);
739 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
740 if (0 != ttsd_data_add_speak_data(uid, speak_data)) {
741 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
742 if (NULL != temp_lang) free(temp_lang);
743 if (NULL != speak_data) {
744 if (NULL != speak_data->lang) free(speak_data->lang);
745 if (NULL != speak_data->text) free(speak_data->text);
747 speak_data->lang = NULL;
748 speak_data->text = NULL;
754 return TTSD_ERROR_OPERATION_FAILED;
757 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
758 /* Request load voice */
759 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
760 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
761 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
765 if (NULL != temp_lang) free(temp_lang);
767 if (APP_STATE_PLAYING == state) {
768 /* check if engine use network */
769 if (ttsd_engine_agent_need_network()) {
770 if (false == ttsd_network_is_connected()) {
771 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
772 return TTSD_ERROR_OPERATION_FAILED;
776 /* Check whether tts-engine is running or not */
777 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
778 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
780 __synthesis(uid, credential);
784 return TTSD_ERROR_NONE;
787 Eina_Bool __send_interrupt_client(void *data)
789 intptr_t puid = (intptr_t)data;
792 int pid = ttsd_data_get_pid(uid);
794 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
795 /* send message to client about changing state */
796 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
798 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
804 int ttsd_server_play(int uid, const char* credential)
807 if (0 > ttsd_data_get_client_state(uid, &state)) {
808 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
809 return TTSD_ERROR_INVALID_PARAMETER;
812 if (APP_STATE_PLAYING == state) {
813 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
814 return TTSD_ERROR_NONE;
817 /* check if engine use network */
818 if (ttsd_engine_agent_need_network()) {
819 if (false == ttsd_network_is_connected()) {
820 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
821 return TTSD_ERROR_OUT_OF_NETWORK;
825 int current_uid = ttsd_data_get_current_playing();
826 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
828 if (uid != current_uid && -1 != current_uid) {
829 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
830 /* Send interrupt message */
831 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
834 if (0 != ttsd_server_stop(current_uid)) {
835 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
837 if (0 != ttsd_player_stop(current_uid)) {
838 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
841 intptr_t pcurrent_uid = (intptr_t)current_uid;
842 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
844 /* Default mode policy of interrupt is "Pause" */
846 /* Send interrupt message */
847 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
850 if (0 != ttsd_player_pause(current_uid)) {
851 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
855 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
857 intptr_t pcurrent_uid = (intptr_t)current_uid;
858 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
862 /* Change current play */
863 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
864 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
865 return TTSD_ERROR_OPERATION_FAILED;
868 if (APP_STATE_PAUSED == state) {
869 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
872 if (0 != ttsd_player_resume(uid)) {
873 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
877 /* Check whether tts-engine is running or not */
878 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
879 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
881 __synthesis(uid, credential);
884 return TTSD_ERROR_NONE;
888 int ttsd_server_stop(int uid)
891 if (0 > ttsd_data_get_client_state(uid, &state)) {
892 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
893 return TTSD_ERROR_INVALID_PARAMETER;
896 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
898 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
899 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
900 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
903 ret = ttsd_engine_cancel_synthesis();
905 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
908 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
910 if (0 != ttsd_player_clear(uid))
911 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
913 ttsd_data_set_client_state(uid, APP_STATE_READY);
917 ttsd_data_clear_data(uid);
919 return TTSD_ERROR_NONE;
922 int ttsd_server_pause(int uid, int* utt_id)
925 if (0 > ttsd_data_get_client_state(uid, &state)) {
926 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
927 return TTSD_ERROR_INVALID_PARAMETER;
930 if (APP_STATE_PLAYING != state) {
931 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
932 return TTSD_ERROR_INVALID_STATE;
935 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), utt_id(%d)", uid, utt_id);
938 ret = ttsd_player_pause(uid);
940 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
941 return TTSD_ERROR_OPERATION_FAILED;
944 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
946 return TTSD_ERROR_NONE;
949 int ttsd_server_get_support_voices(int uid, GList** voice_list)
952 if (0 > ttsd_data_get_client_state(uid, &state)) {
953 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
954 return TTSD_ERROR_INVALID_PARAMETER;
958 int ret = ttsd_engine_get_voice_list(voice_list);
960 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
964 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
966 return TTSD_ERROR_NONE;
969 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
972 if (0 > ttsd_data_get_client_state(uid, &state)) {
973 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
974 return TTSD_ERROR_INVALID_PARAMETER;
977 /* get current voice */
978 int ret = ttsd_engine_get_default_voice(language, voice_type);
980 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
984 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
986 return TTSD_ERROR_NONE;
989 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
992 if (0 > ttsd_data_get_client_state(uid, &state)) {
993 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
994 return TTSD_ERROR_INVALID_PARAMETER;
997 if (APP_STATE_READY != state) {
998 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
999 return TTSD_ERROR_INVALID_STATE;
1002 int ret = ttsd_engine_set_private_data(key, data);
1004 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1006 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1012 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1015 if (0 > ttsd_data_get_client_state(uid, &state)) {
1016 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1017 return TTSD_ERROR_INVALID_PARAMETER;
1020 if (APP_STATE_READY != state) {
1021 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1022 return TTSD_ERROR_INVALID_STATE;
1025 int ret = ttsd_engine_get_private_data(key, data);
1027 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1029 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1035 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1037 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1040 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1042 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1048 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1050 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1053 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1055 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);