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;
312 ttsd_playing_mode_e playing_mode = ttsd_data_get_playing_mode(uid);
313 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] uid(%d), playing mode(%d)", uid, playing_mode);
314 if (TTSD_PLAYING_MODE_BY_SERVICE == playing_mode) {
316 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
317 if (NULL == sound_data) {
318 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
319 return TTSD_ERROR_OUT_OF_MEMORY;
322 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
323 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
324 ttsd_data_destroy_sound_data(sound_data);
326 return TTSD_ERROR_OPERATION_FAILED;
328 } else { /* TTSD_PLAYING_MODE_BY_CLIENT */
329 /* send pcm data to client */
330 int tmp_pid = ttsd_data_get_pid(uid);
331 if (0 != ttsdc_ipc_send_pcm(tmp_pid, uid, uttid, event, data, data_size, audio_type, rate)) {
332 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to send pcm data to client");
333 return TTSD_ERROR_OPERATION_FAILED;
337 if (event == TTSE_RESULT_EVENT_FINISH) {
338 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
341 return TTSD_ERROR_NONE;
344 static bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
346 if (0 > ttsd_data_is_client(uid)) {
347 SLOG(LOG_ERROR, tts_tag(), "[Server] Client(%d) is not valid.", uid);
348 ttsd_data_delete_client(uid);
352 /* clear client data */
353 ttsd_data_clear_data(uid);
355 if (APP_STATE_READY != state) {
356 ttsd_data_set_client_state(uid, APP_STATE_READY);
357 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
363 static void __stop_all_client()
365 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
367 ttsd_engine_cancel_synthesis();
368 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
370 /* stop all player */
371 ttsd_player_all_stop();
373 /* send interrupt message to all clients */
374 ttsd_data_foreach_clients(__get_client_cb, NULL);
377 static void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
380 case TTS_CONFIG_TYPE_ENGINE:
382 /* TODO - Determine the policy when engine process get engine changed cb */
383 if (NULL == str_param) {
384 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
389 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
390 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
391 ret = ttsd_engine_agent_unload_current_engine();
393 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
396 ret = ttsd_engine_agent_load_current_engine();
398 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
407 case TTS_CONFIG_TYPE_VOICE:
409 if (NULL == str_param) {
410 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
414 char* out_lang = NULL;
418 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
419 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
420 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
422 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
424 /* Current language is not available */
425 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
427 if (NULL != out_lang) {
434 case TTS_CONFIG_TYPE_SPEED:
436 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
437 /* set default speed */
439 ret = ttsd_engine_agent_set_default_speed(int_param);
441 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
447 case TTS_CONFIG_TYPE_PITCH:
449 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
450 /* set default speed */
452 ret = ttsd_engine_agent_set_default_pitch(int_param);
454 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
460 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
462 if (0.0 <= double_param && double_param <= 1.0) {
463 /* set default bg volume ratio */
465 ret = ttsd_player_set_background_volume_ratio(double_param);
467 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
477 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
479 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
480 ttsd_server_finalize(uid);
484 static int __terminate_server()
486 if (g_is_terminated) {
487 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
488 return TTSD_ERROR_NONE;
491 int ret = TTSD_ERROR_NONE;
492 ret = ttsd_ipc_close_connection();
493 if (TTSD_ERROR_NONE != ret) {
494 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_ipc_close_connection(). ret(%d)", ret);
495 return TTSD_ERROR_OPERATION_FAILED;
498 ret = ttsd_finalize();
499 if (TTSD_ERROR_NONE != ret) {
500 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_finalize(). ret(%d)", ret);
501 return TTSD_ERROR_OPERATION_FAILED;
504 g_is_terminated = true;
505 return TTSD_ERROR_NONE;
508 static Eina_Bool __quit_ecore_loop(void *data)
510 __terminate_server();
511 ecore_main_loop_quit();
516 void __screen_reader_changed_cb(bool value)
518 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
522 static void __read_proc()
525 struct dirent *dirp = NULL;
529 if (0 < g_list_length(g_proc_list)) {
530 iter = g_list_first(g_proc_list);
531 while (NULL != iter) {
532 g_proc_list = g_list_remove_link(g_proc_list, iter);
534 iter = g_list_first(g_proc_list);
538 dp = opendir("/proc");
540 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
546 tmp = atoi(dirp->d_name);
547 if (0 >= tmp) continue;
548 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
550 } while (NULL != dirp);
556 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
562 for (i = 0; i < g_list_length(g_proc_list); i++) {
563 iter = g_list_nth(g_proc_list, i);
565 if (pid == GPOINTER_TO_INT(iter->data)) {
566 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
573 if (false == exist) {
574 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
575 ttsd_server_finalize(uid);
581 static Eina_Bool __cleanup_client(void *data)
583 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
586 if (0 < ttsd_data_get_client_count()) {
587 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
589 if (NULL == g_quit_loop_timer) {
590 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
591 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
593 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
594 g_check_client_timer = NULL;
598 SLOG(LOG_DEBUG, tts_tag(), "@@@");
603 static bool __notify_state_changed_event(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
605 state_changed_args_t* args = (state_changed_args_t*)user_data;
610 if (ttsd_data_is_service_state_changed_cb_set(uid)) {
611 ttsdc_ipc_send_set_service_state_message(pid, uid, args->before, args->current);
617 static Eina_Bool __state_changed_timer_cb(void *data)
620 SLOG(LOG_ERROR, tts_tag(), "[Server] Invalid data passed.");
621 return ECORE_CALLBACK_CANCEL;
624 int ret = ttsd_data_foreach_clients(__notify_state_changed_event, data);
625 if (TTSD_ERROR_NONE != ret) {
626 SLOG(LOG_ERROR, tts_tag(), "[Server] Fail to notify state change.");
630 g_notify_state_timer = NULL;
631 return ECORE_CALLBACK_CANCEL;
634 static void __state_changed_cb(ttsd_state_e before, ttsd_state_e current)
636 state_changed_args_t* args = (state_changed_args_t*)calloc(1, sizeof(state_changed_args_t));
638 args->before = before;
639 args->current = current;
642 SLOG(LOG_INFO, tts_tag(), "[Server] Notify state chanaged from (%d) to (%d).", before, current);
643 g_notify_state_timer = ecore_timer_add(0.0, __state_changed_timer_cb, args);
649 int ttsd_initialize(ttse_request_callback_s *callback)
651 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
652 g_is_terminated = false;
654 if (ttsd_config_initialize(__config_changed_cb)) {
655 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
658 if (TTSD_ERROR_NONE != ttsd_state_initialize(__state_changed_cb)) {
659 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize state.");
663 if (ttsd_player_init()) {
664 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
665 return TTSD_ERROR_OPERATION_FAILED;
669 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
671 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
672 int ret = ttsd_player_set_background_volume_ratio(ratio);
674 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
676 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
679 /* Engine Agent initialize */
680 if (TTSD_ERROR_NONE != ttsd_engine_agent_init(callback)) {
681 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
682 return TTSD_ERROR_OPERATION_FAILED;
685 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
686 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
688 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, __cleanup_client, NULL);
689 if (NULL == g_check_client_timer) {
690 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
693 g_activated_mode = 0;
695 return TTSD_ERROR_NONE;
700 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
703 if (0 < g_list_length(g_proc_list)) {
704 iter = g_list_first(g_proc_list);
705 while (NULL != iter) {
706 g_proc_list = g_list_remove_link(g_proc_list, iter);
708 iter = g_list_first(g_proc_list);
712 if (g_notify_state_timer) {
713 void* data = ecore_timer_del(g_notify_state_timer);
714 g_notify_state_timer = NULL;
717 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore notify state timer handle");
721 ecore_timer_del(g_wait_timer);
723 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
726 if (g_check_client_timer) {
727 ecore_timer_del(g_check_client_timer);
728 g_check_client_timer = NULL;
729 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
732 if (g_quit_loop_timer) {
733 ecore_timer_del(g_quit_loop_timer);
734 g_quit_loop_timer = NULL;
735 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
738 ttsd_config_finalize();
739 ttsd_state_finalize();
741 int ret = TTSD_ERROR_NONE;
742 ret = ttsd_player_release();
743 if (TTSD_ERROR_NONE != ret) {
744 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_player_release(). ret(%d)", ret);
745 return TTSD_ERROR_OPERATION_FAILED;
748 ret = ttsd_engine_agent_release();
749 if (TTSD_ERROR_NONE != ret) {
750 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_engine_agent_release(). ret(%d)", ret);
751 return TTSD_ERROR_OPERATION_FAILED;
754 return TTSD_ERROR_NONE;
759 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
760 int ret = ttsd_data_foreach_clients(__terminate_client, NULL);
761 if (TTSD_ERROR_NONE != ret) {
762 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to terminate all clients. ret(%d)", ret);
763 return TTSD_ERROR_OPERATION_FAILED;
766 if (g_quit_loop_timer) {
767 ecore_timer_del(g_quit_loop_timer);
768 g_quit_loop_timer = NULL;
769 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
772 return __terminate_server();
776 * TTS Server Functions for Client
779 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
781 if (-1 != ttsd_data_is_client(uid))
782 *is_initialized = true;
784 *is_initialized = false;
786 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
787 return TTSD_ERROR_NONE;
790 static void __set_and_notify_activated_mode()
792 int activated_mode = ttsd_data_get_activated_mode();
793 if (g_activated_mode == activated_mode) {
797 SLOG(LOG_INFO, tts_tag(), "[Server] Activated mode is changed from(%d) to (%d)", g_activated_mode, activated_mode);
799 g_activated_mode = activated_mode;
800 int ret = ttsd_engine_notify_activated_mode_changed(g_activated_mode);
801 if (TTSD_ERROR_NONE != ret) {
802 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to notify activated mode changed event.(%d)", ret);
806 static int __check_app_agreed(int pid, unsigned int uid, bool credential_needed)
808 if (false == credential_needed) {
809 return TTSD_ERROR_NONE;
813 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
814 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
817 bool is_agreed = false;
818 int ret = ttsd_engine_check_app_agreed(appid, &is_agreed);
822 if (TTSD_ERROR_NONE != ret) {
823 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
824 return TTSD_ERROR_OPERATION_FAILED;
827 if (false == is_agreed) {
828 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
829 return TTSD_ERROR_PERMISSION_DENIED;
832 return TTSD_ERROR_NONE;
835 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)
837 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
839 if (-1 != ttsd_data_is_client(uid)) {
840 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
841 return TTSD_ERROR_NONE;
844 if (TTSD_ERROR_NONE != ttsd_data_new_client(pid, uid, mode, playing_mode, registered_event_mask, method)) {
845 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
846 return TTSD_ERROR_OPERATION_FAILED;
849 __set_and_notify_activated_mode();
851 if (TTSD_ERROR_NONE != ttsd_engine_agent_load_current_engine()) {
852 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
853 ttsd_data_delete_client(uid);
854 return TTSD_ERROR_OPERATION_FAILED;
857 bool is_needed = false;
858 if (TTSD_ERROR_NONE != ttsd_engine_agent_is_credential_needed(uid, &is_needed)) {
859 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
860 ttsd_data_delete_client(uid);
861 return TTSD_ERROR_OPERATION_FAILED;
864 int ret = __check_app_agreed(pid, uid, is_needed);
865 if (TTSD_ERROR_NONE != ret) {
866 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Checking credential is finished(%d)", ret);
867 ttsd_data_delete_client(uid);
871 ttsd_state_e tmp_service_state = ttsd_state_get_state();
872 if (TTSD_STATE_INVALID == tmp_service_state) {
873 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state");
874 ttsd_data_delete_client(uid);
875 return TTSD_ERROR_OPERATION_FAILED;
878 *credential_needed = is_needed;
879 *service_state = tmp_service_state;
881 return TTSD_ERROR_NONE;
884 void __used_voice_cb(const char* lang, int type)
886 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
887 if (0 != ttsd_engine_unload_voice(lang, type)) {
888 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
892 int ttsd_server_finalize(unsigned int uid)
894 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
895 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);
897 if (0 > ttsd_data_is_client(uid)) {
898 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
901 int ret = ttsd_server_stop(uid);
902 if (TTSD_ERROR_NONE != ret) {
903 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS server top(%u) fail (%d/%s) <<<<<", uid, ret, get_error_message(ret));
906 /* Need to unload voice when used voice is unregistered */
907 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
908 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
909 return TTSD_ERROR_OPERATION_FAILED;
912 ttsd_data_delete_client(uid);
913 __set_and_notify_activated_mode();
915 /* unload engine, if ref count of client is 0 */
916 if (0 == ttsd_data_get_client_count()) {
917 if (NULL == g_quit_loop_timer) {
918 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
919 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
921 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
925 return TTSD_ERROR_NONE;
928 int ttsd_server_update_instant_reprepare_client()
930 SLOG(LOG_INFO, tts_tag(), "[Server] Update instant reprepare client");
931 unsigned int instant_reprepare_uid = TTS_INVALID_UID;
932 ttsd_config_get_instant_reprepare_client(&instant_reprepare_uid);
934 if (0 <= ttsd_data_is_client(instant_reprepare_uid)) {
935 SLOG(LOG_INFO, tts_tag(), "[Server] Uid(%u) is valid. Still no need to changee the value", instant_reprepare_uid);
936 return TTSD_ERROR_NONE;
939 unsigned int new_instant_reprepare_uid = ttsd_data_get_first_client_uid();
940 int ret = ttsd_config_set_instant_reprepare_client(new_instant_reprepare_uid);
941 if (TTSD_ERROR_NONE != ret) {
942 SLOG(LOG_ERROR, tts_tag(), "[Server] Fail to set UID(%u) of instant reprepare client", instant_reprepare_uid);
943 return TTSD_ERROR_OPERATION_FAILED;
945 SLOG(LOG_INFO, tts_tag(), "[Server] Update UID(%u) of instant reprepare client", instant_reprepare_uid);
947 return TTSD_ERROR_NONE;
950 static bool __is_connected_to_network()
953 int network_status = 0;
954 int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
956 SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
960 if (network_status == VCONFKEY_NETWORK_OFF) {
961 SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
965 SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
970 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)
972 app_tts_state_e state = ttsd_data_get_client_state(uid);
973 if (APP_STATE_NONE == state) {
974 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_text : uid is not valid");
975 return TTSD_ERROR_INVALID_PARAMETER;
978 /* check valid voice */
979 char* temp_lang = NULL;
981 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
982 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
983 if (NULL != temp_lang) {
987 return TTSD_ERROR_INVALID_VOICE;
990 if (NULL == temp_lang) {
991 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
992 return TTSD_ERROR_INVALID_VOICE;
995 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
996 if (NULL == speak_data) {
997 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
998 if (NULL != temp_lang) {
1003 return TTSD_ERROR_OPERATION_FAILED;
1006 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);
1008 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
1010 ret = ttsd_data_add_speak_data(uid, speak_data);
1012 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
1013 if (NULL != temp_lang) {
1018 ttsd_data_destroy_speak_data(speak_data);
1024 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
1025 /* Request load voice */
1026 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
1027 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
1028 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
1032 if (NULL != temp_lang) {
1037 if (APP_STATE_PLAYING == state) {
1038 /* check if engine use network */
1039 if (ttsd_engine_agent_need_network()) {
1040 if (false == __is_connected_to_network()) {
1041 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
1042 return TTSD_ERROR_OPERATION_FAILED;
1046 ttsd_data_set_credential(uid, credential);
1048 /* Check whether tts-engine is running or not */
1049 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1050 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1051 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1052 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1058 return TTSD_ERROR_NONE;
1061 static int __interrupt_player_by_policy(unsigned int request_app_uid)
1063 unsigned int playing_app_uid = ttsd_data_get_current_playing();
1064 if (0 > ttsd_data_is_client(playing_app_uid)) {
1065 SLOG(LOG_INFO, tts_tag(), "[Server] There is no app which is in 'Play' state");
1066 return TTSD_ERROR_NONE;
1069 if (request_app_uid == playing_app_uid) {
1070 SLOG(LOG_WARN, tts_tag(), "[Server] This is played uid. Skip policy behavior");
1071 return TTSD_ERROR_NONE;
1074 ttsd_mode_e request_app_mode = ttsd_data_get_mode(request_app_uid);
1075 ttsd_mode_e playing_app_mode = ttsd_data_get_mode(playing_app_uid);
1076 switch (playing_app_mode) {
1077 case TTSD_MODE_DEFAULT:
1078 case TTSD_MODE_NOTIFICATION:
1079 case TTSD_MODE_SCREEN_READER:
1080 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
1081 __stop_and_send_ready_state(playing_app_uid);
1084 case TTSD_MODE_INTERRUPT:
1085 if (TTSD_MODE_INTERRUPT != request_app_mode) {
1086 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Default, Screen reader, and Noti are prohibitted.");
1087 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1090 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
1091 __stop_and_send_ready_state(playing_app_uid);
1095 return TTSD_ERROR_NONE;
1098 int ttsd_server_play(unsigned int uid, const char* credential)
1100 app_tts_state_e state = ttsd_data_get_client_state(uid);
1101 if (APP_STATE_NONE == state) {
1102 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1103 return TTSD_ERROR_INVALID_PARAMETER;
1106 if (APP_STATE_PLAYING == state) {
1107 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1108 return TTSD_ERROR_NONE;
1111 /* check if engine use network */
1112 if (ttsd_engine_agent_need_network()) {
1113 if (false == __is_connected_to_network()) {
1114 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
1115 return TTSD_ERROR_OUT_OF_NETWORK;
1119 int ret = __interrupt_player_by_policy(uid);
1120 if (TTSD_ERROR_NONE != ret) {
1121 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1125 /* Change current play */
1126 if (0 != ttsd_player_wait_to_play(uid)) {
1127 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1130 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1131 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1132 return TTSD_ERROR_OPERATION_FAILED;
1135 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1136 ret = ttsd_player_play(uid);
1137 if (TTSD_ERROR_NONE != ret) {
1138 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1139 ttsd_data_set_client_state(uid, state);
1140 return TTSD_ERROR_OPERATION_FAILED;
1143 ttsd_data_set_credential(uid, credential);
1145 /* Check whether tts-engine is running or not */
1146 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
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);
1149 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1150 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1154 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_SYNTH);
1156 return TTSD_ERROR_NONE;
1159 static void __stop_engine_synthesis(unsigned int uid)
1161 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1162 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1164 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1165 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1167 int ret = ttsd_engine_cancel_synthesis();
1168 if (TTSD_ERROR_NONE != ret)
1169 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1172 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1175 int ttsd_server_stop(unsigned int uid)
1177 app_tts_state_e state = ttsd_data_get_client_state(uid);
1178 if (APP_STATE_NONE == state) {
1179 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1180 return TTSD_ERROR_INVALID_PARAMETER;
1183 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1185 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1186 if (TTS_APP_PLAY_TYPE_SYNTH == ttsd_data_get_play_type(uid)) {
1187 __stop_engine_synthesis(uid);
1191 ttsd_data_set_client_state(uid, APP_STATE_READY);
1192 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1193 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1197 /* Reset all data */
1198 ttsd_data_clear_data(uid);
1200 return TTSD_ERROR_NONE;
1203 int ttsd_server_pause(unsigned int uid)
1205 app_tts_state_e state = ttsd_data_get_client_state(uid);
1206 if (APP_STATE_NONE == state) {
1207 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1208 return TTSD_ERROR_INVALID_PARAMETER;
1211 if (APP_STATE_PLAYING != state) {
1212 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1213 return TTSD_ERROR_INVALID_STATE;
1216 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1217 if (TTSD_ERROR_NONE != ret) {
1218 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
1219 return TTSD_ERROR_OPERATION_FAILED;
1222 ret = ttsd_player_pause(uid);
1223 if (TTSD_ERROR_NONE != ret) {
1224 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1225 return TTSD_ERROR_OPERATION_FAILED;
1228 return TTSD_ERROR_NONE;
1231 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1233 if (0 > ttsd_data_is_client(uid)) {
1234 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1235 return TTSD_ERROR_INVALID_PARAMETER;
1239 int ret = ttsd_engine_get_voice_list(voice_list);
1241 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1245 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1247 return TTSD_ERROR_NONE;
1250 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1252 if (0 > ttsd_data_is_client(uid)) {
1253 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1254 return TTSD_ERROR_INVALID_PARAMETER;
1257 /* get current voice */
1258 int ret = ttsd_engine_get_default_voice(language, voice_type);
1260 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1264 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1266 return TTSD_ERROR_NONE;
1269 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1271 app_tts_state_e state = ttsd_data_get_client_state(uid);
1272 if (APP_STATE_NONE == state) {
1273 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1274 return TTSD_ERROR_INVALID_PARAMETER;
1277 if (APP_STATE_READY != state) {
1278 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1279 return TTSD_ERROR_INVALID_STATE;
1282 int ret = ttsd_engine_set_private_data(key, data);
1284 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1286 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1292 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1294 app_tts_state_e state = ttsd_data_get_client_state(uid);
1295 if (APP_STATE_NONE == state) {
1296 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1297 return TTSD_ERROR_INVALID_PARAMETER;
1300 if (APP_STATE_READY != state) {
1301 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1302 return TTSD_ERROR_INVALID_STATE;
1305 char* temp_data = NULL;
1306 int ret = ttsd_engine_get_private_data(key, &temp_data);
1308 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1310 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1313 if (NULL == temp_data) {
1314 temp_data = strdup("NULL");
1321 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1323 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1326 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1328 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1334 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1336 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1339 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1341 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1347 int ttsd_get_activated_mode(int* activated_mode)
1349 if (NULL == activated_mode) {
1350 return TTSD_ERROR_INVALID_PARAMETER;
1353 *activated_mode = ttsd_data_get_activated_mode();
1354 return TTSD_ERROR_NONE;
1357 int ttsd_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
1359 int ret = ttsd_engine_agent_set_activated_mode_changed_cb(callback);
1360 if (TTSD_ERROR_NONE != ret) {
1361 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1367 int ttsd_server_play_pcm(unsigned int uid)
1369 app_tts_state_e state = ttsd_data_get_client_state(uid);
1370 if (APP_STATE_NONE == state) {
1371 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1372 return TTSD_ERROR_INVALID_PARAMETER;
1375 if (APP_STATE_PLAYING == state) {
1376 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1377 return TTSD_ERROR_NONE;
1380 int ret = __interrupt_player_by_policy(uid);
1381 if (TTSD_ERROR_NONE != ret) {
1382 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1386 /* Change current play */
1387 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1388 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1389 return TTSD_ERROR_OPERATION_FAILED;
1392 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1393 ret = ttsd_player_play(uid);
1394 if (TTSD_ERROR_NONE != ret) {
1395 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1396 ttsd_data_set_client_state(uid, state);
1397 return TTSD_ERROR_OPERATION_FAILED;
1400 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_PCM);
1402 return TTSD_ERROR_NONE;
1405 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1407 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1411 /* Synthesis is success */
1412 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1413 if (TTSE_RESULT_EVENT_START == event) {
1414 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1415 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1416 uid, uttid, data, data_size, audio_type, rate);
1417 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1418 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1419 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1420 uid, uttid, data, data_size, audio_type, rate);
1422 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1425 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1426 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1427 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1428 return TTSD_ERROR_INVALID_PARAMETER;
1431 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1432 if (NULL == sound_data) {
1433 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1434 return TTSD_ERROR_OUT_OF_MEMORY;
1437 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1438 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1439 ttsd_data_destroy_sound_data(sound_data);
1442 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1445 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1447 return TTSD_ERROR_NONE;
1450 int ttsd_server_get_service_state(unsigned int uid, int* service_state)
1452 if (0 > ttsd_data_is_client(uid)) {
1453 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_service_state : uid is not valid");
1454 return TTSD_ERROR_INVALID_PARAMETER;
1457 *service_state = (int)ttsd_state_get_state();
1458 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get current service state. service state(%d) ", *service_state);
1460 return TTSD_ERROR_NONE;