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>
18 #include "ttsd_config.h"
19 #include "ttsd_data.h"
20 #include "ttsd_dbus.h"
21 #include "ttsd_dbus_server.h"
22 #include "ttsd_engine_agent.h"
23 #include "ttsd_main.h"
24 #include "ttsd_network.h"
25 #include "ttsd_player.h"
26 #include "ttsd_server.h"
29 #define CLIENT_CLEAN_UP_TIME 500
36 /* If current engine exist */
37 //static bool g_is_engine;
39 static Ecore_Timer* g_check_client_timer = NULL;
40 static Ecore_Timer* g_wait_timer = NULL;
42 static utterance_t g_utt;
44 static GList *g_proc_list = NULL;
46 static bool g_is_paused;
49 /* Function definitions */
50 static int __synthesis(int uid, const char* credential);
52 static Eina_Bool __wait_synthesis(void *data)
54 /* get current play */
55 char* credential = (char*)data;
56 int uid = ttsd_data_get_current_playing();
59 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
63 if (TTSD_SYNTHESIS_CONTROL_DONE == ttsd_get_synth_control()) {
64 /* Start next synthesis */
65 __synthesis(uid, credential);
75 static int __synthesis(int uid, const char* credential)
77 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
79 speak_data_s* speak_data = NULL;
80 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
81 if (NULL == speak_data) {
82 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
86 int pid = ttsd_data_get_pid(uid);
87 char appid[1024] = {0, };
88 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
89 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
92 if (NULL == speak_data->lang || NULL == speak_data->text) {
93 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
94 ttsd_server_stop(uid);
96 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
98 ttsd_data_clear_speak_data(uid, &speak_data);
104 g_utt.uttid = speak_data->utt_id;
106 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
107 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
108 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
109 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
110 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
111 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
114 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
115 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
117 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
119 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
121 ttsd_server_stop(uid);
123 int pid = ttsd_data_get_pid(uid);
124 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
126 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
129 ttsd_data_clear_speak_data(uid, &speak_data);
131 ttsd_data_clear_speak_data(uid, &speak_data);
134 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
140 * TTS Server Callback Functions
142 int ttsd_send_error(ttse_error_e error, const char* msg)
145 int uttid = g_utt.uttid;
146 int tmp_pid = ttsd_data_get_pid(uid);
148 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, (NULL == msg ? "NULL" : msg));
150 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
152 if (0 != ttsd_player_clear(uid))
153 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
155 if (0 != ttsd_data_clear_data(uid))
156 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
158 if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
159 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
161 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
162 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
164 if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
165 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
170 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)
172 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
175 int uttid = g_utt.uttid;
177 /* Synthesis is success */
178 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
179 if (TTSE_RESULT_EVENT_START == event) {
180 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
181 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
182 uid, uttid, data, data_size, audio_type, rate);
183 } else if (TTSE_RESULT_EVENT_FINISH == event) {
184 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
185 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
186 uid, uttid, data, data_size, audio_type, rate);
188 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
192 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
193 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
194 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
195 SLOG(LOG_DEBUG, tts_tag(), "@@@");
196 return TTSD_ERROR_OPERATION_FAILED;
199 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
200 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
201 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
202 SLOG(LOG_DEBUG, tts_tag(), "@@@");
203 return TTSD_ERROR_INVALID_PARAMETER;
207 sound_data_s* temp_sound_data = NULL;
208 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
209 if (NULL == temp_sound_data) {
210 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
211 return TTSD_ERROR_OUT_OF_MEMORY;
214 temp_sound_data->data = NULL;
215 temp_sound_data->rate = 0;
216 temp_sound_data->data_size = 0;
219 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
220 if (NULL != temp_sound_data->data) {
221 memcpy(temp_sound_data->data, data, data_size);
222 temp_sound_data->data_size = data_size;
223 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
224 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
226 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
229 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
232 temp_sound_data->utt_id = uttid;
233 temp_sound_data->event = event;
234 temp_sound_data->audio_type = audio_type;
235 temp_sound_data->rate = rate;
237 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
238 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
239 if (NULL != temp_sound_data->data) {
240 free(temp_sound_data->data);
241 temp_sound_data->data = NULL;
244 free(temp_sound_data);
245 temp_sound_data = NULL;
247 return TTSD_ERROR_OPERATION_FAILED;
250 if (event == TTSE_RESULT_EVENT_FINISH) {
251 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
254 /* If the app state is paused, do not result to play */
255 if (true == g_is_paused) {
256 SLOG(LOG_DEBUG, tts_tag(), "[Server DEBUG] tts_pause is called. Do not request to play");
257 return TTSD_ERROR_NONE;
260 if (0 != ttsd_player_play(uid)) {
261 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
263 /* Change ready state */
264 ttsd_server_stop(uid);
267 tmp_pid = ttsd_data_get_pid(uid);
268 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
271 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
272 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
276 /*SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS RESULT CALLBACK END");
277 SLOG(LOG_DEBUG, tts_tag(), " ");*/
279 return TTSD_ERROR_NONE;
282 bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
284 /* clear client data */
285 ttsd_data_clear_data(uid);
286 ttsd_data_set_client_state(uid, APP_STATE_READY);
289 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
291 ttsd_data_delete_client(uid);
297 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
300 case TTS_CONFIG_TYPE_ENGINE:
302 /* TODO - Determine the policy when engine process get engine changed cb */
303 if (NULL == str_param) {
304 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
309 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
310 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
311 ret = ttsd_engine_agent_unload_current_engine();
313 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
316 ret = ttsd_engine_agent_load_current_engine();
318 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
323 /* stop all player */
324 ttsd_player_all_stop();
326 /* send interrupt message to all clients */
327 ttsd_data_foreach_clients(__get_client_cb, NULL);
329 ttsd_engine_cancel_synthesis();
334 case TTS_CONFIG_TYPE_VOICE:
336 if (NULL == str_param) {
337 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
341 char* out_lang = NULL;
345 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
346 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
347 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
349 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
351 /* Current language is not available */
352 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
354 if (NULL != out_lang) {
361 case TTS_CONFIG_TYPE_SPEED:
363 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
364 /* set default speed */
366 ret = ttsd_engine_agent_set_default_speed(int_param);
368 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
374 case TTS_CONFIG_TYPE_PITCH:
376 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
377 /* set default speed */
379 ret = ttsd_engine_agent_set_default_pitch(int_param);
381 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
387 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
389 if (0.0 <= double_param && double_param <= 1.0) {
390 /* set default bg volume ratio */
392 ret = ttsd_player_set_background_volume_ratio(double_param);
394 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
404 bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
406 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
407 ttsd_server_finalize(uid);
411 Eina_Bool ttsd_terminate_daemon(void *data)
413 ttsd_data_foreach_clients(__terminate_client, NULL);
417 void __screen_reader_changed_cb(bool value)
419 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
420 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
421 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
423 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
431 int ttsd_initialize(ttse_request_callback_s *callback)
433 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
435 if (ttsd_config_initialize(__config_changed_cb)) {
436 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
440 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
442 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
443 int ret = ttsd_player_set_background_volume_ratio(ratio);
445 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
448 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
451 if (ttsd_player_init()) {
452 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
453 return TTSD_ERROR_OPERATION_FAILED;
456 /* Engine Agent initialize */
457 if (0 != ttsd_engine_agent_init()) {
458 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
459 return TTSD_ERROR_OPERATION_FAILED;
462 /* set current engine */
463 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
464 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
465 // g_is_engine = false;
467 // g_is_engine = true;
469 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
470 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
471 return TTSD_ERROR_OPERATION_FAILED;
474 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
476 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
477 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
480 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
481 if (NULL == g_check_client_timer) {
482 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
485 return TTSD_ERROR_NONE;
490 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
493 if (0 < g_list_length(g_proc_list)) {
494 iter = g_list_first(g_proc_list);
495 while (NULL != iter) {
496 g_proc_list = g_list_remove_link(g_proc_list, iter);
498 iter = g_list_first(g_proc_list);
502 ttsd_config_finalize();
504 ttsd_player_release();
506 ttsd_engine_agent_release();
508 if (NULL != g_check_client_timer) {
509 ecore_timer_del(g_check_client_timer);
510 g_check_client_timer = NULL;
512 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore timer handle");
515 return TTSD_ERROR_NONE;
520 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
522 ttsd_terminate_daemon(NULL);
524 ttsd_dbus_close_connection();
525 ttsd_network_finalize();
528 return TTSD_ERROR_NONE;
532 * TTS Server Functions for Client
535 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
537 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
539 if (-1 != ttsd_data_is_client(uid)) {
540 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
541 return TTSD_ERROR_NONE;
544 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
545 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
546 return TTSD_ERROR_OPERATION_FAILED;
549 if (true == *credential_needed) {
551 if (0 != app_manager_get_app_id(pid, &appid)) {
552 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
554 bool is_agreed = false;
555 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
556 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
559 return TTSD_ERROR_OPERATION_FAILED;
564 if (false == is_agreed) {
565 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
566 return TTSD_ERROR_PERMISSION_DENIED;
570 if (0 != ttsd_data_new_client(pid, uid)) {
571 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
572 return TTSD_ERROR_OPERATION_FAILED;
575 if (0 != ttsd_player_create_instance(uid)) {
576 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
577 return TTSD_ERROR_OPERATION_FAILED;
580 return TTSD_ERROR_NONE;
583 static Eina_Bool __quit_ecore_loop(void *data)
585 ttsd_dbus_close_connection();
586 ttsd_network_finalize();
588 ecore_main_loop_quit();
594 static void __read_proc()
597 struct dirent *dirp = NULL;
601 if (0 < g_list_length(g_proc_list)) {
602 iter = g_list_first(g_proc_list);
603 while (NULL != iter) {
604 g_proc_list = g_list_remove_link(g_proc_list, iter);
606 iter = g_list_first(g_proc_list);
610 dp = opendir("/proc");
612 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
618 tmp = atoi(dirp->d_name);
619 if (0 >= tmp) continue;
620 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
622 } while (NULL != dirp);
628 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
634 for (i = 0; i < g_list_length(g_proc_list); i++) {
635 iter = g_list_nth(g_proc_list, i);
637 if (pid == GPOINTER_TO_INT(iter->data)) {
638 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
645 if (false == exist) {
646 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
647 ttsd_server_finalize(uid);
652 char appid[1024] = {0, };
653 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
654 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
657 if (0 < strlen(appid)) {
658 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
660 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
663 result = ttsdc_send_hello(pid, uid);
666 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
667 ttsd_server_finalize(uid);
668 } else if (-1 == result) {
669 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
677 Eina_Bool ttsd_cleanup_client(void *data)
679 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
682 if (0 < ttsd_data_get_client_count()) {
683 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
685 ecore_timer_add(0, __quit_ecore_loop, NULL);
686 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
690 SLOG(LOG_DEBUG, tts_tag(), "@@@");
695 void __used_voice_cb(const char* lang, int type)
697 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
698 if (0 != ttsd_engine_unload_voice(lang, type)) {
699 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
703 int ttsd_server_finalize(int uid)
705 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
707 app_tts_state_e state;
708 if (0 > ttsd_data_get_client_state(uid, &state)) {
709 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
712 ttsd_server_stop(uid);
713 ttsd_player_stop(uid);
715 ttsd_player_destroy_instance(uid);
717 /* Need to unload voice when used voice is unregistered */
718 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
719 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
720 return TTSD_ERROR_OPERATION_FAILED;
723 ttsd_data_delete_client(uid);
725 /* unload engine, if ref count of client is 0 */
726 if (0 == ttsd_data_get_client_count()) {
727 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
728 ecore_timer_add(0, __quit_ecore_loop, NULL);
731 return TTSD_ERROR_NONE;
734 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
736 app_tts_state_e state;
737 if (0 > ttsd_data_get_client_state(uid, &state)) {
738 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
739 return TTSD_ERROR_INVALID_PARAMETER;
742 /* check valid voice */
743 char* temp_lang = NULL;
745 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
746 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
747 if (NULL != temp_lang) {
751 return TTSD_ERROR_INVALID_VOICE;
754 if (NULL == temp_lang) {
755 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
756 return TTSD_ERROR_INVALID_VOICE;
759 speak_data_s* speak_data = NULL;
760 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
761 if (NULL == speak_data) {
762 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
763 if (NULL != temp_lang) {
767 return TTSD_ERROR_OPERATION_FAILED;
770 speak_data->lang = strdup(lang);
771 speak_data->vctype = voice_type;
773 speak_data->speed = speed;
774 speak_data->utt_id = utt_id;
776 speak_data->text = strdup(text);
778 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);
780 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
782 ret = ttsd_data_add_speak_data(uid, speak_data);
784 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
785 if (NULL != temp_lang) {
789 if (NULL != speak_data) {
790 if (NULL != speak_data->lang) free(speak_data->lang);
791 if (NULL != speak_data->text) free(speak_data->text);
793 speak_data->lang = NULL;
794 speak_data->text = NULL;
803 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
804 /* Request load voice */
805 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
806 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
807 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
811 if (NULL != temp_lang) {
816 if (APP_STATE_PLAYING == state) {
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.");
821 return TTSD_ERROR_OPERATION_FAILED;
825 /* Check whether tts-engine is running or not */
826 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
827 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
829 __synthesis(uid, credential);
833 return TTSD_ERROR_NONE;
836 Eina_Bool __send_interrupt_client(void *data)
838 intptr_t puid = (intptr_t)data;
841 int pid = ttsd_data_get_pid(uid);
843 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
844 /* send message to client about changing state */
845 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
847 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
853 int ttsd_server_play(int uid, const char* credential)
855 app_tts_state_e state;
856 if (0 > ttsd_data_get_client_state(uid, &state)) {
857 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
858 return TTSD_ERROR_INVALID_PARAMETER;
861 if (APP_STATE_PLAYING == state) {
862 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
863 return TTSD_ERROR_NONE;
866 /* check if engine use network */
867 if (ttsd_engine_agent_need_network()) {
868 if (false == ttsd_network_is_connected()) {
869 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
870 return TTSD_ERROR_OUT_OF_NETWORK;
874 /* check the current playback focus */
875 if (TTSD_MODE_INTERRUPT != ttsd_get_mode()) {
876 bool is_current_interrupt = false;
877 if (0 != ttsd_player_check_current_playback_focus(&is_current_interrupt)) {
878 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to check the current playback focus");
880 if (true == is_current_interrupt) {
881 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
882 ttsd_data_clear_data(uid);
883 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
888 int current_uid = ttsd_data_get_current_playing();
889 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
891 if (uid != current_uid && -1 != current_uid) {
892 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
893 /* Send interrupt message */
894 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
897 if (0 != ttsd_server_stop(current_uid)) {
898 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
900 if (0 != ttsd_player_stop(current_uid)) {
901 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
904 intptr_t pcurrent_uid = (intptr_t)current_uid;
905 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
907 /* Default mode policy of interrupt is "Pause" */
909 /* Send interrupt message */
910 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
913 if (0 != ttsd_player_pause(current_uid)) {
914 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
918 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
920 intptr_t pcurrent_uid = (intptr_t)current_uid;
921 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
925 /* Change current play */
926 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
927 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
928 return TTSD_ERROR_OPERATION_FAILED;
931 if (APP_STATE_PAUSED == state) {
932 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
937 if (0 != ttsd_player_resume(uid)) {
938 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
942 /* Check whether tts-engine is running or not */
943 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
944 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
946 __synthesis(uid, credential);
949 return TTSD_ERROR_NONE;
952 int ttsd_server_stop(int uid)
954 app_tts_state_e state;
955 if (0 > ttsd_data_get_client_state(uid, &state)) {
956 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
957 return TTSD_ERROR_INVALID_PARAMETER;
960 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
962 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
963 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
964 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
967 ret = ttsd_engine_cancel_synthesis();
969 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
972 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
974 if (0 != ttsd_player_clear(uid))
975 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
977 ttsd_data_set_client_state(uid, APP_STATE_READY);
981 ttsd_data_clear_data(uid);
984 return TTSD_ERROR_NONE;
987 int ttsd_server_pause(int uid, int* utt_id)
989 app_tts_state_e state;
990 if (0 > ttsd_data_get_client_state(uid, &state)) {
991 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
992 return TTSD_ERROR_INVALID_PARAMETER;
995 if (APP_STATE_PLAYING != state) {
996 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
997 return TTSD_ERROR_INVALID_STATE;
1002 *utt_id = g_utt.uttid;
1003 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
1006 ret = ttsd_player_pause(uid);
1008 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1009 return TTSD_ERROR_OPERATION_FAILED;
1012 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1014 return TTSD_ERROR_NONE;
1017 int ttsd_server_get_support_voices(int uid, GList** voice_list)
1019 app_tts_state_e state;
1020 if (0 > ttsd_data_get_client_state(uid, &state)) {
1021 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1022 return TTSD_ERROR_INVALID_PARAMETER;
1026 int ret = ttsd_engine_get_voice_list(voice_list);
1028 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1032 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1034 return TTSD_ERROR_NONE;
1037 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
1039 app_tts_state_e state;
1040 if (0 > ttsd_data_get_client_state(uid, &state)) {
1041 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1042 return TTSD_ERROR_INVALID_PARAMETER;
1045 /* get current voice */
1046 int ret = ttsd_engine_get_default_voice(language, voice_type);
1048 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1052 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1054 return TTSD_ERROR_NONE;
1057 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1059 app_tts_state_e state;
1060 if (0 > ttsd_data_get_client_state(uid, &state)) {
1061 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1062 return TTSD_ERROR_INVALID_PARAMETER;
1065 if (APP_STATE_READY != state) {
1066 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1067 return TTSD_ERROR_INVALID_STATE;
1070 int ret = ttsd_engine_set_private_data(key, data);
1072 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1074 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1080 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1082 app_tts_state_e state;
1083 if (0 > ttsd_data_get_client_state(uid, &state)) {
1084 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1085 return TTSD_ERROR_INVALID_PARAMETER;
1088 if (APP_STATE_READY != state) {
1089 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1090 return TTSD_ERROR_INVALID_STATE;
1093 int ret = ttsd_engine_get_private_data(key, data);
1095 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1097 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1103 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1105 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1108 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1110 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1116 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1118 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1121 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1123 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1129 int ttsd_server_play_pcm(int uid)
1131 app_tts_state_e state;
1132 if (0 > ttsd_data_get_client_state(uid, &state)) {
1133 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1134 return TTSD_ERROR_INVALID_PARAMETER;
1137 if (APP_STATE_PLAYING == state) {
1138 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1139 return TTSD_ERROR_NONE;
1142 int current_uid = ttsd_data_get_current_playing();
1143 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1145 if (uid != current_uid && -1 != current_uid) {
1146 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1147 /* Send interrupt message */
1148 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1151 if (0 != ttsd_server_stop(current_uid)) {
1152 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1154 if (0 != ttsd_player_stop(current_uid)) {
1155 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1158 intptr_t pcurrent_uid = (intptr_t)current_uid;
1159 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1161 /* Default mode policy of interrupt is "Pause" */
1163 /* Send interrupt message */
1164 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1167 if (0 != ttsd_player_pause(current_uid)) {
1168 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1172 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1174 intptr_t pcurrent_uid = (intptr_t)current_uid;
1175 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1179 /* Change current play */
1180 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1181 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1182 return TTSD_ERROR_OPERATION_FAILED;
1185 if (APP_STATE_PAUSED == state) {
1186 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1189 if (0 != ttsd_player_resume(uid)) {
1190 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1193 if (0 != ttsd_player_play_pcm(uid)) {
1194 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1196 // Change ready state
1197 ttsd_server_stop_pcm(uid);
1200 tmp_pid = ttsd_data_get_pid(uid);
1201 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1205 return TTSD_ERROR_NONE;
1208 int ttsd_server_stop_pcm(int uid)
1210 app_tts_state_e state;
1211 if (0 > ttsd_data_get_client_state(uid, &state)) {
1212 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1213 return TTSD_ERROR_INVALID_PARAMETER;
1216 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1218 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1219 ttsd_data_set_client_state(uid, APP_STATE_READY);
1222 /* Reset all data */
1223 ttsd_data_clear_data(uid);
1225 ttsd_player_stop(uid);
1228 return TTSD_ERROR_NONE;
1231 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1233 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1237 /* Synthesis is success */
1238 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1239 if (TTSE_RESULT_EVENT_START == event) {
1240 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1241 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1242 uid, uttid, data, data_size, audio_type, rate);
1243 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1244 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1245 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1246 uid, uttid, data, data_size, audio_type, rate);
1248 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1251 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1252 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1253 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1254 return TTSD_ERROR_INVALID_PARAMETER;
1258 sound_data_s* temp_sound_data = NULL;
1259 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1260 if (NULL == temp_sound_data) {
1261 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1262 return TTSD_ERROR_OUT_OF_MEMORY;
1265 temp_sound_data->data = NULL;
1266 temp_sound_data->rate = 0;
1267 temp_sound_data->data_size = 0;
1269 if (0 < data_size) {
1270 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1271 if (NULL != temp_sound_data->data) {
1272 memcpy(temp_sound_data->data, data, data_size);
1273 temp_sound_data->data_size = data_size;
1274 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1275 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1277 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1280 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1283 temp_sound_data->utt_id = uttid;
1284 temp_sound_data->event = event;
1285 temp_sound_data->audio_type = audio_type;
1286 temp_sound_data->rate = rate;
1288 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1289 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1291 if (NULL != temp_sound_data->data) {
1292 free(temp_sound_data->data);
1293 temp_sound_data->data = NULL;
1296 free(temp_sound_data);
1297 temp_sound_data = NULL;
1300 /* if (0 != ttsd_player_play(uid)) {
1301 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1303 // Change ready state
1304 ttsd_server_stop(uid);
1307 tmp_pid = ttsd_data_get_pid(uid);
1308 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1312 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1315 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1317 return TTSD_ERROR_NONE;