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