Unify TTSD processes into two processes (default, interrupt)
[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_dbus.h"
22 #include "ttsd_dbus_server.h"
23 #include "ttsd_ipc.h"
24 #include "ttsd_engine_agent.h"
25 #include "ttsd_main.h"
26 #include "ttsd_network.h"
27 #include "ttsd_player.h"
28 #include "ttsd_server.h"
29
30
31 #define CLIENT_CLEAN_UP_TIME 500
32
33 /* for checking time */
34 #define TIME_DIFF(start, end) \
35         ((uint64_t)((end).tv_sec - (start).tv_sec) * 1000000 + (((end).tv_nsec - (start).tv_nsec) / 1000)) / 1000
36
37 static struct timespec g_request_playing, g_start_playing;
38 static double g_avg_latency;
39 static double g_min_latency;
40 static double g_max_latency;
41 static int g_file_num;
42
43 typedef struct {
44         int uid;
45         int uttid;
46 } utterance_t;
47
48 /* If current engine exist */
49 //static bool   g_is_engine;
50
51 static Ecore_Timer* g_check_client_timer = NULL;
52 static Ecore_Timer* g_wait_timer = NULL;
53 static Ecore_Timer* g_terminate_timer = NULL;
54 static Ecore_Timer* g_quit_loop_timer = NULL;
55
56 static utterance_t g_utt;
57
58 static GList *g_proc_list = NULL;
59
60 static bool g_is_paused;
61
62
63 /* Function definitions */
64 static int __synthesis(int uid, const char* credential);
65
66 static Eina_Bool __wait_synthesis(void *data)
67 {
68         /* get current play */
69         char* credential = (char*)data;
70         int uid = ttsd_data_get_current_playing();
71
72         if (uid > 0) {
73                 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
74                         return EINA_TRUE;
75                 } else {
76                         g_wait_timer = NULL;
77                         if (TTSD_SYNTHESIS_CONTROL_DONE == ttsd_get_synth_control()) {
78                                 /* Start next synthesis */
79                                 __synthesis(uid, credential);
80                         }
81                 }
82         } else {
83                 g_wait_timer = NULL;
84         }
85
86         return EINA_FALSE;
87 }
88
89 static int __synthesis(int uid, const char* credential)
90 {
91         SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  START");
92
93         speak_data_s* speak_data = NULL;
94         if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
95                 if (NULL == speak_data) {
96                         SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
97                         return 0;
98                 }
99
100                 int pid = ttsd_data_get_pid(uid);
101                 if (pid <= 0) {
102                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%d)", uid);
103                         return -1;
104                 }
105
106                 char appid[1024] = {0, };
107                 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
108                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
109                 }
110
111                 if (NULL == speak_data->lang || NULL == speak_data->text) {
112                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
113                         ttsd_server_stop(uid);
114
115                         ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
116
117                         ttsd_data_clear_speak_data(&speak_data);
118
119                         return 0;
120                 }
121
122                 g_utt.uid = uid;
123                 g_utt.uttid = speak_data->utt_id;
124
125                 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
126                 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
127                 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
128                 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
129                 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
130                 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
131
132                 int ret = 0;
133                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
134                 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
135                 if (0 != ret) {
136                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
137
138                         ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
139
140                         ttsd_server_stop(uid);
141
142                         int pid = ttsd_data_get_pid(uid);
143                         if (pid <= 0) {
144                                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%d)", uid);
145                         } else {
146                                 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
147                         }
148                 } else {
149                         g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
150                 }
151
152                 ttsd_data_clear_speak_data(&speak_data);
153         } else {
154                 ttsd_data_clear_speak_data(&speak_data);
155         }
156
157         SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  END");
158
159         return 0;
160 }
161
162 /*
163 * TTS Server Callback Functions
164 */
165 int ttsd_send_error(ttse_error_e error, const char* msg)
166 {
167         int uid = g_utt.uid;
168         int uttid = g_utt.uttid;
169         int tmp_pid = ttsd_data_get_pid(uid);
170
171         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%d), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, (NULL == msg ? "NULL" : msg));
172
173         ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
174
175         if (0 != ttsd_player_clear(uid))
176                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
177
178         if (0 != ttsd_data_clear_data(uid))
179                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
180
181         if (tmp_pid > 0) {
182                 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
183                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
184         }
185
186         if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
187                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
188
189         if (tmp_pid > 0) {
190                 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
191                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
192         }
193         return 0;
194 }
195
196 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)
197 {
198         SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
199
200         int uid = g_utt.uid;
201         int uttid = g_utt.uttid;
202
203         if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
204                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
205                 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
206                 SLOG(LOG_DEBUG, tts_tag(), "@@@");
207                 return TTSD_ERROR_INVALID_PARAMETER;
208         }
209
210         if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
211                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
212                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
213                 return TTSD_ERROR_INVALID_PARAMETER;
214         }
215
216         if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
217                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
218                 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
219                 SLOG(LOG_DEBUG, tts_tag(), "@@@");
220                 return TTSD_ERROR_INVALID_PARAMETER;
221         }
222
223         /* check the current state */
224         app_tts_state_e state;
225         if (0 > ttsd_data_get_client_state(uid, &state)) {
226                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
227                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
228                 return TTSD_ERROR_INVALID_PARAMETER;
229         }
230
231         SLOG(LOG_INFO, tts_tag(), "[Server] uid(%d), current state(%d)", uid, state);
232         if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
233                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
234                 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
235                 return TTSD_ERROR_NONE;
236         }
237
238         /* Synthesis is success */
239         if (TTSE_RESULT_EVENT_START == event) {
240                 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
241                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
242                         uid, uttid, data, data_size, audio_type, rate);
243
244                 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
245                 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
246                 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
247                 double d_latency = (double)latency;
248                 g_avg_latency += d_latency;
249                 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
250                 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
251                 g_file_num++;
252                 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);
253         } else if (TTSE_RESULT_EVENT_FINISH == event) {
254                 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
255                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
256                         uid, uttid, data, data_size, audio_type, rate);
257         } else {
258                 /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
259         }
260
261         /* add wav data */
262         sound_data_s* temp_sound_data = NULL;
263         temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
264         if (NULL == temp_sound_data) {
265                 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
266                 return TTSD_ERROR_OUT_OF_MEMORY;
267         }
268
269         temp_sound_data->data = NULL;
270         temp_sound_data->rate = 0;
271         temp_sound_data->data_size = 0;
272
273         if (NULL != data && 0 < data_size) {
274                 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
275                 if (NULL != temp_sound_data->data) {
276                         memcpy(temp_sound_data->data, data, data_size);
277                         temp_sound_data->data_size = data_size;
278                         SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
279                                 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
280                 } else {
281                         SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
282                 }
283         } else {
284                 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
285         }
286
287         temp_sound_data->utt_id = uttid;
288         temp_sound_data->event = event;
289         temp_sound_data->audio_type = audio_type;
290         temp_sound_data->rate = rate;
291
292         if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
293                 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
294                 if (NULL != temp_sound_data->data) {
295                         free(temp_sound_data->data);
296                         temp_sound_data->data = NULL;
297                 }
298
299                 free(temp_sound_data);
300                 temp_sound_data = NULL;
301
302                 return TTSD_ERROR_OPERATION_FAILED;
303         }
304
305         if (event == TTSE_RESULT_EVENT_FINISH) {
306                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
307         }
308
309         return TTSD_ERROR_NONE;
310 }
311
312 bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
313 {
314         /* clear client data */
315         ttsd_data_clear_data(uid);
316
317         if (APP_STATE_READY != state) {
318                 ttsd_data_set_client_state(uid, APP_STATE_READY);
319
320                 /* send message */
321                 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
322                         /* remove client */
323                         ttsd_data_delete_client(uid);
324                 }
325         }
326
327         return true;
328 }
329
330 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
331 {
332         switch (type) {
333         case TTS_CONFIG_TYPE_ENGINE:
334         {
335                 /* TODO - Determine the policy when engine process get engine changed cb */
336                 if (NULL == str_param) {
337                         SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
338                         return;
339                 }
340
341                 int ret = 0;
342                 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
343                         SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
344                         ret = ttsd_engine_agent_unload_current_engine();
345                         if (0 != ret) {
346                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
347                         }
348
349                         ret = ttsd_engine_agent_load_current_engine();
350                         if (0 != ret) {
351                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
352                         }
353                         return;
354                 }
355
356                 /* stop all player */
357                 ttsd_player_all_stop();
358
359                 /* send interrupt message to  all clients */
360                 ttsd_data_foreach_clients(__get_client_cb, NULL);
361
362                 ttsd_engine_cancel_synthesis();
363
364                 break;
365         }
366
367         case TTS_CONFIG_TYPE_VOICE:
368         {
369                 if (NULL == str_param) {
370                         SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
371                         return;
372                 }
373
374                 char* out_lang = NULL;
375                 int out_type;
376                 int ret = -1;
377
378                 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
379                         SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
380                         ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
381                         if (0 != ret)
382                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
383                 } else {
384                         /* Current language is not available */
385                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
386                 }
387                 if (NULL != out_lang) {
388                         free(out_lang);
389                         out_lang = NULL;
390                 }
391                 break;
392         }
393
394         case TTS_CONFIG_TYPE_SPEED:
395         {
396                 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
397                         /* set default speed */
398                         int ret = 0;
399                         ret = ttsd_engine_agent_set_default_speed(int_param);
400                         if (0 != ret) {
401                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
402                         }
403                 }
404                 break;
405         }
406
407         case TTS_CONFIG_TYPE_PITCH:
408         {
409                 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
410                         /* set default speed */
411                         int ret = 0;
412                         ret = ttsd_engine_agent_set_default_pitch(int_param);
413                         if (0 != ret) {
414                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
415                         }
416                 }
417                 break;
418         }
419
420         case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
421         {
422                 if (0.0 <= double_param && double_param <= 1.0) {
423                         /* set default bg volume ratio */
424                         int ret = 0;
425                         ret = ttsd_player_set_background_volume_ratio(double_param);
426                         if (0 != ret) {
427                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
428                         }
429                 }
430                 break;
431         }
432         }
433
434         return;
435 }
436
437 bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
438 {
439         SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
440         ttsd_server_finalize(uid);
441         return true;
442 }
443
444 Eina_Bool ttsd_terminate_daemon(void *data)
445 {
446         ttsd_data_foreach_clients(__terminate_client, NULL);
447         g_terminate_timer = NULL;
448         return EINA_FALSE;
449 }
450
451 void __screen_reader_changed_cb(bool value)
452 {
453 /*      if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
454                 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
455                 if (g_terminate_timer) {
456                         ecore_timer_del(g_terminate_timer);
457                         g_terminate_timer = NULL;
458                 }
459                 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
460         } else {
461 */
462                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
463 //      }
464         return;
465 }
466
467 int ttsd_send_all_stop()
468 {
469         SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
470
471         /* stop all player */
472         ttsd_player_all_stop();
473
474         /* send interrupt message to  all clients */
475         ttsd_data_foreach_clients(__get_client_cb, NULL);
476
477         return TTSD_ERROR_NONE;
478 }
479
480 /*
481 * Server APIs
482 */
483 int ttsd_initialize(ttse_request_callback_s *callback)
484 {
485         SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
486
487         if (ttsd_config_initialize(__config_changed_cb)) {
488                 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
489         }
490
491         double ratio;
492         if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
493         {
494                 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
495                 int ret = ttsd_player_set_background_volume_ratio(ratio);
496                 if (0 != ret)
497                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
498         }
499         else
500                 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
501
502         /* player init */
503         if (ttsd_player_init()) {
504                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
505                 return TTSD_ERROR_OPERATION_FAILED;
506         }
507
508         /* Engine Agent initialize */
509         if (0 != ttsd_engine_agent_init()) {
510                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
511                 return TTSD_ERROR_OPERATION_FAILED;
512         }
513
514         /* set current engine */
515         //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
516         //      SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
517         //      g_is_engine = false;
518         //} else
519         //      g_is_engine = true;
520
521         if (0 != ttsd_engine_agent_load_current_engine(callback)) {
522                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
523                 return TTSD_ERROR_OPERATION_FAILED;
524         }
525
526         ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
527
528 //      if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
529                 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
530 //      }
531
532         g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
533         if (NULL == g_check_client_timer) {
534                 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
535         }
536
537         return TTSD_ERROR_NONE;
538 }
539
540 int ttsd_finalize()
541 {
542         SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
543
544         GList *iter = NULL;
545         if (0 < g_list_length(g_proc_list)) {
546                 iter = g_list_first(g_proc_list);
547                 while (NULL != iter) {
548                         g_proc_list = g_list_remove_link(g_proc_list, iter);
549                         g_list_free(iter);
550                         iter = g_list_first(g_proc_list);
551                 }
552         }
553
554         ttsd_config_finalize();
555
556         ttsd_player_release();
557
558         ttsd_engine_agent_release();
559
560
561         if (g_wait_timer) {
562                 ecore_timer_del(g_wait_timer);
563                 g_wait_timer = NULL;
564                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
565         }
566
567         if (g_terminate_timer) {
568                 ecore_timer_del(g_terminate_timer);
569                 g_terminate_timer = NULL;
570                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
571         }
572
573         if (g_check_client_timer) {
574                 ecore_timer_del(g_check_client_timer);
575                 g_check_client_timer = NULL;
576                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
577         }
578
579         if (g_quit_loop_timer) {
580                 ecore_timer_del(g_quit_loop_timer);
581                 g_quit_loop_timer = NULL;
582                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
583         }
584
585         return TTSD_ERROR_NONE;
586 }
587
588 int ttsd_terminate()
589 {
590         SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
591
592         ttsd_terminate_daemon(NULL);
593
594         return TTSD_ERROR_NONE;
595 }
596
597 /*
598 * TTS Server Functions for Client
599 */
600
601 int ttsd_server_is_already_initialized(int pid, int uid, bool* is_initialized)
602 {
603         if (-1 != ttsd_data_is_client(uid))
604                 *is_initialized = true;
605         else
606                 *is_initialized = false;
607
608         SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), Uid(%d) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
609         return TTSD_ERROR_NONE;
610 }
611
612 int ttsd_server_initialize(int pid, int uid, tts_ipc_method_e method, bool* credential_needed)
613 {
614         SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
615
616         if (-1 != ttsd_data_is_client(uid)) {
617                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
618                 return TTSD_ERROR_NONE;
619         }
620
621         if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
622                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
623                 return TTSD_ERROR_OPERATION_FAILED;
624         }
625
626         if (true == *credential_needed) {
627                 char* appid = NULL;
628                 if (0 != app_manager_get_app_id(pid, &appid)) {
629                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
630                 }
631                 bool is_agreed = false;
632                 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
633                         SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
634                         if (!appid)
635                                 free(appid);
636                         return TTSD_ERROR_OPERATION_FAILED;
637                 }
638                 if (!appid)
639                         free(appid);
640
641                 if (false == is_agreed) {
642                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
643                         return TTSD_ERROR_PERMISSION_DENIED;
644                 }
645         }
646
647         if (0 != ttsd_data_new_client(pid, uid)) {
648                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
649                 return TTSD_ERROR_OPERATION_FAILED;
650         }
651
652         if (0 != ttsd_data_set_ipc_method(uid, method)) {
653                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
654                 return TTSD_ERROR_OPERATION_FAILED;
655         }
656
657         if (0 != ttsd_player_create_instance(uid)) {
658                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
659                 return TTSD_ERROR_OPERATION_FAILED;
660         }
661
662         g_file_num = 0;
663         g_min_latency = 10000.0;
664         g_max_latency = 0.0;
665         g_avg_latency = 0.0;
666
667
668         return TTSD_ERROR_NONE;
669 }
670
671 static Eina_Bool __quit_ecore_loop(void *data)
672 {
673         ttsd_ipc_close_connection();
674         ttsd_network_finalize();
675         ttsd_finalize();
676         ecore_main_loop_quit();
677
678         return EINA_FALSE;
679 }
680
681
682 static void __read_proc()
683 {
684         DIR *dp = NULL;
685         struct dirent *dirp = NULL;
686         int tmp;
687
688         GList *iter = NULL;
689         if (0 < g_list_length(g_proc_list)) {
690                 iter = g_list_first(g_proc_list);
691                 while (NULL != iter) {
692                         g_proc_list = g_list_remove_link(g_proc_list, iter);
693                         g_list_free(iter);
694                         iter = g_list_first(g_proc_list);
695                 }
696         }
697
698         dp = opendir("/proc");
699         if (NULL == dp) {
700                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
701         } else {
702                 do {
703                         dirp = readdir(dp);
704
705                         if (NULL != dirp) {
706                                 tmp = atoi(dirp->d_name);
707                                 if (0 >= tmp)   continue;
708                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
709                         }
710                 } while (NULL != dirp);
711                 closedir(dp);
712         }
713         return;
714 }
715
716 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
717 {
718         bool exist = false;
719         int i = 0;
720
721         GList *iter = NULL;
722         for (i = 0; i < g_list_length(g_proc_list); i++) {
723                 iter = g_list_nth(g_proc_list, i);
724                 if (NULL != iter) {
725                         if (pid == GPOINTER_TO_INT(iter->data)) {
726                                 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
727                                 exist = true;
728                                 break;
729                         }
730                 }
731         }
732
733         if (false == exist) {
734                 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
735                 ttsd_server_finalize(uid);
736         }
737
738         return true;
739 #if 0
740         char appid[1024] = {0, };
741         if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
742                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
743         }
744
745         if (0 < strlen(appid)) {
746                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
747         } else {
748                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
749
750                 int result = 1;
751                 result = ttsdc_send_hello(pid, uid);
752
753                 if (0 == result) {
754                         SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
755                         ttsd_server_finalize(uid);
756                 } else if (-1 == result) {
757                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
758                 }
759         }
760         return true;
761 #endif
762 }
763
764
765 Eina_Bool ttsd_cleanup_client(void *data)
766 {
767         SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
768         __read_proc();
769
770         if (0 < ttsd_data_get_client_count()) {
771                 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
772         } else {
773                 if (NULL == g_quit_loop_timer) {
774                         SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
775                         g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
776                 }
777                 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
778                 g_check_client_timer = NULL;
779                 return EINA_FALSE;
780         }
781
782         SLOG(LOG_DEBUG, tts_tag(), "@@@");
783
784         return EINA_TRUE;
785 }
786
787 void __used_voice_cb(const char* lang, int type)
788 {
789         SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
790         if (0 != ttsd_engine_unload_voice(lang, type)) {
791                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
792         }
793 }
794
795 int ttsd_server_finalize(int uid)
796 {
797         SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
798         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);
799
800         app_tts_state_e state;
801         if (0 > ttsd_data_get_client_state(uid, &state)) {
802                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
803         }
804
805         ttsd_server_stop(uid);
806         ttsd_player_clear(uid);
807
808         ttsd_player_destroy_instance(uid);
809
810         /* Need to unload voice when used voice is unregistered */
811         if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
812                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
813                 return TTSD_ERROR_OPERATION_FAILED;
814         }
815
816         ttsd_data_delete_client(uid);
817
818         /* unload engine, if ref count of client is 0 */
819         if (0 == ttsd_data_get_client_count()) {
820                 if (NULL == g_quit_loop_timer) {
821                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
822                         g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
823                 } else {
824                         SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
825                 }
826         }
827
828         return TTSD_ERROR_NONE;
829 }
830
831 int ttsd_server_set_mode(int uid, ttsd_mode_e mode)
832 {
833         app_tts_state_e state;
834         if (0 > ttsd_data_get_client_state(uid, &state)) {
835                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
836                 return TTSD_ERROR_INVALID_PARAMETER;
837         }
838
839         if (APP_STATE_READY != state) {
840                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
841                 return TTSD_ERROR_INVALID_STATE;
842         }
843
844         int ret = ttsd_data_set_mode(uid, mode);
845         if (0 != ret) {
846                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
847         } else {
848                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%d), mode(%d)", uid, mode);
849         }
850
851         return ret;
852 }
853
854 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
855 {
856         app_tts_state_e state;
857         if (0 > ttsd_data_get_client_state(uid, &state)) {
858                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
859                 return TTSD_ERROR_INVALID_PARAMETER;
860         }
861
862         /* check valid voice */
863         char* temp_lang = NULL;
864         int temp_type;
865         if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
866                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
867                 if (NULL != temp_lang) {
868                         free(temp_lang);
869                         temp_lang = NULL;
870                 }
871                 return TTSD_ERROR_INVALID_VOICE;
872         }
873
874         if (NULL == temp_lang) {
875                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
876                 return TTSD_ERROR_INVALID_VOICE;
877         }
878
879         speak_data_s* speak_data = NULL;
880         speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
881         if (NULL == speak_data) {
882                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
883                 if (NULL != temp_lang) {
884                         free(temp_lang);
885                         temp_lang = NULL;
886                 }
887                 return TTSD_ERROR_OPERATION_FAILED;
888         }
889
890         speak_data->lang = strdup(lang);
891         speak_data->vctype = voice_type;
892
893         speak_data->speed = speed;
894         speak_data->utt_id = utt_id;
895
896         speak_data->text = strdup(text);
897
898         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);
899
900         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
901         int ret = -1;
902         ret = ttsd_data_add_speak_data(uid, speak_data);
903         if (0 != ret) {
904                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
905                 if (NULL != temp_lang) {
906                         free(temp_lang);
907                         temp_lang = NULL;
908                 }
909                 if (NULL != speak_data) {
910                         if (NULL != speak_data->lang)   free(speak_data->lang);
911                         if (NULL != speak_data->text)   free(speak_data->text);
912
913                         speak_data->lang = NULL;
914                         speak_data->text = NULL;
915
916                         free(speak_data);
917                         speak_data = NULL;
918                 }
919
920                 return ret;
921         }
922
923         if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
924                 /* Request load voice */
925                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
926                 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
927                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
928                 }
929         }
930
931         if (NULL != temp_lang) {
932                 free(temp_lang);
933                 temp_lang = NULL;
934         }
935
936         if (APP_STATE_PLAYING == state) {
937                 /* check if engine use network */
938                 if (ttsd_engine_agent_need_network()) {
939                         if (false == ttsd_network_is_connected()) {
940                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
941                                 return TTSD_ERROR_OPERATION_FAILED;
942                         }
943                 }
944
945                 /* Check whether tts-engine is running or not */
946                 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
947                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
948                 } else {
949                         __synthesis(uid, credential);
950                 }
951         }
952
953         return TTSD_ERROR_NONE;
954 }
955
956 Eina_Bool __send_interrupt_client(void *data)
957 {
958         intptr_t puid = (intptr_t)data;
959         int uid = (int)puid;
960
961         int pid = ttsd_data_get_pid(uid);
962         if (pid <= 0) {
963                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%d), pid(%d)", uid, pid);
964                 return EINA_FALSE;
965         }
966
967         ttsd_mode_e mode = ttsd_data_get_mode(uid);
968         if (TTSD_MODE_DEFAULT != mode) {
969                 /* send message to client about changing state */
970                 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
971         } else {
972                 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
973         }
974
975         return EINA_FALSE;
976 }
977
978 int ttsd_server_play(int uid, const char* credential)
979 {
980         app_tts_state_e state;
981         if (0 > ttsd_data_get_client_state(uid, &state)) {
982                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
983                 return TTSD_ERROR_INVALID_PARAMETER;
984         }
985
986         if (APP_STATE_PLAYING == state) {
987                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
988                 return TTSD_ERROR_NONE;
989         }
990
991         /* check if engine use network */
992         if (ttsd_engine_agent_need_network()) {
993                 if (false == ttsd_network_is_connected()) {
994                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
995                         return TTSD_ERROR_OUT_OF_NETWORK;
996                 }
997         }
998
999         /* check the current playback focus */
1000         if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
1001                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
1002                 ttsd_data_clear_data(uid);
1003                 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1004         }
1005
1006         int current_uid = ttsd_data_get_current_playing();
1007         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1008
1009         if (uid != current_uid && -1 != current_uid) {
1010                 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1011                         /* Send interrupt message */
1012                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1013
1014                         /* pause player */
1015                         if (0 != ttsd_server_stop(current_uid)) {
1016                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
1017                         }
1018
1019                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1020                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1021                 } else {
1022                         /* Default mode policy of interrupt is "Pause" */
1023
1024                         /* Send interrupt message */
1025                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1026
1027                         /* pause player */
1028                         if (0 != ttsd_player_pause(current_uid)) {
1029                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1030                         }
1031
1032                         /* change state */
1033                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1034
1035                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1036                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1037                 }
1038         }
1039
1040         /* Change current play */
1041         if (0 != ttsd_player_wait_to_play(uid)) {
1042                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%d)", uid);
1043         }
1044
1045         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1046                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1047                 return TTSD_ERROR_OPERATION_FAILED;
1048         }
1049
1050         if (APP_STATE_PAUSED == state) {
1051                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1052
1053                 g_is_paused = false;
1054
1055                 /* Resume player */
1056                 if (0 != ttsd_player_resume(uid)) {
1057                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1058                 }
1059         } else {
1060                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%d)", uid);
1061
1062                 if (0 != ttsd_player_play(uid)) {
1063                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_play()");
1064                 }
1065         }
1066
1067         /* Check whether tts-engine is running or not */
1068         clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1069         if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
1070                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1071         } else {
1072                 __synthesis(uid, credential);
1073         }
1074
1075         return TTSD_ERROR_NONE;
1076 }
1077
1078 int ttsd_server_stop(int uid)
1079 {
1080         app_tts_state_e state;
1081         if (0 > ttsd_data_get_client_state(uid, &state)) {
1082                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1083                 return TTSD_ERROR_INVALID_PARAMETER;
1084         }
1085
1086         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1087
1088         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1089                 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
1090                         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1091
1092                         int ret = 0;
1093                         ret = ttsd_engine_cancel_synthesis();
1094                         if (0 != ret)
1095                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1096                 }
1097                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1098
1099                 /* stop player */
1100                 if (0 != ttsd_player_stop(uid)) {
1101                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1102                 }
1103
1104                 ttsd_data_set_client_state(uid, APP_STATE_READY);
1105         }
1106
1107         /* Reset all data */
1108         ttsd_data_clear_data(uid);
1109         g_is_paused = false;
1110
1111         return TTSD_ERROR_NONE;
1112 }
1113
1114 int ttsd_server_pause(int uid, int* utt_id)
1115 {
1116         app_tts_state_e state;
1117         if (0 > ttsd_data_get_client_state(uid, &state)) {
1118                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1119                 return TTSD_ERROR_INVALID_PARAMETER;
1120         }
1121
1122         if (APP_STATE_PLAYING != state) {
1123                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1124                 return TTSD_ERROR_INVALID_STATE;
1125         }
1126
1127         g_is_paused = true;
1128
1129         *utt_id = g_utt.uttid;
1130         SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
1131
1132         int ret = 0;
1133         ret = ttsd_player_pause(uid);
1134         if (0 != ret) {
1135                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1136                 return TTSD_ERROR_OPERATION_FAILED;
1137         }
1138
1139         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1140
1141         return TTSD_ERROR_NONE;
1142 }
1143
1144 int ttsd_server_get_support_voices(int uid, GList** voice_list)
1145 {
1146         app_tts_state_e state;
1147         if (0 > ttsd_data_get_client_state(uid, &state)) {
1148                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1149                 return TTSD_ERROR_INVALID_PARAMETER;
1150         }
1151
1152         /* get voice list*/
1153         int ret = ttsd_engine_get_voice_list(voice_list);
1154         if (0 != ret) {
1155                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1156                 return ret;
1157         }
1158
1159         SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1160
1161         return TTSD_ERROR_NONE;
1162 }
1163
1164 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
1165 {
1166         app_tts_state_e state;
1167         if (0 > ttsd_data_get_client_state(uid, &state)) {
1168                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1169                 return TTSD_ERROR_INVALID_PARAMETER;
1170         }
1171
1172         /* get current voice */
1173         int ret = ttsd_engine_get_default_voice(language, voice_type);
1174         if (0 != ret) {
1175                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1176                 return ret;
1177         }
1178
1179         SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1180
1181         return TTSD_ERROR_NONE;
1182 }
1183
1184 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1185 {
1186         app_tts_state_e state;
1187         if (0 > ttsd_data_get_client_state(uid, &state)) {
1188                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1189                 return TTSD_ERROR_INVALID_PARAMETER;
1190         }
1191
1192         if (APP_STATE_READY != state) {
1193                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1194                 return TTSD_ERROR_INVALID_STATE;
1195         }
1196
1197         int ret = ttsd_engine_set_private_data(key, data);
1198         if (0 != ret) {
1199                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1200         } else {
1201                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1202         }
1203
1204         return ret;
1205 }
1206
1207 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1208 {
1209         app_tts_state_e state;
1210         if (0 > ttsd_data_get_client_state(uid, &state)) {
1211                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1212                 return TTSD_ERROR_INVALID_PARAMETER;
1213         }
1214
1215         if (APP_STATE_READY != state) {
1216                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1217                 return TTSD_ERROR_INVALID_STATE;
1218         }
1219
1220         int ret = ttsd_engine_get_private_data(key, data);
1221         if (0 != ret) {
1222                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1223         } else {
1224                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1225         }
1226
1227         return ret;
1228 }
1229
1230 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1231 {
1232         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1233
1234         int ret = 0;
1235         ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1236         if (0 != ret) {
1237                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1238         }
1239
1240         return ret;
1241 }
1242
1243 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1244 {
1245         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1246
1247         int ret = 0;
1248         ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1249         if (0 != ret) {
1250                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1251         }
1252
1253         return ret;
1254 }
1255
1256 int ttsd_server_play_pcm(int uid)
1257 {
1258         app_tts_state_e state;
1259         if (0 > ttsd_data_get_client_state(uid, &state)) {
1260                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
1261                 return TTSD_ERROR_INVALID_PARAMETER;
1262         }
1263
1264         if (APP_STATE_PLAYING == state) {
1265                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1266                 return TTSD_ERROR_NONE;
1267         }
1268
1269         int current_uid = ttsd_data_get_current_playing();
1270         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1271
1272         if (uid != current_uid && -1 != current_uid) {
1273                 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1274                         /* Send interrupt message */
1275                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1276
1277                         /* pause player */
1278                         if (0 != ttsd_server_stop(current_uid)) {
1279                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
1280                         }
1281
1282                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1283                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1284                 } else {
1285                         /* Default mode policy of interrupt is "Pause" */
1286
1287                         /* Send interrupt message */
1288                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1289
1290                         /* pause player */
1291                         if (0 != ttsd_player_pause(current_uid)) {
1292                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1293                         }
1294
1295                         /* change state */
1296                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1297
1298                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1299                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1300                 }
1301         }
1302
1303         /* Change current play */
1304         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1305                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1306                 return TTSD_ERROR_OPERATION_FAILED;
1307         }
1308
1309         if (APP_STATE_PAUSED == state) {
1310                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1311
1312                 /* Resume player */
1313                 if (0 != ttsd_player_resume(uid)) {
1314                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1315                 }
1316         } else {
1317                 if (0 != ttsd_player_play(uid)) {
1318                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1319
1320                         // Change ready state
1321                         ttsd_server_stop_pcm(uid);
1322
1323                         int tmp_pid = ttsd_data_get_pid(uid);
1324                         if (tmp_pid <= 0) {
1325                                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%d)", uid);
1326                         } else {
1327                                 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1328                         }
1329                 }
1330         }
1331
1332         return TTSD_ERROR_NONE;
1333 }
1334
1335 int ttsd_server_stop_pcm(int uid)
1336 {
1337         app_tts_state_e state;
1338         if (0 > ttsd_data_get_client_state(uid, &state)) {
1339                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1340                 return TTSD_ERROR_INVALID_PARAMETER;
1341         }
1342
1343         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1344
1345         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1346                 ttsd_data_set_client_state(uid, APP_STATE_READY);
1347         }
1348
1349         /* Reset all data */
1350         ttsd_data_clear_data(uid);
1351
1352         ttsd_player_stop(uid);
1353
1354
1355         return TTSD_ERROR_NONE;
1356 }
1357
1358 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1359 {
1360         SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1361
1362         int uttid = -1;
1363
1364         /* Synthesis is success */
1365         if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1366                 if (TTSE_RESULT_EVENT_START == event) {
1367                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1368                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1369                                 uid, uttid, data, data_size, audio_type, rate);
1370                 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1371                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1372                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1373                                 uid, uttid, data, data_size, audio_type, rate);
1374                 } else {
1375                         /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1376                 }
1377
1378                 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1379                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1380                         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1381                         return TTSD_ERROR_INVALID_PARAMETER;
1382                 }
1383
1384                 /* add wav data */
1385                 sound_data_s* temp_sound_data = NULL;
1386                 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1387                 if (NULL == temp_sound_data) {
1388                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1389                         return TTSD_ERROR_OUT_OF_MEMORY;
1390                 }
1391
1392                 temp_sound_data->data = NULL;
1393                 temp_sound_data->rate = 0;
1394                 temp_sound_data->data_size = 0;
1395
1396                 if (0 < data_size) {
1397                         temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1398                         if (NULL != temp_sound_data->data) {
1399                                 memcpy(temp_sound_data->data, data, data_size);
1400                                 temp_sound_data->data_size = data_size;
1401                                 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1402                                         uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1403                         } else {
1404                                 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1405                         }
1406                 } else {
1407                         SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1408                 }
1409
1410                 temp_sound_data->utt_id = uttid;
1411                 temp_sound_data->event = event;
1412                 temp_sound_data->audio_type = audio_type;
1413                 temp_sound_data->rate = rate;
1414
1415                 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1416                         SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1417
1418                         if (NULL != temp_sound_data->data) {
1419                                 free(temp_sound_data->data);
1420                                 temp_sound_data->data = NULL;
1421                         }
1422
1423                         free(temp_sound_data);
1424                         temp_sound_data = NULL;
1425                 }
1426
1427 /*              if (0 != ttsd_player_play(uid)) {
1428                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1429
1430                         // Change ready state
1431                         ttsd_server_stop(uid);
1432
1433                         int tmp_pid;
1434                         tmp_pid = ttsd_data_get_pid(uid);
1435                         ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1436                 }
1437 */
1438         } else {
1439                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1440         }
1441
1442         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1443
1444         return TTSD_ERROR_NONE;
1445 }