Merge "Remove unnecessary logs and files" into tizen
[platform/core/uifw/stt.git] / server / sttd_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 <pthread.h>
15 #include <sound_manager.h>
16 #include <wav_player.h>
17
18 #include "stt_network.h"
19 #include "sttd_client_data.h"
20 #include "sttd_config.h"
21 #include "sttd_dbus.h"
22 #include "sttd_engine_agent.h"
23 #include "sttd_main.h"
24 #include "sttd_recorder.h"
25 #include "sttd_server.h"
26
27 static pthread_mutex_t stte_result_mutex = PTHREAD_MUTEX_INITIALIZER;
28 static pthread_mutex_t stte_result_time_mutex = PTHREAD_MUTEX_INITIALIZER;
29
30
31 /*
32 * STT Server static variable
33 */
34 static double g_processing_timeout = 30;
35
36 static double g_recording_timeout = 60;
37
38 Ecore_Timer*    g_recording_timer = NULL;
39 Ecore_Timer*    g_processing_timer = NULL;
40
41 static int g_recording_log_count = 0;
42
43 static GList *g_proc_list = NULL;
44
45 /*
46 * STT Server Callback Functions
47 */
48 void __stop_by_silence(void *data)
49 {
50         SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by silence detection");
51
52         int uid = 0;
53
54         uid = stt_client_get_current_recognition();
55
56         int ret;
57         if (0 != uid) {
58                 ret = sttd_server_stop(uid);
59                 if (0 > ret) {
60                         return;
61                 }
62
63                 if (STTD_RESULT_STATE_DONE == ret) {
64                         ret = sttdc_send_set_state(uid, (int)APP_STATE_PROCESSING);
65                         if (0 != ret) {
66                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send state : result(%d)", ret);
67
68                                 /* Remove client */
69                                 sttd_server_finalize(uid);
70                                 stt_client_unset_current_recognition();
71                         }
72                 }
73         } else {
74                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
75         }
76
77         SLOG(LOG_DEBUG, TAG_STTD, "=====");
78         SLOG(LOG_DEBUG, TAG_STTD, "  ");
79
80         return;
81 }
82
83 static void __cancel_recognition_internal()
84 {
85         if (NULL != g_recording_timer)  {
86                 ecore_timer_del(g_recording_timer);
87                 g_recording_timer = NULL;
88         }
89
90         int ret = 0;
91         int uid = 0;
92         uid = stt_client_get_current_recognition();
93
94         app_state_e state = 0;
95         ret = sttd_client_get_state(uid, &state);
96
97         if (0 != ret) {
98                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
99                 return;
100         }
101
102         if (0 != uid && (APP_STATE_PROCESSING == state || APP_STATE_RECORDING == state)) {
103                 /* cancel engine recognition */
104                 ret = sttd_server_cancel(uid);
105                 if (0 != ret) {
106                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
107                 }
108         } else {
109                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
110         }
111 }
112
113 static void __cancel_by_error(void *data)
114 {
115         SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by error");
116
117         __cancel_recognition_internal();
118
119         SLOG(LOG_DEBUG, TAG_STTD, "=====");
120         SLOG(LOG_DEBUG, TAG_STTD, "  ");
121
122         return;
123 }
124
125 int __server_audio_recorder_callback(const void* data, const unsigned int length)
126 {
127         int uid = -1;
128         int ret;
129
130         if (NULL == data || 0 == length) {
131                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Recording data is not valid");
132                 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
133                 return -1;
134         }
135
136         uid = stt_client_get_current_recognition();
137         if (0 != uid) {
138                 ret = sttd_engine_agent_set_recording_data(data, length);
139                 if (ret < 0) {
140                         ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
141                         return -1;
142                 }
143                 g_recording_log_count++;
144                 if (200 <= g_recording_log_count) {
145                         SLOG(LOG_DEBUG, TAG_STTD, "=== Set recording data ===");
146                         g_recording_log_count = 0;
147                 }
148         } else {
149                 if (NULL != g_recording_timer)  {
150                         ecore_timer_del(g_recording_timer);
151                         g_recording_timer = NULL;
152                 }
153                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current uid in recording is is not valid");
154                 return -1;
155         }
156
157         return 0;
158 }
159
160 void __server_audio_interrupt_callback()
161 {
162         SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by sound interrupt");
163
164         __cancel_recognition_internal();
165
166         SLOG(LOG_DEBUG, TAG_STTD, "=====");
167         SLOG(LOG_DEBUG, TAG_STTD, "  ");
168 }
169
170 void __cancel_by_no_record(void *data)
171 {
172         SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by no record");
173
174         __cancel_recognition_internal();
175
176         SLOG(LOG_DEBUG, TAG_STTD, "=====");
177         SLOG(LOG_DEBUG, TAG_STTD, "  ");
178
179         return;
180 }
181
182 int __server_recognition_result_callback(stte_result_event_e event, const char* type,
183                                         const char** data, int data_count, const char* msg, void *user_data)
184 {
185         // critical section
186         pthread_mutex_lock(&stte_result_mutex);
187
188         SLOG(LOG_DEBUG, TAG_STTD, "===== RESULT event[%d] type[%s] data[%p] data_count[%d]", event, type, data, data_count);
189
190         /* check uid */
191         int uid = stt_client_get_current_recognition();
192
193         app_state_e state;
194         if (0 == uid || 0 != sttd_client_get_state(uid, &state)) {
195                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
196                 SLOG(LOG_DEBUG, TAG_STTD, "=====");
197                 SLOG(LOG_DEBUG, TAG_STTD, "  ");
198                 pthread_mutex_unlock(&stte_result_mutex);
199                 return STTD_ERROR_OPERATION_FAILED;
200         }
201
202         SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid (%d), event(%d)", uid, event);
203
204         /* send result to client */
205         if (STTE_RESULT_EVENT_FINAL_RESULT == event) {
206                 if (APP_STATE_PROCESSING != state) {
207                         SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is NOT 'Processing'.");
208                 }
209                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] the size of result from engine is '%d'", data_count);
210
211                 /* Delete timer for processing time out */
212                 if (NULL != g_processing_timer) {
213                         ecore_timer_del(g_processing_timer);
214                         g_processing_timer = NULL;
215                 }
216
217                 sttd_config_time_save();
218                 sttd_config_time_reset();
219
220                 sttd_client_set_state(uid, APP_STATE_READY);
221                 stt_client_unset_current_recognition();
222
223                 if (NULL == data || 0 == data_count) {
224                         if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
225                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
226                                 int reason = (int)STTD_ERROR_OPERATION_FAILED;
227
228                                 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
229                                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
230                                 }
231                         }
232                 } else {
233                         if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
234                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
235                                 int reason = (int)STTD_ERROR_OPERATION_FAILED;
236
237                                 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
238                                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
239                                 }
240                         }
241                 }
242
243                 /* change state of uid */
244 //              sttd_client_set_state(uid, APP_STATE_READY);
245 //              stt_client_unset_current_recognition();
246
247         } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) {
248                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event,  data_count);
249
250                 sttd_config_time_save();
251                 sttd_config_time_reset();
252
253                 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
254                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
255                         int reason = (int)STTD_ERROR_OPERATION_FAILED;
256
257                         if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
258                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
259                         }
260                 }
261
262         } else if (STTE_RESULT_EVENT_ERROR == event) {
263                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
264
265                 /* Delete timer for processing time out */
266                 if (NULL != g_processing_timer) {
267                         ecore_timer_del(g_processing_timer);
268                         g_processing_timer = NULL;
269                 }
270                 sttd_config_time_reset();
271
272                 int ret = 0;
273                 if (APP_STATE_RECORDING == state) {
274                         ret = sttd_engine_agent_recognize_cancel();
275                         if (0 != ret) {
276                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel: result(%d)", ret);
277                         }
278                 }
279
280                 sttd_client_set_state(uid, APP_STATE_READY);
281                 stt_client_unset_current_recognition();
282
283                 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
284                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
285
286                         /* send error msg */
287                         int reason = (int)STTD_ERROR_INVALID_STATE;
288                         if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
289                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
290                         }
291                 }
292
293                 /* change state of uid */
294 //              sttd_client_set_state(uid, APP_STATE_READY);
295 //              stt_client_unset_current_recognition();
296         } else {
297                 /* nothing */
298         }
299
300         SLOG(LOG_DEBUG, TAG_STTD, "=====");
301         SLOG(LOG_DEBUG, TAG_STTD, "  ");
302         pthread_mutex_unlock(&stte_result_mutex);
303
304         return STTD_ERROR_NONE;
305 }
306
307 bool __server_result_time_callback(int index, stte_result_time_event_e event, const char* text, long start_time, long end_time, void* user_data)
308 {
309         pthread_mutex_lock(&stte_result_time_mutex);
310
311         SLOG(LOG_DEBUG, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)",
312                 index, event, text, start_time, end_time);
313
314         int ret;
315         ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
316         if (0 != ret) {
317                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
318                 pthread_mutex_unlock(&stte_result_time_mutex);
319                 return false;
320         }
321
322         pthread_mutex_unlock(&stte_result_time_mutex);
323
324         return true;
325 }
326
327 int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
328 {
329         SLOG(LOG_DEBUG, TAG_STTD, "===== Speech status detected Callback");
330
331         int uid = stt_client_get_current_recognition();
332         if (0 != uid) {
333                 app_state_e state;
334                 if (0 != sttd_client_get_state(uid, &state)) {
335                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid ");
336                         return STTD_ERROR_OPERATION_FAILED;
337                 }
338
339                 if (APP_STATE_RECORDING != state) {
340                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
341                         return STTD_ERROR_INVALID_STATE;
342                 }
343
344                 if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) {
345                         SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected");
346                         sttdc_send_speech_status(uid, status);
347                 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
348                         SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
349                         ecore_main_loop_thread_safe_call_async(__stop_by_silence, NULL);
350                 }
351         } else {
352                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recogntion uid is not valid ");
353         }
354
355         SLOG(LOG_DEBUG, TAG_STTD, "=====");
356         SLOG(LOG_DEBUG, TAG_STTD, "  ");
357
358         return STTD_ERROR_NONE;
359 }
360
361 int __server_error_callback(stte_error_e error, const char* msg)
362 {
363         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Error Callback is called");
364         ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
365
366         return STTD_ERROR_NONE;
367 }
368
369 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
370 {
371         if (NULL == engine_id) {
372                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
373                 return;
374         } else {
375                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] New default engine : %s", engine_id);
376         }
377
378         /* need to change state of app to ready */
379         int uid;
380         uid = stt_client_get_current_recognition();
381
382         if (0 != uid) {
383                 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%d)", uid);
384
385                 sttd_server_cancel(uid);
386                 sttdc_send_set_state(uid, (int)APP_STATE_READY);
387
388                 stt_client_unset_current_recognition();
389         }
390
391         /* set engine */
392         int ret = sttd_engine_agent_set_default_engine(engine_id);
393         if (0 != ret)
394                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
395
396         if (NULL != language) {
397                 ret = sttd_engine_agent_set_default_language(language);
398                 if (0 != ret)
399                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
400         }
401
402         ret = sttd_engine_agent_set_silence_detection(support_silence);
403         if (0 != ret)
404                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
405
406         return;
407 }
408
409 void __sttd_server_language_changed_cb(const char* language, void* user_data)
410 {
411         if (NULL == language) {
412                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
413                 return;
414         } else {
415                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get language changed : %s", language);
416         }
417
418         int ret = sttd_engine_agent_set_default_language(language);
419         if (0 != ret)
420                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
421
422         return;
423 }
424
425 void __sttd_server_silence_changed_cb(bool value, void* user_data)
426 {
427         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
428
429         int ret = 0;
430         ret = sttd_engine_agent_set_silence_detection(value);
431         if (0 != ret)
432                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
433
434         return;
435 }
436
437 /*
438 * Daemon function
439 */
440 int sttd_initialize(stte_request_callback_s *callback)
441 {
442         int ret = 0;
443
444         if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
445                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
446         }
447
448         if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) {
449                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex.");
450         }
451
452         if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
453                 __sttd_server_silence_changed_cb, NULL)) {
454                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
455         }
456
457         ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
458         if (0 != ret) {
459                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
460                 return ret;
461         }
462
463         /* Engine Agent initialize */
464         ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
465                 __server_speech_status_callback, __server_error_callback);
466         if (0 != ret) {
467                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
468                 return ret;
469         }
470
471         /* load engine */
472         ret = sttd_engine_agent_load_current_engine(callback);
473         if (0 != ret) {
474                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
475                 return ret;
476         }
477
478         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] initialize");
479
480         return 0;
481 }
482
483 int sttd_finalize()
484 {
485         if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
486                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
487         }
488
489         if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) {
490                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex.");
491         }
492
493         GList *iter = NULL;
494         if (0 < g_list_length(g_proc_list)) {
495                 iter = g_list_first(g_proc_list);
496                 while (NULL != iter) {
497                         g_proc_list = g_list_remove_link(g_proc_list, iter);
498                         iter = g_list_first(g_proc_list);
499                 }
500         }
501
502         sttd_recorder_deinitialize();
503
504         sttd_config_finalize();
505
506         sttd_engine_agent_release();
507
508         return STTD_ERROR_NONE;
509 }
510
511 static void __read_proc()
512 {
513         DIR *dp = NULL;
514         struct dirent *dirp = NULL;
515         int tmp;
516
517         GList *iter = NULL;
518         if (0 < g_list_length(g_proc_list)) {
519                 iter = g_list_first(g_proc_list);
520                 while (NULL != iter) {
521                         g_proc_list = g_list_remove_link(g_proc_list, iter);
522                         iter = g_list_first(g_proc_list);
523                 }
524         }
525
526         dp = opendir("/proc");
527         if (NULL == dp) {
528                 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
529         } else {
530                 do {
531                         dirp = readdir(dp);
532
533                         if (NULL != dirp) {
534                                 tmp = atoi(dirp->d_name);
535                                 if (0 >= tmp)   continue;
536                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
537                         }
538                 } while (NULL != dirp);
539                 closedir(dp);
540         }
541         return;
542 }
543
544 Eina_Bool sttd_cleanup_client(void *data)
545 {
546         int* client_list = NULL;
547         int client_count = 0;
548         int i = 0;
549         int j = 0;
550         bool exist = false;
551
552         if (0 != sttd_client_get_list(&client_list, &client_count)) {
553                 if (NULL != client_list)
554                         free(client_list);
555
556                 return EINA_TRUE;
557         }
558
559         if (NULL != client_list) {
560                 SLOG(LOG_DEBUG, TAG_STTD, "===== Clean up client ");
561
562                 __read_proc();
563
564                 for (i = 0; i < client_count; i++) {
565                         int pid = sttd_client_get_pid(client_list[i]);
566                         if (0 > pid) {
567                                 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
568                                 continue;
569                         }
570
571                         exist = false;
572                         GList *iter = NULL;
573                         for (j = 0; j < g_list_length(g_proc_list); j++) {
574                                 iter = g_list_nth(g_proc_list, j);
575                                 if (NULL != iter) {
576                                         if (pid == GPOINTER_TO_INT(iter->data)) {
577                                                 SLOG(LOG_DEBUG, TAG_STTD, "uid (%d) is running", client_list[i]);
578                                                 exist = true;
579                                                 break;
580                                         }
581                                 }
582                         }
583
584                         if (false == exist) {
585                                 SLOG(LOG_ERROR, TAG_STTD, "uid (%d) should be removed", client_list[i]);
586                                 sttd_server_finalize(client_list[i]);
587                         }
588 #if 0
589                         result = sttdc_send_hello(client_list[i]);
590
591                         if (0 == result) {
592                                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", client_list[i]);
593                                 sttd_server_finalize(client_list[i]);
594                         } else if (-1 == result) {
595                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
596                         }
597 #endif
598                 }
599
600                 SLOG(LOG_DEBUG, TAG_STTD, "=====");
601                 SLOG(LOG_DEBUG, TAG_STTD, "  ");
602
603                 free(client_list);
604         }
605
606         return EINA_TRUE;
607 }
608
609 /*
610 * STT Server Functions for Client
611 */
612
613 int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential)
614 {
615         int ret = STTD_ERROR_NONE;
616
617         /* check if uid is valid */
618         app_state_e state;
619         if (0 == sttd_client_get_state(uid, &state)) {
620                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
621                 return STTD_ERROR_NONE;
622         }
623
624         ret = sttd_engine_agent_get_option_supported(silence);
625         if (0 != ret) {
626                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
627                 return ret;
628         }
629
630         ret = sttd_engine_agent_is_credential_needed(uid, credential);
631         if (0 != ret) {
632                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
633                 return ret;
634         }
635
636         /* Add client information to client manager */
637         ret = sttd_client_add(pid, uid);
638         if (0 != ret) {
639                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
640                 return ret;
641         }
642
643         return STTD_ERROR_NONE;
644 }
645
646 static Eina_Bool __quit_ecore_loop(void *data)
647 {
648         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Quit");
649
650         stt_network_finalize();
651         sttd_finalize();
652         sttd_dbus_close_connection();
653         ecore_main_loop_quit();
654
655         SLOG(LOG_DEBUG, TAG_STTD, "");
656
657         return EINA_FALSE;
658 }
659
660 int sttd_server_finalize(int uid)
661 {
662         /* check if uid is valid */
663         app_state_e state;
664         if (0 != sttd_client_get_state(uid, &state)) {
665                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
666                 return STTD_ERROR_INVALID_PARAMETER;
667         }
668
669         /* release recorder */
670         if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
671                 if (APP_STATE_RECORDING == state) {
672                         if (NULL != g_recording_timer)  {
673                                 ecore_timer_del(g_recording_timer);
674                                 g_recording_timer = NULL;
675                         }
676                 }
677
678                 if (APP_STATE_PROCESSING == state) {
679                         if (NULL != g_processing_timer) {
680                                 ecore_timer_del(g_processing_timer);
681                                 g_processing_timer = NULL;
682                         }
683                 }
684
685                 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
686                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
687                 }
688
689                 stt_client_unset_current_recognition();
690         }
691
692         /* Remove client information */
693         if (0 != sttd_client_delete(uid)) {
694                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
695         }
696
697         /* unload engine, if ref count of client is 0 */
698         if (0 == sttd_client_get_ref_count()) {
699 //              sttd_dbus_close_connection();
700                 ecore_timer_add(0, __quit_ecore_loop, NULL);
701         }
702
703         return STTD_ERROR_NONE;
704 }
705
706 int sttd_server_get_supported_engines(int uid, GSList** engine_list)
707 {
708         /* Check if uid is valid */
709         app_state_e state;
710         if (0 != sttd_client_get_state(uid, &state)) {
711                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
712                 return STTD_ERROR_INVALID_PARAMETER;
713         }
714
715         /* Check state of uid */
716         if (APP_STATE_READY != state) {
717                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
718                 return STTD_ERROR_INVALID_STATE;
719         }
720
721         int ret;
722         ret = sttd_engine_agent_get_engine_list(engine_list);
723         if (0 != ret) {
724                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
725                 return ret;
726         }
727
728         return STTD_ERROR_NONE;
729 }
730
731 int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence, bool* credential)
732 {
733         /* Check if uid is valid */
734         app_state_e state;
735         if (0 != sttd_client_get_state(uid, &state)) {
736                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
737                 return STTD_ERROR_INVALID_PARAMETER;
738         }
739
740         /* Check state of uid */
741         if (APP_STATE_READY != state) {
742                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
743                 return STTD_ERROR_INVALID_STATE;
744         }
745
746         int ret;
747         ret = sttd_engine_agent_load_current_engine(NULL);
748         if (0 != ret) {
749                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
750                 return ret;
751         }
752
753         ret = sttd_engine_agent_get_option_supported(silence);
754         if (0 != ret) {
755                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
756                 return ret;
757         }
758
759         if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
760                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
761                 return ret;
762         }
763
764         return STTD_ERROR_NONE;
765 }
766
767 int sttd_server_get_current_engine(int uid, char** engine_id)
768 {
769         /* Check if uid is valid */
770         app_state_e state;
771         if (0 != sttd_client_get_state(uid, &state)) {
772                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
773                 return STTD_ERROR_INVALID_PARAMETER;
774         }
775
776         /* Check state of uid */
777         if (APP_STATE_READY != state) {
778                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
779                 return STTD_ERROR_INVALID_STATE;
780         }
781
782         int ret;
783         ret = sttd_engine_agent_get_current_engine(engine_id);
784         if (0 != ret) {
785                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
786                 return ret;
787         }
788
789         return STTD_ERROR_NONE;
790 }
791
792 int sttd_server_check_app_agreed(int uid, const char* appid, bool* available)
793 {
794         /* Check if uid is valid */
795         app_state_e state;
796         if (0 != sttd_client_get_state(uid, &state)) {
797                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
798                 return STTD_ERROR_INVALID_PARAMETER;
799         }
800
801         /* Check state of uid */
802         if (APP_STATE_READY != state) {
803                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
804                 return STTD_ERROR_INVALID_STATE;
805         }
806
807         /* Ask engine available */
808         int ret;
809         bool temp = false;
810         ret = sttd_engine_agent_check_app_agreed(appid, &temp);
811         if (0 != ret) {
812                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
813                 return ret;
814         }
815
816         if (true == temp) {
817                 stt_client_set_app_agreed(uid);
818                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
819         }
820
821         *available = temp;
822
823         return STTD_ERROR_NONE;
824 }
825
826
827 int sttd_server_get_supported_languages(int uid, GSList** lang_list)
828 {
829         /* check if uid is valid */
830         app_state_e state;
831         if (0 != sttd_client_get_state(uid, &state)) {
832                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
833                 return STTD_ERROR_INVALID_PARAMETER;
834         }
835
836         /* get language list from engine */
837         int ret = sttd_engine_agent_supported_langs(lang_list);
838         if (0 != ret) {
839                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
840                 return ret;
841         }
842
843         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
844
845         return STTD_ERROR_NONE;
846 }
847
848 int sttd_server_get_current_langauage(int uid, char** current_lang)
849 {
850         /* check if uid is valid */
851         app_state_e state;
852         if (0 != sttd_client_get_state(uid, &state)) {
853                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
854                 return STTD_ERROR_INVALID_PARAMETER;
855         }
856
857         if (NULL == current_lang) {
858                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
859                 return STTD_ERROR_INVALID_PARAMETER;
860         }
861
862         /*get current language from engine */
863         int ret = sttd_engine_agent_get_default_lang(current_lang);
864         if (0 != ret) {
865                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
866                 return ret;
867         }
868
869         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get default language");
870
871         return STTD_ERROR_NONE;
872 }
873
874 int sttd_server_set_private_data(int uid, const char* key, const char* data)
875 {
876         /* check if uid is valid */
877         app_state_e state;
878         if (0 != sttd_client_get_state(uid, &state)) {
879                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
880                 return STTD_ERROR_INVALID_PARAMETER;
881         }
882
883         if (NULL == key || NULL == data) {
884                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
885                 return STTD_ERROR_INVALID_PARAMETER;
886         }
887
888         /* set private data to engine */
889         int ret = -1;
890         ret = sttd_engine_agent_set_private_data(key, data);
891         if (0 != ret) {
892                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
893                 return ret;
894         }
895
896         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Set private data");
897
898         return STTD_ERROR_NONE;
899 }
900
901 int sttd_server_get_private_data(int uid, const char* key, char** data)
902 {
903         /* check if uid is valid */
904         app_state_e state;
905         if (0 != sttd_client_get_state(uid, &state)) {
906                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
907                 return STTD_ERROR_INVALID_PARAMETER;
908         }
909
910         if (NULL == key || NULL == data) {
911                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
912                 return STTD_ERROR_INVALID_PARAMETER;
913         }
914
915         /* get private data to engine */
916         int ret = -1;
917         ret = sttd_engine_agent_get_private_data(key, data);
918         if (0 != ret) {
919                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
920                 return ret;
921         }
922
923         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
924
925         return STTD_ERROR_NONE;
926 }
927
928 int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support)
929 {
930         /* check if uid is valid */
931         app_state_e state;
932         if (0 != sttd_client_get_state(uid, &state)) {
933                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
934                 return STTD_ERROR_INVALID_PARAMETER;
935         }
936
937         if (NULL == type || NULL == support) {
938                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
939                 return STTD_ERROR_INVALID_PARAMETER;
940         }
941
942         bool temp;
943         int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
944         if (0 != ret) {
945                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
946                 return ret;
947         }
948
949         *support = (int)temp;
950
951         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
952
953         return STTD_ERROR_NONE;
954 }
955
956 int sttd_server_set_start_sound(int uid, const char* file)
957 {
958         /* check if uid is valid */
959         app_state_e state;
960         if (0 != sttd_client_get_state(uid, &state)) {
961                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
962                 return STTD_ERROR_INVALID_PARAMETER;
963         }
964
965         int ret = sttd_client_set_start_sound(uid, file);
966         if (0 != ret) {
967                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
968                 return ret;
969         }
970
971         return 0;
972 }
973
974 int sttd_server_set_stop_sound(int uid, const char* file)
975 {
976         /* check if uid is valid */
977         app_state_e state;
978         if (0 != sttd_client_get_state(uid, &state)) {
979                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
980                 return STTD_ERROR_INVALID_PARAMETER;
981         }
982
983         int ret = sttd_client_set_stop_sound(uid, file);
984         if (0 != ret) {
985                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
986                 return ret;
987         }
988
989         return 0;
990 }
991
992 #if 0
993 Eina_Bool __check_recording_state(void *data)
994 {
995         /* current uid */
996         int uid = stt_client_get_current_recognition();
997         if (0 == uid)
998                 return EINA_FALSE;
999
1000         app_state_e state;
1001         if (0 != sttdc_send_get_state(uid, (int*)&state)) {
1002                 /* client is removed */
1003                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", uid);
1004                 sttd_server_finalize(uid);
1005                 return EINA_FALSE;
1006         }
1007
1008         if (APP_STATE_READY == state) {
1009                 /* Cancel stt */
1010                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The STT service should cancel recording", uid);
1011                 sttd_server_cancel(uid);
1012         } else if (APP_STATE_PROCESSING == state) {
1013                 /* Cancel stt and send change state */
1014                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The STT service should cancel recording", uid);
1015                 sttd_server_cancel(uid);
1016                 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1017         } else {
1018                 /* Normal state */
1019                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1020                 return EINA_TRUE;
1021         }
1022
1023         return EINA_FALSE;
1024 }
1025 #endif
1026
1027 Eina_Bool __stop_by_recording_timeout(void *data)
1028 {
1029         SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by timeout");
1030
1031         g_recording_timer = NULL;
1032
1033         int uid = 0;
1034         uid = stt_client_get_current_recognition();
1035         if (0 != uid) {
1036                 /* cancel engine recognition */
1037                 int ret = sttd_server_stop(uid);
1038                 if (0 != ret) {
1039                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1040                 }
1041         }
1042
1043         SLOG(LOG_DEBUG, TAG_STTD, "=====");
1044         SLOG(LOG_DEBUG, TAG_STTD, "  ");
1045
1046         return EINA_FALSE;
1047 }
1048
1049 void __sttd_server_recorder_start(void* data)
1050 {
1051         intptr_t puid = (intptr_t)data;
1052         int uid = (int)puid;
1053         int current_uid = stt_client_get_current_recognition();
1054
1055         if (uid != current_uid) {
1056                 stt_client_unset_current_recognition();
1057                 return;
1058         }
1059
1060         int ret = sttd_engine_agent_recognize_start_recorder(uid);
1061         if (0 != ret) {
1062                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1063                 return;
1064         }
1065
1066         /* Notify uid state change */
1067         sttdc_send_set_state(uid, APP_STATE_RECORDING);
1068
1069         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1070 }
1071
1072 void __sttd_start_sound_completed_cb(int id, void *user_data)
1073 {
1074         SLOG(LOG_DEBUG, TAG_STTD, "===== Start sound completed");
1075
1076         /* After wav play callback, recorder start */
1077         ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1078
1079         SLOG(LOG_DEBUG, TAG_STTD, "=====");
1080         SLOG(LOG_DEBUG, TAG_STTD, "  ");
1081         return;
1082 }
1083
1084 int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
1085 {
1086         if (NULL == lang || NULL == recognition_type) {
1087                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1088                 return STTD_ERROR_INVALID_PARAMETER;
1089         }
1090
1091         /* check if uid is valid */
1092         app_state_e state;
1093         if (0 != sttd_client_get_state(uid, &state)) {
1094                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1095                 return STTD_ERROR_INVALID_PARAMETER;
1096         }
1097
1098         /* check uid state */
1099         if (APP_STATE_READY != state) {
1100                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1101                 return STTD_ERROR_INVALID_STATE;
1102         }
1103
1104         int ret = 0;
1105         if (false == stt_client_get_app_agreed(uid)) {
1106                 bool temp = false;
1107                 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1108                 if (0 != ret) {
1109                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1110                         return ret;
1111                 }
1112
1113                 if (false == temp) {
1114                         SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1115                         return STTD_ERROR_PERMISSION_DENIED;
1116                 }
1117
1118                 stt_client_set_app_agreed(uid);
1119         }
1120
1121         /* check if engine use network */
1122         if (true == sttd_engine_agent_need_network()) {
1123                 if (false == stt_network_is_connected()) {
1124                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1125                         return STTD_ERROR_OUT_OF_NETWORK;
1126                 }
1127         }
1128
1129         char* sound = NULL;
1130         ret = sttd_client_get_start_sound(uid, &sound);
1131         if (0 != ret) {
1132                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1133                 return ret;
1134         }
1135
1136         if (0 != stt_client_set_current_recognition(uid)) {
1137                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1138                 if (NULL != sound)      free(sound);
1139                 return STTD_ERROR_RECORDER_BUSY;
1140         }
1141
1142         /* engine start recognition */
1143         SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)",
1144                         uid, lang, recognition_type);
1145         if (NULL != sound)
1146                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start sound : %s", sound);
1147
1148         /* 1. Set audio session */
1149         ret = sttd_recorder_set_audio_session();
1150         if (0 != ret) {
1151                 stt_client_unset_current_recognition();
1152                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1153                 if (NULL != sound)      free(sound);
1154                 return ret;
1155         }
1156
1157         bool is_sound_done = false;
1158
1159         /* 2. Request wav play */
1160         if (NULL != sound) {
1161                 int id = 0;
1162                 intptr_t puid = (intptr_t)uid;
1163                 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_start_sound_completed_cb, (void*)puid, &id);
1164                 if (WAV_PLAYER_ERROR_NONE != ret) {
1165                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1166                         is_sound_done = true;
1167                 }
1168                 free(sound);
1169                 sound = NULL;
1170         } else {
1171                 is_sound_done = true;
1172         }
1173
1174         /* 3. Create recorder & engine initialize */
1175         ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1176         if (0 != ret) {
1177                 stt_client_unset_current_recognition();
1178                 sttd_recorder_unset_audio_session();
1179                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1180                 return ret;
1181         }
1182
1183         if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1184                 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1185         }
1186
1187         /* change uid state */
1188         sttd_client_set_state(uid, APP_STATE_RECORDING);
1189
1190         g_recording_log_count = 0;
1191
1192         if (true == is_sound_done) {
1193                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1194
1195                 ret = sttd_engine_agent_recognize_start_recorder(uid);
1196                 if (0 != ret) {
1197                         stt_client_unset_current_recognition();
1198                         sttd_recorder_unset_audio_session();
1199
1200                         sttd_engine_agent_recognize_cancel();
1201                         ecore_timer_del(g_recording_timer);
1202                         sttd_client_set_state(uid, APP_STATE_READY);
1203
1204                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1205                         return ret;
1206                 }
1207
1208                 /* Notify uid state change */
1209                 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1210
1211                 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1212                 return STTD_RESULT_STATE_DONE;
1213         }
1214
1215         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Wait sound finish");
1216
1217         return STTD_RESULT_STATE_NOT_DONE;
1218 }
1219
1220 Eina_Bool __time_out_for_processing(void *data)
1221 {
1222         g_processing_timer = NULL;
1223
1224         /* current uid */
1225         int uid = stt_client_get_current_recognition();
1226         if (0 == uid)   return EINA_FALSE;
1227
1228         /* Cancel engine */
1229         int ret = sttd_engine_agent_recognize_cancel();
1230         if (0 != ret) {
1231                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1232         }
1233
1234         if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1235                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1236
1237                 /* send error msg */
1238                 int reason = (int)STTD_ERROR_TIMED_OUT;
1239                 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1240                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1241                 }
1242         }
1243
1244         /* Change uid state */
1245         sttd_client_set_state(uid, APP_STATE_READY);
1246
1247         stt_client_unset_current_recognition();
1248
1249         return EINA_FALSE;
1250 }
1251
1252 void __sttd_server_engine_stop(void* data)
1253 {
1254         intptr_t puid = (intptr_t)data;
1255         int uid = (int)puid;
1256         /* change uid state */
1257         sttd_client_set_state(uid, APP_STATE_PROCESSING);
1258
1259         /* Notify uid state change */
1260         sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1261
1262         /* Unset audio session */
1263         int ret = sttd_recorder_unset_audio_session();
1264         if (0 != ret) {
1265                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1266                 return;
1267         }
1268
1269         /* Stop engine */
1270         ret = sttd_engine_agent_recognize_stop_engine();
1271         if (0 != ret) {
1272                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1273                 return;
1274         }
1275
1276         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1277 }
1278
1279 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1280 {
1281         SLOG(LOG_DEBUG, TAG_STTD, "===== Stop sound completed");
1282
1283         /* After wav play callback, engine stop */
1284         ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1285
1286         SLOG(LOG_DEBUG, TAG_STTD, "=====");
1287         SLOG(LOG_DEBUG, TAG_STTD, "  ");
1288         return;
1289 }
1290
1291 int sttd_server_stop(int uid)
1292 {
1293         /* check if uid is valid */
1294         app_state_e state;
1295         if (0 != sttd_client_get_state(uid, &state)) {
1296                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1297                 return STTD_ERROR_INVALID_PARAMETER;
1298         }
1299
1300         /* check uid state */
1301         if (APP_STATE_RECORDING != state) {
1302                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1303                 return STTD_ERROR_INVALID_STATE;
1304         }
1305
1306         if (NULL != g_recording_timer)  {
1307                 ecore_timer_del(g_recording_timer);
1308                 g_recording_timer = NULL;
1309         }
1310
1311         char* sound = NULL;
1312         if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1313                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1314                 return STTD_ERROR_OPERATION_FAILED;
1315         }
1316
1317         SLOG(LOG_DEBUG, TAG_STTD, "[Server] stop sound path : %s", sound);
1318
1319         int ret;
1320         /* 1. Stop recorder */
1321         ret = sttd_engine_agent_recognize_stop_recorder();
1322         if (0 != ret) {
1323                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1324                 if (0 != sttd_engine_agent_recognize_cancel()) {
1325                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1326                 }
1327                 if (NULL != sound)      free(sound);
1328                 return ret;
1329         }
1330
1331         /* 2. Request wav play */
1332         if (NULL != sound) {
1333                 int id = 0;
1334                 intptr_t puid = (intptr_t)uid;
1335                 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1336                 if (WAV_PLAYER_ERROR_NONE != ret) {
1337                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1338                 } else {
1339                         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1340                 }
1341
1342                 free(sound);
1343
1344                 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1345
1346                 return STTD_RESULT_STATE_NOT_DONE;
1347         } else {
1348                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1349
1350                 /* Unset audio session */
1351                 ret = sttd_recorder_unset_audio_session();
1352                 if (0 != ret) {
1353                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1354                         return ret;
1355                 }
1356
1357                 /* Stop engine */
1358                 ret = sttd_engine_agent_recognize_stop_engine();
1359                 if (0 != ret) {
1360                         stt_client_unset_current_recognition();
1361                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1362                         return ret;
1363                 }
1364
1365                 /* change uid state */
1366                 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1367
1368                 /* Notify uid state change */
1369                 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1370
1371                 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1372
1373                 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1374
1375                 return STTD_RESULT_STATE_DONE;
1376         }
1377
1378         return STTD_ERROR_NONE;
1379 }
1380
1381 int sttd_server_cancel(int uid)
1382 {
1383         /* check if uid is valid */
1384         app_state_e state;
1385         if (0 != sttd_client_get_state(uid, &state)) {
1386                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1387                 return STTD_ERROR_INVALID_PARAMETER;
1388         }
1389
1390         /* check uid state */
1391         if (APP_STATE_READY == state) {
1392                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1393                 return STTD_ERROR_NONE;
1394         }
1395
1396         stt_client_unset_current_recognition();
1397
1398         if (NULL != g_recording_timer) {
1399                 ecore_timer_del(g_recording_timer);
1400                 g_recording_timer = NULL;
1401         }
1402
1403         if (NULL != g_processing_timer) {
1404                 ecore_timer_del(g_processing_timer);
1405                 g_processing_timer = NULL;
1406         }
1407
1408         if (APP_STATE_RECORDING == state) {
1409                 /* Unset audio session */
1410                 int ret = sttd_recorder_unset_audio_session();
1411                 if (0 != ret) {
1412                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1413                         return ret;
1414                 }
1415         }
1416
1417         /* change uid state */
1418         sttd_client_set_state(uid, APP_STATE_READY);
1419
1420         /* cancel engine recognition */
1421         int ret = sttd_engine_agent_recognize_cancel();
1422         if (0 != ret) {
1423                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1424                 return ret;
1425         }
1426
1427         /* Notify uid state change */
1428         sttdc_send_set_state(uid, APP_STATE_READY);
1429
1430         return STTD_ERROR_NONE;
1431 }