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