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