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