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