Create play thread if the app state is still playing
[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(uid, &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(uid, &speak_data);
141         } else {
142                 ttsd_data_clear_speak_data(uid, &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 (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, bool* credential_needed)
606 {
607         SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
608
609         if (-1 != ttsd_data_is_client(uid) && false == ttsd_data_is_client_use_tidl(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 (false == ttsd_data_is_client_use_tidl(uid)) {
641                 if (0 != ttsd_data_new_client(pid, uid)) {
642                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
643                         return TTSD_ERROR_OPERATION_FAILED;
644                 }
645         }
646
647         if (0 != ttsd_player_create_instance(uid)) {
648                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
649                 return TTSD_ERROR_OPERATION_FAILED;
650         }
651
652         return TTSD_ERROR_NONE;
653 }
654
655 static Eina_Bool __quit_ecore_loop(void *data)
656 {
657         ttsd_ipc_close_connection();
658         ttsd_network_finalize();
659         ttsd_finalize();
660         ecore_main_loop_quit();
661
662         return EINA_FALSE;
663 }
664
665
666 static void __read_proc()
667 {
668         DIR *dp = NULL;
669         struct dirent *dirp = NULL;
670         int tmp;
671
672         GList *iter = NULL;
673         if (0 < g_list_length(g_proc_list)) {
674                 iter = g_list_first(g_proc_list);
675                 while (NULL != iter) {
676                         g_proc_list = g_list_remove_link(g_proc_list, iter);
677                         g_list_free(iter);
678                         iter = g_list_first(g_proc_list);
679                 }
680         }
681
682         dp = opendir("/proc");
683         if (NULL == dp) {
684                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
685         } else {
686                 do {
687                         dirp = readdir(dp);
688
689                         if (NULL != dirp) {
690                                 tmp = atoi(dirp->d_name);
691                                 if (0 >= tmp)   continue;
692                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
693                         }
694                 } while (NULL != dirp);
695                 closedir(dp);
696         }
697         return;
698 }
699
700 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
701 {
702         bool exist = false;
703         int i = 0;
704
705         GList *iter = NULL;
706         for (i = 0; i < g_list_length(g_proc_list); i++) {
707                 iter = g_list_nth(g_proc_list, i);
708                 if (NULL != iter) {
709                         if (pid == GPOINTER_TO_INT(iter->data)) {
710                                 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
711                                 exist = true;
712                                 break;
713                         }
714                 }
715         }
716
717         if (false == exist) {
718                 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
719                 ttsd_server_finalize(uid);
720         }
721
722         return true;
723 #if 0
724         char appid[1024] = {0, };
725         if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
726                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
727         }
728
729         if (0 < strlen(appid)) {
730                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
731         } else {
732                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
733
734                 int result = 1;
735                 result = ttsdc_send_hello(pid, uid);
736
737                 if (0 == result) {
738                         SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
739                         ttsd_server_finalize(uid);
740                 } else if (-1 == result) {
741                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
742                 }
743         }
744         return true;
745 #endif
746 }
747
748
749 Eina_Bool ttsd_cleanup_client(void *data)
750 {
751         SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
752         __read_proc();
753
754         if (0 < ttsd_data_get_client_count()) {
755                 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
756         } else {
757                 ecore_timer_add(0, __quit_ecore_loop, NULL);
758                 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
759                 g_check_client_timer = NULL;
760                 return EINA_FALSE;
761         }
762
763         SLOG(LOG_DEBUG, tts_tag(), "@@@");
764
765         return EINA_TRUE;
766 }
767
768 void __used_voice_cb(const char* lang, int type)
769 {
770         SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
771         if (0 != ttsd_engine_unload_voice(lang, type)) {
772                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
773         }
774 }
775
776 int ttsd_server_finalize(int uid)
777 {
778         SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
779
780         app_tts_state_e state;
781         if (0 > ttsd_data_get_client_state(uid, &state)) {
782                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
783         }
784
785         ttsd_server_stop(uid);
786         ttsd_player_clear(uid);
787
788         ttsd_player_destroy_instance(uid);
789
790         /* Need to unload voice when used voice is unregistered */
791         if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
792                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
793                 return TTSD_ERROR_OPERATION_FAILED;
794         }
795
796         ttsd_data_delete_client(uid);
797
798         /* unload engine, if ref count of client is 0 */
799         if (0 == ttsd_data_get_client_count()) {
800                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
801                 ecore_timer_add(0, __quit_ecore_loop, NULL);
802         }
803
804         return TTSD_ERROR_NONE;
805 }
806
807 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
808 {
809         app_tts_state_e state;
810         if (0 > ttsd_data_get_client_state(uid, &state)) {
811                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
812                 return TTSD_ERROR_INVALID_PARAMETER;
813         }
814
815         /* check valid voice */
816         char* temp_lang = NULL;
817         int temp_type;
818         if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
819                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
820                 if (NULL != temp_lang) {
821                         free(temp_lang);
822                         temp_lang = NULL;
823                 }
824                 return TTSD_ERROR_INVALID_VOICE;
825         }
826
827         if (NULL == temp_lang) {
828                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
829                 return TTSD_ERROR_INVALID_VOICE;
830         }
831
832         speak_data_s* speak_data = NULL;
833         speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
834         if (NULL == speak_data) {
835                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
836                 if (NULL != temp_lang) {
837                         free(temp_lang);
838                         temp_lang = NULL;
839                 }
840                 return TTSD_ERROR_OPERATION_FAILED;
841         }
842
843         speak_data->lang = strdup(lang);
844         speak_data->vctype = voice_type;
845
846         speak_data->speed = speed;
847         speak_data->utt_id = utt_id;
848
849         speak_data->text = strdup(text);
850
851         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);
852
853         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
854         int ret = -1;
855         ret = ttsd_data_add_speak_data(uid, speak_data);
856         if (0 != ret) {
857                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
858                 if (NULL != temp_lang) {
859                         free(temp_lang);
860                         temp_lang = NULL;
861                 }
862                 if (NULL != speak_data) {
863                         if (NULL != speak_data->lang)   free(speak_data->lang);
864                         if (NULL != speak_data->text)   free(speak_data->text);
865
866                         speak_data->lang = NULL;
867                         speak_data->text = NULL;
868
869                         free(speak_data);
870                         speak_data = NULL;
871                 }
872
873                 return ret;
874         }
875
876         if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
877                 /* Request load voice */
878                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
879                 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
880                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
881                 }
882         }
883
884         if (NULL != temp_lang) {
885                 free(temp_lang);
886                 temp_lang = NULL;
887         }
888
889         if (APP_STATE_PLAYING == state) {
890                 /* check if engine use network */
891                 if (ttsd_engine_agent_need_network()) {
892                         if (false == ttsd_network_is_connected()) {
893                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
894                                 return TTSD_ERROR_OPERATION_FAILED;
895                         }
896                 }
897
898                 /* Check whether tts-engine is running or not */
899                 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
900                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
901                 } else {
902                         __synthesis(uid, credential);
903                 }
904         }
905
906         return TTSD_ERROR_NONE;
907 }
908
909 Eina_Bool __send_interrupt_client(void *data)
910 {
911         intptr_t puid = (intptr_t)data;
912         int uid = (int)puid;
913
914         int pid = ttsd_data_get_pid(uid);
915         if (pid <= 0) {
916                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%d), pid(%d)", uid, pid);
917                 return EINA_FALSE;
918         }
919
920         if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
921                 /* send message to client about changing state */
922                 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
923         } else {
924                 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
925         }
926
927         return EINA_FALSE;
928 }
929
930 int ttsd_server_play(int uid, const char* credential)
931 {
932         app_tts_state_e state;
933         if (0 > ttsd_data_get_client_state(uid, &state)) {
934                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
935                 return TTSD_ERROR_INVALID_PARAMETER;
936         }
937
938         if (APP_STATE_PLAYING == state) {
939                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
940                 return TTSD_ERROR_NONE;
941         }
942
943         /* check if engine use network */
944         if (ttsd_engine_agent_need_network()) {
945                 if (false == ttsd_network_is_connected()) {
946                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
947                         return TTSD_ERROR_OUT_OF_NETWORK;
948                 }
949         }
950
951         /* check the current playback focus */
952         if (TTSD_MODE_INTERRUPT != ttsd_get_mode()) {
953                 bool is_current_interrupt = false;
954                 if (0 != ttsd_player_check_current_playback_focus(&is_current_interrupt)) {
955                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to check the current playback focus");
956                 } else {
957                         if (true == is_current_interrupt) {
958                                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
959                                 ttsd_data_clear_data(uid);
960                                 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
961                         }
962                 }
963         }
964
965         int current_uid = ttsd_data_get_current_playing();
966         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
967
968         if (uid != current_uid && -1 != current_uid) {
969                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
970                         /* Send interrupt message */
971                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
972
973                         /* pause player */
974                         if (0 != ttsd_server_stop(current_uid)) {
975                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
976                         }
977
978                         intptr_t pcurrent_uid = (intptr_t)current_uid;
979                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
980                 } else {
981                         /* Default mode policy of interrupt is "Pause" */
982
983                         /* Send interrupt message */
984                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
985
986                         /* pause player */
987                         if (0 != ttsd_player_pause(current_uid)) {
988                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
989                         }
990
991                         /* change state */
992                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
993
994                         intptr_t pcurrent_uid = (intptr_t)current_uid;
995                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
996                 }
997         }
998
999         /* Change current play */
1000         if (0 != ttsd_player_wait_to_play(uid)) {
1001                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%d)", uid);
1002         }
1003
1004         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1005                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1006                 return TTSD_ERROR_OPERATION_FAILED;
1007         }
1008
1009         if (APP_STATE_PAUSED == state) {
1010                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1011
1012                 g_is_paused = false;
1013
1014                 /* Resume player */
1015                 if (0 != ttsd_player_resume(uid)) {
1016                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1017                 }
1018         }
1019
1020         /* Check whether tts-engine is running or not */
1021         if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
1022                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1023         } else {
1024                 __synthesis(uid, credential);
1025         }
1026
1027         return TTSD_ERROR_NONE;
1028 }
1029
1030 int ttsd_server_stop(int uid)
1031 {
1032         app_tts_state_e state;
1033         if (0 > ttsd_data_get_client_state(uid, &state)) {
1034                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1035                 return TTSD_ERROR_INVALID_PARAMETER;
1036         }
1037
1038         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1039
1040         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1041                 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
1042                         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1043
1044                         int ret = 0;
1045                         ret = ttsd_engine_cancel_synthesis();
1046                         if (0 != ret)
1047                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1048                 }
1049                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1050
1051                 /* stop player */
1052                 if (0 != ttsd_player_stop(uid)) {
1053                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1054                 }
1055
1056                 ttsd_data_set_client_state(uid, APP_STATE_READY);
1057         }
1058
1059         /* Reset all data */
1060         ttsd_data_clear_data(uid);
1061         g_is_paused = false;
1062
1063         return TTSD_ERROR_NONE;
1064 }
1065
1066 int ttsd_server_pause(int uid, int* utt_id)
1067 {
1068         app_tts_state_e state;
1069         if (0 > ttsd_data_get_client_state(uid, &state)) {
1070                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1071                 return TTSD_ERROR_INVALID_PARAMETER;
1072         }
1073
1074         if (APP_STATE_PLAYING != state) {
1075                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1076                 return TTSD_ERROR_INVALID_STATE;
1077         }
1078
1079         g_is_paused = true;
1080
1081         *utt_id = g_utt.uttid;
1082         SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
1083
1084         int ret = 0;
1085         ret = ttsd_player_pause(uid);
1086         if (0 != ret) {
1087                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1088                 return TTSD_ERROR_OPERATION_FAILED;
1089         }
1090
1091         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1092
1093         return TTSD_ERROR_NONE;
1094 }
1095
1096 int ttsd_server_get_support_voices(int uid, GList** voice_list)
1097 {
1098         app_tts_state_e state;
1099         if (0 > ttsd_data_get_client_state(uid, &state)) {
1100                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1101                 return TTSD_ERROR_INVALID_PARAMETER;
1102         }
1103
1104         /* get voice list*/
1105         int ret = ttsd_engine_get_voice_list(voice_list);
1106         if (0 != ret) {
1107                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1108                 return ret;
1109         }
1110
1111         SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1112
1113         return TTSD_ERROR_NONE;
1114 }
1115
1116 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
1117 {
1118         app_tts_state_e state;
1119         if (0 > ttsd_data_get_client_state(uid, &state)) {
1120                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1121                 return TTSD_ERROR_INVALID_PARAMETER;
1122         }
1123
1124         /* get current voice */
1125         int ret = ttsd_engine_get_default_voice(language, voice_type);
1126         if (0 != ret) {
1127                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1128                 return ret;
1129         }
1130
1131         SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1132
1133         return TTSD_ERROR_NONE;
1134 }
1135
1136 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1137 {
1138         app_tts_state_e state;
1139         if (0 > ttsd_data_get_client_state(uid, &state)) {
1140                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1141                 return TTSD_ERROR_INVALID_PARAMETER;
1142         }
1143
1144         if (APP_STATE_READY != state) {
1145                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1146                 return TTSD_ERROR_INVALID_STATE;
1147         }
1148
1149         int ret = ttsd_engine_set_private_data(key, data);
1150         if (0 != ret) {
1151                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1152         } else {
1153                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1154         }
1155
1156         return ret;
1157 }
1158
1159 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1160 {
1161         app_tts_state_e state;
1162         if (0 > ttsd_data_get_client_state(uid, &state)) {
1163                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1164                 return TTSD_ERROR_INVALID_PARAMETER;
1165         }
1166
1167         if (APP_STATE_READY != state) {
1168                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1169                 return TTSD_ERROR_INVALID_STATE;
1170         }
1171
1172         int ret = ttsd_engine_get_private_data(key, data);
1173         if (0 != ret) {
1174                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1175         } else {
1176                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1177         }
1178
1179         return ret;
1180 }
1181
1182 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1183 {
1184         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1185
1186         int ret = 0;
1187         ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1188         if (0 != ret) {
1189                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1190         }
1191
1192         return ret;
1193 }
1194
1195 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1196 {
1197         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1198
1199         int ret = 0;
1200         ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1201         if (0 != ret) {
1202                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1203         }
1204
1205         return ret;
1206 }
1207
1208 int ttsd_server_play_pcm(int uid)
1209 {
1210         app_tts_state_e state;
1211         if (0 > ttsd_data_get_client_state(uid, &state)) {
1212                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
1213                 return TTSD_ERROR_INVALID_PARAMETER;
1214         }
1215
1216         if (APP_STATE_PLAYING == state) {
1217                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1218                 return TTSD_ERROR_NONE;
1219         }
1220
1221         int current_uid = ttsd_data_get_current_playing();
1222         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1223
1224         if (uid != current_uid && -1 != current_uid) {
1225                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1226                         /* Send interrupt message */
1227                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1228
1229                         /* pause player */
1230                         if (0 != ttsd_server_stop(current_uid)) {
1231                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
1232                         }
1233
1234                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1235                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1236                 } else {
1237                         /* Default mode policy of interrupt is "Pause" */
1238
1239                         /* Send interrupt message */
1240                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1241
1242                         /* pause player */
1243                         if (0 != ttsd_player_pause(current_uid)) {
1244                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1245                         }
1246
1247                         /* change state */
1248                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1249
1250                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1251                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1252                 }
1253         }
1254
1255         /* Change current play */
1256         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1257                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1258                 return TTSD_ERROR_OPERATION_FAILED;
1259         }
1260
1261         if (APP_STATE_PAUSED == state) {
1262                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1263
1264                 /* Resume player */
1265                 if (0 != ttsd_player_resume(uid)) {
1266                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1267                 }
1268         } else {
1269                 if (0 != ttsd_player_play_pcm(uid)) {
1270                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1271
1272                         // Change ready state
1273                         ttsd_server_stop_pcm(uid);
1274
1275                         int tmp_pid = ttsd_data_get_pid(uid);
1276                         if (tmp_pid <= 0) {
1277                                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%d)", uid);
1278                         } else {
1279                                 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1280                         }
1281                 }
1282         }
1283
1284         return TTSD_ERROR_NONE;
1285 }
1286
1287 int ttsd_server_stop_pcm(int uid)
1288 {
1289         app_tts_state_e state;
1290         if (0 > ttsd_data_get_client_state(uid, &state)) {
1291                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1292                 return TTSD_ERROR_INVALID_PARAMETER;
1293         }
1294
1295         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1296
1297         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1298                 ttsd_data_set_client_state(uid, APP_STATE_READY);
1299         }
1300
1301         /* Reset all data */
1302         ttsd_data_clear_data(uid);
1303
1304         ttsd_player_stop(uid);
1305
1306
1307         return TTSD_ERROR_NONE;
1308 }
1309
1310 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1311 {
1312         SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1313
1314         int uttid = -1;
1315
1316         /* Synthesis is success */
1317         if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1318                 if (TTSE_RESULT_EVENT_START == event) {
1319                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1320                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1321                                 uid, uttid, data, data_size, audio_type, rate);
1322                 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1323                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1324                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1325                                 uid, uttid, data, data_size, audio_type, rate);
1326                 } else {
1327                         /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1328                 }
1329
1330                 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1331                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1332                         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1333                         return TTSD_ERROR_INVALID_PARAMETER;
1334                 }
1335
1336                 /* add wav data */
1337                 sound_data_s* temp_sound_data = NULL;
1338                 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1339                 if (NULL == temp_sound_data) {
1340                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1341                         return TTSD_ERROR_OUT_OF_MEMORY;
1342                 }
1343
1344                 temp_sound_data->data = NULL;
1345                 temp_sound_data->rate = 0;
1346                 temp_sound_data->data_size = 0;
1347
1348                 if (0 < data_size) {
1349                         temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1350                         if (NULL != temp_sound_data->data) {
1351                                 memcpy(temp_sound_data->data, data, data_size);
1352                                 temp_sound_data->data_size = data_size;
1353                                 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1354                                         uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1355                         } else {
1356                                 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1357                         }
1358                 } else {
1359                         SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1360                 }
1361
1362                 temp_sound_data->utt_id = uttid;
1363                 temp_sound_data->event = event;
1364                 temp_sound_data->audio_type = audio_type;
1365                 temp_sound_data->rate = rate;
1366
1367                 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1368                         SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1369
1370                         if (NULL != temp_sound_data->data) {
1371                                 free(temp_sound_data->data);
1372                                 temp_sound_data->data = NULL;
1373                         }
1374
1375                         free(temp_sound_data);
1376                         temp_sound_data = NULL;
1377                 }
1378
1379 /*              if (0 != ttsd_player_play(uid)) {
1380                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1381
1382                         // Change ready state
1383                         ttsd_server_stop(uid);
1384
1385                         int tmp_pid;
1386                         tmp_pid = ttsd_data_get_pid(uid);
1387                         ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1388                 }
1389 */
1390         } else {
1391                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1392         }
1393
1394         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1395
1396         return TTSD_ERROR_NONE;
1397 }