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