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