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