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