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