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
30 #define CLIENT_PRESENCE_CHECK_TIME 30
32 /* for checking time */
33 #define TIME_DIFF(start, end) \
34 ((uint64_t)((end).tv_sec - (start).tv_sec) * 1000000 + (((end).tv_nsec - (start).tv_nsec) / 1000)) / 1000
36 static struct timespec g_request_playing, g_start_playing, g_finish_playing;
37 static double g_avg_latency;
38 static double g_min_latency;
39 static double g_max_latency;
40 static int g_file_num;
41 static int g_total_data_size;
51 } state_changed_args_t;
53 /* If current engine exist */
54 //static bool g_is_engine;
56 static Ecore_Timer* g_check_client_timer = NULL;
57 static Ecore_Timer* g_wait_timer = NULL;
58 static Ecore_Timer* g_quit_loop_timer = NULL;
59 static Ecore_Timer* g_notify_state_timer = NULL;
61 static utterance_t g_utt;
63 static GList *g_proc_list = NULL;
65 static bool g_is_terminated = false;
67 static int g_activated_mode = 0;
69 /* Function definitions */
70 static int __stop_and_send_ready_state(unsigned int uid)
72 int ret = ttsd_server_stop(uid);
73 if (TTSD_ERROR_NONE != ret) {
74 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_stop() : uid(%u). ret(%d)", uid, ret);
78 int pid = ttsd_data_get_pid(uid);
80 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
81 return TTSD_ERROR_OPERATION_FAILED;
83 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
89 static void __synthesis(unsigned int uid);
90 static bool __generate_silent(long duration_in_msec);
92 static Eina_Bool __wait_synthesis(void *data)
94 /* get current play */
95 unsigned int uid = ttsd_data_get_current_playing();
98 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
99 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
102 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
104 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
105 /* Start next synthesis */
116 static void write_debugger_information(speak_data_s *speak_data, const char *appid, const int ret)
118 if(aul_app_is_running("org.tizen.TTSDebugger") != 1) {
122 SLOG(LOG_ERROR, tts_tag(), "TTS Debugger is Running");
124 char* temp_lang = NULL;
126 if (true != ttsd_engine_select_valid_voice(speak_data->voice.language, speak_data->voice.type, &temp_lang,
128 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select default voice");
130 char value_buffer[2048] = {0, };
131 snprintf(value_buffer, 2048, "client=%s|type=%d|speed=%d|language=%s|text=%s|error=%d", appid, temp_type,
132 speak_data->synth_parameter.speed, temp_lang, speak_data->text, ret);
134 SLOG(LOG_INFO, tts_tag(), "keyVal : %s", value_buffer);
136 if (0 != vconf_set_str("db/ttsdebugger/tts_info", value_buffer)) {
137 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set vconf key db/ttsdebugger/tts_info");
145 static void __synthesis(unsigned int uid)
147 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
149 speak_data_s* speak_data = NULL;
150 int ret = ttsd_data_get_speak_data(uid, &speak_data);
151 if (TTSD_ERROR_NONE != ret || NULL == speak_data) {
152 SLOG(LOG_DEBUG, tts_tag(), "@@@ No speak data. ret(%d)", ret);
153 SLOG(LOG_INFO, tts_tag(), "@@@ SYNTHESIS END");
157 int pid = ttsd_data_get_pid(uid);
159 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
160 ttsd_data_destroy_speak_data(speak_data);
165 char appid[512] = {0, };
166 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
167 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
171 g_utt.uttid = speak_data->utt_id;
172 char* credential = ttsd_data_get_credential(uid);
174 if (speak_data->is_silent) {
175 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
176 if (__generate_silent(speak_data->duration_in_msec)) {
177 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
179 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to make GENERATE silent !!!! * ");
181 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
182 __stop_and_send_ready_state(uid);
186 if (NULL == speak_data->voice.language || NULL == speak_data->text) {
187 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
188 __stop_and_send_ready_state(uid);
189 ttsd_data_destroy_speak_data(speak_data);
194 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
195 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
196 SLOG(LOG_INFO, tts_tag(), "Voice : language(%s), type(%d), speed(%d)", speak_data->voice.language,
197 speak_data->voice.type, speak_data->synth_parameter.speed);
198 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
199 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
200 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
202 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
204 ret = ttsd_engine_agent_set_default_pitch(speak_data->synth_parameter.pitch);
205 if (TTSD_ERROR_NONE != ret) {
206 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to set pitch !!!! * ");
209 ret = ttsd_engine_agent_set_personal_tts_id(speak_data->voice.ptts_id);
210 if (TTSD_ERROR_NONE != ret) {
211 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to set personal tts id !!!! * ");
214 ret = ttsd_engine_start_synthesis(speak_data->voice.language, speak_data->voice.type, speak_data->text,
215 speak_data->synth_parameter.speed, appid, credential, NULL);
216 if (TTSD_ERROR_NONE != ret) {
217 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
219 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
220 __stop_and_send_ready_state(uid);
222 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
225 if (ttsd_data_get_synth_control() == TTSD_SYNTHESIS_CONTROL_DOING && ttsd_state_get_state() == TTSD_STATE_READY) {
226 int ret = vconf_set_bool(TTS_PLAYING_STATUS_KEY, 1);
227 SLOG(LOG_INFO, tts_tag(), "[Server] Synthesis starting. Set playing status (True). ret(%d)", ret);
229 ttsd_state_set_state(TTSD_STATE_SYNTHESIZING);
232 write_debugger_information(speak_data, appid, ret);
238 ttsd_data_destroy_speak_data(speak_data);
240 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
245 static bool __generate_silent(long duration_in_msec)
247 SLOG(LOG_DEBUG, tts_tag(), "@@@ GENERATE SILENT START");
249 int audio_type = TTSE_AUDIO_TYPE_RAW_S16;
250 int sample_bytes = 2;
251 int sample_rate = 16000;
253 long bytes = duration_in_msec * sample_bytes * sample_rate * 0.001;
254 SLOG(LOG_INFO, TAG_TTSC, "[INFO] data size for duration_in_msec in silence utterance is [%ld]", bytes);
255 char* data = (char*)calloc(sizeof(char), bytes);
258 sound_data_s* sound_data_start = ttsd_data_create_sound_data(g_utt.uttid, data, bytes, TTSE_RESULT_EVENT_START, audio_type, sample_rate, 0);
259 if (NULL == sound_data_start) {
260 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
264 if (0 != ttsd_data_add_sound_data(g_utt.uid, sound_data_start)) {
265 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", g_utt.uid);
266 ttsd_data_destroy_sound_data(sound_data_start);
270 /* add finish wav data */
271 sound_data_s* sound_data_finish = ttsd_data_create_sound_data(g_utt.uttid, NULL, 0, TTSE_RESULT_EVENT_FINISH, audio_type, sample_rate, 0);
272 if (NULL == sound_data_finish) {
273 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
277 if (0 != ttsd_data_add_sound_data(g_utt.uid, sound_data_finish)) {
278 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", g_utt.uid);
279 ttsd_data_destroy_sound_data(sound_data_finish);
283 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
285 SLOG(LOG_DEBUG, tts_tag(), "@@@ GENERATE SILENT END");
291 * TTS Server Callback Functions
293 int ttsd_send_error(ttse_error_e error, const char* msg)
295 unsigned int uid = g_utt.uid;
296 int uttid = g_utt.uttid;
297 int tmp_pid = ttsd_data_get_pid(uid);
299 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));
301 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
303 if (0 != ttsd_player_stop(uid)) {
304 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to stop for uid(%u)", uid);
307 if (0 != ttsd_data_clear_data(uid))
308 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
311 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
312 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
315 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
316 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
319 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
320 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
325 static int ttsd_convert_audio_type_to_bytes(ttse_audio_type_e audio_type)
328 switch (audio_type) {
329 case TTSE_AUDIO_TYPE_RAW_S16:
332 case TTSE_AUDIO_TYPE_RAW_U8:
336 ret = 2; // Default 16bit
342 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)
344 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
346 unsigned int uid = g_utt.uid;
347 int uttid = g_utt.uttid;
349 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
350 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
351 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
352 SLOG(LOG_DEBUG, tts_tag(), "@@@");
353 return TTSD_ERROR_INVALID_PARAMETER;
356 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
357 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
358 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
359 return TTSD_ERROR_INVALID_PARAMETER;
362 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
363 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
364 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
365 SLOG(LOG_DEBUG, tts_tag(), "@@@");
366 return TTSD_ERROR_INVALID_PARAMETER;
369 /* check the current state */
370 app_tts_state_e state = ttsd_data_get_client_state(uid);
371 if (APP_STATE_NONE == state) {
372 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
373 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
374 return TTSD_ERROR_INVALID_PARAMETER;
377 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
378 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
379 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
380 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
381 return TTSD_ERROR_NONE;
384 /* Synthesis is success */
385 if (TTSE_RESULT_EVENT_START == event) {
386 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
387 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
388 uid, uttid, data, data_size, audio_type, rate);
390 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
391 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
392 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
393 double d_latency = (double)latency;
394 g_avg_latency += d_latency;
395 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
396 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
398 g_total_data_size = 0;
399 g_total_data_size += data_size;
400 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);
401 } else if (TTSE_RESULT_EVENT_FINISH == event) {
402 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
403 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
404 uid, uttid, data, data_size, audio_type, rate);
406 g_total_data_size += data_size;
407 int audio_type_bytes = ttsd_convert_audio_type_to_bytes(audio_type);
409 clock_gettime(CLOCK_MONOTONIC_RAW, &g_finish_playing);
410 long long int time_processing = (uint64_t)TIME_DIFF(g_request_playing, g_finish_playing);
411 long long int time_speech = (long long int)(1000 * g_total_data_size * sizeof(char) / (rate * audio_type_bytes));
412 double xRT = (double)(time_processing) / (double)time_speech;
413 SLOG(LOG_INFO, tts_tag(), "[SERVER] time_processing : %lld, time_speech : %lld, total data size: %d", time_processing, time_speech, g_total_data_size);
414 SLOG(LOG_INFO, tts_tag(), "[SERVER] xRT : %lf", xRT);
416 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
417 g_total_data_size += data_size;
420 ttsd_playing_mode_e playing_mode = ttsd_data_get_playing_mode(uid);
421 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] uid(%d), playing mode(%d)", uid, playing_mode);
422 if (TTSD_PLAYING_MODE_BY_SERVICE == playing_mode) {
424 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
425 if (NULL == sound_data) {
426 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
427 return TTSD_ERROR_OUT_OF_MEMORY;
430 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
431 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
432 ttsd_data_destroy_sound_data(sound_data);
434 return TTSD_ERROR_OPERATION_FAILED;
436 } else { /* TTSD_PLAYING_MODE_BY_CLIENT */
437 /* send pcm data to client */
438 int tmp_pid = ttsd_data_get_pid(uid);
439 if (0 != ttsdc_ipc_send_pcm(tmp_pid, uid, uttid, event, data, data_size, audio_type, rate)) {
440 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to send pcm data to client");
441 return TTSD_ERROR_OPERATION_FAILED;
445 if (event == TTSE_RESULT_EVENT_FINISH) {
446 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
449 return TTSD_ERROR_NONE;
452 static bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
454 if (0 > ttsd_data_is_client(uid)) {
455 SLOG(LOG_ERROR, tts_tag(), "[Server] Client(%d) is not valid.", uid);
456 ttsd_data_delete_client(uid);
460 /* clear client data */
461 ttsd_data_clear_data(uid);
463 if (APP_STATE_READY != state) {
464 ttsd_data_set_client_state(uid, APP_STATE_READY);
465 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
471 static void __stop_all_client()
473 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
475 ttsd_engine_cancel_synthesis();
476 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
478 /* stop all player */
479 ttsd_player_all_stop();
481 /* send interrupt message to all clients */
482 ttsd_data_foreach_clients(__get_client_cb, NULL);
485 static void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
488 case TTS_CONFIG_TYPE_ENGINE:
490 /* TODO - Determine the policy when engine process get engine changed cb */
491 if (NULL == str_param) {
492 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
497 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
498 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
499 ret = ttsd_engine_agent_unload_current_engine();
501 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
504 ret = ttsd_engine_agent_load_current_engine();
506 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
515 case TTS_CONFIG_TYPE_VOICE:
517 if (NULL == str_param) {
518 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
522 char* out_lang = NULL;
526 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
527 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
528 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
530 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
532 /* Current language is not available */
533 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
535 if (NULL != out_lang) {
542 case TTS_CONFIG_TYPE_SPEED:
544 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
545 /* set default speed */
547 ret = ttsd_engine_agent_set_default_speed(int_param);
549 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
555 case TTS_CONFIG_TYPE_PITCH:
557 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
558 /* set default speed */
560 ret = ttsd_engine_agent_set_default_pitch(int_param);
562 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
568 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
570 if (0.0 <= double_param && double_param <= 1.0) {
571 /* set default bg volume ratio */
573 ret = ttsd_player_set_background_volume_ratio(double_param);
575 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
585 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
587 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
588 ttsd_server_finalize(uid);
592 static int __terminate_server()
594 if (g_is_terminated) {
595 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
596 return TTSD_ERROR_NONE;
599 int ret = TTSD_ERROR_NONE;
600 ret = ttsd_ipc_close_connection();
601 if (TTSD_ERROR_NONE != ret) {
602 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_ipc_close_connection(). ret(%d)", ret);
603 return TTSD_ERROR_OPERATION_FAILED;
606 ret = ttsd_finalize();
607 if (TTSD_ERROR_NONE != ret) {
608 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_finalize(). ret(%d)", ret);
609 return TTSD_ERROR_OPERATION_FAILED;
612 g_is_terminated = true;
613 return TTSD_ERROR_NONE;
616 static Eina_Bool __quit_ecore_loop(void *data)
618 __terminate_server();
619 ecore_main_loop_quit();
624 void __screen_reader_changed_cb(bool value)
626 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
630 static void __read_proc()
633 struct dirent *dirp = NULL;
637 if (0 < g_list_length(g_proc_list)) {
638 iter = g_list_first(g_proc_list);
639 while (NULL != iter) {
640 g_proc_list = g_list_remove_link(g_proc_list, iter);
642 iter = g_list_first(g_proc_list);
646 dp = opendir("/proc");
648 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
654 tmp = atoi(dirp->d_name);
655 if (0 >= tmp) continue;
656 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
658 } while (NULL != dirp);
664 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
670 for (i = 0; i < g_list_length(g_proc_list); i++) {
671 iter = g_list_nth(g_proc_list, i);
673 if (pid == GPOINTER_TO_INT(iter->data)) {
674 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
681 if (false == exist) {
682 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
683 ttsd_server_finalize(uid);
689 static Eina_Bool __cleanup_client(void *data)
691 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
694 if (0 < ttsd_data_get_client_count()) {
695 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
697 if (NULL == g_quit_loop_timer) {
698 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
699 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
701 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
702 g_check_client_timer = NULL;
706 ecore_timer_interval_set(g_check_client_timer, CLIENT_CLEAN_UP_TIME);
707 SLOG(LOG_DEBUG, tts_tag(), "@@@");
712 static bool __notify_state_changed_event(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
714 state_changed_args_t* args = (state_changed_args_t*)user_data;
719 if (ttsd_data_is_service_state_changed_cb_set(uid)) {
720 ttsdc_ipc_send_set_service_state_message(pid, uid, args->before, args->current);
726 static Eina_Bool __state_changed_timer_cb(void *data)
729 SLOG(LOG_ERROR, tts_tag(), "[Server] Invalid data passed.");
730 return ECORE_CALLBACK_CANCEL;
733 int ret = ttsd_data_foreach_clients(__notify_state_changed_event, data);
734 if (TTSD_ERROR_NONE != ret) {
735 SLOG(LOG_ERROR, tts_tag(), "[Server] Fail to notify state change.");
739 g_notify_state_timer = NULL;
740 return ECORE_CALLBACK_CANCEL;
743 static void __state_changed_cb(ttsd_state_e before, ttsd_state_e current)
745 state_changed_args_t* args = (state_changed_args_t*)calloc(1, sizeof(state_changed_args_t));
747 args->before = before;
748 args->current = current;
751 SLOG(LOG_INFO, tts_tag(), "[Server] Notify state chanaged from (%d) to (%d).", before, current);
752 g_notify_state_timer = ecore_timer_add(0.0, __state_changed_timer_cb, args);
758 int ttsd_initialize(ttse_request_callback_s *callback)
760 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
761 g_is_terminated = false;
763 if (ttsd_config_initialize(__config_changed_cb)) {
764 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
767 if (TTSD_ERROR_NONE != ttsd_state_initialize(__state_changed_cb)) {
768 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize state.");
772 if (ttsd_player_init()) {
773 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
774 return TTSD_ERROR_OPERATION_FAILED;
778 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
780 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
781 int ret = ttsd_player_set_background_volume_ratio(ratio);
783 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
785 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
788 /* Engine Agent initialize */
789 if (TTSD_ERROR_NONE != ttsd_engine_agent_init(callback)) {
790 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
791 return TTSD_ERROR_OPERATION_FAILED;
794 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
795 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
797 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, __cleanup_client, NULL);
798 if (NULL == g_check_client_timer) {
799 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
802 g_activated_mode = 0;
804 return TTSD_ERROR_NONE;
809 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
812 if (0 < g_list_length(g_proc_list)) {
813 iter = g_list_first(g_proc_list);
814 while (NULL != iter) {
815 g_proc_list = g_list_remove_link(g_proc_list, iter);
817 iter = g_list_first(g_proc_list);
821 if (g_notify_state_timer) {
822 void* data = ecore_timer_del(g_notify_state_timer);
823 g_notify_state_timer = NULL;
826 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore notify state timer handle");
830 ecore_timer_del(g_wait_timer);
832 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
835 if (g_check_client_timer) {
836 ecore_timer_del(g_check_client_timer);
837 g_check_client_timer = NULL;
838 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
841 if (g_quit_loop_timer) {
842 ecore_timer_del(g_quit_loop_timer);
843 g_quit_loop_timer = NULL;
844 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
847 ttsd_config_finalize();
848 ttsd_state_finalize();
850 int ret = TTSD_ERROR_NONE;
851 ret = ttsd_player_release();
852 if (TTSD_ERROR_NONE != ret) {
853 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_player_release(). ret(%d)", ret);
854 return TTSD_ERROR_OPERATION_FAILED;
857 ret = ttsd_engine_agent_release();
858 if (TTSD_ERROR_NONE != ret) {
859 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_engine_agent_release(). ret(%d)", ret);
860 return TTSD_ERROR_OPERATION_FAILED;
863 return TTSD_ERROR_NONE;
868 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
869 int ret = ttsd_data_foreach_clients(__terminate_client, NULL);
870 if (TTSD_ERROR_NONE != ret) {
871 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to terminate all clients. ret(%d)", ret);
872 return TTSD_ERROR_OPERATION_FAILED;
875 if (g_quit_loop_timer) {
876 ecore_timer_del(g_quit_loop_timer);
877 g_quit_loop_timer = NULL;
878 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
881 return __terminate_server();
885 * TTS Server Functions for Client
888 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
890 if (-1 != ttsd_data_is_client(uid))
891 *is_initialized = true;
893 *is_initialized = false;
895 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
896 return TTSD_ERROR_NONE;
899 static void __set_and_notify_activated_mode()
901 int activated_mode = ttsd_data_get_activated_mode();
902 if (g_activated_mode == activated_mode) {
906 SLOG(LOG_INFO, tts_tag(), "[Server] Activated mode is changed from(%d) to (%d)", g_activated_mode, activated_mode);
908 g_activated_mode = activated_mode;
909 int ret = ttsd_engine_notify_activated_mode_changed(g_activated_mode);
910 if (TTSD_ERROR_NONE != ret) {
911 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to notify activated mode changed event.(%d)", ret);
915 static int __check_app_agreed(int pid, unsigned int uid, bool credential_needed)
917 if (false == credential_needed) {
918 return TTSD_ERROR_NONE;
922 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
923 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
926 bool is_agreed = false;
927 int ret = ttsd_engine_check_app_agreed(appid, &is_agreed);
931 if (TTSD_ERROR_NONE != ret) {
932 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
933 return TTSD_ERROR_OPERATION_FAILED;
936 if (false == is_agreed) {
937 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
938 return TTSD_ERROR_PERMISSION_DENIED;
941 return TTSD_ERROR_NONE;
944 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)
946 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
948 if (-1 != ttsd_data_is_client(uid)) {
949 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
950 return TTSD_ERROR_NONE;
953 if (TTSD_ERROR_NONE != ttsd_data_new_client(pid, uid, mode, playing_mode, registered_event_mask, method)) {
954 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
955 return TTSD_ERROR_OPERATION_FAILED;
958 __set_and_notify_activated_mode();
960 if (TTSD_ERROR_NONE != ttsd_engine_agent_load_current_engine()) {
961 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
962 ttsd_data_delete_client(uid);
963 return TTSD_ERROR_OPERATION_FAILED;
966 bool is_needed = false;
967 if (TTSD_ERROR_NONE != ttsd_engine_agent_is_credential_needed(uid, &is_needed)) {
968 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
969 ttsd_data_delete_client(uid);
970 return TTSD_ERROR_OPERATION_FAILED;
973 int ret = __check_app_agreed(pid, uid, is_needed);
974 if (TTSD_ERROR_NONE != ret) {
975 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Checking credential is finished(%d)", ret);
976 ttsd_data_delete_client(uid);
980 ttsd_state_e tmp_service_state = ttsd_state_get_state();
981 if (TTSD_STATE_INVALID == tmp_service_state) {
982 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state");
983 ttsd_data_delete_client(uid);
984 return TTSD_ERROR_OPERATION_FAILED;
987 *credential_needed = is_needed;
988 *service_state = tmp_service_state;
990 return TTSD_ERROR_NONE;
993 void __used_voice_cb(const char* lang, int type)
995 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
996 if (0 != ttsd_engine_unload_voice(lang, type)) {
997 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
1001 int ttsd_server_finalize(unsigned int uid)
1003 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
1004 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);
1006 if (0 > ttsd_data_is_client(uid)) {
1007 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
1010 int ret = ttsd_server_stop(uid);
1011 if (TTSD_ERROR_NONE != ret) {
1012 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS server top(%u) fail (%d/%s) <<<<<", uid, ret, get_error_message(ret));
1015 /* Need to unload voice when used voice is unregistered */
1016 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
1017 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
1018 return TTSD_ERROR_OPERATION_FAILED;
1021 ttsd_data_delete_client(uid);
1022 __set_and_notify_activated_mode();
1024 /* unload engine, if ref count of client is 0 */
1025 if (0 == ttsd_data_get_client_count()) {
1026 SLOG(LOG_INFO, tts_tag(), "[Server] Delay quitting ecore main loop for %d seconds.", CLIENT_PRESENCE_CHECK_TIME);
1027 if (NULL != g_check_client_timer) {
1028 SLOG(LOG_INFO, tts_tag(), "[Server] Delete check_client_timer");
1029 ecore_timer_del(g_check_client_timer);
1031 g_check_client_timer = ecore_timer_add(CLIENT_PRESENCE_CHECK_TIME, __cleanup_client, NULL);
1034 return TTSD_ERROR_NONE;
1037 int ttsd_server_update_instant_reprepare_client()
1039 SLOG(LOG_INFO, tts_tag(), "[Server] Update instant reprepare client");
1040 unsigned int instant_reprepare_uid = TTS_INVALID_UID;
1041 ttsd_config_get_instant_reprepare_client(&instant_reprepare_uid);
1043 if (0 <= ttsd_data_is_client(instant_reprepare_uid)) {
1044 SLOG(LOG_INFO, tts_tag(), "[Server] Uid(%u) is valid. Still no need to changee the value", instant_reprepare_uid);
1045 return TTSD_ERROR_NONE;
1048 unsigned int new_instant_reprepare_uid = ttsd_data_get_first_client_uid();
1049 int ret = ttsd_config_set_instant_reprepare_client(new_instant_reprepare_uid);
1050 if (TTSD_ERROR_NONE != ret) {
1051 SLOG(LOG_ERROR, tts_tag(), "[Server] Fail to set UID(%u) of instant reprepare client", instant_reprepare_uid);
1052 return TTSD_ERROR_OPERATION_FAILED;
1054 SLOG(LOG_INFO, tts_tag(), "[Server] Update UID(%u) of instant reprepare client", instant_reprepare_uid);
1056 return TTSD_ERROR_NONE;
1059 static bool __is_connected_to_network()
1062 int network_status = 0;
1063 int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
1065 SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
1069 if (network_status == VCONFKEY_NETWORK_OFF) {
1070 SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
1074 SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
1079 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)
1081 app_tts_state_e state = ttsd_data_get_client_state(uid);
1082 if (APP_STATE_NONE == state) {
1083 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_text : uid is not valid");
1084 return TTSD_ERROR_INVALID_PARAMETER;
1087 /* check valid voice */
1088 char* temp_lang = NULL;
1090 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
1091 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
1092 if (NULL != temp_lang) {
1096 return TTSD_ERROR_INVALID_VOICE;
1099 if (NULL == temp_lang) {
1100 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
1101 return TTSD_ERROR_INVALID_VOICE;
1104 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, 0, -1.0, -1.0, utt_id, false, 0);
1105 if (NULL == speak_data) {
1106 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
1107 if (NULL != temp_lang) {
1112 return TTSD_ERROR_OPERATION_FAILED;
1115 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);
1117 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
1119 ret = ttsd_data_add_speak_data(uid, speak_data);
1121 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
1122 if (NULL != temp_lang) {
1127 ttsd_data_destroy_speak_data(speak_data);
1133 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
1134 /* Request load voice */
1135 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
1136 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
1137 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
1141 if (NULL != temp_lang) {
1146 if (APP_STATE_PLAYING == state) {
1147 /* check if engine use network */
1148 if (ttsd_engine_agent_need_network()) {
1149 if (false == __is_connected_to_network()) {
1150 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
1151 return TTSD_ERROR_OPERATION_FAILED;
1155 ttsd_data_set_credential(uid, credential);
1157 /* Check whether tts-engine is running or not */
1158 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1159 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1160 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1161 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1167 return TTSD_ERROR_NONE;
1170 int ttsd_server_add_text_with_synthesis_parameter(unsigned int uid, const char* text, const char* lang, const char* ptts_id, int voice_type, int speed, int pitch, double volume, double background_volume_ratio, int utt_id, const char* credential)
1172 app_tts_state_e state = ttsd_data_get_client_state(uid);
1173 if (APP_STATE_NONE == state) {
1174 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_text : uid is not valid");
1175 return TTSD_ERROR_INVALID_PARAMETER;
1178 /* check valid voice */
1179 char* temp_lang = NULL;
1181 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
1182 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
1183 if (NULL != temp_lang) {
1187 return TTSD_ERROR_INVALID_VOICE;
1190 if (NULL == temp_lang) {
1191 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
1192 return TTSD_ERROR_INVALID_VOICE;
1195 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, pitch, volume, background_volume_ratio, utt_id, false, 0);
1196 if (NULL == speak_data) {
1197 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
1198 if (NULL != temp_lang) {
1203 return TTSD_ERROR_OPERATION_FAILED;
1206 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);
1208 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
1210 ret = ttsd_data_add_speak_data(uid, speak_data);
1212 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
1213 if (NULL != temp_lang) {
1218 ttsd_data_destroy_speak_data(speak_data);
1224 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
1225 /* Request load voice */
1226 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
1227 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
1228 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
1232 if (NULL != temp_lang) {
1237 if (APP_STATE_PLAYING == state) {
1238 /* check if engine use network */
1239 if (ttsd_engine_agent_need_network()) {
1240 if (false == __is_connected_to_network()) {
1241 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
1242 return TTSD_ERROR_OPERATION_FAILED;
1246 ttsd_data_set_credential(uid, credential);
1248 /* Check whether tts-engine is running or not */
1249 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1250 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1251 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1252 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1258 return TTSD_ERROR_NONE;
1261 int ttsd_server_add_silent_utterance(unsigned int uid, unsigned int duration_in_msec, int utt_id, const char* credential)
1263 app_tts_state_e state = ttsd_data_get_client_state(uid);
1264 if (APP_STATE_NONE == state) {
1265 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_silent_utterance : uid is not valid");
1266 return TTSD_ERROR_INVALID_PARAMETER;
1269 speak_data_s* speak_data = ttsd_data_create_speak_data(NULL, NULL, 0, 0, 0, -1.0, -1.0, utt_id, true, duration_in_msec);
1270 if (NULL == speak_data) {
1271 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
1272 return TTSD_ERROR_OPERATION_FAILED;
1275 SLOG(LOG_INFO, tts_tag(), "[Server] Add queue, uttid(%d), credential(%s)", utt_id, credential);
1278 ret = ttsd_data_add_speak_data(uid, speak_data);
1280 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
1281 ttsd_data_destroy_speak_data(speak_data);
1287 if (APP_STATE_PLAYING == state) {
1288 ttsd_data_set_credential(uid, credential);
1289 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1290 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1291 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1292 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1298 return TTSD_ERROR_NONE;
1301 static int __interrupt_player_by_policy(unsigned int request_app_uid)
1303 unsigned int playing_app_uid = ttsd_data_get_current_playing();
1304 if (0 > ttsd_data_is_client(playing_app_uid)) {
1305 SLOG(LOG_INFO, tts_tag(), "[Server] There is no app which is in 'Play' state");
1306 return TTSD_ERROR_NONE;
1309 if (request_app_uid == playing_app_uid) {
1310 SLOG(LOG_WARN, tts_tag(), "[Server] This is played uid. Skip policy behavior");
1311 return TTSD_ERROR_NONE;
1314 ttsd_mode_e request_app_mode = ttsd_data_get_mode(request_app_uid);
1315 ttsd_mode_e playing_app_mode = ttsd_data_get_mode(playing_app_uid);
1316 switch (playing_app_mode) {
1317 case TTSD_MODE_DEFAULT:
1318 case TTSD_MODE_NOTIFICATION:
1319 case TTSD_MODE_SCREEN_READER:
1320 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
1321 __stop_and_send_ready_state(playing_app_uid);
1324 case TTSD_MODE_INTERRUPT:
1325 if (TTSD_MODE_INTERRUPT != request_app_mode) {
1326 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Default, Screen reader, and Noti are prohibitted.");
1327 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1330 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
1331 __stop_and_send_ready_state(playing_app_uid);
1335 return TTSD_ERROR_NONE;
1338 int ttsd_server_play(unsigned int uid, const char* credential)
1340 app_tts_state_e state = ttsd_data_get_client_state(uid);
1341 if (APP_STATE_NONE == state) {
1342 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1343 return TTSD_ERROR_INVALID_PARAMETER;
1346 if (APP_STATE_PLAYING == state) {
1347 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1348 return TTSD_ERROR_NONE;
1351 /* check if engine use network */
1352 if (ttsd_engine_agent_need_network()) {
1353 if (false == __is_connected_to_network()) {
1354 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
1355 return TTSD_ERROR_OUT_OF_NETWORK;
1359 int ret = __interrupt_player_by_policy(uid);
1360 if (TTSD_ERROR_NONE != ret) {
1361 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1365 /* Change current play */
1366 if (0 != ttsd_player_wait_to_play(uid)) {
1367 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1370 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1371 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1372 return TTSD_ERROR_OPERATION_FAILED;
1375 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1376 ret = ttsd_player_play(uid);
1377 if (TTSD_ERROR_NONE != ret) {
1378 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1379 ttsd_data_set_client_state(uid, state);
1380 return TTSD_ERROR_OPERATION_FAILED;
1383 ttsd_data_set_credential(uid, credential);
1385 /* Check whether tts-engine is running or not */
1386 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1387 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1388 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1389 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1390 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1394 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_SYNTH);
1396 return TTSD_ERROR_NONE;
1399 static void __stop_engine_synthesis(unsigned int uid)
1401 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1402 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1404 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1405 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1407 int ret = ttsd_engine_cancel_synthesis();
1408 if (TTSD_ERROR_NONE != ret)
1409 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1412 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1415 int ttsd_server_stop(unsigned int uid)
1417 app_tts_state_e state = ttsd_data_get_client_state(uid);
1418 if (APP_STATE_NONE == state) {
1419 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1420 return TTSD_ERROR_INVALID_PARAMETER;
1423 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1425 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1426 if (TTS_APP_PLAY_TYPE_SYNTH == ttsd_data_get_play_type(uid)) {
1427 __stop_engine_synthesis(uid);
1431 ttsd_data_set_client_state(uid, APP_STATE_READY);
1432 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1433 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1437 /* Reset all data */
1438 ttsd_data_clear_data(uid);
1440 return TTSD_ERROR_NONE;
1443 int ttsd_server_pause(unsigned int uid)
1445 app_tts_state_e state = ttsd_data_get_client_state(uid);
1446 if (APP_STATE_NONE == state) {
1447 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1448 return TTSD_ERROR_INVALID_PARAMETER;
1451 if (APP_STATE_PLAYING != state) {
1452 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1453 return TTSD_ERROR_INVALID_STATE;
1456 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1457 if (TTSD_ERROR_NONE != ret) {
1458 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
1459 return TTSD_ERROR_OPERATION_FAILED;
1462 ret = ttsd_player_pause(uid);
1463 if (TTSD_ERROR_NONE != ret) {
1464 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1465 return TTSD_ERROR_OPERATION_FAILED;
1468 return TTSD_ERROR_NONE;
1471 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1473 if (0 > ttsd_data_is_client(uid)) {
1474 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1475 return TTSD_ERROR_INVALID_PARAMETER;
1479 int ret = ttsd_engine_get_voice_list(voice_list);
1481 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1485 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1487 return TTSD_ERROR_NONE;
1490 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1492 if (0 > ttsd_data_is_client(uid)) {
1493 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1494 return TTSD_ERROR_INVALID_PARAMETER;
1497 /* get current voice */
1498 int ret = ttsd_engine_get_default_voice(language, voice_type);
1500 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1504 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1506 return TTSD_ERROR_NONE;
1509 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1511 app_tts_state_e state = ttsd_data_get_client_state(uid);
1512 if (APP_STATE_NONE == state) {
1513 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1514 return TTSD_ERROR_INVALID_PARAMETER;
1517 if (APP_STATE_READY != state) {
1518 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1519 return TTSD_ERROR_INVALID_STATE;
1522 int ret = ttsd_engine_set_private_data(key, data);
1524 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1526 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1532 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1534 app_tts_state_e state = ttsd_data_get_client_state(uid);
1535 if (APP_STATE_NONE == state) {
1536 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1537 return TTSD_ERROR_INVALID_PARAMETER;
1540 if (APP_STATE_READY != state) {
1541 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1542 return TTSD_ERROR_INVALID_STATE;
1545 char* temp_data = NULL;
1546 int ret = ttsd_engine_get_private_data(key, &temp_data);
1548 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1550 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1553 if (NULL == temp_data) {
1554 temp_data = strdup("NULL");
1561 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1563 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1566 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1568 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1574 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1576 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1579 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1581 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1587 int ttsd_get_activated_mode(int* activated_mode)
1589 if (NULL == activated_mode) {
1590 return TTSD_ERROR_INVALID_PARAMETER;
1593 *activated_mode = ttsd_data_get_activated_mode();
1594 return TTSD_ERROR_NONE;
1597 int ttsd_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
1599 int ret = ttsd_engine_agent_set_activated_mode_changed_cb(callback);
1600 if (TTSD_ERROR_NONE != ret) {
1601 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1607 int ttsd_server_play_pcm(unsigned int uid)
1609 app_tts_state_e state = ttsd_data_get_client_state(uid);
1610 if (APP_STATE_NONE == state) {
1611 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1612 return TTSD_ERROR_INVALID_PARAMETER;
1615 if (APP_STATE_PLAYING == state) {
1616 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1617 return TTSD_ERROR_NONE;
1620 int ret = __interrupt_player_by_policy(uid);
1621 if (TTSD_ERROR_NONE != ret) {
1622 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1626 /* Change current play */
1627 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1628 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1629 return TTSD_ERROR_OPERATION_FAILED;
1632 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1633 ret = ttsd_player_play(uid);
1634 if (TTSD_ERROR_NONE != ret) {
1635 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1636 ttsd_data_set_client_state(uid, state);
1637 return TTSD_ERROR_OPERATION_FAILED;
1640 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_PCM);
1642 return TTSD_ERROR_NONE;
1645 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1647 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1651 /* Synthesis is success */
1652 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1653 if (TTSE_RESULT_EVENT_START == event) {
1654 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1655 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1656 uid, uttid, data, data_size, audio_type, rate);
1657 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1658 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1659 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1660 uid, uttid, data, data_size, audio_type, rate);
1662 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1665 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1666 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1667 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1668 return TTSD_ERROR_INVALID_PARAMETER;
1671 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1672 if (NULL == sound_data) {
1673 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1674 return TTSD_ERROR_OUT_OF_MEMORY;
1677 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1678 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1679 ttsd_data_destroy_sound_data(sound_data);
1682 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1685 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1687 return TTSD_ERROR_NONE;
1690 int ttsd_server_get_service_state(unsigned int uid, int* service_state)
1692 if (0 > ttsd_data_is_client(uid)) {
1693 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_service_state : uid is not valid");
1694 return TTSD_ERROR_INVALID_PARAMETER;
1697 *service_state = (int)ttsd_state_get_state();
1698 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get current service state. service state(%d) ", *service_state);
1700 return TTSD_ERROR_NONE;