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