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 ttsd_state_set_state(TTSD_STATE_SYNTHESIZING);
176 ttsd_data_destroy_speak_data(speak_data);
178 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
184 * TTS Server Callback Functions
186 int ttsd_send_error(ttse_error_e error, const char* msg)
188 unsigned int uid = g_utt.uid;
189 int uttid = g_utt.uttid;
190 int tmp_pid = ttsd_data_get_pid(uid);
192 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));
194 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
196 if (0 != ttsd_data_clear_data(uid))
197 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
200 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
201 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
204 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
205 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
208 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
209 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
214 static int ttsd_convert_audio_type_to_bytes(ttse_audio_type_e audio_type)
217 switch (audio_type) {
218 case TTSE_AUDIO_TYPE_RAW_S16:
221 case TTSE_AUDIO_TYPE_RAW_U8:
225 ret = 2; // Default 16bit
231 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)
233 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
235 unsigned int uid = g_utt.uid;
236 int uttid = g_utt.uttid;
238 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
239 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
240 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
241 SLOG(LOG_DEBUG, tts_tag(), "@@@");
242 return TTSD_ERROR_INVALID_PARAMETER;
245 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
246 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
247 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
248 return TTSD_ERROR_INVALID_PARAMETER;
251 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
252 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
253 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
254 SLOG(LOG_DEBUG, tts_tag(), "@@@");
255 return TTSD_ERROR_INVALID_PARAMETER;
258 /* check the current state */
259 app_tts_state_e state = ttsd_data_get_client_state(uid);
260 if (APP_STATE_NONE == state) {
261 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
262 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
263 return TTSD_ERROR_INVALID_PARAMETER;
266 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
267 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
268 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
269 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
270 return TTSD_ERROR_NONE;
273 /* Synthesis is success */
274 if (TTSE_RESULT_EVENT_START == event) {
275 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
276 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
277 uid, uttid, data, data_size, audio_type, rate);
279 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
280 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
281 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
282 double d_latency = (double)latency;
283 g_avg_latency += d_latency;
284 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
285 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
287 g_total_data_size = 0;
288 g_total_data_size += data_size;
289 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);
290 } else if (TTSE_RESULT_EVENT_FINISH == event) {
291 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
292 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
293 uid, uttid, data, data_size, audio_type, rate);
295 g_total_data_size += data_size;
296 int audio_type_bytes = ttsd_convert_audio_type_to_bytes(audio_type);
298 clock_gettime(CLOCK_MONOTONIC_RAW, &g_finish_playing);
299 long long int time_processing = (uint64_t)TIME_DIFF(g_request_playing, g_finish_playing);
300 long long int time_speech = (long long int)(1000 * g_total_data_size * sizeof(short) / (rate * audio_type_bytes));
301 double xRT = (double)(time_processing) / (double)time_speech;
302 SLOG(LOG_INFO, tts_tag(), "[SERVER] time_processing : %lld, time_speech : %lld, total data size: %d", time_processing, time_speech, g_total_data_size);
303 SLOG(LOG_INFO, tts_tag(), "[SERVER] xRT : %lf", xRT);
305 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
306 g_total_data_size += data_size;
310 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
311 if (NULL == sound_data) {
312 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
313 return TTSD_ERROR_OUT_OF_MEMORY;
316 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
317 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
318 ttsd_data_destroy_sound_data(sound_data);
320 return TTSD_ERROR_OPERATION_FAILED;
323 if (event == TTSE_RESULT_EVENT_FINISH) {
324 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
327 return TTSD_ERROR_NONE;
330 static bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
332 if (0 > ttsd_data_is_client(uid)) {
333 SLOG(LOG_ERROR, tts_tag(), "[Server] Client(%d) is not valid.", uid);
334 ttsd_data_delete_client(uid);
338 /* clear client data */
339 ttsd_data_clear_data(uid);
341 if (APP_STATE_READY != state) {
342 ttsd_data_set_client_state(uid, APP_STATE_READY);
343 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
349 static void __stop_all_client()
351 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
353 ttsd_engine_cancel_synthesis();
354 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
356 /* stop all player */
357 ttsd_player_all_stop();
359 /* send interrupt message to all clients */
360 ttsd_data_foreach_clients(__get_client_cb, NULL);
363 static void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
366 case TTS_CONFIG_TYPE_ENGINE:
368 /* TODO - Determine the policy when engine process get engine changed cb */
369 if (NULL == str_param) {
370 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
375 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
376 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
377 ret = ttsd_engine_agent_unload_current_engine();
379 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
382 ret = ttsd_engine_agent_load_current_engine();
384 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
393 case TTS_CONFIG_TYPE_VOICE:
395 if (NULL == str_param) {
396 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
400 char* out_lang = NULL;
404 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
405 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
406 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
408 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
410 /* Current language is not available */
411 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
413 if (NULL != out_lang) {
420 case TTS_CONFIG_TYPE_SPEED:
422 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
423 /* set default speed */
425 ret = ttsd_engine_agent_set_default_speed(int_param);
427 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
433 case TTS_CONFIG_TYPE_PITCH:
435 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
436 /* set default speed */
438 ret = ttsd_engine_agent_set_default_pitch(int_param);
440 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
446 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
448 if (0.0 <= double_param && double_param <= 1.0) {
449 /* set default bg volume ratio */
451 ret = ttsd_player_set_background_volume_ratio(double_param);
453 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
463 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
465 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
466 ttsd_server_finalize(uid);
470 static int __terminate_server()
472 if (g_is_terminated) {
473 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
474 return TTSD_ERROR_NONE;
477 int ret = TTSD_ERROR_NONE;
478 ret = ttsd_ipc_close_connection();
479 if (TTSD_ERROR_NONE != ret) {
480 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_ipc_close_connection(). ret(%d)", ret);
481 return TTSD_ERROR_OPERATION_FAILED;
484 ret = ttsd_finalize();
485 if (TTSD_ERROR_NONE != ret) {
486 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_finalize(). ret(%d)", ret);
487 return TTSD_ERROR_OPERATION_FAILED;
490 g_is_terminated = true;
491 return TTSD_ERROR_NONE;
494 static Eina_Bool __quit_ecore_loop(void *data)
496 __terminate_server();
497 ecore_main_loop_quit();
502 void __screen_reader_changed_cb(bool value)
504 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
508 static void __read_proc()
511 struct dirent *dirp = NULL;
515 if (0 < g_list_length(g_proc_list)) {
516 iter = g_list_first(g_proc_list);
517 while (NULL != iter) {
518 g_proc_list = g_list_remove_link(g_proc_list, iter);
520 iter = g_list_first(g_proc_list);
524 dp = opendir("/proc");
526 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
532 tmp = atoi(dirp->d_name);
533 if (0 >= tmp) continue;
534 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
536 } while (NULL != dirp);
542 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
548 for (i = 0; i < g_list_length(g_proc_list); i++) {
549 iter = g_list_nth(g_proc_list, i);
551 if (pid == GPOINTER_TO_INT(iter->data)) {
552 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
559 if (false == exist) {
560 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
561 ttsd_server_finalize(uid);
567 static Eina_Bool __cleanup_client(void *data)
569 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
572 if (0 < ttsd_data_get_client_count()) {
573 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
575 if (NULL == g_quit_loop_timer) {
576 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
577 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
579 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
580 g_check_client_timer = NULL;
584 SLOG(LOG_DEBUG, tts_tag(), "@@@");
589 static bool __notify_state_changed_event(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
591 state_changed_args_t* args = (state_changed_args_t*)user_data;
596 if (ttsd_data_is_service_state_changed_cb_set(uid)) {
597 ttsdc_ipc_send_set_service_state_message(pid, uid, args->before, args->current);
603 static Eina_Bool __state_changed_timer_cb(void *data)
606 SLOG(LOG_ERROR, tts_tag(), "[Server] Invalid data passed.");
607 return ECORE_CALLBACK_CANCEL;
610 int ret = ttsd_data_foreach_clients(__notify_state_changed_event, data);
611 if (TTSD_ERROR_NONE != ret) {
612 SLOG(LOG_ERROR, tts_tag(), "[Server] Fail to notify state change.");
616 g_notify_state_timer = NULL;
617 return ECORE_CALLBACK_CANCEL;
620 static void __state_changed_cb(ttsd_state_e before, ttsd_state_e current)
622 state_changed_args_t* args = (state_changed_args_t*)calloc(1, sizeof(state_changed_args_t));
624 args->before = before;
625 args->current = current;
628 SLOG(LOG_INFO, tts_tag(), "[Server] Notify state chanaged from (%d) to (%d).", before, current);
629 g_notify_state_timer = ecore_timer_add(0.0, __state_changed_timer_cb, args);
635 int ttsd_initialize(ttse_request_callback_s *callback)
637 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
638 g_is_terminated = false;
640 if (ttsd_config_initialize(__config_changed_cb)) {
641 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
644 if (TTSD_ERROR_NONE != ttsd_state_initialize(__state_changed_cb)) {
645 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize state.");
649 if (ttsd_player_init()) {
650 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
651 return TTSD_ERROR_OPERATION_FAILED;
655 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
657 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
658 int ret = ttsd_player_set_background_volume_ratio(ratio);
660 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
662 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
665 /* Engine Agent initialize */
666 if (TTSD_ERROR_NONE != ttsd_engine_agent_init(callback)) {
667 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
668 return TTSD_ERROR_OPERATION_FAILED;
671 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
672 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
674 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, __cleanup_client, NULL);
675 if (NULL == g_check_client_timer) {
676 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
679 g_activated_mode = 0;
681 return TTSD_ERROR_NONE;
686 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
689 if (0 < g_list_length(g_proc_list)) {
690 iter = g_list_first(g_proc_list);
691 while (NULL != iter) {
692 g_proc_list = g_list_remove_link(g_proc_list, iter);
694 iter = g_list_first(g_proc_list);
698 if (g_notify_state_timer) {
699 void* data = ecore_timer_del(g_notify_state_timer);
700 g_notify_state_timer = NULL;
703 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore notify state timer handle");
707 ecore_timer_del(g_wait_timer);
709 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
712 if (g_check_client_timer) {
713 ecore_timer_del(g_check_client_timer);
714 g_check_client_timer = NULL;
715 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
718 if (g_quit_loop_timer) {
719 ecore_timer_del(g_quit_loop_timer);
720 g_quit_loop_timer = NULL;
721 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
724 ttsd_config_finalize();
725 ttsd_state_finalize();
727 int ret = TTSD_ERROR_NONE;
728 ret = ttsd_player_release();
729 if (TTSD_ERROR_NONE != ret) {
730 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_player_release(). ret(%d)", ret);
731 return TTSD_ERROR_OPERATION_FAILED;
734 ret = ttsd_engine_agent_release();
735 if (TTSD_ERROR_NONE != ret) {
736 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_engine_agent_release(). ret(%d)", ret);
737 return TTSD_ERROR_OPERATION_FAILED;
740 return TTSD_ERROR_NONE;
745 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
746 int ret = ttsd_data_foreach_clients(__terminate_client, NULL);
747 if (TTSD_ERROR_NONE != ret) {
748 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to terminate all clients. ret(%d)", ret);
749 return TTSD_ERROR_OPERATION_FAILED;
752 if (g_quit_loop_timer) {
753 ecore_timer_del(g_quit_loop_timer);
754 g_quit_loop_timer = NULL;
755 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
758 return __terminate_server();
762 * TTS Server Functions for Client
765 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
767 if (-1 != ttsd_data_is_client(uid))
768 *is_initialized = true;
770 *is_initialized = false;
772 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
773 return TTSD_ERROR_NONE;
776 static void __set_and_notify_activated_mode()
778 int activated_mode = ttsd_data_get_activated_mode();
779 if (g_activated_mode == activated_mode) {
783 SLOG(LOG_INFO, tts_tag(), "[Server] Activated mode is changed from(%d) to (%d)", g_activated_mode, activated_mode);
785 g_activated_mode = activated_mode;
786 int ret = ttsd_engine_notify_activated_mode_changed(g_activated_mode);
787 if (TTSD_ERROR_NONE != ret) {
788 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to notify activated mode changed event.(%d)", ret);
792 static int __check_app_agreed(int pid, unsigned int uid, bool credential_needed)
794 if (false == credential_needed) {
795 return TTSD_ERROR_NONE;
799 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
800 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
803 bool is_agreed = false;
804 int ret = ttsd_engine_check_app_agreed(appid, &is_agreed);
808 if (TTSD_ERROR_NONE != ret) {
809 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
810 return TTSD_ERROR_OPERATION_FAILED;
813 if (false == is_agreed) {
814 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
815 return TTSD_ERROR_PERMISSION_DENIED;
818 return TTSD_ERROR_NONE;
821 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)
823 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
825 if (-1 != ttsd_data_is_client(uid)) {
826 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
827 return TTSD_ERROR_NONE;
830 if (TTSD_ERROR_NONE != ttsd_data_new_client(pid, uid, mode, playing_mode, registered_event_mask, method)) {
831 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
832 return TTSD_ERROR_OPERATION_FAILED;
835 __set_and_notify_activated_mode();
837 if (TTSD_ERROR_NONE != ttsd_engine_agent_load_current_engine()) {
838 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
839 ttsd_data_delete_client(uid);
840 return TTSD_ERROR_OPERATION_FAILED;
843 bool is_needed = false;
844 if (TTSD_ERROR_NONE != ttsd_engine_agent_is_credential_needed(uid, &is_needed)) {
845 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
846 ttsd_data_delete_client(uid);
847 return TTSD_ERROR_OPERATION_FAILED;
850 int ret = __check_app_agreed(pid, uid, is_needed);
851 if (TTSD_ERROR_NONE != ret) {
852 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Checking credential is finished(%d)", ret);
853 ttsd_data_delete_client(uid);
857 ttsd_state_e tmp_service_state = ttsd_state_get_state();
858 if (TTSD_STATE_INVALID == tmp_service_state) {
859 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state");
860 ttsd_data_delete_client(uid);
861 return TTSD_ERROR_OPERATION_FAILED;
864 *credential_needed = is_needed;
865 *service_state = tmp_service_state;
867 return TTSD_ERROR_NONE;
870 void __used_voice_cb(const char* lang, int type)
872 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
873 if (0 != ttsd_engine_unload_voice(lang, type)) {
874 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
878 int ttsd_server_finalize(unsigned int uid)
880 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
881 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);
883 if (0 > ttsd_data_is_client(uid)) {
884 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
887 int ret = ttsd_server_stop(uid);
888 if (TTSD_ERROR_NONE != ret) {
889 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS server top(%u) fail (%d/%s) <<<<<", uid, ret, get_error_message(ret));
892 /* Need to unload voice when used voice is unregistered */
893 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
894 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
895 return TTSD_ERROR_OPERATION_FAILED;
898 ttsd_data_delete_client(uid);
899 __set_and_notify_activated_mode();
901 /* unload engine, if ref count of client is 0 */
902 if (0 == ttsd_data_get_client_count()) {
903 if (NULL == g_quit_loop_timer) {
904 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
905 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
907 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
911 return TTSD_ERROR_NONE;
914 int ttsd_server_update_instant_reprepare_client()
916 SLOG(LOG_INFO, tts_tag(), "[Server] Update instant reprepare client");
917 unsigned int instant_reprepare_uid = TTS_INVALID_UID;
918 ttsd_config_get_instant_reprepare_client(&instant_reprepare_uid);
920 if (0 <= ttsd_data_is_client(instant_reprepare_uid)) {
921 SLOG(LOG_INFO, tts_tag(), "[Server] Uid(%u) is valid. Still no need to changee the value", instant_reprepare_uid);
922 return TTSD_ERROR_NONE;
925 unsigned int new_instant_reprepare_uid = ttsd_data_get_first_client_uid();
926 int ret = ttsd_config_set_instant_reprepare_client(new_instant_reprepare_uid);
927 if (TTSD_ERROR_NONE != ret) {
928 SLOG(LOG_ERROR, tts_tag(), "[Server] Fail to set UID(%u) of instant reprepare client", instant_reprepare_uid);
929 return TTSD_ERROR_OPERATION_FAILED;
931 SLOG(LOG_INFO, tts_tag(), "[Server] Update UID(%u) of instant reprepare client", instant_reprepare_uid);
933 return TTSD_ERROR_NONE;
936 static bool __is_connected_to_network()
939 int network_status = 0;
940 int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
942 SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
946 if (network_status == VCONFKEY_NETWORK_OFF) {
947 SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
951 SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
956 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)
958 app_tts_state_e state = ttsd_data_get_client_state(uid);
959 if (APP_STATE_NONE == state) {
960 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_text : uid is not valid");
961 return TTSD_ERROR_INVALID_PARAMETER;
964 /* check valid voice */
965 char* temp_lang = NULL;
967 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
968 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
969 if (NULL != temp_lang) {
973 return TTSD_ERROR_INVALID_VOICE;
976 if (NULL == temp_lang) {
977 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
978 return TTSD_ERROR_INVALID_VOICE;
981 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
982 if (NULL == speak_data) {
983 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
984 if (NULL != temp_lang) {
989 return TTSD_ERROR_OPERATION_FAILED;
992 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);
994 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
996 ret = ttsd_data_add_speak_data(uid, speak_data);
998 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
999 if (NULL != temp_lang) {
1004 ttsd_data_destroy_speak_data(speak_data);
1010 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
1011 /* Request load voice */
1012 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
1013 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
1014 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
1018 if (NULL != temp_lang) {
1023 if (APP_STATE_PLAYING == state) {
1024 /* check if engine use network */
1025 if (ttsd_engine_agent_need_network()) {
1026 if (false == __is_connected_to_network()) {
1027 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
1028 return TTSD_ERROR_OPERATION_FAILED;
1032 ttsd_data_set_credential(uid, credential);
1034 /* Check whether tts-engine is running or not */
1035 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1036 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1037 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1038 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1044 return TTSD_ERROR_NONE;
1047 static int __interrupt_player_by_policy(unsigned int request_app_uid)
1049 unsigned int playing_app_uid = ttsd_data_get_current_playing();
1050 if (0 > ttsd_data_is_client(playing_app_uid)) {
1051 SLOG(LOG_INFO, tts_tag(), "[Server] There is no app which is in 'Play' state");
1052 return TTSD_ERROR_NONE;
1055 if (request_app_uid == playing_app_uid) {
1056 SLOG(LOG_WARN, tts_tag(), "[Server] This is played uid. Skip policy behavior");
1057 return TTSD_ERROR_NONE;
1060 ttsd_mode_e request_app_mode = ttsd_data_get_mode(request_app_uid);
1061 ttsd_mode_e playing_app_mode = ttsd_data_get_mode(playing_app_uid);
1062 switch (playing_app_mode) {
1063 case TTSD_MODE_DEFAULT:
1064 case TTSD_MODE_NOTIFICATION:
1065 case TTSD_MODE_SCREEN_READER:
1066 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
1067 __stop_and_send_ready_state(playing_app_uid);
1070 case TTSD_MODE_INTERRUPT:
1071 if (TTSD_MODE_INTERRUPT != request_app_mode) {
1072 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Default, Screen reader, and Noti are prohibitted.");
1073 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1076 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
1077 __stop_and_send_ready_state(playing_app_uid);
1081 return TTSD_ERROR_NONE;
1084 int ttsd_server_play(unsigned int uid, const char* credential)
1086 app_tts_state_e state = ttsd_data_get_client_state(uid);
1087 if (APP_STATE_NONE == state) {
1088 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1089 return TTSD_ERROR_INVALID_PARAMETER;
1092 if (APP_STATE_PLAYING == state) {
1093 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1094 return TTSD_ERROR_NONE;
1097 /* check if engine use network */
1098 if (ttsd_engine_agent_need_network()) {
1099 if (false == __is_connected_to_network()) {
1100 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
1101 return TTSD_ERROR_OUT_OF_NETWORK;
1105 int ret = __interrupt_player_by_policy(uid);
1106 if (TTSD_ERROR_NONE != ret) {
1107 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1111 /* Change current play */
1112 if (0 != ttsd_player_wait_to_play(uid)) {
1113 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1116 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1117 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1118 return TTSD_ERROR_OPERATION_FAILED;
1121 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1122 ret = ttsd_player_play(uid);
1123 if (TTSD_ERROR_NONE != ret) {
1124 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1125 ttsd_data_set_client_state(uid, state);
1126 return TTSD_ERROR_OPERATION_FAILED;
1129 ttsd_data_set_credential(uid, credential);
1131 /* Check whether tts-engine is running or not */
1132 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1133 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1134 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1135 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1136 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1140 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_SYNTH);
1142 return TTSD_ERROR_NONE;
1145 static void __stop_engine_synthesis(unsigned int uid)
1147 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1148 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1150 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1151 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1153 int ret = ttsd_engine_cancel_synthesis();
1154 if (TTSD_ERROR_NONE != ret)
1155 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1158 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1161 int ttsd_server_stop(unsigned int uid)
1163 app_tts_state_e state = ttsd_data_get_client_state(uid);
1164 if (APP_STATE_NONE == state) {
1165 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1166 return TTSD_ERROR_INVALID_PARAMETER;
1169 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1171 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1172 if (TTS_APP_PLAY_TYPE_SYNTH == ttsd_data_get_play_type(uid)) {
1173 __stop_engine_synthesis(uid);
1177 ttsd_data_set_client_state(uid, APP_STATE_READY);
1178 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1179 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1183 /* Reset all data */
1184 ttsd_data_clear_data(uid);
1186 return TTSD_ERROR_NONE;
1189 int ttsd_server_pause(unsigned int uid)
1191 app_tts_state_e state = ttsd_data_get_client_state(uid);
1192 if (APP_STATE_NONE == state) {
1193 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1194 return TTSD_ERROR_INVALID_PARAMETER;
1197 if (APP_STATE_PLAYING != state) {
1198 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1199 return TTSD_ERROR_INVALID_STATE;
1202 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1203 if (TTSD_ERROR_NONE != ret) {
1204 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
1205 return TTSD_ERROR_OPERATION_FAILED;
1208 ret = ttsd_player_pause(uid);
1209 if (TTSD_ERROR_NONE != ret) {
1210 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1211 return TTSD_ERROR_OPERATION_FAILED;
1214 return TTSD_ERROR_NONE;
1217 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1219 if (0 > ttsd_data_is_client(uid)) {
1220 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1221 return TTSD_ERROR_INVALID_PARAMETER;
1225 int ret = ttsd_engine_get_voice_list(voice_list);
1227 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1231 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1233 return TTSD_ERROR_NONE;
1236 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1238 if (0 > ttsd_data_is_client(uid)) {
1239 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1240 return TTSD_ERROR_INVALID_PARAMETER;
1243 /* get current voice */
1244 int ret = ttsd_engine_get_default_voice(language, voice_type);
1246 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1250 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1252 return TTSD_ERROR_NONE;
1255 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1257 app_tts_state_e state = ttsd_data_get_client_state(uid);
1258 if (APP_STATE_NONE == state) {
1259 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1260 return TTSD_ERROR_INVALID_PARAMETER;
1263 if (APP_STATE_READY != state) {
1264 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1265 return TTSD_ERROR_INVALID_STATE;
1268 int ret = ttsd_engine_set_private_data(key, data);
1270 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1272 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1278 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1280 app_tts_state_e state = ttsd_data_get_client_state(uid);
1281 if (APP_STATE_NONE == state) {
1282 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1283 return TTSD_ERROR_INVALID_PARAMETER;
1286 if (APP_STATE_READY != state) {
1287 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1288 return TTSD_ERROR_INVALID_STATE;
1291 char* temp_data = NULL;
1292 int ret = ttsd_engine_get_private_data(key, &temp_data);
1294 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1296 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1299 if (NULL == temp_data) {
1300 temp_data = strdup("NULL");
1307 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1309 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1312 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1314 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1320 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1322 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1325 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1327 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1333 int ttsd_get_activated_mode(int* activated_mode)
1335 if (NULL == activated_mode) {
1336 return TTSD_ERROR_INVALID_PARAMETER;
1339 *activated_mode = ttsd_data_get_activated_mode();
1340 return TTSD_ERROR_NONE;
1343 int ttsd_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
1345 int ret = ttsd_engine_agent_set_activated_mode_changed_cb(callback);
1346 if (TTSD_ERROR_NONE != ret) {
1347 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1353 int ttsd_server_play_pcm(unsigned int uid)
1355 app_tts_state_e state = ttsd_data_get_client_state(uid);
1356 if (APP_STATE_NONE == state) {
1357 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1358 return TTSD_ERROR_INVALID_PARAMETER;
1361 if (APP_STATE_PLAYING == state) {
1362 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1363 return TTSD_ERROR_NONE;
1366 int ret = __interrupt_player_by_policy(uid);
1367 if (TTSD_ERROR_NONE != ret) {
1368 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1372 /* Change current play */
1373 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1374 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1375 return TTSD_ERROR_OPERATION_FAILED;
1378 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1379 ret = ttsd_player_play(uid);
1380 if (TTSD_ERROR_NONE != ret) {
1381 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1382 ttsd_data_set_client_state(uid, state);
1383 return TTSD_ERROR_OPERATION_FAILED;
1386 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_PCM);
1388 return TTSD_ERROR_NONE;
1391 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1393 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1397 /* Synthesis is success */
1398 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1399 if (TTSE_RESULT_EVENT_START == event) {
1400 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1401 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1402 uid, uttid, data, data_size, audio_type, rate);
1403 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1404 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1405 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1406 uid, uttid, data, data_size, audio_type, rate);
1408 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1411 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1412 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1413 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1414 return TTSD_ERROR_INVALID_PARAMETER;
1417 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1418 if (NULL == sound_data) {
1419 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1420 return TTSD_ERROR_OUT_OF_MEMORY;
1423 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1424 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1425 ttsd_data_destroy_sound_data(sound_data);
1428 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1431 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1433 return TTSD_ERROR_NONE;
1436 int ttsd_server_get_service_state(unsigned int uid, int* service_state)
1438 if (0 > ttsd_data_is_client(uid)) {
1439 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_service_state : uid is not valid");
1440 return TTSD_ERROR_INVALID_PARAMETER;
1443 *service_state = (int)ttsd_state_get_state();
1444 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get current service state. service state(%d) ", *service_state);
1446 return TTSD_ERROR_NONE;