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