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