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