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