Merge "Pass tts handle to connect_daemon in prepare_sync function" 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(&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(&speak_data);
127         } else {
128                 ttsd_data_clear_speak_data(&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                                 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
826                         }
827                 }
828         }
829
830         int current_uid = ttsd_data_get_current_playing();
831         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
832
833         if (uid != current_uid && -1 != current_uid) {
834                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
835                         /* Send interrupt message */
836                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
837
838                         /* pause player */
839                         if (0 != ttsd_server_stop(current_uid)) {
840                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
841                         }
842                         if (0 != ttsd_player_stop(current_uid)) {
843                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
844                         }
845
846                         intptr_t pcurrent_uid = (intptr_t)current_uid;
847                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
848                 } else {
849                         /* Default mode policy of interrupt is "Pause" */
850
851                         /* Send interrupt message */
852                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
853
854                         /* pause player */
855                         if (0 != ttsd_player_pause(current_uid)) {
856                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
857                         }
858
859                         /* change state */
860                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
861
862                         intptr_t pcurrent_uid = (intptr_t)current_uid;
863                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
864                 }
865         }
866
867         /* Change current play */
868         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
869                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
870                 return TTSD_ERROR_OPERATION_FAILED;
871         }
872
873         if (APP_STATE_PAUSED == state) {
874                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
875
876                 /* Resume player */
877                 if (0 != ttsd_player_resume(uid)) {
878                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
879                 }
880         }
881
882         /* Check whether tts-engine is running or not */
883         if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
884                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
885         } else {
886                 __synthesis(uid, credential);
887         }
888
889         return TTSD_ERROR_NONE;
890 }
891
892 int ttsd_server_stop(int uid)
893 {
894         app_tts_state_e state;
895         if (0 > ttsd_data_get_client_state(uid, &state)) {
896                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
897                 return TTSD_ERROR_INVALID_PARAMETER;
898         }
899
900         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
901
902         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
903                 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
904                         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
905
906                         int ret = 0;
907                         ret = ttsd_engine_cancel_synthesis();
908                         if (0 != ret)
909                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
910                 }
911
912                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
913
914                 if (0 != ttsd_player_clear(uid))
915                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
916
917                 ttsd_data_set_client_state(uid, APP_STATE_READY);
918         }
919
920         /* Reset all data */
921         ttsd_data_clear_data(uid);
922
923         return TTSD_ERROR_NONE;
924 }
925
926 int ttsd_server_pause(int uid, int* utt_id)
927 {
928         app_tts_state_e state;
929         if (0 > ttsd_data_get_client_state(uid, &state)) {
930                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
931                 return TTSD_ERROR_INVALID_PARAMETER;
932         }
933
934         if (APP_STATE_PLAYING != state) {
935                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
936                 return TTSD_ERROR_INVALID_STATE;
937         }
938
939         *utt_id = g_utt.uttid;
940         SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
941
942         int ret = 0;
943         ret = ttsd_player_pause(uid);
944         if (0 != ret) {
945                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
946                 return TTSD_ERROR_OPERATION_FAILED;
947         }
948
949         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
950
951         return TTSD_ERROR_NONE;
952 }
953
954 int ttsd_server_get_support_voices(int uid, GList** voice_list)
955 {
956         app_tts_state_e state;
957         if (0 > ttsd_data_get_client_state(uid, &state)) {
958                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
959                 return TTSD_ERROR_INVALID_PARAMETER;
960         }
961
962         /* get voice list*/
963         int ret = ttsd_engine_get_voice_list(voice_list);
964         if (0 != ret) {
965                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
966                 return ret;
967         }
968
969         SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
970
971         return TTSD_ERROR_NONE;
972 }
973
974 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
975 {
976         app_tts_state_e state;
977         if (0 > ttsd_data_get_client_state(uid, &state)) {
978                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
979                 return TTSD_ERROR_INVALID_PARAMETER;
980         }
981
982         /* get current voice */
983         int ret = ttsd_engine_get_default_voice(language, voice_type);
984         if (0 != ret) {
985                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
986                 return ret;
987         }
988
989         SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
990
991         return TTSD_ERROR_NONE;
992 }
993
994 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
995 {
996         app_tts_state_e state;
997         if (0 > ttsd_data_get_client_state(uid, &state)) {
998                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
999                 return TTSD_ERROR_INVALID_PARAMETER;
1000         }
1001
1002         if (APP_STATE_READY != state) {
1003                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1004                 return TTSD_ERROR_INVALID_STATE;
1005         }
1006
1007         int ret = ttsd_engine_set_private_data(key, data);
1008         if (0 != ret) {
1009                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1010         } else {
1011                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1012         }
1013
1014         return ret;
1015 }
1016
1017 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1018 {
1019         app_tts_state_e state;
1020         if (0 > ttsd_data_get_client_state(uid, &state)) {
1021                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1022                 return TTSD_ERROR_INVALID_PARAMETER;
1023         }
1024
1025         if (APP_STATE_READY != state) {
1026                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1027                 return TTSD_ERROR_INVALID_STATE;
1028         }
1029
1030         int ret = ttsd_engine_get_private_data(key, data);
1031         if (0 != ret) {
1032                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1033         } else {
1034                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1035         }
1036
1037         return ret;
1038 }
1039
1040 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1041 {
1042         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1043
1044         int ret = 0;
1045         ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1046         if (0 != ret) {
1047                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1048         }
1049
1050         return ret;
1051 }
1052
1053 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1054 {
1055         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1056
1057         int ret = 0;
1058         ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1059         if (0 != ret) {
1060                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1061         }
1062
1063         return ret;
1064 }
1065
1066 int ttsd_server_play_pcm(int uid)
1067 {
1068         app_tts_state_e state;
1069         if (0 > ttsd_data_get_client_state(uid, &state)) {
1070                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
1071                 return TTSD_ERROR_INVALID_PARAMETER;
1072         }
1073
1074         if (APP_STATE_PLAYING == state) {
1075                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1076                 return TTSD_ERROR_NONE;
1077         }
1078
1079         int current_uid = ttsd_data_get_current_playing();
1080         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1081
1082         if (uid != current_uid && -1 != current_uid) {
1083                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1084                         /* Send interrupt message */
1085                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1086
1087                         /* pause player */
1088                         if (0 != ttsd_server_stop(current_uid)) {
1089                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1090                         }
1091                         if (0 != ttsd_player_stop(current_uid)) {
1092                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1093                         }
1094
1095                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1096                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1097                 } else {
1098                         /* Default mode policy of interrupt is "Pause" */
1099
1100                         /* Send interrupt message */
1101                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1102
1103                         /* pause player */
1104                         if (0 != ttsd_player_pause(current_uid)) {
1105                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1106                         }
1107
1108                         /* change state */
1109                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1110
1111                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1112                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1113                 }
1114         }
1115
1116         /* Change current play */
1117         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1118                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1119                 return TTSD_ERROR_OPERATION_FAILED;
1120         }
1121
1122         if (APP_STATE_PAUSED == state) {
1123                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1124
1125                 /* Resume player */
1126                 if (0 != ttsd_player_resume(uid)) {
1127                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1128                 }
1129         } else {
1130                 if (0 != ttsd_player_play_pcm(uid)) {
1131                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1132
1133                         // Change ready state
1134                         ttsd_server_stop_pcm(uid);
1135
1136                         int tmp_pid;
1137                         tmp_pid = ttsd_data_get_pid(uid);
1138                         ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1139                 }
1140         }
1141
1142         return TTSD_ERROR_NONE;
1143 }
1144
1145 int ttsd_server_stop_pcm(int uid)
1146 {
1147         app_tts_state_e state;
1148         if (0 > ttsd_data_get_client_state(uid, &state)) {
1149                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1150                 return TTSD_ERROR_INVALID_PARAMETER;
1151         }
1152
1153         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1154
1155         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1156                 ttsd_data_set_client_state(uid, APP_STATE_READY);
1157         }
1158
1159         /* Reset all data */
1160         ttsd_data_clear_data(uid);
1161
1162         ttsd_player_stop(uid);
1163
1164
1165         return TTSD_ERROR_NONE;
1166 }
1167
1168 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1169 {
1170         SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1171
1172         int uttid = -1;
1173
1174         /* Synthesis is success */
1175         if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1176                 if (TTSE_RESULT_EVENT_START == event) {
1177                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1178                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
1179                                 uid, uttid, data, data_size, audio_type, rate);
1180                 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1181                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1182                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
1183                                 uid, uttid, data, data_size, audio_type, rate);
1184                 } else {
1185                         /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1186                 }
1187
1188                 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1189                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1190                         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1191                         return TTSD_ERROR_INVALID_PARAMETER;
1192                 }
1193
1194                 /* add wav data */
1195                 sound_data_s* temp_sound_data = NULL;
1196                 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1197                 if (NULL == temp_sound_data) {
1198                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1199                         return TTSD_ERROR_OUT_OF_MEMORY;
1200                 }
1201
1202                 temp_sound_data->data = NULL;
1203                 temp_sound_data->rate = 0;
1204                 temp_sound_data->data_size = 0;
1205
1206                 if (0 < data_size) {
1207                         temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1208                         if (NULL != temp_sound_data->data) {
1209                                 memcpy(temp_sound_data->data, data, data_size);
1210                                 temp_sound_data->data_size = data_size;
1211                                 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)", 
1212                                         uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1213                         } else {
1214                                 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1215                         }
1216                 } else {
1217                         SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1218                 }
1219
1220                 temp_sound_data->utt_id = uttid;
1221                 temp_sound_data->event = event;
1222                 temp_sound_data->audio_type = audio_type;
1223                 temp_sound_data->rate = rate;
1224
1225                 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1226                         SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1227
1228                         if (NULL != temp_sound_data->data) {
1229                                 free(temp_sound_data->data);
1230                                 temp_sound_data->data = NULL;
1231                         }
1232
1233                         free(temp_sound_data);
1234                         temp_sound_data = NULL;
1235                 }
1236
1237 /*              if (0 != ttsd_player_play(uid)) {
1238                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1239
1240                         // Change ready state
1241                         ttsd_server_stop(uid);
1242
1243                         int tmp_pid;
1244                         tmp_pid = ttsd_data_get_pid(uid);
1245                         ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1246                 }
1247 */
1248         } else {
1249                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1250         }
1251
1252         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1253
1254         return TTSD_ERROR_NONE;
1255 }