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_state.h"
26 #include "ttsd_server.h"
29 #define CLIENT_CLEAN_UP_TIME 500
31 /* for checking time */
32 #define TIME_DIFF(start, end) \
33 ((uint64_t)((end).tv_sec - (start).tv_sec) * 1000000 + (((end).tv_nsec - (start).tv_nsec) / 1000)) / 1000
35 static struct timespec g_request_playing, g_start_playing, g_finish_playing;
36 static double g_avg_latency;
37 static double g_min_latency;
38 static double g_max_latency;
39 static int g_file_num;
40 static int g_total_data_size;
50 } state_changed_args_t;
52 /* If current engine exist */
53 //static bool g_is_engine;
55 static Ecore_Timer* g_check_client_timer = NULL;
56 static Ecore_Timer* g_wait_timer = NULL;
57 static Ecore_Timer* g_quit_loop_timer = NULL;
58 static Ecore_Timer* g_notify_state_timer = NULL;
60 static utterance_t g_utt;
62 static GList *g_proc_list = NULL;
64 static bool g_is_terminated = false;
66 static int g_activated_mode = 0;
68 /* Function definitions */
69 static int __stop_and_send_ready_state(unsigned int uid)
71 int ret = ttsd_server_stop(uid);
72 if (TTSD_ERROR_NONE != ret) {
73 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_stop() : uid(%u). ret(%d)", uid, ret);
77 int pid = ttsd_data_get_pid(uid);
79 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
80 return TTSD_ERROR_OPERATION_FAILED;
82 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
88 static void __synthesis(unsigned int uid);
90 static Eina_Bool __wait_synthesis(void *data)
92 /* get current play */
93 unsigned int uid = ttsd_data_get_current_playing();
96 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
97 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
100 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
102 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
103 /* Start next synthesis */
114 static void __synthesis(unsigned int uid)
116 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
118 speak_data_s* speak_data = NULL;
119 int ret = ttsd_data_get_speak_data(uid, &speak_data);
120 if (TTSD_ERROR_NONE != ret || NULL == speak_data) {
121 SLOG(LOG_DEBUG, tts_tag(), "@@@ Fail to get speak data. ret(%d)", ret);
122 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
126 int pid = ttsd_data_get_pid(uid);
128 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
129 ttsd_data_destroy_speak_data(speak_data);
134 char appid[1024] = {0, };
135 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
136 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
139 if (NULL == speak_data->lang || NULL == speak_data->text) {
140 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
141 __stop_and_send_ready_state(uid);
142 ttsd_data_destroy_speak_data(speak_data);
148 g_utt.uttid = speak_data->utt_id;
150 char* credential = ttsd_data_get_credential(uid);
152 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
153 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
154 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
155 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
156 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
157 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
159 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
160 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
161 if (TTSD_ERROR_NONE != ret) {
162 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
164 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
165 __stop_and_send_ready_state(uid);
167 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
170 if (ttsd_data_get_synth_control() == TTSD_SYNTHESIS_CONTROL_DOING && ttsd_state_get_state() == TTSD_STATE_READY) {
171 int ret = vconf_set_bool(TTS_PLAYING_STATUS_KEY, 1);
172 SLOG(LOG_INFO, tts_tag(), "[Server] Synthesis starting. Set playing status (True). ret(%d)", ret);
174 ttsd_state_set_state(TTSD_STATE_SYNTHESIZING);
179 ttsd_data_destroy_speak_data(speak_data);
181 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
187 * TTS Server Callback Functions
189 int ttsd_send_error(ttse_error_e error, const char* msg)
191 unsigned int uid = g_utt.uid;
192 int uttid = g_utt.uttid;
193 int tmp_pid = ttsd_data_get_pid(uid);
195 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));
197 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
199 if (0 != ttsd_data_clear_data(uid))
200 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
203 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
204 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
207 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
208 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
211 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
212 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
217 static int ttsd_convert_audio_type_to_bytes(ttse_audio_type_e audio_type)
220 switch (audio_type) {
221 case TTSE_AUDIO_TYPE_RAW_S16:
224 case TTSE_AUDIO_TYPE_RAW_U8:
228 ret = 2; // Default 16bit
234 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)
236 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
238 unsigned int uid = g_utt.uid;
239 int uttid = g_utt.uttid;
241 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
242 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
243 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
244 SLOG(LOG_DEBUG, tts_tag(), "@@@");
245 return TTSD_ERROR_INVALID_PARAMETER;
248 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
249 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
250 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
251 return TTSD_ERROR_INVALID_PARAMETER;
254 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
255 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
256 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
257 SLOG(LOG_DEBUG, tts_tag(), "@@@");
258 return TTSD_ERROR_INVALID_PARAMETER;
261 /* check the current state */
262 app_tts_state_e state = ttsd_data_get_client_state(uid);
263 if (APP_STATE_NONE == state) {
264 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
265 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
266 return TTSD_ERROR_INVALID_PARAMETER;
269 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
270 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
271 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
272 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
273 return TTSD_ERROR_NONE;
276 /* Synthesis is success */
277 if (TTSE_RESULT_EVENT_START == event) {
278 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
279 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
280 uid, uttid, data, data_size, audio_type, rate);
282 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
283 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
284 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
285 double d_latency = (double)latency;
286 g_avg_latency += d_latency;
287 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
288 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
290 g_total_data_size = 0;
291 g_total_data_size += data_size;
292 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);
293 } else if (TTSE_RESULT_EVENT_FINISH == event) {
294 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
295 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
296 uid, uttid, data, data_size, audio_type, rate);
298 g_total_data_size += data_size;
299 int audio_type_bytes = ttsd_convert_audio_type_to_bytes(audio_type);
301 clock_gettime(CLOCK_MONOTONIC_RAW, &g_finish_playing);
302 long long int time_processing = (uint64_t)TIME_DIFF(g_request_playing, g_finish_playing);
303 long long int time_speech = (long long int)(1000 * g_total_data_size * sizeof(short) / (rate * audio_type_bytes));
304 double xRT = (double)(time_processing) / (double)time_speech;
305 SLOG(LOG_INFO, tts_tag(), "[SERVER] time_processing : %lld, time_speech : %lld, total data size: %d", time_processing, time_speech, g_total_data_size);
306 SLOG(LOG_INFO, tts_tag(), "[SERVER] xRT : %lf", xRT);
308 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
309 g_total_data_size += data_size;
313 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
314 if (NULL == sound_data) {
315 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
316 return TTSD_ERROR_OUT_OF_MEMORY;
319 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
320 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
321 ttsd_data_destroy_sound_data(sound_data);
323 return TTSD_ERROR_OPERATION_FAILED;
326 if (event == TTSE_RESULT_EVENT_FINISH) {
327 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
330 return TTSD_ERROR_NONE;
333 static bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
335 if (0 > ttsd_data_is_client(uid)) {
336 SLOG(LOG_ERROR, tts_tag(), "[Server] Client(%d) is not valid.", uid);
337 ttsd_data_delete_client(uid);
341 /* clear client data */
342 ttsd_data_clear_data(uid);
344 if (APP_STATE_READY != state) {
345 ttsd_data_set_client_state(uid, APP_STATE_READY);
346 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
352 static void __stop_all_client()
354 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
356 ttsd_engine_cancel_synthesis();
357 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
359 /* stop all player */
360 ttsd_player_all_stop();
362 /* send interrupt message to all clients */
363 ttsd_data_foreach_clients(__get_client_cb, NULL);
366 static void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
369 case TTS_CONFIG_TYPE_ENGINE:
371 /* TODO - Determine the policy when engine process get engine changed cb */
372 if (NULL == str_param) {
373 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
378 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
379 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
380 ret = ttsd_engine_agent_unload_current_engine();
382 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
385 ret = ttsd_engine_agent_load_current_engine();
387 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
396 case TTS_CONFIG_TYPE_VOICE:
398 if (NULL == str_param) {
399 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
403 char* out_lang = NULL;
407 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
408 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
409 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
411 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
413 /* Current language is not available */
414 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
416 if (NULL != out_lang) {
423 case TTS_CONFIG_TYPE_SPEED:
425 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
426 /* set default speed */
428 ret = ttsd_engine_agent_set_default_speed(int_param);
430 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
436 case TTS_CONFIG_TYPE_PITCH:
438 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
439 /* set default speed */
441 ret = ttsd_engine_agent_set_default_pitch(int_param);
443 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
449 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
451 if (0.0 <= double_param && double_param <= 1.0) {
452 /* set default bg volume ratio */
454 ret = ttsd_player_set_background_volume_ratio(double_param);
456 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
466 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
468 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
469 ttsd_server_finalize(uid);
473 static int __terminate_server()
475 if (g_is_terminated) {
476 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
477 return TTSD_ERROR_NONE;
480 int ret = TTSD_ERROR_NONE;
481 ret = ttsd_ipc_close_connection();
482 if (TTSD_ERROR_NONE != ret) {
483 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_ipc_close_connection(). ret(%d)", ret);
484 return TTSD_ERROR_OPERATION_FAILED;
487 ret = ttsd_finalize();
488 if (TTSD_ERROR_NONE != ret) {
489 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_finalize(). ret(%d)", ret);
490 return TTSD_ERROR_OPERATION_FAILED;
493 g_is_terminated = true;
494 return TTSD_ERROR_NONE;
497 static Eina_Bool __quit_ecore_loop(void *data)
499 __terminate_server();
500 ecore_main_loop_quit();
505 void __screen_reader_changed_cb(bool value)
507 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
511 static void __read_proc()
514 struct dirent *dirp = NULL;
518 if (0 < g_list_length(g_proc_list)) {
519 iter = g_list_first(g_proc_list);
520 while (NULL != iter) {
521 g_proc_list = g_list_remove_link(g_proc_list, iter);
523 iter = g_list_first(g_proc_list);
527 dp = opendir("/proc");
529 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
535 tmp = atoi(dirp->d_name);
536 if (0 >= tmp) continue;
537 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
539 } while (NULL != dirp);
545 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
551 for (i = 0; i < g_list_length(g_proc_list); i++) {
552 iter = g_list_nth(g_proc_list, i);
554 if (pid == GPOINTER_TO_INT(iter->data)) {
555 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
562 if (false == exist) {
563 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
564 ttsd_server_finalize(uid);
570 static Eina_Bool __cleanup_client(void *data)
572 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
575 if (0 < ttsd_data_get_client_count()) {
576 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
578 if (NULL == g_quit_loop_timer) {
579 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
580 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
582 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
583 g_check_client_timer = NULL;
587 SLOG(LOG_DEBUG, tts_tag(), "@@@");
592 static bool __notify_state_changed_event(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
594 state_changed_args_t* args = (state_changed_args_t*)user_data;
599 if (ttsd_data_is_service_state_changed_cb_set(uid)) {
600 ttsdc_ipc_send_set_service_state_message(pid, uid, args->before, args->current);
606 static Eina_Bool __state_changed_timer_cb(void *data)
609 SLOG(LOG_ERROR, tts_tag(), "[Server] Invalid data passed.");
610 return ECORE_CALLBACK_CANCEL;
613 int ret = ttsd_data_foreach_clients(__notify_state_changed_event, data);
614 if (TTSD_ERROR_NONE != ret) {
615 SLOG(LOG_ERROR, tts_tag(), "[Server] Fail to notify state change.");
619 g_notify_state_timer = NULL;
620 return ECORE_CALLBACK_CANCEL;
623 static void __state_changed_cb(ttsd_state_e before, ttsd_state_e current)
625 state_changed_args_t* args = (state_changed_args_t*)calloc(1, sizeof(state_changed_args_t));
627 args->before = before;
628 args->current = current;
631 SLOG(LOG_INFO, tts_tag(), "[Server] Notify state chanaged from (%d) to (%d).", before, current);
632 g_notify_state_timer = ecore_timer_add(0.0, __state_changed_timer_cb, args);
638 int ttsd_initialize(ttse_request_callback_s *callback)
640 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
641 g_is_terminated = false;
643 if (ttsd_config_initialize(__config_changed_cb)) {
644 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
647 if (TTSD_ERROR_NONE != ttsd_state_initialize(__state_changed_cb)) {
648 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize state.");
652 if (ttsd_player_init()) {
653 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
654 return TTSD_ERROR_OPERATION_FAILED;
658 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
660 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
661 int ret = ttsd_player_set_background_volume_ratio(ratio);
663 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
665 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
668 /* Engine Agent initialize */
669 if (TTSD_ERROR_NONE != ttsd_engine_agent_init(callback)) {
670 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
671 return TTSD_ERROR_OPERATION_FAILED;
674 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
675 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
677 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, __cleanup_client, NULL);
678 if (NULL == g_check_client_timer) {
679 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
682 g_activated_mode = 0;
684 return TTSD_ERROR_NONE;
689 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
692 if (0 < g_list_length(g_proc_list)) {
693 iter = g_list_first(g_proc_list);
694 while (NULL != iter) {
695 g_proc_list = g_list_remove_link(g_proc_list, iter);
697 iter = g_list_first(g_proc_list);
701 if (g_notify_state_timer) {
702 void* data = ecore_timer_del(g_notify_state_timer);
703 g_notify_state_timer = NULL;
706 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore notify state timer handle");
710 ecore_timer_del(g_wait_timer);
712 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
715 if (g_check_client_timer) {
716 ecore_timer_del(g_check_client_timer);
717 g_check_client_timer = NULL;
718 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
721 if (g_quit_loop_timer) {
722 ecore_timer_del(g_quit_loop_timer);
723 g_quit_loop_timer = NULL;
724 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
727 ttsd_config_finalize();
728 ttsd_state_finalize();
730 int ret = TTSD_ERROR_NONE;
731 ret = ttsd_player_release();
732 if (TTSD_ERROR_NONE != ret) {
733 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_player_release(). ret(%d)", ret);
734 return TTSD_ERROR_OPERATION_FAILED;
737 ret = ttsd_engine_agent_release();
738 if (TTSD_ERROR_NONE != ret) {
739 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_engine_agent_release(). ret(%d)", ret);
740 return TTSD_ERROR_OPERATION_FAILED;
743 return TTSD_ERROR_NONE;
748 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
749 int ret = ttsd_data_foreach_clients(__terminate_client, NULL);
750 if (TTSD_ERROR_NONE != ret) {
751 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to terminate all clients. ret(%d)", ret);
752 return TTSD_ERROR_OPERATION_FAILED;
755 if (g_quit_loop_timer) {
756 ecore_timer_del(g_quit_loop_timer);
757 g_quit_loop_timer = NULL;
758 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
761 return __terminate_server();
765 * TTS Server Functions for Client
768 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
770 if (-1 != ttsd_data_is_client(uid))
771 *is_initialized = true;
773 *is_initialized = false;
775 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
776 return TTSD_ERROR_NONE;
779 static void __set_and_notify_activated_mode()
781 int activated_mode = ttsd_data_get_activated_mode();
782 if (g_activated_mode == activated_mode) {
786 SLOG(LOG_INFO, tts_tag(), "[Server] Activated mode is changed from(%d) to (%d)", g_activated_mode, activated_mode);
788 g_activated_mode = activated_mode;
789 int ret = ttsd_engine_notify_activated_mode_changed(g_activated_mode);
790 if (TTSD_ERROR_NONE != ret) {
791 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to notify activated mode changed event.(%d)", ret);
795 static int __check_app_agreed(int pid, unsigned int uid, bool credential_needed)
797 if (false == credential_needed) {
798 return TTSD_ERROR_NONE;
802 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
803 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
806 bool is_agreed = false;
807 int ret = ttsd_engine_check_app_agreed(appid, &is_agreed);
811 if (TTSD_ERROR_NONE != ret) {
812 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
813 return TTSD_ERROR_OPERATION_FAILED;
816 if (false == is_agreed) {
817 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
818 return TTSD_ERROR_PERMISSION_DENIED;
821 return TTSD_ERROR_NONE;
824 int ttsd_server_initialize(int pid, unsigned int uid, ttsd_mode_e mode, ttsd_playing_mode_e playing_mode, int registered_event_mask, tts_ipc_method_e method, ttsd_state_e* service_state, bool* credential_needed)
826 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
828 if (-1 != ttsd_data_is_client(uid)) {
829 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
830 return TTSD_ERROR_NONE;
833 if (TTSD_ERROR_NONE != ttsd_data_new_client(pid, uid, mode, playing_mode, registered_event_mask, method)) {
834 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
835 return TTSD_ERROR_OPERATION_FAILED;
838 __set_and_notify_activated_mode();
840 if (TTSD_ERROR_NONE != ttsd_engine_agent_load_current_engine()) {
841 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
842 ttsd_data_delete_client(uid);
843 return TTSD_ERROR_OPERATION_FAILED;
846 bool is_needed = false;
847 if (TTSD_ERROR_NONE != ttsd_engine_agent_is_credential_needed(uid, &is_needed)) {
848 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
849 ttsd_data_delete_client(uid);
850 return TTSD_ERROR_OPERATION_FAILED;
853 int ret = __check_app_agreed(pid, uid, is_needed);
854 if (TTSD_ERROR_NONE != ret) {
855 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Checking credential is finished(%d)", ret);
856 ttsd_data_delete_client(uid);
860 ttsd_state_e tmp_service_state = ttsd_state_get_state();
861 if (TTSD_STATE_INVALID == tmp_service_state) {
862 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state");
863 ttsd_data_delete_client(uid);
864 return TTSD_ERROR_OPERATION_FAILED;
867 *credential_needed = is_needed;
868 *service_state = tmp_service_state;
870 return TTSD_ERROR_NONE;
873 void __used_voice_cb(const char* lang, int type)
875 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
876 if (0 != ttsd_engine_unload_voice(lang, type)) {
877 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
881 int ttsd_server_finalize(unsigned int uid)
883 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
884 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);
886 if (0 > ttsd_data_is_client(uid)) {
887 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
890 int ret = ttsd_server_stop(uid);
891 if (TTSD_ERROR_NONE != ret) {
892 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS server top(%u) fail (%d/%s) <<<<<", uid, ret, get_error_message(ret));
895 /* Need to unload voice when used voice is unregistered */
896 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
897 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
898 return TTSD_ERROR_OPERATION_FAILED;
901 ttsd_data_delete_client(uid);
902 __set_and_notify_activated_mode();
904 /* unload engine, if ref count of client is 0 */
905 if (0 == ttsd_data_get_client_count()) {
906 if (NULL == g_quit_loop_timer) {
907 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
908 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
910 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
914 return TTSD_ERROR_NONE;
917 int ttsd_server_update_instant_reprepare_client()
919 SLOG(LOG_INFO, tts_tag(), "[Server] Update instant reprepare client");
920 unsigned int instant_reprepare_uid = TTS_INVALID_UID;
921 ttsd_config_get_instant_reprepare_client(&instant_reprepare_uid);
923 if (0 <= ttsd_data_is_client(instant_reprepare_uid)) {
924 SLOG(LOG_INFO, tts_tag(), "[Server] Uid(%u) is valid. Still no need to changee the value", instant_reprepare_uid);
925 return TTSD_ERROR_NONE;
928 unsigned int new_instant_reprepare_uid = ttsd_data_get_first_client_uid();
929 int ret = ttsd_config_set_instant_reprepare_client(new_instant_reprepare_uid);
930 if (TTSD_ERROR_NONE != ret) {
931 SLOG(LOG_ERROR, tts_tag(), "[Server] Fail to set UID(%u) of instant reprepare client", instant_reprepare_uid);
932 return TTSD_ERROR_OPERATION_FAILED;
934 SLOG(LOG_INFO, tts_tag(), "[Server] Update UID(%u) of instant reprepare client", instant_reprepare_uid);
936 return TTSD_ERROR_NONE;
939 static bool __is_connected_to_network()
942 int network_status = 0;
943 int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
945 SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
949 if (network_status == VCONFKEY_NETWORK_OFF) {
950 SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
954 SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
959 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)
961 app_tts_state_e state = ttsd_data_get_client_state(uid);
962 if (APP_STATE_NONE == state) {
963 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_text : uid is not valid");
964 return TTSD_ERROR_INVALID_PARAMETER;
967 /* check valid voice */
968 char* temp_lang = NULL;
970 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
971 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
972 if (NULL != temp_lang) {
976 return TTSD_ERROR_INVALID_VOICE;
979 if (NULL == temp_lang) {
980 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
981 return TTSD_ERROR_INVALID_VOICE;
984 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
985 if (NULL == speak_data) {
986 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
987 if (NULL != temp_lang) {
992 return TTSD_ERROR_OPERATION_FAILED;
995 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);
997 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
999 ret = ttsd_data_add_speak_data(uid, speak_data);
1001 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
1002 if (NULL != temp_lang) {
1007 ttsd_data_destroy_speak_data(speak_data);
1013 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
1014 /* Request load voice */
1015 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
1016 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
1017 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
1021 if (NULL != temp_lang) {
1026 if (APP_STATE_PLAYING == state) {
1027 /* check if engine use network */
1028 if (ttsd_engine_agent_need_network()) {
1029 if (false == __is_connected_to_network()) {
1030 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
1031 return TTSD_ERROR_OPERATION_FAILED;
1035 ttsd_data_set_credential(uid, credential);
1037 /* Check whether tts-engine is running or not */
1038 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1039 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1040 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1041 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1047 return TTSD_ERROR_NONE;
1050 static int __interrupt_player_by_policy(unsigned int request_app_uid)
1052 unsigned int playing_app_uid = ttsd_data_get_current_playing();
1053 if (0 > ttsd_data_is_client(playing_app_uid)) {
1054 SLOG(LOG_INFO, tts_tag(), "[Server] There is no app which is in 'Play' state");
1055 return TTSD_ERROR_NONE;
1058 if (request_app_uid == playing_app_uid) {
1059 SLOG(LOG_WARN, tts_tag(), "[Server] This is played uid. Skip policy behavior");
1060 return TTSD_ERROR_NONE;
1063 ttsd_mode_e request_app_mode = ttsd_data_get_mode(request_app_uid);
1064 ttsd_mode_e playing_app_mode = ttsd_data_get_mode(playing_app_uid);
1065 switch (playing_app_mode) {
1066 case TTSD_MODE_DEFAULT:
1067 case TTSD_MODE_NOTIFICATION:
1068 case TTSD_MODE_SCREEN_READER:
1069 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
1070 __stop_and_send_ready_state(playing_app_uid);
1073 case TTSD_MODE_INTERRUPT:
1074 if (TTSD_MODE_INTERRUPT != request_app_mode) {
1075 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Default, Screen reader, and Noti are prohibitted.");
1076 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1079 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
1080 __stop_and_send_ready_state(playing_app_uid);
1084 return TTSD_ERROR_NONE;
1087 int ttsd_server_play(unsigned int uid, const char* credential)
1089 app_tts_state_e state = ttsd_data_get_client_state(uid);
1090 if (APP_STATE_NONE == state) {
1091 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1092 return TTSD_ERROR_INVALID_PARAMETER;
1095 if (APP_STATE_PLAYING == state) {
1096 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1097 return TTSD_ERROR_NONE;
1100 /* check if engine use network */
1101 if (ttsd_engine_agent_need_network()) {
1102 if (false == __is_connected_to_network()) {
1103 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
1104 return TTSD_ERROR_OUT_OF_NETWORK;
1108 int ret = __interrupt_player_by_policy(uid);
1109 if (TTSD_ERROR_NONE != ret) {
1110 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1114 /* Change current play */
1115 if (0 != ttsd_player_wait_to_play(uid)) {
1116 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1119 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1120 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1121 return TTSD_ERROR_OPERATION_FAILED;
1124 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1125 ret = ttsd_player_play(uid);
1126 if (TTSD_ERROR_NONE != ret) {
1127 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1128 ttsd_data_set_client_state(uid, state);
1129 return TTSD_ERROR_OPERATION_FAILED;
1132 ttsd_data_set_credential(uid, credential);
1134 /* Check whether tts-engine is running or not */
1135 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1136 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1137 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1138 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1139 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1143 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_SYNTH);
1145 return TTSD_ERROR_NONE;
1148 static void __stop_engine_synthesis(unsigned int uid)
1150 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1151 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1153 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1154 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1156 int ret = ttsd_engine_cancel_synthesis();
1157 if (TTSD_ERROR_NONE != ret)
1158 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1161 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1164 int ttsd_server_stop(unsigned int uid)
1166 app_tts_state_e state = ttsd_data_get_client_state(uid);
1167 if (APP_STATE_NONE == state) {
1168 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1169 return TTSD_ERROR_INVALID_PARAMETER;
1172 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1174 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1175 if (TTS_APP_PLAY_TYPE_SYNTH == ttsd_data_get_play_type(uid)) {
1176 __stop_engine_synthesis(uid);
1180 ttsd_data_set_client_state(uid, APP_STATE_READY);
1181 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1182 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1186 /* Reset all data */
1187 ttsd_data_clear_data(uid);
1189 return TTSD_ERROR_NONE;
1192 int ttsd_server_pause(unsigned int uid)
1194 app_tts_state_e state = ttsd_data_get_client_state(uid);
1195 if (APP_STATE_NONE == state) {
1196 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1197 return TTSD_ERROR_INVALID_PARAMETER;
1200 if (APP_STATE_PLAYING != state) {
1201 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1202 return TTSD_ERROR_INVALID_STATE;
1205 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1206 if (TTSD_ERROR_NONE != ret) {
1207 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
1208 return TTSD_ERROR_OPERATION_FAILED;
1211 ret = ttsd_player_pause(uid);
1212 if (TTSD_ERROR_NONE != ret) {
1213 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1214 return TTSD_ERROR_OPERATION_FAILED;
1217 return TTSD_ERROR_NONE;
1220 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1222 if (0 > ttsd_data_is_client(uid)) {
1223 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1224 return TTSD_ERROR_INVALID_PARAMETER;
1228 int ret = ttsd_engine_get_voice_list(voice_list);
1230 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1234 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1236 return TTSD_ERROR_NONE;
1239 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1241 if (0 > ttsd_data_is_client(uid)) {
1242 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1243 return TTSD_ERROR_INVALID_PARAMETER;
1246 /* get current voice */
1247 int ret = ttsd_engine_get_default_voice(language, voice_type);
1249 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1253 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1255 return TTSD_ERROR_NONE;
1258 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1260 app_tts_state_e state = ttsd_data_get_client_state(uid);
1261 if (APP_STATE_NONE == state) {
1262 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1263 return TTSD_ERROR_INVALID_PARAMETER;
1266 if (APP_STATE_READY != state) {
1267 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1268 return TTSD_ERROR_INVALID_STATE;
1271 int ret = ttsd_engine_set_private_data(key, data);
1273 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1275 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1281 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1283 app_tts_state_e state = ttsd_data_get_client_state(uid);
1284 if (APP_STATE_NONE == state) {
1285 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1286 return TTSD_ERROR_INVALID_PARAMETER;
1289 if (APP_STATE_READY != state) {
1290 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1291 return TTSD_ERROR_INVALID_STATE;
1294 char* temp_data = NULL;
1295 int ret = ttsd_engine_get_private_data(key, &temp_data);
1297 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1299 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1302 if (NULL == temp_data) {
1303 temp_data = strdup("NULL");
1310 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1312 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1315 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1317 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1323 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1325 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1328 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1330 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1336 int ttsd_get_activated_mode(int* activated_mode)
1338 if (NULL == activated_mode) {
1339 return TTSD_ERROR_INVALID_PARAMETER;
1342 *activated_mode = ttsd_data_get_activated_mode();
1343 return TTSD_ERROR_NONE;
1346 int ttsd_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
1348 int ret = ttsd_engine_agent_set_activated_mode_changed_cb(callback);
1349 if (TTSD_ERROR_NONE != ret) {
1350 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1356 int ttsd_server_play_pcm(unsigned int uid)
1358 app_tts_state_e state = ttsd_data_get_client_state(uid);
1359 if (APP_STATE_NONE == state) {
1360 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1361 return TTSD_ERROR_INVALID_PARAMETER;
1364 if (APP_STATE_PLAYING == state) {
1365 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1366 return TTSD_ERROR_NONE;
1369 int ret = __interrupt_player_by_policy(uid);
1370 if (TTSD_ERROR_NONE != ret) {
1371 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1375 /* Change current play */
1376 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1377 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1378 return TTSD_ERROR_OPERATION_FAILED;
1381 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1382 ret = ttsd_player_play(uid);
1383 if (TTSD_ERROR_NONE != ret) {
1384 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1385 ttsd_data_set_client_state(uid, state);
1386 return TTSD_ERROR_OPERATION_FAILED;
1389 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_PCM);
1391 return TTSD_ERROR_NONE;
1394 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1396 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1400 /* Synthesis is success */
1401 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1402 if (TTSE_RESULT_EVENT_START == event) {
1403 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1404 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1405 uid, uttid, data, data_size, audio_type, rate);
1406 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1407 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1408 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1409 uid, uttid, data, data_size, audio_type, rate);
1411 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1414 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1415 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1416 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1417 return TTSD_ERROR_INVALID_PARAMETER;
1420 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1421 if (NULL == sound_data) {
1422 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1423 return TTSD_ERROR_OUT_OF_MEMORY;
1426 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1427 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1428 ttsd_data_destroy_sound_data(sound_data);
1431 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1434 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1436 return TTSD_ERROR_NONE;
1439 int ttsd_server_get_service_state(unsigned int uid, int* service_state)
1441 if (0 > ttsd_data_is_client(uid)) {
1442 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_service_state : uid is not valid");
1443 return TTSD_ERROR_INVALID_PARAMETER;
1446 *service_state = (int)ttsd_state_get_state();
1447 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get current service state. service state(%d) ", *service_state);
1449 return TTSD_ERROR_NONE;