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"
21 #include "ttsd_dbus.h"
22 #include "ttsd_dbus_server.h"
24 #include "ttsd_engine_agent.h"
25 #include "ttsd_main.h"
26 #include "ttsd_network.h"
27 #include "ttsd_player.h"
28 #include "ttsd_server.h"
31 #define CLIENT_CLEAN_UP_TIME 500
33 /* for checking time */
34 #define TIME_DIFF(start, end) \
35 ((uint64_t)((end).tv_sec - (start).tv_sec) * 1000000 + (((end).tv_nsec - (start).tv_nsec) / 1000)) / 1000
37 static struct timespec g_request_playing, g_start_playing;
38 static double g_avg_latency;
39 static double g_min_latency;
40 static double g_max_latency;
41 static int g_file_num;
48 /* If current engine exist */
49 //static bool g_is_engine;
51 static Ecore_Timer* g_check_client_timer = NULL;
52 static Ecore_Timer* g_wait_timer = NULL;
53 static Ecore_Timer* g_terminate_timer = NULL;
54 static Ecore_Timer* g_quit_loop_timer = NULL;
56 static utterance_t g_utt;
58 static GList *g_proc_list = NULL;
60 static bool g_is_paused;
62 static bool g_is_terminated = false;
65 /* Function definitions */
66 static int __synthesis(unsigned int uid);
68 static Eina_Bool __wait_synthesis(void *data)
70 /* get current play */
71 unsigned int uid = ttsd_data_get_current_playing();
74 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
75 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
78 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
80 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
81 /* Start next synthesis */
92 static int __synthesis(unsigned int uid)
94 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
96 speak_data_s* speak_data = NULL;
97 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
98 if (NULL == speak_data) {
99 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
103 int pid = ttsd_data_get_pid(uid);
105 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
106 ttsd_data_destroy_speak_data(speak_data);
111 char appid[1024] = {0, };
112 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
113 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
116 if (NULL == speak_data->lang || NULL == speak_data->text) {
117 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
118 ttsd_server_stop(uid);
120 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
122 ttsd_data_destroy_speak_data(speak_data);
129 g_utt.uttid = speak_data->utt_id;
131 char* credential = ttsd_data_get_credential(uid);
133 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
134 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
135 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
136 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
137 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
138 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
141 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
142 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
144 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
146 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
148 ttsd_server_stop(uid);
150 int pid = ttsd_data_get_pid(uid);
152 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
154 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
157 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, NULL);
162 ttsd_data_destroy_speak_data(speak_data);
165 ttsd_data_destroy_speak_data(speak_data);
169 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
175 * TTS Server Callback Functions
177 int ttsd_send_error(ttse_error_e error, const char* msg)
179 unsigned int uid = g_utt.uid;
180 int uttid = g_utt.uttid;
181 int tmp_pid = ttsd_data_get_pid(uid);
183 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%u), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, (NULL == msg ? "NULL" : msg));
185 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
187 if (0 != ttsd_player_clear(uid))
188 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
190 if (0 != ttsd_data_clear_data(uid))
191 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
194 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
195 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
198 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
199 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
202 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
203 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
208 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)
210 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
212 unsigned int uid = g_utt.uid;
213 int uttid = g_utt.uttid;
215 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
216 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
217 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
218 SLOG(LOG_DEBUG, tts_tag(), "@@@");
219 return TTSD_ERROR_INVALID_PARAMETER;
222 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
223 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
224 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
225 return TTSD_ERROR_INVALID_PARAMETER;
228 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
229 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
230 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
231 SLOG(LOG_DEBUG, tts_tag(), "@@@");
232 return TTSD_ERROR_INVALID_PARAMETER;
235 /* check the current state */
236 app_tts_state_e state;
237 if (0 > ttsd_data_get_client_state(uid, &state)) {
238 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
239 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
240 return TTSD_ERROR_INVALID_PARAMETER;
243 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
244 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
245 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
246 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
247 return TTSD_ERROR_NONE;
250 /* Synthesis is success */
251 if (TTSE_RESULT_EVENT_START == event) {
252 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
253 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
254 uid, uttid, data, data_size, audio_type, rate);
256 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
257 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
258 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
259 double d_latency = (double)latency;
260 g_avg_latency += d_latency;
261 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
262 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
264 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);
265 } else if (TTSE_RESULT_EVENT_FINISH == event) {
266 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
267 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
268 uid, uttid, data, data_size, audio_type, rate);
270 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
274 sound_data_s* temp_sound_data = NULL;
275 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
276 if (NULL == temp_sound_data) {
277 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
278 return TTSD_ERROR_OUT_OF_MEMORY;
281 temp_sound_data->data = NULL;
282 temp_sound_data->rate = 0;
283 temp_sound_data->data_size = 0;
285 if (NULL != data && 0 < data_size) {
286 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
287 if (NULL != temp_sound_data->data) {
288 memcpy(temp_sound_data->data, data, data_size);
289 temp_sound_data->data_size = data_size;
290 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%u), event(%d) sound_data(%p) data(%p) size(%d)",
291 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
293 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
296 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
299 temp_sound_data->utt_id = uttid;
300 temp_sound_data->event = event;
301 temp_sound_data->audio_type = audio_type;
302 temp_sound_data->rate = rate;
304 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
305 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
306 if (NULL != temp_sound_data->data) {
307 free(temp_sound_data->data);
308 temp_sound_data->data = NULL;
311 free(temp_sound_data);
312 temp_sound_data = NULL;
314 return TTSD_ERROR_OPERATION_FAILED;
317 if (event == TTSE_RESULT_EVENT_FINISH) {
318 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
321 return TTSD_ERROR_NONE;
324 bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
326 /* clear client data */
327 ttsd_data_clear_data(uid);
329 if (APP_STATE_READY != state) {
330 ttsd_data_set_client_state(uid, APP_STATE_READY);
333 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
335 ttsd_data_delete_client(uid);
342 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
345 case TTS_CONFIG_TYPE_ENGINE:
347 /* TODO - Determine the policy when engine process get engine changed cb */
348 if (NULL == str_param) {
349 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
354 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
355 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
356 ret = ttsd_engine_agent_unload_current_engine();
358 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
361 ret = ttsd_engine_agent_load_current_engine();
363 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
368 /* stop all player */
369 ttsd_player_all_stop();
371 /* send interrupt message to all clients */
372 ttsd_data_foreach_clients(__get_client_cb, NULL);
374 ttsd_engine_cancel_synthesis();
379 case TTS_CONFIG_TYPE_VOICE:
381 if (NULL == str_param) {
382 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
386 char* out_lang = NULL;
390 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
391 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
392 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
394 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
396 /* Current language is not available */
397 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
399 if (NULL != out_lang) {
406 case TTS_CONFIG_TYPE_SPEED:
408 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
409 /* set default speed */
411 ret = ttsd_engine_agent_set_default_speed(int_param);
413 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
419 case TTS_CONFIG_TYPE_PITCH:
421 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
422 /* set default speed */
424 ret = ttsd_engine_agent_set_default_pitch(int_param);
426 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
432 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
434 if (0.0 <= double_param && double_param <= 1.0) {
435 /* set default bg volume ratio */
437 ret = ttsd_player_set_background_volume_ratio(double_param);
439 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
449 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
451 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
452 ttsd_server_finalize(uid);
456 static void __terminate_server()
458 if (g_is_terminated) {
459 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
463 g_is_terminated = true;
465 ttsd_ipc_close_connection();
466 ttsd_network_finalize();
470 static Eina_Bool __quit_ecore_loop(void *data)
472 __terminate_server();
473 ecore_main_loop_quit();
478 Eina_Bool ttsd_terminate_daemon(void *data)
480 ttsd_data_foreach_clients(__terminate_client, NULL);
482 if (g_quit_loop_timer) {
483 ecore_timer_del(g_quit_loop_timer);
484 g_quit_loop_timer = NULL;
485 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
488 __terminate_server();
490 g_terminate_timer = NULL;
494 void __screen_reader_changed_cb(bool value)
496 /* if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
497 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
498 if (g_terminate_timer) {
499 ecore_timer_del(g_terminate_timer);
500 g_terminate_timer = NULL;
502 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
505 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
510 int ttsd_send_all_stop()
512 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
514 /* stop all player */
515 ttsd_player_all_stop();
517 /* send interrupt message to all clients */
518 ttsd_data_foreach_clients(__get_client_cb, NULL);
520 return TTSD_ERROR_NONE;
526 int ttsd_initialize(ttse_request_callback_s *callback)
528 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
529 g_is_terminated = false;
531 if (ttsd_config_initialize(__config_changed_cb)) {
532 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
536 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
538 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
539 int ret = ttsd_player_set_background_volume_ratio(ratio);
541 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
544 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
547 if (ttsd_player_init()) {
548 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
549 return TTSD_ERROR_OPERATION_FAILED;
552 /* Engine Agent initialize */
553 if (0 != ttsd_engine_agent_init()) {
554 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
555 return TTSD_ERROR_OPERATION_FAILED;
558 /* set current engine */
559 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
560 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
561 // g_is_engine = false;
563 // g_is_engine = true;
565 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
566 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
567 return TTSD_ERROR_OPERATION_FAILED;
570 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
572 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
573 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
576 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
577 if (NULL == g_check_client_timer) {
578 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
581 return TTSD_ERROR_NONE;
586 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
589 if (0 < g_list_length(g_proc_list)) {
590 iter = g_list_first(g_proc_list);
591 while (NULL != iter) {
592 g_proc_list = g_list_remove_link(g_proc_list, iter);
594 iter = g_list_first(g_proc_list);
598 ttsd_config_finalize();
600 ttsd_player_release();
602 ttsd_engine_agent_release();
606 ecore_timer_del(g_wait_timer);
608 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
611 if (g_terminate_timer) {
612 ecore_timer_del(g_terminate_timer);
613 g_terminate_timer = NULL;
614 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
617 if (g_check_client_timer) {
618 ecore_timer_del(g_check_client_timer);
619 g_check_client_timer = NULL;
620 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
623 if (g_quit_loop_timer) {
624 ecore_timer_del(g_quit_loop_timer);
625 g_quit_loop_timer = NULL;
626 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
629 return TTSD_ERROR_NONE;
634 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
636 ttsd_terminate_daemon(NULL);
638 return TTSD_ERROR_NONE;
642 * TTS Server Functions for Client
645 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
647 if (-1 != ttsd_data_is_client(uid))
648 *is_initialized = true;
650 *is_initialized = false;
652 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
653 return TTSD_ERROR_NONE;
656 int ttsd_server_initialize(int pid, unsigned int uid, tts_ipc_method_e method, bool* credential_needed)
658 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
660 if (-1 != ttsd_data_is_client(uid)) {
661 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
662 return TTSD_ERROR_NONE;
665 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
666 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
667 return TTSD_ERROR_OPERATION_FAILED;
670 if (true == *credential_needed) {
672 if (0 != app_manager_get_app_id(pid, &appid)) {
673 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
675 bool is_agreed = false;
676 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
677 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
680 return TTSD_ERROR_OPERATION_FAILED;
685 if (false == is_agreed) {
686 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
687 return TTSD_ERROR_PERMISSION_DENIED;
691 if (0 != ttsd_data_new_client(pid, uid)) {
692 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
693 return TTSD_ERROR_OPERATION_FAILED;
696 if (0 != ttsd_data_set_ipc_method(uid, method)) {
697 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
698 return TTSD_ERROR_OPERATION_FAILED;
701 if (0 != ttsd_player_create_instance(uid)) {
702 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
703 return TTSD_ERROR_OPERATION_FAILED;
707 g_min_latency = 10000.0;
712 return TTSD_ERROR_NONE;
715 static void __read_proc()
718 struct dirent *dirp = NULL;
722 if (0 < g_list_length(g_proc_list)) {
723 iter = g_list_first(g_proc_list);
724 while (NULL != iter) {
725 g_proc_list = g_list_remove_link(g_proc_list, iter);
727 iter = g_list_first(g_proc_list);
731 dp = opendir("/proc");
733 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
739 tmp = atoi(dirp->d_name);
740 if (0 >= tmp) continue;
741 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
743 } while (NULL != dirp);
749 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
755 for (i = 0; i < g_list_length(g_proc_list); i++) {
756 iter = g_list_nth(g_proc_list, i);
758 if (pid == GPOINTER_TO_INT(iter->data)) {
759 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
766 if (false == exist) {
767 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
768 ttsd_server_finalize(uid);
773 char appid[1024] = {0, };
774 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
775 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
778 if (0 < strlen(appid)) {
779 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
781 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
784 result = ttsdc_send_hello(pid, uid);
787 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) should be removed.", uid);
788 ttsd_server_finalize(uid);
789 } else if (-1 == result) {
790 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
798 Eina_Bool ttsd_cleanup_client(void *data)
800 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
803 if (0 < ttsd_data_get_client_count()) {
804 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
806 if (NULL == g_quit_loop_timer) {
807 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
808 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
810 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
811 g_check_client_timer = NULL;
815 SLOG(LOG_DEBUG, tts_tag(), "@@@");
820 void __used_voice_cb(const char* lang, int type)
822 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
823 if (0 != ttsd_engine_unload_voice(lang, type)) {
824 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
828 int ttsd_server_finalize(unsigned int uid)
830 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
831 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);
833 app_tts_state_e state;
834 if (0 > ttsd_data_get_client_state(uid, &state)) {
835 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
838 ttsd_server_stop(uid);
839 ttsd_player_clear(uid);
841 ttsd_player_destroy_instance(uid);
843 /* Need to unload voice when used voice is unregistered */
844 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
845 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
846 return TTSD_ERROR_OPERATION_FAILED;
849 ttsd_data_delete_client(uid);
851 /* unload engine, if ref count of client is 0 */
852 if (0 == ttsd_data_get_client_count()) {
853 if (NULL == g_quit_loop_timer) {
854 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
855 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
857 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
861 return TTSD_ERROR_NONE;
864 int ttsd_server_set_mode(unsigned int uid, ttsd_mode_e mode)
866 app_tts_state_e state;
867 if (0 > ttsd_data_get_client_state(uid, &state)) {
868 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
869 return TTSD_ERROR_INVALID_PARAMETER;
872 if (APP_STATE_READY != state) {
873 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
874 return TTSD_ERROR_INVALID_STATE;
877 int ret = ttsd_data_set_mode(uid, mode);
879 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
881 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%u), mode(%d)", uid, mode);
887 int ttsd_server_add_queue(unsigned int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
889 app_tts_state_e state;
890 if (0 > ttsd_data_get_client_state(uid, &state)) {
891 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
892 return TTSD_ERROR_INVALID_PARAMETER;
895 /* check valid voice */
896 char* temp_lang = NULL;
898 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
899 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
900 if (NULL != temp_lang) {
904 return TTSD_ERROR_INVALID_VOICE;
907 if (NULL == temp_lang) {
908 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
909 return TTSD_ERROR_INVALID_VOICE;
912 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
913 if (NULL == speak_data) {
914 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
915 if (NULL != temp_lang) {
920 return TTSD_ERROR_OPERATION_FAILED;
923 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);
925 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
927 ret = ttsd_data_add_speak_data(uid, speak_data);
929 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
930 if (NULL != temp_lang) {
935 ttsd_data_destroy_speak_data(speak_data);
941 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
942 /* Request load voice */
943 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
944 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
945 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
949 if (NULL != temp_lang) {
954 if (APP_STATE_PLAYING == state) {
955 /* check if engine use network */
956 if (ttsd_engine_agent_need_network()) {
957 if (false == ttsd_network_is_connected()) {
958 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
959 return TTSD_ERROR_OPERATION_FAILED;
963 ttsd_data_set_credential(uid, credential);
965 /* Check whether tts-engine is running or not */
966 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
967 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
968 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
969 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
975 return TTSD_ERROR_NONE;
978 static int __pause_client(unsigned int uid)
980 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
981 if (TTSD_ERROR_NONE != ret) {
982 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
983 return TTSD_ERROR_OPERATION_FAILED;
986 ret = ttsd_player_pause(uid);
987 if (TTSD_ERROR_NONE != ret) {
988 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
989 return TTSD_ERROR_OPERATION_FAILED;
992 return TTSD_ERROR_NONE;
995 void __send_interrupt_client(unsigned int uid)
997 SLOG(LOG_INFO, tts_tag(), "[Server] Send new state by policy. uid(%u)", uid);
998 int pid = ttsd_data_get_pid(uid);
1000 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
1004 app_tts_state_e state = APP_STATE_CREATED;
1006 if (0 != ttsd_data_get_client_state(uid, &state)) {
1007 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state. uid(%u), pid(%d)", uid, pid);
1011 ttsdc_ipc_send_set_state_message(pid, uid, state);
1012 SLOG(LOG_INFO, tts_tag(), "[Server] Finish to send new state by policy. uid(%u)", uid);
1016 int ttsd_server_play(unsigned int uid, const char* credential)
1018 app_tts_state_e state;
1019 if (0 > ttsd_data_get_client_state(uid, &state)) {
1020 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1021 return TTSD_ERROR_INVALID_PARAMETER;
1024 if (APP_STATE_PLAYING == state) {
1025 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1026 return TTSD_ERROR_NONE;
1029 /* check if engine use network */
1030 if (ttsd_engine_agent_need_network()) {
1031 if (false == ttsd_network_is_connected()) {
1032 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
1033 return TTSD_ERROR_OUT_OF_NETWORK;
1037 /* check the current playback focus */
1038 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
1039 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
1040 ttsd_data_clear_data(uid);
1041 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1044 unsigned int current_uid = ttsd_data_get_current_playing();
1045 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1047 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1048 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1049 /* Send interrupt message */
1050 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1053 if (0 != ttsd_server_stop(current_uid)) {
1054 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1057 /* Default mode policy of interrupt is "Pause" */
1059 /* Send interrupt message */
1060 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1063 if (0 != __pause_client(current_uid)) {
1064 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1068 __send_interrupt_client(current_uid);
1071 /* Change current play */
1072 if (0 != ttsd_player_wait_to_play(uid)) {
1073 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1076 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1077 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1078 return TTSD_ERROR_OPERATION_FAILED;
1081 int ret = TTSD_ERROR_NONE;
1082 if (APP_STATE_PAUSED == state) {
1083 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1084 g_is_paused = false;
1085 ret = ttsd_player_resume(uid);
1087 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1088 ret = ttsd_player_play(uid);
1091 if (TTSD_ERROR_NONE != ret) {
1092 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1093 ttsd_data_set_client_state(uid, state);
1094 return TTSD_ERROR_OPERATION_FAILED;
1097 ttsd_data_set_credential(uid, credential);
1099 /* Check whether tts-engine is running or not */
1100 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1101 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1102 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1103 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1104 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1109 return TTSD_ERROR_NONE;
1112 int ttsd_server_stop(unsigned int uid)
1114 app_tts_state_e state;
1115 if (0 > ttsd_data_get_client_state(uid, &state)) {
1116 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1117 return TTSD_ERROR_INVALID_PARAMETER;
1120 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1122 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1123 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1124 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1125 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1126 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1128 int ret = ttsd_engine_cancel_synthesis();
1129 if (TTSD_ERROR_NONE != ret)
1130 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1132 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1135 ttsd_data_set_client_state(uid, APP_STATE_READY);
1136 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1137 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1141 /* Reset all data */
1142 ttsd_data_clear_data(uid);
1143 g_is_paused = false;
1145 return TTSD_ERROR_NONE;
1148 int ttsd_server_pause(unsigned int uid, int* utt_id)
1150 app_tts_state_e state;
1151 if (0 > ttsd_data_get_client_state(uid, &state)) {
1152 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1153 return TTSD_ERROR_INVALID_PARAMETER;
1156 if (APP_STATE_PLAYING != state) {
1157 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1158 return TTSD_ERROR_INVALID_STATE;
1163 *utt_id = g_utt.uttid;
1164 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%u), g_uid(%u), utt_id(%d)", uid, g_utt.uid, *utt_id);
1166 int ret = __pause_client(uid);
1167 if (TTSD_ERROR_NONE != ret) {
1168 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to pause player : ret(%d)", ret);
1169 return TTSD_ERROR_OPERATION_FAILED;
1172 return TTSD_ERROR_NONE;
1175 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1177 app_tts_state_e state;
1178 if (0 > ttsd_data_get_client_state(uid, &state)) {
1179 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1180 return TTSD_ERROR_INVALID_PARAMETER;
1184 int ret = ttsd_engine_get_voice_list(voice_list);
1186 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1190 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1192 return TTSD_ERROR_NONE;
1195 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1197 app_tts_state_e state;
1198 if (0 > ttsd_data_get_client_state(uid, &state)) {
1199 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1200 return TTSD_ERROR_INVALID_PARAMETER;
1203 /* get current voice */
1204 int ret = ttsd_engine_get_default_voice(language, voice_type);
1206 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1210 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1212 return TTSD_ERROR_NONE;
1215 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1217 app_tts_state_e state;
1218 if (0 > ttsd_data_get_client_state(uid, &state)) {
1219 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1220 return TTSD_ERROR_INVALID_PARAMETER;
1223 if (APP_STATE_READY != state) {
1224 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1225 return TTSD_ERROR_INVALID_STATE;
1228 int ret = ttsd_engine_set_private_data(key, data);
1230 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1232 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1238 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1240 app_tts_state_e state;
1241 if (0 > ttsd_data_get_client_state(uid, &state)) {
1242 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1243 return TTSD_ERROR_INVALID_PARAMETER;
1246 if (APP_STATE_READY != state) {
1247 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1248 return TTSD_ERROR_INVALID_STATE;
1251 int ret = ttsd_engine_get_private_data(key, data);
1253 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1255 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1261 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1263 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1266 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1268 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1274 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1276 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1279 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1281 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1287 int ttsd_server_play_pcm(unsigned int uid)
1289 app_tts_state_e state;
1290 if (0 > ttsd_data_get_client_state(uid, &state)) {
1291 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1292 return TTSD_ERROR_INVALID_PARAMETER;
1295 if (APP_STATE_PLAYING == state) {
1296 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1297 return TTSD_ERROR_NONE;
1300 unsigned int current_uid = ttsd_data_get_current_playing();
1301 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1303 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1304 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1305 /* Send interrupt message */
1306 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1309 if (0 != ttsd_server_stop(current_uid)) {
1310 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1313 /* Default mode policy of interrupt is "Pause" */
1315 /* Send interrupt message */
1316 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1319 if (0 != __pause_client(current_uid)) {
1320 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1324 __send_interrupt_client(current_uid);
1327 /* Change current play */
1328 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1329 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1330 return TTSD_ERROR_OPERATION_FAILED;
1333 int ret = TTSD_ERROR_NONE;
1334 if (APP_STATE_PAUSED == state) {
1335 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1336 g_is_paused = false;
1337 ret = ttsd_player_resume(uid);
1339 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1340 ret = ttsd_player_play(uid);
1343 if (TTSD_ERROR_NONE != ret) {
1344 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1345 ttsd_data_set_client_state(uid, state);
1346 return TTSD_ERROR_OPERATION_FAILED;
1349 return TTSD_ERROR_NONE;
1352 int ttsd_server_stop_pcm(unsigned int uid)
1354 app_tts_state_e state;
1355 if (0 > ttsd_data_get_client_state(uid, &state)) {
1356 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1357 return TTSD_ERROR_INVALID_PARAMETER;
1360 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1362 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1363 ttsd_data_set_client_state(uid, APP_STATE_READY);
1366 /* Reset all data */
1367 ttsd_data_clear_data(uid);
1369 ttsd_player_stop(uid);
1372 return TTSD_ERROR_NONE;
1375 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1377 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1381 /* Synthesis is success */
1382 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1383 if (TTSE_RESULT_EVENT_START == event) {
1384 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1385 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1386 uid, uttid, data, data_size, audio_type, rate);
1387 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1388 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1389 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1390 uid, uttid, data, data_size, audio_type, rate);
1392 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1395 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1396 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1397 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1398 return TTSD_ERROR_INVALID_PARAMETER;
1402 sound_data_s* temp_sound_data = NULL;
1403 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1404 if (NULL == temp_sound_data) {
1405 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1406 return TTSD_ERROR_OUT_OF_MEMORY;
1409 temp_sound_data->data = NULL;
1410 temp_sound_data->rate = 0;
1411 temp_sound_data->data_size = 0;
1413 if (0 < data_size) {
1414 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1415 if (NULL != temp_sound_data->data) {
1416 memcpy(temp_sound_data->data, data, data_size);
1417 temp_sound_data->data_size = data_size;
1418 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%u), event(%d) sound_data(%p) data(%p) size(%d)",
1419 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1421 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1424 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1427 temp_sound_data->utt_id = uttid;
1428 temp_sound_data->event = event;
1429 temp_sound_data->audio_type = audio_type;
1430 temp_sound_data->rate = rate;
1432 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1433 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1435 if (NULL != temp_sound_data->data) {
1436 free(temp_sound_data->data);
1437 temp_sound_data->data = NULL;
1440 free(temp_sound_data);
1441 temp_sound_data = NULL;
1444 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1447 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1449 return TTSD_ERROR_NONE;