Add g_steal_pointer to avoid double-free
[platform/core/uifw/tts.git] / server / ttsd_server.c
1 /*
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.
12 */
13
14 #include <app_manager.h>
15 #include <aul.h>
16 #include <Ecore.h>
17 #include <sys/time.h>
18
19 #include "ttsd_config.h"
20 #include "ttsd_data.h"
21 #include "ttsd_ipc.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"
27
28
29 #define CLIENT_CLEAN_UP_TIME 500
30 #define CLIENT_PRESENCE_CHECK_TIME 30
31
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
35
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;
42
43 typedef struct {
44         unsigned int uid;
45         int uttid;
46 } utterance_t;
47
48 typedef struct {
49         ttsd_state_e before;
50         ttsd_state_e current;
51 } state_changed_args_t;
52
53 /* If current engine exist */
54 //static bool   g_is_engine;
55
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;
60
61 static utterance_t g_utt;
62
63 static GList *g_proc_list = NULL;
64
65 static bool g_is_terminated = false;
66
67 static int g_activated_mode = 0;
68
69 /* Function definitions */
70 static int __stop_and_send_ready_state(unsigned int uid)
71 {
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);
75                 return ret;
76         }
77
78         int pid = ttsd_data_get_pid(uid);
79         if (pid <= 0) {
80                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
81                 return TTSD_ERROR_OPERATION_FAILED;
82         } else {
83                 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
84         }
85
86         return ret;
87 }
88
89 static void __synthesis(unsigned int uid);
90 static bool __generate_silent(long duration_in_msec);
91
92 static Eina_Bool __wait_synthesis(void *data)
93 {
94         /* get current play */
95         unsigned int uid = ttsd_data_get_current_playing();
96
97         if (uid > 0) {
98                 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
99                 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
100                         return EINA_TRUE;
101                 } else {
102                         SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
103                         g_wait_timer = NULL;
104                         if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
105                                 /* Start next synthesis */
106                                 __synthesis(uid);
107                         }
108                 }
109         } else {
110                 g_wait_timer = NULL;
111         }
112
113         return EINA_FALSE;
114 }
115
116 static void write_debugger_information(speak_data_s *speak_data, const char *appid, const int ret)
117 {
118         if(aul_app_is_running("org.tizen.TTSDebugger") != 1) {
119                 return;
120         }
121
122         SLOG(LOG_ERROR, tts_tag(), "TTS Debugger is Running");
123
124         char* temp_lang = NULL;
125         int temp_type;
126         if (true != ttsd_engine_select_valid_voice(speak_data->voice.language, speak_data->voice.type, &temp_lang,
127                                                                                            &temp_type)) {
128                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select default voice");
129         } else {
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);
133
134                 SLOG(LOG_INFO, tts_tag(), "keyVal : %s", value_buffer);
135
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");
138                 }
139         }
140
141         free(temp_lang);
142         temp_lang = NULL;
143 }
144
145 static void __synthesis(unsigned int uid)
146 {
147         SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  START");
148
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");
154                 return;
155         }
156
157         int pid = ttsd_data_get_pid(uid);
158         if (pid <= 0) {
159                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
160                 ttsd_data_destroy_speak_data(speak_data);
161                 speak_data = NULL;
162                 return;
163         }
164
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");
168         }
169
170         g_utt.uid = uid;
171         g_utt.uttid = speak_data->utt_id;
172         char* credential = ttsd_data_get_credential(uid);
173
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);
178                 } else {
179                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to make GENERATE silent !!!! * ");
180
181                         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
182                         __stop_and_send_ready_state(uid);
183                 }
184         } else {
185
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);
190                         speak_data = NULL;
191                         return;
192                 }
193
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(), "-----------------------------------------------------------");
201
202                 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
203
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 !!!! * ");
207                 }
208
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 !!!! * ");
212                 }
213
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 !!!! * ");
218
219                         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
220                         __stop_and_send_ready_state(uid);
221                 } else {
222                         g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
223                 }
224
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);
228
229                         ttsd_state_set_state(TTSD_STATE_SYNTHESIZING);
230                 }
231
232                 write_debugger_information(speak_data, appid, ret);
233
234                 free(credential);
235                 credential = NULL;
236         }
237
238         ttsd_data_destroy_speak_data(speak_data);
239         speak_data = NULL;
240         SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  END");
241
242         return;
243 }
244
245 static bool __generate_silent(long duration_in_msec)
246 {
247         SLOG(LOG_DEBUG, tts_tag(), "@@@ GENERATE SILENT  START");
248
249         int audio_type = TTSE_AUDIO_TYPE_RAW_S16;
250         int sample_bytes = 2;
251         int sample_rate = 16000;
252
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);
256
257         /* add wav data */
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");
261                 return false;
262         }
263
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);
267                 return false;
268         }
269
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");
274                 return false;
275         }
276
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);
280                 return false;
281         }
282
283         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
284
285         SLOG(LOG_DEBUG, tts_tag(), "@@@ GENERATE SILENT  END");
286
287         return true;
288 }
289
290 /*
291 * TTS Server Callback Functions
292 */
293 int ttsd_send_error(ttse_error_e error, const char* msg)
294 {
295         unsigned int uid = g_utt.uid;
296         int uttid = g_utt.uttid;
297         int tmp_pid = ttsd_data_get_pid(uid);
298
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));
300
301         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
302
303         if (0 != ttsd_player_stop(uid)) {
304                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to stop for uid(%u)", uid);
305         }
306
307         if (0 != ttsd_data_clear_data(uid))
308                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
309
310         if (tmp_pid > 0) {
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()");
313         }
314
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()");
317
318         if (tmp_pid > 0) {
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()");
321         }
322         return 0;
323 }
324
325 static int ttsd_convert_audio_type_to_bytes(ttse_audio_type_e audio_type)
326 {
327         int ret = 0;
328         switch (audio_type) {
329         case TTSE_AUDIO_TYPE_RAW_S16:
330                 ret = 2; // 16bit
331                 break;
332         case TTSE_AUDIO_TYPE_RAW_U8:
333                 ret = 1; // 8bit
334                 break;
335         default:
336                 ret = 2; // Default 16bit
337                 break;
338         }
339         return ret;
340 }
341
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)
343 {
344         SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
345
346         unsigned int uid = g_utt.uid;
347         int uttid = g_utt.uttid;
348
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;
354         }
355
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;
360         }
361
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;
367         }
368
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;
375         }
376
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;
382         }
383
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);
389
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;
397                 g_file_num++;
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);
405
406                 g_total_data_size += data_size;
407                 int audio_type_bytes = ttsd_convert_audio_type_to_bytes(audio_type);
408                 /* Calculate xRT */
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);
415         } else {
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;
418         }
419
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) {
423                 /* add wav data */
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;
428                 }
429
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);
433
434                         return TTSD_ERROR_OPERATION_FAILED;
435                 }
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;
442                 }
443         }
444
445         if (event == TTSE_RESULT_EVENT_FINISH) {
446                 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
447         }
448
449         return TTSD_ERROR_NONE;
450 }
451
452 static bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
453 {
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);
457                 return true;
458         }
459
460         /* clear client data */
461         ttsd_data_clear_data(uid);
462
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);
466         }
467
468         return true;
469 }
470
471 static void __stop_all_client()
472 {
473         SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
474
475         ttsd_engine_cancel_synthesis();
476         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
477
478         /* stop all player */
479         ttsd_player_all_stop();
480
481         /* send interrupt message to  all clients */
482         ttsd_data_foreach_clients(__get_client_cb, NULL);
483 }
484
485 static void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
486 {
487         switch (type) {
488         case TTS_CONFIG_TYPE_ENGINE:
489         {
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");
493                         return;
494                 }
495
496                 int ret = 0;
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();
500                         if (0 != ret) {
501                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
502                         }
503
504                         ret = ttsd_engine_agent_load_current_engine();
505                         if (0 != ret) {
506                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
507                         }
508                         return;
509                 }
510
511                 __stop_all_client();
512                 break;
513         }
514
515         case TTS_CONFIG_TYPE_VOICE:
516         {
517                 if (NULL == str_param) {
518                         SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
519                         return;
520                 }
521
522                 char* out_lang = NULL;
523                 int out_type;
524                 int ret = -1;
525
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);
529                         if (0 != ret)
530                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
531                 } else {
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);
534                 }
535                 if (NULL != out_lang) {
536                         free(out_lang);
537                         out_lang = NULL;
538                 }
539                 break;
540         }
541
542         case TTS_CONFIG_TYPE_SPEED:
543         {
544                 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
545                         /* set default speed */
546                         int ret = 0;
547                         ret = ttsd_engine_agent_set_default_speed(int_param);
548                         if (0 != ret) {
549                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
550                         }
551                 }
552                 break;
553         }
554
555         case TTS_CONFIG_TYPE_PITCH:
556         {
557                 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
558                         /* set default speed */
559                         int ret = 0;
560                         ret = ttsd_engine_agent_set_default_pitch(int_param);
561                         if (0 != ret) {
562                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
563                         }
564                 }
565                 break;
566         }
567
568         case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
569         {
570                 if (0.0 <= double_param && double_param <= 1.0) {
571                         /* set default bg volume ratio */
572                         int ret = 0;
573                         ret = ttsd_player_set_background_volume_ratio(double_param);
574                         if (0 != ret) {
575                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
576                         }
577                 }
578                 break;
579         }
580         }
581
582         return;
583 }
584
585 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
586 {
587         SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
588         ttsd_server_finalize(uid);
589         return true;
590 }
591
592 static int __terminate_server()
593 {
594         if (g_is_terminated) {
595                 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
596                 return TTSD_ERROR_NONE;
597         }
598
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;
604         }
605
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;
610         }
611
612         g_is_terminated = true;
613         return TTSD_ERROR_NONE;
614 }
615
616 static Eina_Bool __quit_ecore_loop(void *data)
617 {
618         __terminate_server();
619         ecore_main_loop_quit();
620
621         return EINA_FALSE;
622 }
623
624 void __screen_reader_changed_cb(bool value)
625 {
626         SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
627         return;
628 }
629
630 static void __read_proc()
631 {
632         DIR *dp = NULL;
633         struct dirent *dirp = NULL;
634         int tmp;
635
636         GList *iter = 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);
641                         g_list_free(iter);
642                         iter = g_list_first(g_proc_list);
643                 }
644         }
645
646         dp = opendir("/proc");
647         if (NULL == dp) {
648                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
649         } else {
650                 do {
651                         dirp = readdir(dp);
652
653                         if (NULL != dirp) {
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));
657                         }
658                 } while (NULL != dirp);
659                 closedir(dp);
660         }
661         return;
662 }
663
664 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
665 {
666         bool exist = false;
667         int i = 0;
668
669         GList *iter = NULL;
670         for (i = 0; i < g_list_length(g_proc_list); i++) {
671                 iter = g_list_nth(g_proc_list, i);
672                 if (NULL != iter) {
673                         if (pid == GPOINTER_TO_INT(iter->data)) {
674                                 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
675                                 exist = true;
676                                 break;
677                         }
678                 }
679         }
680
681         if (false == exist) {
682                 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
683                 ttsd_server_finalize(uid);
684         }
685
686         return true;
687 }
688
689 static Eina_Bool __cleanup_client(void *data)
690 {
691         SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
692         __read_proc();
693
694         if (0 < ttsd_data_get_client_count()) {
695                 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
696         } else {
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);
700                 }
701                 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
702                 g_check_client_timer = NULL;
703                 return EINA_FALSE;
704         }
705
706         ecore_timer_interval_set(g_check_client_timer, CLIENT_CLEAN_UP_TIME);
707         SLOG(LOG_DEBUG, tts_tag(), "@@@");
708
709         return EINA_TRUE;
710 }
711
712 static bool __notify_state_changed_event(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
713 {
714         state_changed_args_t* args = (state_changed_args_t*)user_data;
715         if (NULL == args) {
716                 return false;
717         }
718
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);
721         }
722
723         return true;
724 }
725
726 static Eina_Bool __state_changed_timer_cb(void *data)
727 {
728         if (NULL == data) {
729                 SLOG(LOG_ERROR, tts_tag(), "[Server] Invalid data passed.");
730                 return ECORE_CALLBACK_CANCEL;
731         }
732
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.");
736         }
737
738         free(data);
739         g_notify_state_timer = NULL;
740         return ECORE_CALLBACK_CANCEL;
741 }
742
743 static void __state_changed_cb(ttsd_state_e before, ttsd_state_e current)
744 {
745         state_changed_args_t* args = (state_changed_args_t*)calloc(1, sizeof(state_changed_args_t));
746         if (NULL != args) {
747                 args->before = before;
748                 args->current = current;
749         }
750
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);
753 }
754
755 /*
756 * Server APIs
757 */
758 int ttsd_initialize(ttse_request_callback_s *callback)
759 {
760         SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
761         g_is_terminated = false;
762
763         if (ttsd_config_initialize(__config_changed_cb)) {
764                 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
765         }
766
767         if (TTSD_ERROR_NONE != ttsd_state_initialize(__state_changed_cb)) {
768                 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize state.");
769         }
770
771         /* player init */
772         if (ttsd_player_init()) {
773                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
774                 return TTSD_ERROR_OPERATION_FAILED;
775         }
776
777         double ratio;
778         if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
779         {
780                 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
781                 int ret = ttsd_player_set_background_volume_ratio(ratio);
782                 if (0 != ret)
783                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
784         } else {
785                 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
786         }
787
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;
792         }
793
794         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
795         ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
796
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");
800         }
801
802         g_activated_mode = 0;
803
804         return TTSD_ERROR_NONE;
805 }
806
807 int ttsd_finalize()
808 {
809         SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
810
811         GList *iter = NULL;
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);
816                         g_list_free(iter);
817                         iter = g_list_first(g_proc_list);
818                 }
819         }
820
821         if (g_notify_state_timer) {
822                 void* data = ecore_timer_del(g_notify_state_timer);
823                 g_notify_state_timer = NULL;
824
825                 free(data);
826                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore notify state timer handle");
827         }
828
829         if (g_wait_timer) {
830                 ecore_timer_del(g_wait_timer);
831                 g_wait_timer = NULL;
832                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
833         }
834
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");
839         }
840
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");
845         }
846
847         ttsd_config_finalize();
848         ttsd_state_finalize();
849
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;
855         }
856
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;
861         }
862
863         return TTSD_ERROR_NONE;
864 }
865
866 int ttsd_terminate()
867 {
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;
873         }
874
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");
879         }
880
881         return __terminate_server();
882 }
883
884 /*
885 * TTS Server Functions for Client
886 */
887
888 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
889 {
890         if (-1 != ttsd_data_is_client(uid))
891                 *is_initialized = true;
892         else
893                 *is_initialized = false;
894
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;
897 }
898
899 static void __set_and_notify_activated_mode()
900 {
901         int activated_mode = ttsd_data_get_activated_mode();
902         if (g_activated_mode == activated_mode) {
903                 return;
904         }
905
906         SLOG(LOG_INFO, tts_tag(), "[Server] Activated mode is changed from(%d) to (%d)", g_activated_mode, activated_mode);
907
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);
912         }
913 }
914
915 static int __check_app_agreed(int pid, unsigned int uid, bool credential_needed)
916 {
917         if (false == credential_needed) {
918                 return TTSD_ERROR_NONE;
919         }
920
921         char* appid = NULL;
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);
924         }
925
926         bool is_agreed = false;
927         int ret = ttsd_engine_check_app_agreed(appid, &is_agreed);
928         free(appid);
929         appid = NULL;
930
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;
934         }
935
936         if (false == is_agreed) {
937                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
938                 return TTSD_ERROR_PERMISSION_DENIED;
939         }
940
941         return TTSD_ERROR_NONE;
942 }
943
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)
945 {
946         SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
947
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;
951         }
952
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;
956         }
957
958         __set_and_notify_activated_mode();
959
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;
964         }
965
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;
971         }
972
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);
977                 return ret;
978         }
979
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;
985         }
986
987         *credential_needed = is_needed;
988         *service_state = tmp_service_state;
989
990         return TTSD_ERROR_NONE;
991 }
992
993 void __used_voice_cb(const char* lang, int type)
994 {
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");
998         }
999 }
1000
1001 int ttsd_server_finalize(unsigned int uid)
1002 {
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);
1005
1006         if (0 > ttsd_data_is_client(uid)) {
1007                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
1008         }
1009
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));
1013         }
1014
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;
1019         }
1020
1021         ttsd_data_delete_client(uid);
1022         __set_and_notify_activated_mode();
1023
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);
1030                 }
1031                 g_check_client_timer = ecore_timer_add(CLIENT_PRESENCE_CHECK_TIME, __cleanup_client, NULL);
1032         }
1033
1034         return TTSD_ERROR_NONE;
1035 }
1036
1037 int ttsd_server_update_instant_reprepare_client()
1038 {
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);
1042
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;
1046         }
1047
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;
1053         }
1054         SLOG(LOG_INFO, tts_tag(), "[Server] Update UID(%u) of instant reprepare client", instant_reprepare_uid);
1055
1056         return TTSD_ERROR_NONE;
1057 }
1058
1059 static bool __is_connected_to_network()
1060 {
1061         /* Check network */
1062         int network_status = 0;
1063         int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
1064         if (0 != ret) {
1065                 SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
1066                 return false;
1067         }
1068
1069         if (network_status == VCONFKEY_NETWORK_OFF) {
1070                 SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
1071                 return false;
1072         }
1073
1074         SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
1075
1076         return true;
1077 }
1078
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)
1080 {
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;
1085         }
1086
1087         /* check valid voice */
1088         char* temp_lang = NULL;
1089         int temp_type;
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) {
1093                         free(temp_lang);
1094                         temp_lang = NULL;
1095                 }
1096                 return TTSD_ERROR_INVALID_VOICE;
1097         }
1098
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;
1102         }
1103
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) {
1108                         free(temp_lang);
1109                         temp_lang = NULL;
1110                 }
1111
1112                 return TTSD_ERROR_OPERATION_FAILED;
1113         }
1114
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);
1116
1117         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
1118         int ret = -1;
1119         ret = ttsd_data_add_speak_data(uid, speak_data);
1120         if (0 != ret) {
1121                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
1122                 if (NULL != temp_lang) {
1123                         free(temp_lang);
1124                         temp_lang = NULL;
1125                 }
1126
1127                 ttsd_data_destroy_speak_data(speak_data);
1128                 speak_data = NULL;
1129
1130                 return ret;
1131         }
1132
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");
1138                 }
1139         }
1140
1141         if (NULL != temp_lang) {
1142                 free(temp_lang);
1143                 temp_lang = NULL;
1144         }
1145
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;
1152                         }
1153                 }
1154
1155                 ttsd_data_set_credential(uid, credential);
1156
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.");
1162                 } else {
1163                         __synthesis(uid);
1164                 }
1165         }
1166
1167         return TTSD_ERROR_NONE;
1168 }
1169
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)
1171 {
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;
1176         }
1177
1178         /* check valid voice */
1179         char* temp_lang = NULL;
1180         int temp_type;
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) {
1184                         free(temp_lang);
1185                         temp_lang = NULL;
1186                 }
1187                 return TTSD_ERROR_INVALID_VOICE;
1188         }
1189
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;
1193         }
1194
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) {
1199                         free(temp_lang);
1200                         temp_lang = NULL;
1201                 }
1202
1203                 return TTSD_ERROR_OPERATION_FAILED;
1204         }
1205
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);
1207
1208         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
1209         int ret = -1;
1210         ret = ttsd_data_add_speak_data(uid, speak_data);
1211         if (0 != ret) {
1212                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
1213                 if (NULL != temp_lang) {
1214                         free(temp_lang);
1215                         temp_lang = NULL;
1216                 }
1217
1218                 ttsd_data_destroy_speak_data(speak_data);
1219                 speak_data = NULL;
1220
1221                 return ret;
1222         }
1223
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");
1229                 }
1230         }
1231
1232         if (NULL != temp_lang) {
1233                 free(temp_lang);
1234                 temp_lang = NULL;
1235         }
1236
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;
1243                         }
1244                 }
1245
1246                 ttsd_data_set_credential(uid, credential);
1247
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.");
1253                 } else {
1254                         __synthesis(uid);
1255                 }
1256         }
1257
1258         return TTSD_ERROR_NONE;
1259 }
1260
1261 int ttsd_server_add_silent_utterance(unsigned int uid, unsigned int duration_in_msec, int utt_id, const char* credential)
1262 {
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;
1267         }
1268
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;
1273         }
1274
1275         SLOG(LOG_INFO, tts_tag(), "[Server] Add queue, uttid(%d), credential(%s)", utt_id, credential);
1276
1277         int ret = -1;
1278         ret = ttsd_data_add_speak_data(uid, speak_data);
1279         if (0 != ret) {
1280                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
1281                 ttsd_data_destroy_speak_data(speak_data);
1282                 speak_data = NULL;
1283
1284                 return ret;
1285         }
1286
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.");
1293                 } else {
1294                         __synthesis(uid);
1295                 }
1296         }
1297
1298         return TTSD_ERROR_NONE;
1299 }
1300
1301 static int __interrupt_player_by_policy(unsigned int request_app_uid)
1302 {
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;
1307         }
1308
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;
1312         }
1313
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);
1322                 break;
1323
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;
1328                 }
1329
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);
1332                 break;
1333         }
1334
1335         return TTSD_ERROR_NONE;
1336 }
1337
1338 int ttsd_server_play(unsigned int uid, const char* credential)
1339 {
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;
1344         }
1345
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;
1349         }
1350
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;
1356                 }
1357         }
1358
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.");
1362                 return ret;
1363         }
1364
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);
1368         }
1369
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;
1373         }
1374
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;
1381         }
1382
1383         ttsd_data_set_credential(uid, credential);
1384
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.");
1391         } else {
1392                 __synthesis(uid);
1393         }
1394         ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_SYNTH);
1395
1396         return TTSD_ERROR_NONE;
1397 }
1398
1399 static void __stop_engine_synthesis(unsigned int uid)
1400 {
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);
1403
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");
1406
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);
1410         }
1411
1412         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1413 }
1414
1415 int ttsd_server_stop(unsigned int uid)
1416 {
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;
1421         }
1422
1423         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1424
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);
1428                 }
1429
1430                 /* stop player */
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()");
1434                 }
1435         }
1436
1437         /* Reset all data */
1438         ttsd_data_clear_data(uid);
1439
1440         return TTSD_ERROR_NONE;
1441 }
1442
1443 int ttsd_server_pause(unsigned int uid)
1444 {
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;
1449         }
1450
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;
1454         }
1455
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;
1460         }
1461
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;
1466         }
1467
1468         return TTSD_ERROR_NONE;
1469 }
1470
1471 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1472 {
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;
1476         }
1477
1478         /* get voice list*/
1479         int ret = ttsd_engine_get_voice_list(voice_list);
1480         if (0 != ret) {
1481                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1482                 return ret;
1483         }
1484
1485         SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1486
1487         return TTSD_ERROR_NONE;
1488 }
1489
1490 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1491 {
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;
1495         }
1496
1497         /* get current voice */
1498         int ret = ttsd_engine_get_default_voice(language, voice_type);
1499         if (0 != ret) {
1500                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1501                 return ret;
1502         }
1503
1504         SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1505
1506         return TTSD_ERROR_NONE;
1507 }
1508
1509 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1510 {
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;
1515         }
1516
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;
1520         }
1521
1522         int ret = ttsd_engine_set_private_data(key, data);
1523         if (0 != ret) {
1524                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1525         } else {
1526                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1527         }
1528
1529         return ret;
1530 }
1531
1532 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1533 {
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;
1538         }
1539
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;
1543         }
1544
1545         char* temp_data = NULL;
1546         int ret = ttsd_engine_get_private_data(key, &temp_data);
1547         if (0 != ret) {
1548                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1549         } else {
1550                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1551         }
1552
1553         if (NULL == temp_data) {
1554                 temp_data = strdup("NULL");
1555         }
1556         *data = temp_data;
1557
1558         return ret;
1559 }
1560
1561 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1562 {
1563         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1564
1565         int ret = 0;
1566         ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1567         if (0 != ret) {
1568                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1569         }
1570
1571         return ret;
1572 }
1573
1574 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1575 {
1576         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1577
1578         int ret = 0;
1579         ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1580         if (0 != ret) {
1581                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1582         }
1583
1584         return ret;
1585 }
1586
1587 int ttsd_get_activated_mode(int* activated_mode)
1588 {
1589         if (NULL == activated_mode) {
1590                 return TTSD_ERROR_INVALID_PARAMETER;
1591         }
1592
1593         *activated_mode = ttsd_data_get_activated_mode();
1594         return TTSD_ERROR_NONE;
1595 }
1596
1597 int ttsd_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
1598 {
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);
1602         }
1603
1604         return ret;
1605 }
1606
1607 int ttsd_server_play_pcm(unsigned int uid)
1608 {
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;
1613         }
1614
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;
1618         }
1619
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.");
1623                 return ret;
1624         }
1625
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;
1630         }
1631
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;
1638         }
1639
1640         ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_PCM);
1641
1642         return TTSD_ERROR_NONE;
1643 }
1644
1645 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1646 {
1647         SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1648
1649         int uttid = -1;
1650
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);
1661                 } else {
1662                         /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1663                 }
1664
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;
1669                 }
1670
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;
1675                 }
1676
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);
1680                 }
1681         } else {
1682                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1683         }
1684
1685         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1686
1687         return TTSD_ERROR_NONE;
1688 }
1689
1690 int ttsd_server_get_service_state(unsigned int uid, int* service_state)
1691 {
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;
1695         }
1696
1697         *service_state = (int)ttsd_state_get_state();
1698         SLOG(LOG_DEBUG, tts_tag(), "[Server] Get current service state. service state(%d) ", *service_state);
1699
1700         return TTSD_ERROR_NONE;
1701 }