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