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