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);
109 char appid[1024] = {0, };
110 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
111 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
114 if (NULL == speak_data->lang || NULL == speak_data->text) {
115 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
116 ttsd_server_stop(uid);
118 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
120 ttsd_data_clear_speak_data(&speak_data);
126 g_utt.uttid = speak_data->utt_id;
128 char* credential = ttsd_data_get_credential(uid);
130 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
131 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
132 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
133 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
134 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
135 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
138 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
139 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
141 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
143 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
145 ttsd_server_stop(uid);
147 int pid = ttsd_data_get_pid(uid);
149 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
151 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
154 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, NULL);
159 ttsd_data_clear_speak_data(&speak_data);
161 ttsd_data_clear_speak_data(&speak_data);
164 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
170 * TTS Server Callback Functions
172 int ttsd_send_error(ttse_error_e error, const char* msg)
174 unsigned int uid = g_utt.uid;
175 int uttid = g_utt.uttid;
176 int tmp_pid = ttsd_data_get_pid(uid);
178 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));
180 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
182 if (0 != ttsd_player_clear(uid))
183 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
185 if (0 != ttsd_data_clear_data(uid))
186 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
189 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
190 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
193 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
194 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
197 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
198 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
203 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)
205 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
207 unsigned int uid = g_utt.uid;
208 int uttid = g_utt.uttid;
210 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
211 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
212 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
213 SLOG(LOG_DEBUG, tts_tag(), "@@@");
214 return TTSD_ERROR_INVALID_PARAMETER;
217 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
218 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
219 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
220 return TTSD_ERROR_INVALID_PARAMETER;
223 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
224 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
225 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
226 SLOG(LOG_DEBUG, tts_tag(), "@@@");
227 return TTSD_ERROR_INVALID_PARAMETER;
230 /* check the current state */
231 app_tts_state_e state;
232 if (0 > ttsd_data_get_client_state(uid, &state)) {
233 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
234 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
235 return TTSD_ERROR_INVALID_PARAMETER;
238 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
239 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
240 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
241 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
242 return TTSD_ERROR_NONE;
245 /* Synthesis is success */
246 if (TTSE_RESULT_EVENT_START == event) {
247 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
248 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
249 uid, uttid, data, data_size, audio_type, rate);
251 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
252 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
253 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
254 double d_latency = (double)latency;
255 g_avg_latency += d_latency;
256 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
257 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
259 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);
260 } else if (TTSE_RESULT_EVENT_FINISH == event) {
261 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
262 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
263 uid, uttid, data, data_size, audio_type, rate);
265 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
269 sound_data_s* temp_sound_data = NULL;
270 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
271 if (NULL == temp_sound_data) {
272 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
273 return TTSD_ERROR_OUT_OF_MEMORY;
276 temp_sound_data->data = NULL;
277 temp_sound_data->rate = 0;
278 temp_sound_data->data_size = 0;
280 if (NULL != data && 0 < data_size) {
281 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
282 if (NULL != temp_sound_data->data) {
283 memcpy(temp_sound_data->data, data, data_size);
284 temp_sound_data->data_size = data_size;
285 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%u), event(%d) sound_data(%p) data(%p) size(%d)",
286 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
288 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
291 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
294 temp_sound_data->utt_id = uttid;
295 temp_sound_data->event = event;
296 temp_sound_data->audio_type = audio_type;
297 temp_sound_data->rate = rate;
299 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
300 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
301 if (NULL != temp_sound_data->data) {
302 free(temp_sound_data->data);
303 temp_sound_data->data = NULL;
306 free(temp_sound_data);
307 temp_sound_data = NULL;
309 return TTSD_ERROR_OPERATION_FAILED;
312 if (event == TTSE_RESULT_EVENT_FINISH) {
313 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
316 return TTSD_ERROR_NONE;
319 bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
321 /* clear client data */
322 ttsd_data_clear_data(uid);
324 if (APP_STATE_READY != state) {
325 ttsd_data_set_client_state(uid, APP_STATE_READY);
328 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
330 ttsd_data_delete_client(uid);
337 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
340 case TTS_CONFIG_TYPE_ENGINE:
342 /* TODO - Determine the policy when engine process get engine changed cb */
343 if (NULL == str_param) {
344 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
349 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
350 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
351 ret = ttsd_engine_agent_unload_current_engine();
353 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
356 ret = ttsd_engine_agent_load_current_engine();
358 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
363 /* stop all player */
364 ttsd_player_all_stop();
366 /* send interrupt message to all clients */
367 ttsd_data_foreach_clients(__get_client_cb, NULL);
369 ttsd_engine_cancel_synthesis();
374 case TTS_CONFIG_TYPE_VOICE:
376 if (NULL == str_param) {
377 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
381 char* out_lang = NULL;
385 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
386 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
387 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
389 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
391 /* Current language is not available */
392 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
394 if (NULL != out_lang) {
401 case TTS_CONFIG_TYPE_SPEED:
403 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
404 /* set default speed */
406 ret = ttsd_engine_agent_set_default_speed(int_param);
408 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
414 case TTS_CONFIG_TYPE_PITCH:
416 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
417 /* set default speed */
419 ret = ttsd_engine_agent_set_default_pitch(int_param);
421 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
427 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
429 if (0.0 <= double_param && double_param <= 1.0) {
430 /* set default bg volume ratio */
432 ret = ttsd_player_set_background_volume_ratio(double_param);
434 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
444 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
446 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
447 ttsd_server_finalize(uid);
451 static void __terminate_server()
453 if (g_is_terminated) {
454 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
458 g_is_terminated = true;
460 ttsd_ipc_close_connection();
461 ttsd_network_finalize();
465 static Eina_Bool __quit_ecore_loop(void *data)
467 __terminate_server();
468 ecore_main_loop_quit();
473 Eina_Bool ttsd_terminate_daemon(void *data)
475 ttsd_data_foreach_clients(__terminate_client, NULL);
477 if (g_quit_loop_timer) {
478 ecore_timer_del(g_quit_loop_timer);
479 g_quit_loop_timer = NULL;
480 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
483 __terminate_server();
485 g_terminate_timer = NULL;
489 void __screen_reader_changed_cb(bool value)
491 /* if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
492 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
493 if (g_terminate_timer) {
494 ecore_timer_del(g_terminate_timer);
495 g_terminate_timer = NULL;
497 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
500 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
505 int ttsd_send_all_stop()
507 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
509 /* stop all player */
510 ttsd_player_all_stop();
512 /* send interrupt message to all clients */
513 ttsd_data_foreach_clients(__get_client_cb, NULL);
515 return TTSD_ERROR_NONE;
521 int ttsd_initialize(ttse_request_callback_s *callback)
523 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
524 g_is_terminated = false;
526 if (ttsd_config_initialize(__config_changed_cb)) {
527 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
531 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
533 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
534 int ret = ttsd_player_set_background_volume_ratio(ratio);
536 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
539 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
542 if (ttsd_player_init()) {
543 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
544 return TTSD_ERROR_OPERATION_FAILED;
547 /* Engine Agent initialize */
548 if (0 != ttsd_engine_agent_init()) {
549 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
550 return TTSD_ERROR_OPERATION_FAILED;
553 /* set current engine */
554 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
555 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
556 // g_is_engine = false;
558 // g_is_engine = true;
560 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
561 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
562 return TTSD_ERROR_OPERATION_FAILED;
565 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
567 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
568 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
571 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
572 if (NULL == g_check_client_timer) {
573 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
576 return TTSD_ERROR_NONE;
581 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
584 if (0 < g_list_length(g_proc_list)) {
585 iter = g_list_first(g_proc_list);
586 while (NULL != iter) {
587 g_proc_list = g_list_remove_link(g_proc_list, iter);
589 iter = g_list_first(g_proc_list);
593 ttsd_config_finalize();
595 ttsd_player_release();
597 ttsd_engine_agent_release();
601 ecore_timer_del(g_wait_timer);
603 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
606 if (g_terminate_timer) {
607 ecore_timer_del(g_terminate_timer);
608 g_terminate_timer = NULL;
609 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
612 if (g_check_client_timer) {
613 ecore_timer_del(g_check_client_timer);
614 g_check_client_timer = NULL;
615 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
618 if (g_quit_loop_timer) {
619 ecore_timer_del(g_quit_loop_timer);
620 g_quit_loop_timer = NULL;
621 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
624 return TTSD_ERROR_NONE;
629 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
631 ttsd_terminate_daemon(NULL);
633 return TTSD_ERROR_NONE;
637 * TTS Server Functions for Client
640 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
642 if (-1 != ttsd_data_is_client(uid))
643 *is_initialized = true;
645 *is_initialized = false;
647 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
648 return TTSD_ERROR_NONE;
651 int ttsd_server_initialize(int pid, unsigned int uid, tts_ipc_method_e method, bool* credential_needed)
653 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
655 if (-1 != ttsd_data_is_client(uid)) {
656 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
657 return TTSD_ERROR_NONE;
660 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
661 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
662 return TTSD_ERROR_OPERATION_FAILED;
665 if (true == *credential_needed) {
667 if (0 != app_manager_get_app_id(pid, &appid)) {
668 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
670 bool is_agreed = false;
671 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
672 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
675 return TTSD_ERROR_OPERATION_FAILED;
680 if (false == is_agreed) {
681 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
682 return TTSD_ERROR_PERMISSION_DENIED;
686 if (0 != ttsd_data_new_client(pid, uid)) {
687 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
688 return TTSD_ERROR_OPERATION_FAILED;
691 if (0 != ttsd_data_set_ipc_method(uid, method)) {
692 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
693 return TTSD_ERROR_OPERATION_FAILED;
696 if (0 != ttsd_player_create_instance(uid)) {
697 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
698 return TTSD_ERROR_OPERATION_FAILED;
702 g_min_latency = 10000.0;
707 return TTSD_ERROR_NONE;
710 static void __read_proc()
713 struct dirent *dirp = NULL;
717 if (0 < g_list_length(g_proc_list)) {
718 iter = g_list_first(g_proc_list);
719 while (NULL != iter) {
720 g_proc_list = g_list_remove_link(g_proc_list, iter);
722 iter = g_list_first(g_proc_list);
726 dp = opendir("/proc");
728 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
734 tmp = atoi(dirp->d_name);
735 if (0 >= tmp) continue;
736 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
738 } while (NULL != dirp);
744 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
750 for (i = 0; i < g_list_length(g_proc_list); i++) {
751 iter = g_list_nth(g_proc_list, i);
753 if (pid == GPOINTER_TO_INT(iter->data)) {
754 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
761 if (false == exist) {
762 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
763 ttsd_server_finalize(uid);
768 char appid[1024] = {0, };
769 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
770 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
773 if (0 < strlen(appid)) {
774 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
776 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
779 result = ttsdc_send_hello(pid, uid);
782 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) should be removed.", uid);
783 ttsd_server_finalize(uid);
784 } else if (-1 == result) {
785 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
793 Eina_Bool ttsd_cleanup_client(void *data)
795 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
798 if (0 < ttsd_data_get_client_count()) {
799 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
801 if (NULL == g_quit_loop_timer) {
802 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
803 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
805 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
806 g_check_client_timer = NULL;
810 SLOG(LOG_DEBUG, tts_tag(), "@@@");
815 void __used_voice_cb(const char* lang, int type)
817 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
818 if (0 != ttsd_engine_unload_voice(lang, type)) {
819 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
823 int ttsd_server_finalize(unsigned int uid)
825 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
826 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);
828 app_tts_state_e state;
829 if (0 > ttsd_data_get_client_state(uid, &state)) {
830 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
833 ttsd_server_stop(uid);
834 ttsd_player_clear(uid);
836 ttsd_player_destroy_instance(uid);
838 /* Need to unload voice when used voice is unregistered */
839 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
840 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
841 return TTSD_ERROR_OPERATION_FAILED;
844 ttsd_data_delete_client(uid);
846 /* unload engine, if ref count of client is 0 */
847 if (0 == ttsd_data_get_client_count()) {
848 if (NULL == g_quit_loop_timer) {
849 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
850 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
852 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
856 return TTSD_ERROR_NONE;
859 int ttsd_server_set_mode(unsigned int uid, ttsd_mode_e mode)
861 app_tts_state_e state;
862 if (0 > ttsd_data_get_client_state(uid, &state)) {
863 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
864 return TTSD_ERROR_INVALID_PARAMETER;
867 if (APP_STATE_READY != state) {
868 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
869 return TTSD_ERROR_INVALID_STATE;
872 int ret = ttsd_data_set_mode(uid, mode);
874 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
876 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%u), mode(%d)", uid, mode);
882 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)
884 app_tts_state_e state;
885 if (0 > ttsd_data_get_client_state(uid, &state)) {
886 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
887 return TTSD_ERROR_INVALID_PARAMETER;
890 /* check valid voice */
891 char* temp_lang = NULL;
893 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
894 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
895 if (NULL != temp_lang) {
899 return TTSD_ERROR_INVALID_VOICE;
902 if (NULL == temp_lang) {
903 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
904 return TTSD_ERROR_INVALID_VOICE;
907 speak_data_s* speak_data = NULL;
908 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
909 if (NULL == speak_data) {
910 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
911 if (NULL != temp_lang) {
915 return TTSD_ERROR_OPERATION_FAILED;
918 speak_data->lang = strdup(lang);
919 speak_data->vctype = voice_type;
921 speak_data->speed = speed;
922 speak_data->utt_id = utt_id;
924 speak_data->text = strdup(text);
926 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);
928 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
930 ret = ttsd_data_add_speak_data(uid, speak_data);
932 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
933 if (NULL != temp_lang) {
937 if (NULL != speak_data) {
938 if (NULL != speak_data->lang) free(speak_data->lang);
939 if (NULL != speak_data->text) free(speak_data->text);
941 speak_data->lang = NULL;
942 speak_data->text = NULL;
951 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
952 /* Request load voice */
953 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
954 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
955 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
959 if (NULL != temp_lang) {
964 if (APP_STATE_PLAYING == state) {
965 /* check if engine use network */
966 if (ttsd_engine_agent_need_network()) {
967 if (false == ttsd_network_is_connected()) {
968 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
969 return TTSD_ERROR_OPERATION_FAILED;
973 ttsd_data_set_credential(uid, credential);
975 /* Check whether tts-engine is running or not */
976 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
977 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
978 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
979 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
985 return TTSD_ERROR_NONE;
988 static int __pause_client(unsigned int uid)
990 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
991 if (TTSD_ERROR_NONE != ret) {
992 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
993 return TTSD_ERROR_OPERATION_FAILED;
996 ret = ttsd_player_pause(uid);
997 if (TTSD_ERROR_NONE != ret) {
998 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
999 return TTSD_ERROR_OPERATION_FAILED;
1002 return TTSD_ERROR_NONE;
1005 void __send_interrupt_client(unsigned int uid)
1007 SLOG(LOG_INFO, tts_tag(), "[Server] Send new state by policy. uid(%u)", uid);
1008 int pid = ttsd_data_get_pid(uid);
1010 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
1014 app_tts_state_e state = APP_STATE_CREATED;
1016 if (0 != ttsd_data_get_client_state(uid, &state)) {
1017 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state. uid(%u), pid(%d)", uid, pid);
1021 ttsdc_ipc_send_set_state_message(pid, uid, state);
1022 SLOG(LOG_INFO, tts_tag(), "[Server] Finish to send new state by policy. uid(%u)", uid);
1026 int ttsd_server_play(unsigned int uid, const char* credential)
1028 app_tts_state_e state;
1029 if (0 > ttsd_data_get_client_state(uid, &state)) {
1030 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1031 return TTSD_ERROR_INVALID_PARAMETER;
1034 if (APP_STATE_PLAYING == state) {
1035 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1036 return TTSD_ERROR_NONE;
1039 /* check if engine use network */
1040 if (ttsd_engine_agent_need_network()) {
1041 if (false == ttsd_network_is_connected()) {
1042 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
1043 return TTSD_ERROR_OUT_OF_NETWORK;
1047 /* check the current playback focus */
1048 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
1049 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
1050 ttsd_data_clear_data(uid);
1051 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1054 unsigned int current_uid = ttsd_data_get_current_playing();
1055 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1057 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1058 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1059 /* Send interrupt message */
1060 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1063 if (0 != ttsd_server_stop(current_uid)) {
1064 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1067 /* Default mode policy of interrupt is "Pause" */
1069 /* Send interrupt message */
1070 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1073 if (0 != __pause_client(current_uid)) {
1074 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1078 __send_interrupt_client(current_uid);
1081 /* Change current play */
1082 if (0 != ttsd_player_wait_to_play(uid)) {
1083 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1086 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1087 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1088 return TTSD_ERROR_OPERATION_FAILED;
1091 int ret = TTSD_ERROR_NONE;
1092 if (APP_STATE_PAUSED == state) {
1093 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1094 g_is_paused = false;
1095 ret = ttsd_player_resume(uid);
1097 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1098 ret = ttsd_player_play(uid);
1101 if (TTSD_ERROR_NONE != ret) {
1102 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1103 ttsd_data_set_client_state(uid, state);
1104 return TTSD_ERROR_OPERATION_FAILED;
1107 ttsd_data_set_credential(uid, credential);
1109 /* Check whether tts-engine is running or not */
1110 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1111 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1112 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1113 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1114 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1119 return TTSD_ERROR_NONE;
1122 int ttsd_server_stop(unsigned int uid)
1124 app_tts_state_e state;
1125 if (0 > ttsd_data_get_client_state(uid, &state)) {
1126 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1127 return TTSD_ERROR_INVALID_PARAMETER;
1130 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1132 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1133 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1134 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1135 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1136 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1138 int ret = ttsd_engine_cancel_synthesis();
1139 if (TTSD_ERROR_NONE != ret)
1140 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1142 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1145 ttsd_data_set_client_state(uid, APP_STATE_READY);
1146 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1147 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1151 /* Reset all data */
1152 ttsd_data_clear_data(uid);
1153 g_is_paused = false;
1155 return TTSD_ERROR_NONE;
1158 int ttsd_server_pause(unsigned int uid, int* utt_id)
1160 app_tts_state_e state;
1161 if (0 > ttsd_data_get_client_state(uid, &state)) {
1162 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1163 return TTSD_ERROR_INVALID_PARAMETER;
1166 if (APP_STATE_PLAYING != state) {
1167 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1168 return TTSD_ERROR_INVALID_STATE;
1173 *utt_id = g_utt.uttid;
1174 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%u), g_uid(%u), utt_id(%d)", uid, g_utt.uid, *utt_id);
1176 int ret = __pause_client(uid);
1177 if (TTSD_ERROR_NONE != ret) {
1178 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to pause player : ret(%d)", ret);
1179 return TTSD_ERROR_OPERATION_FAILED;
1182 return TTSD_ERROR_NONE;
1185 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1187 app_tts_state_e state;
1188 if (0 > ttsd_data_get_client_state(uid, &state)) {
1189 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1190 return TTSD_ERROR_INVALID_PARAMETER;
1194 int ret = ttsd_engine_get_voice_list(voice_list);
1196 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1200 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1202 return TTSD_ERROR_NONE;
1205 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1207 app_tts_state_e state;
1208 if (0 > ttsd_data_get_client_state(uid, &state)) {
1209 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1210 return TTSD_ERROR_INVALID_PARAMETER;
1213 /* get current voice */
1214 int ret = ttsd_engine_get_default_voice(language, voice_type);
1216 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1220 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1222 return TTSD_ERROR_NONE;
1225 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1227 app_tts_state_e state;
1228 if (0 > ttsd_data_get_client_state(uid, &state)) {
1229 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1230 return TTSD_ERROR_INVALID_PARAMETER;
1233 if (APP_STATE_READY != state) {
1234 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1235 return TTSD_ERROR_INVALID_STATE;
1238 int ret = ttsd_engine_set_private_data(key, data);
1240 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1242 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1248 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1250 app_tts_state_e state;
1251 if (0 > ttsd_data_get_client_state(uid, &state)) {
1252 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1253 return TTSD_ERROR_INVALID_PARAMETER;
1256 if (APP_STATE_READY != state) {
1257 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1258 return TTSD_ERROR_INVALID_STATE;
1261 int ret = ttsd_engine_get_private_data(key, data);
1263 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1265 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1271 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1273 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1276 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1278 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1284 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1286 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1289 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1291 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1297 int ttsd_server_play_pcm(unsigned int uid)
1299 app_tts_state_e state;
1300 if (0 > ttsd_data_get_client_state(uid, &state)) {
1301 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1302 return TTSD_ERROR_INVALID_PARAMETER;
1305 if (APP_STATE_PLAYING == state) {
1306 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1307 return TTSD_ERROR_NONE;
1310 unsigned int current_uid = ttsd_data_get_current_playing();
1311 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1313 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1314 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1315 /* Send interrupt message */
1316 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1319 if (0 != ttsd_server_stop(current_uid)) {
1320 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1323 /* Default mode policy of interrupt is "Pause" */
1325 /* Send interrupt message */
1326 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1329 if (0 != __pause_client(current_uid)) {
1330 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1334 __send_interrupt_client(current_uid);
1337 /* Change current play */
1338 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1339 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1340 return TTSD_ERROR_OPERATION_FAILED;
1343 int ret = TTSD_ERROR_NONE;
1344 if (APP_STATE_PAUSED == state) {
1345 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1346 g_is_paused = false;
1347 ret = ttsd_player_resume(uid);
1349 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1350 ret = ttsd_player_play(uid);
1353 if (TTSD_ERROR_NONE != ret) {
1354 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1355 ttsd_data_set_client_state(uid, state);
1356 return TTSD_ERROR_OPERATION_FAILED;
1359 return TTSD_ERROR_NONE;
1362 int ttsd_server_stop_pcm(unsigned int uid)
1364 app_tts_state_e state;
1365 if (0 > ttsd_data_get_client_state(uid, &state)) {
1366 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1367 return TTSD_ERROR_INVALID_PARAMETER;
1370 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1372 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1373 ttsd_data_set_client_state(uid, APP_STATE_READY);
1376 /* Reset all data */
1377 ttsd_data_clear_data(uid);
1379 ttsd_player_stop(uid);
1382 return TTSD_ERROR_NONE;
1385 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1387 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1391 /* Synthesis is success */
1392 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1393 if (TTSE_RESULT_EVENT_START == event) {
1394 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1395 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1396 uid, uttid, data, data_size, audio_type, rate);
1397 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1398 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1399 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1400 uid, uttid, data, data_size, audio_type, rate);
1402 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1405 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1406 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1407 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1408 return TTSD_ERROR_INVALID_PARAMETER;
1412 sound_data_s* temp_sound_data = NULL;
1413 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1414 if (NULL == temp_sound_data) {
1415 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1416 return TTSD_ERROR_OUT_OF_MEMORY;
1419 temp_sound_data->data = NULL;
1420 temp_sound_data->rate = 0;
1421 temp_sound_data->data_size = 0;
1423 if (0 < data_size) {
1424 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1425 if (NULL != temp_sound_data->data) {
1426 memcpy(temp_sound_data->data, data, data_size);
1427 temp_sound_data->data_size = data_size;
1428 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%u), event(%d) sound_data(%p) data(%p) size(%d)",
1429 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1431 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1434 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1437 temp_sound_data->utt_id = uttid;
1438 temp_sound_data->event = event;
1439 temp_sound_data->audio_type = audio_type;
1440 temp_sound_data->rate = rate;
1442 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1443 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1445 if (NULL != temp_sound_data->data) {
1446 free(temp_sound_data->data);
1447 temp_sound_data->data = NULL;
1450 free(temp_sound_data);
1451 temp_sound_data = NULL;
1454 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1457 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1459 return TTSD_ERROR_NONE;