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.
15 #include <sound_manager.h>
16 #include <wav_player.h>
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"
28 #define CLIENT_CLEAN_UP_TIME 500
31 static pthread_mutex_t stte_result_mutex = PTHREAD_MUTEX_INITIALIZER;
32 static pthread_mutex_t stte_result_time_mutex = PTHREAD_MUTEX_INITIALIZER;
36 * STT Server static variable
38 static double g_processing_timeout = 30;
40 static double g_recording_timeout = 60;
42 static Ecore_Timer* g_check_client_timer = NULL;
43 Ecore_Timer* g_recording_timer = NULL;
44 Ecore_Timer* g_processing_timer = NULL;
46 static int g_recording_log_count = 0;
48 static GList *g_proc_list = NULL;
51 * STT Server Callback Functions
53 void __stop_by_silence(void *data)
55 SLOG(LOG_INFO, TAG_STTD, "===== Stop by silence detection");
59 uid = stt_client_get_current_recognition();
63 ret = sttd_server_stop(uid);
68 if (STTD_RESULT_STATE_DONE == ret) {
69 ret = sttdc_send_set_state(uid, (int)APP_STATE_PROCESSING);
71 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send state : result(%d)", ret);
74 sttd_server_finalize(uid);
75 stt_client_unset_current_recognition();
79 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
82 SLOG(LOG_INFO, TAG_STTD, "=====");
83 SLOG(LOG_DEBUG, TAG_STTD, " ");
88 static void __cancel_recognition_internal()
90 if (NULL != g_recording_timer) {
91 ecore_timer_del(g_recording_timer);
92 g_recording_timer = NULL;
97 uid = stt_client_get_current_recognition();
99 app_state_e state = 0;
100 ret = sttd_client_get_state(uid, &state);
103 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
107 if (0 != uid && (APP_STATE_PROCESSING == state || APP_STATE_RECORDING == state)) {
108 SLOG(LOG_INFO, TAG_STTD, "===== cancel by internal");
109 /* cancel engine recognition */
110 ret = sttd_server_cancel(uid);
112 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
115 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
119 static void __cancel_by_error(void *data)
121 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by error");
123 __cancel_recognition_internal();
125 SLOG(LOG_DEBUG, TAG_STTD, "=====");
126 SLOG(LOG_DEBUG, TAG_STTD, " ");
131 int __server_audio_recorder_callback(const void* data, const unsigned int length)
136 if (NULL == data || 0 == length) {
137 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Recording data is not valid");
138 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
142 uid = stt_client_get_current_recognition();
144 ret = sttd_engine_agent_set_recording_data(data, length);
146 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
149 g_recording_log_count++;
150 if (200 <= g_recording_log_count) {
151 SLOG(LOG_DEBUG, TAG_STTD, "=== Set recording data ===");
152 g_recording_log_count = 0;
155 if (NULL != g_recording_timer) {
156 ecore_timer_del(g_recording_timer);
157 g_recording_timer = NULL;
159 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current uid in recording is is not valid");
166 void __server_audio_interrupt_callback()
168 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by sound interrupt");
170 __cancel_recognition_internal();
172 SLOG(LOG_DEBUG, TAG_STTD, "=====");
173 SLOG(LOG_DEBUG, TAG_STTD, " ");
176 void __cancel_by_no_record(void *data)
178 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by no record");
180 __cancel_recognition_internal();
182 SLOG(LOG_DEBUG, TAG_STTD, "=====");
183 SLOG(LOG_DEBUG, TAG_STTD, " ");
188 int __server_recognition_result_callback(stte_result_event_e event, const char* type,
189 const char** data, int data_count, const char* msg, void *user_data)
192 pthread_mutex_lock(&stte_result_mutex);
194 SLOG(LOG_INFO, TAG_STTD, "===== RESULT event[%d] type[%s] data[%p] data_count[%d]", event, type, data, data_count);
197 int uid = stt_client_get_current_recognition();
200 if (0 == uid || 0 != sttd_client_get_state(uid, &state)) {
201 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
202 SLOG(LOG_DEBUG, TAG_STTD, "=====");
203 SLOG(LOG_DEBUG, TAG_STTD, " ");
204 pthread_mutex_unlock(&stte_result_mutex);
205 return STTD_ERROR_OPERATION_FAILED;
208 SLOG(LOG_INFO, TAG_STTD, "[Server] uid (%d), event(%d)", uid, event);
210 /* send result to client */
211 if (STTE_RESULT_EVENT_FINAL_RESULT == event) {
212 if (APP_STATE_PROCESSING != state) {
213 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is NOT 'Processing'.");
215 SLOG(LOG_INFO, TAG_STTD, "[Server] the size of result from engine is '%d'", data_count);
217 /* Delete timer for processing time out */
218 if (NULL != g_processing_timer) {
219 ecore_timer_del(g_processing_timer);
220 g_processing_timer = NULL;
223 sttd_config_time_save();
224 sttd_config_time_reset();
226 sttd_recorder_clear();
228 sttd_client_set_state(uid, APP_STATE_READY);
229 stt_client_unset_current_recognition();
231 if (NULL == data || 0 == data_count) {
232 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
233 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
234 int reason = (int)STTD_ERROR_OPERATION_FAILED;
236 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
237 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
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;
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");
251 /* change state of uid */
252 // sttd_client_set_state(uid, APP_STATE_READY);
253 // stt_client_unset_current_recognition();
255 } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) {
256 SLOG(LOG_INFO, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event, data_count);
258 sttd_config_time_save();
259 sttd_config_time_reset();
261 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
262 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
263 int reason = (int)STTD_ERROR_OPERATION_FAILED;
265 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
266 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
270 } else if (STTE_RESULT_EVENT_ERROR == event) {
271 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
273 /* Delete timer for processing time out */
274 if (NULL != g_processing_timer) {
275 ecore_timer_del(g_processing_timer);
276 g_processing_timer = NULL;
278 sttd_config_time_reset();
281 if (APP_STATE_RECORDING == state) {
282 ret = sttd_engine_agent_recognize_cancel();
284 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel: result(%d)", ret);
288 sttd_client_set_state(uid, APP_STATE_READY);
289 stt_client_unset_current_recognition();
291 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
292 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
295 int reason = (int)STTD_ERROR_INVALID_STATE;
296 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
297 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
301 /* change state of uid */
302 // sttd_client_set_state(uid, APP_STATE_READY);
303 // stt_client_unset_current_recognition();
308 SLOG(LOG_INFO, TAG_STTD, "=====");
309 SLOG(LOG_DEBUG, TAG_STTD, " ");
310 pthread_mutex_unlock(&stte_result_mutex);
312 return STTD_ERROR_NONE;
315 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)
317 pthread_mutex_lock(&stte_result_time_mutex);
319 SECURE_SLOG(LOG_INFO, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)",
320 index, event, text, start_time, end_time);
323 ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
325 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
326 pthread_mutex_unlock(&stte_result_time_mutex);
330 pthread_mutex_unlock(&stte_result_time_mutex);
335 int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
337 SLOG(LOG_INFO, TAG_STTD, "===== Speech status detected Callback");
339 int uid = stt_client_get_current_recognition();
342 if (0 != sttd_client_get_state(uid, &state)) {
343 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid ");
344 return STTD_ERROR_OPERATION_FAILED;
347 if (APP_STATE_RECORDING != state && APP_STATE_PROCESSING != state) {
348 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording, state(%d), status(%d)", state, status);
349 return STTD_ERROR_INVALID_STATE;
352 if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) {
353 SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected");
354 sttdc_send_speech_status(uid, status);
355 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
356 SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
357 ecore_main_loop_thread_safe_call_async(__stop_by_silence, NULL);
360 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recognition uid is not valid ");
363 SLOG(LOG_INFO, TAG_STTD, "=====");
364 SLOG(LOG_DEBUG, TAG_STTD, " ");
366 return STTD_ERROR_NONE;
369 int __server_error_callback(stte_error_e error, const char* msg)
371 SLOG(LOG_INFO, TAG_STTD, "[Server] Error Callback is called");
372 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
374 return STTD_ERROR_NONE;
377 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
379 if (NULL == engine_id) {
380 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
383 SLOG(LOG_INFO, TAG_STTD, "[Server] New default engine : %s", engine_id);
387 /* need to change state of app to ready */
389 uid = stt_client_get_current_recognition();
392 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%d)", uid);
394 sttd_server_cancel(uid);
395 sttdc_send_set_state(uid, (int)APP_STATE_READY);
397 stt_client_unset_current_recognition();
401 int ret = sttd_engine_agent_set_default_engine(engine_id);
403 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
405 if (NULL != language) {
406 ret = sttd_engine_agent_set_default_language(language);
408 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
411 ret = sttd_engine_agent_set_silence_detection(support_silence);
413 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
419 void __sttd_server_language_changed_cb(const char* language, void* user_data)
421 if (NULL == language) {
422 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
425 SLOG(LOG_INFO, TAG_STTD, "[Server] Get language changed : %s", language);
428 int ret = sttd_engine_agent_set_default_language(language);
430 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
435 void __sttd_server_silence_changed_cb(bool value, void* user_data)
437 SLOG(LOG_INFO, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
440 ret = sttd_engine_agent_set_silence_detection(value);
442 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
450 int sttd_initialize(stte_request_callback_s *callback)
454 if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
455 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
458 if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) {
459 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex.");
462 if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
463 __sttd_server_silence_changed_cb, NULL)) {
464 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
467 ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
469 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
473 /* Engine Agent initialize */
474 ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
475 __server_speech_status_callback, __server_error_callback);
477 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
482 ret = sttd_engine_agent_load_current_engine(callback);
484 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
488 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, sttd_cleanup_client, NULL);
489 if (NULL == g_check_client_timer) {
490 SLOG(LOG_WARN, TAG_STTD, "[Main Warning] Fail to create timer of client check");
493 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] initialize");
500 if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
501 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
504 if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) {
505 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex.");
509 if (0 < g_list_length(g_proc_list)) {
510 iter = g_list_first(g_proc_list);
511 while (NULL != iter) {
512 g_proc_list = g_list_remove_link(g_proc_list, iter);
513 iter = g_list_first(g_proc_list);
517 sttd_recorder_deinitialize();
519 sttd_config_finalize();
521 sttd_engine_agent_release();
523 if (NULL != g_check_client_timer) {
524 ecore_timer_del(g_check_client_timer);
525 g_check_client_timer = NULL;
527 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete ecore timer handle");
530 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] finalize");
532 return STTD_ERROR_NONE;
535 static void __read_proc()
538 struct dirent *dirp = NULL;
542 if (0 < g_list_length(g_proc_list)) {
543 iter = g_list_first(g_proc_list);
544 while (NULL != iter) {
545 g_proc_list = g_list_remove_link(g_proc_list, iter);
546 iter = g_list_first(g_proc_list);
550 dp = opendir("/proc");
552 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
558 tmp = atoi(dirp->d_name);
559 if (0 >= tmp) continue;
560 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
562 } while (NULL != dirp);
568 Eina_Bool sttd_cleanup_client(void *data)
570 int* client_list = NULL;
571 int client_count = 0;
576 if (0 != sttd_client_get_list(&client_list, &client_count)) {
577 if (NULL != client_list)
583 if (NULL != client_list) {
584 SLOG(LOG_INFO, TAG_STTD, "===== Clean up client ");
588 for (i = 0; i < client_count; i++) {
589 int pid = sttd_client_get_pid(client_list[i]);
591 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
597 for (j = 0; j < g_list_length(g_proc_list); j++) {
598 iter = g_list_nth(g_proc_list, j);
600 if (pid == GPOINTER_TO_INT(iter->data)) {
601 SLOG(LOG_DEBUG, TAG_STTD, "uid (%d) is running", client_list[i]);
608 if (false == exist) {
609 SLOG(LOG_ERROR, TAG_STTD, "uid (%d) should be removed", client_list[i]);
610 sttd_server_finalize(client_list[i]);
613 result = sttdc_send_hello(client_list[i]);
616 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", client_list[i]);
617 sttd_server_finalize(client_list[i]);
618 } else if (-1 == result) {
619 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
624 SLOG(LOG_INFO, TAG_STTD, "=====");
625 SLOG(LOG_DEBUG, TAG_STTD, " ");
634 * STT Server Functions for Client
637 int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential)
639 int ret = STTD_ERROR_NONE;
641 SLOG(LOG_INFO, TAG_STTD, "[Server] server initialize");
643 /* check if uid is valid */
645 if (0 == sttd_client_get_state(uid, &state)) {
646 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
647 return STTD_ERROR_NONE;
650 ret = sttd_engine_agent_get_option_supported(silence);
652 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
656 ret = sttd_engine_agent_is_credential_needed(uid, credential);
658 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
662 /* Add client information to client manager */
663 ret = sttd_client_add(pid, uid);
665 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
669 SLOG(LOG_INFO, TAG_STTD, "[Server Success] server initialize");
671 return STTD_ERROR_NONE;
674 static Eina_Bool __quit_ecore_loop(void *data)
676 SLOG(LOG_INFO, TAG_STTD, "[Server] Quit");
678 stt_network_finalize();
680 sttd_dbus_close_connection();
681 ecore_main_loop_quit();
683 SLOG(LOG_INFO, TAG_STTD, "");
688 int sttd_server_finalize(int uid)
690 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter Finalize");
692 /* check if uid is valid */
694 if (0 != sttd_client_get_state(uid, &state)) {
695 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
696 return STTD_ERROR_INVALID_PARAMETER;
699 /* release recorder */
700 if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
701 if (APP_STATE_RECORDING == state) {
702 if (NULL != g_recording_timer) {
703 ecore_timer_del(g_recording_timer);
704 g_recording_timer = NULL;
708 if (APP_STATE_PROCESSING == state) {
709 if (NULL != g_processing_timer) {
710 ecore_timer_del(g_processing_timer);
711 g_processing_timer = NULL;
715 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] stt_cancel is invoked while state is (%d)", state);
717 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
718 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
721 stt_client_unset_current_recognition();
724 /* Remove client information */
725 if (0 != sttd_client_delete(uid)) {
726 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
729 /* unload engine, if ref count of client is 0 */
730 if (0 == sttd_client_get_ref_count()) {
731 // sttd_dbus_close_connection();
732 ecore_timer_add(0, __quit_ecore_loop, NULL);
735 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End Finalize");
737 return STTD_ERROR_NONE;
740 int sttd_server_get_supported_engines(int uid, GSList** engine_list)
742 /* Check if uid is valid */
744 if (0 != sttd_client_get_state(uid, &state)) {
745 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
746 return STTD_ERROR_INVALID_PARAMETER;
749 /* Check state of uid */
750 if (APP_STATE_READY != state) {
751 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
752 return STTD_ERROR_INVALID_STATE;
755 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported egnines");
758 ret = sttd_engine_agent_get_engine_list(engine_list);
760 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
764 return STTD_ERROR_NONE;
767 int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence, bool* credential)
769 /* Check if uid is valid */
771 if (0 != sttd_client_get_state(uid, &state)) {
772 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
773 return STTD_ERROR_INVALID_PARAMETER;
776 /* Check state of uid */
777 if (APP_STATE_READY != state) {
778 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
779 return STTD_ERROR_INVALID_STATE;
782 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] set current egnines, uid(%d), engine_id(%s)", uid, engine_id);
785 ret = sttd_engine_agent_load_current_engine(NULL);
787 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
791 ret = sttd_engine_agent_get_option_supported(silence);
793 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
797 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
798 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
802 return STTD_ERROR_NONE;
805 int sttd_server_get_current_engine(int uid, char** engine_id)
807 /* Check if uid is valid */
809 if (0 != sttd_client_get_state(uid, &state)) {
810 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
811 return STTD_ERROR_INVALID_PARAMETER;
814 /* Check state of uid */
815 if (APP_STATE_READY != state) {
816 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
817 return STTD_ERROR_INVALID_STATE;
820 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get current egnines, uid(%d)", uid);
823 ret = sttd_engine_agent_get_current_engine(engine_id);
825 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
829 return STTD_ERROR_NONE;
832 int sttd_server_check_app_agreed(int uid, const char* appid, bool* available)
834 /* Check if uid is valid */
836 if (0 != sttd_client_get_state(uid, &state)) {
837 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
838 return STTD_ERROR_INVALID_PARAMETER;
841 /* Check state of uid */
842 if (APP_STATE_READY != state) {
843 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
844 return STTD_ERROR_INVALID_STATE;
847 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] check app agreed");
849 /* Ask engine available */
852 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
854 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
859 stt_client_set_app_agreed(uid);
860 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
865 return STTD_ERROR_NONE;
869 int sttd_server_get_supported_languages(int uid, GSList** lang_list)
871 /* check if uid is valid */
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;
878 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported languages");
880 /* get language list from engine */
881 int ret = sttd_engine_agent_supported_langs(lang_list);
883 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
887 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
889 return STTD_ERROR_NONE;
892 int sttd_server_get_current_langauage(int uid, char** current_lang)
894 /* check if uid is valid */
896 if (0 != sttd_client_get_state(uid, &state)) {
897 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
898 return STTD_ERROR_INVALID_PARAMETER;
901 if (NULL == current_lang) {
902 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
903 return STTD_ERROR_INVALID_PARAMETER;
906 /*get current language from engine */
907 int ret = sttd_engine_agent_get_default_lang(current_lang);
909 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
913 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get default language, current_lang(%s)", *current_lang);
915 return STTD_ERROR_NONE;
918 int sttd_server_set_private_data(int uid, const char* key, const char* data)
920 /* check if uid is valid */
922 if (0 != sttd_client_get_state(uid, &state)) {
923 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
924 return STTD_ERROR_INVALID_PARAMETER;
927 if (NULL == key || NULL == data) {
928 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
929 return STTD_ERROR_INVALID_PARAMETER;
932 /* set private data to engine */
934 ret = sttd_engine_agent_set_private_data(key, data);
936 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
940 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Set private data");
942 return STTD_ERROR_NONE;
945 int sttd_server_get_private_data(int uid, const char* key, char** data)
947 /* check if uid is valid */
949 if (0 != sttd_client_get_state(uid, &state)) {
950 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
951 return STTD_ERROR_INVALID_PARAMETER;
954 if (NULL == key || NULL == data) {
955 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
956 return STTD_ERROR_INVALID_PARAMETER;
959 /* get private data to engine */
961 ret = sttd_engine_agent_get_private_data(key, data);
963 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
967 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
969 return STTD_ERROR_NONE;
972 int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support)
974 /* check if uid is valid */
976 if (0 != sttd_client_get_state(uid, &state)) {
977 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
978 return STTD_ERROR_INVALID_PARAMETER;
981 if (NULL == type || NULL == support) {
982 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
983 return STTD_ERROR_INVALID_PARAMETER;
987 int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
989 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
993 *support = (int)temp;
995 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
997 return STTD_ERROR_NONE;
1000 int sttd_server_set_start_sound(int uid, const char* file)
1002 /* check if uid is valid */
1004 if (0 != sttd_client_get_state(uid, &state)) {
1005 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1006 return STTD_ERROR_INVALID_PARAMETER;
1009 int ret = sttd_client_set_start_sound(uid, file);
1011 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1018 int sttd_server_set_stop_sound(int uid, const char* file)
1020 /* check if uid is valid */
1022 if (0 != sttd_client_get_state(uid, &state)) {
1023 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1024 return STTD_ERROR_INVALID_PARAMETER;
1027 int ret = sttd_client_set_stop_sound(uid, file);
1029 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1037 Eina_Bool __check_recording_state(void *data)
1040 int uid = stt_client_get_current_recognition();
1045 if (0 != sttdc_send_get_state(uid, (int*)&state)) {
1046 /* client is removed */
1047 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", uid);
1048 sttd_server_finalize(uid);
1052 if (APP_STATE_READY == state) {
1054 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The STT service should cancel recording", uid);
1055 sttd_server_cancel(uid);
1056 } else if (APP_STATE_PROCESSING == state) {
1057 /* Cancel stt and send change state */
1058 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The STT service should cancel recording", uid);
1059 sttd_server_cancel(uid);
1060 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1063 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1071 Eina_Bool __stop_by_recording_timeout(void *data)
1073 SLOG(LOG_INFO, TAG_STTD, "===== Stop by timeout");
1075 if (NULL != g_recording_timer) {
1076 ecore_timer_del(g_recording_timer);
1077 g_recording_timer = NULL;
1081 uid = stt_client_get_current_recognition();
1083 /* cancel engine recognition */
1084 int ret = sttd_server_stop(uid);
1086 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1090 SLOG(LOG_INFO, TAG_STTD, "=====");
1091 SLOG(LOG_DEBUG, TAG_STTD, " ");
1096 void __sttd_server_recorder_start(void* data)
1098 intptr_t puid = (intptr_t)data;
1099 int uid = (int)puid;
1100 int current_uid = stt_client_get_current_recognition();
1102 if (uid != current_uid) {
1103 stt_client_unset_current_recognition();
1107 int ret = sttd_engine_agent_recognize_start_recorder(uid);
1109 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1113 app_state_e temp_app_state;
1114 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1115 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1118 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1119 /* Notify uid state change */
1120 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1121 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1125 void __sttd_start_sound_completed_cb(int id, void *user_data)
1127 SLOG(LOG_INFO, TAG_STTD, "===== Start sound completed");
1129 /* After wav play callback, recorder start */
1130 ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1132 SLOG(LOG_INFO, TAG_STTD, "=====");
1133 SLOG(LOG_DEBUG, TAG_STTD, " ");
1137 int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
1139 if (NULL == lang || NULL == recognition_type) {
1140 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1141 return STTD_ERROR_INVALID_PARAMETER;
1144 /* check if uid is valid */
1146 if (0 != sttd_client_get_state(uid, &state)) {
1147 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1148 return STTD_ERROR_INVALID_PARAMETER;
1151 /* check uid state */
1152 if (APP_STATE_READY != state) {
1153 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1154 return STTD_ERROR_INVALID_STATE;
1158 if (false == stt_client_get_app_agreed(uid)) {
1160 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1162 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1166 if (false == temp) {
1167 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1168 return STTD_ERROR_PERMISSION_DENIED;
1171 stt_client_set_app_agreed(uid);
1174 /* check if engine use network */
1175 if (true == sttd_engine_agent_need_network()) {
1176 if (false == stt_network_is_connected()) {
1177 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1178 return STTD_ERROR_OUT_OF_NETWORK;
1182 if (NULL != g_recording_timer) {
1183 ecore_timer_del(g_recording_timer);
1184 g_recording_timer = NULL;
1187 if (NULL != g_processing_timer) {
1188 ecore_timer_del(g_processing_timer);
1189 g_processing_timer = NULL;
1193 ret = sttd_client_get_start_sound(uid, &sound);
1195 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1199 if (0 != stt_client_set_current_recognition(uid)) {
1200 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1201 if (NULL != sound) free(sound);
1202 return STTD_ERROR_RECORDER_BUSY;
1205 /* engine start recognition */
1206 SLOG(LOG_INFO, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)",
1207 uid, lang, recognition_type);
1209 SLOG(LOG_INFO, TAG_STTD, "[Server] start sound : %s", sound);
1211 /* 1. Set audio session */
1212 ret = sttd_recorder_set_audio_session();
1214 stt_client_unset_current_recognition();
1215 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1216 if (NULL != sound) free(sound);
1220 bool is_sound_done = false;
1222 /* 2. Request wav play */
1223 if (NULL != sound) {
1225 intptr_t puid = (intptr_t)uid;
1226 sound_stream_info_h wav_stream_info_h;
1227 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1228 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1229 is_sound_done = true;
1231 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_start_sound_completed_cb, (void*)puid, &id);
1232 if (WAV_PLAYER_ERROR_NONE != ret) {
1233 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1234 is_sound_done = true;
1237 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1238 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1244 is_sound_done = true;
1247 /* 3. Create recorder & engine initialize */
1248 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1250 stt_client_unset_current_recognition();
1251 sttd_recorder_unset_audio_session();
1252 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1256 if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1257 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1260 /* change uid state */
1261 sttd_client_set_state(uid, APP_STATE_RECORDING);
1263 g_recording_log_count = 0;
1265 app_state_e temp_app_state;
1267 if (true == is_sound_done) {
1268 SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
1270 ret = sttd_engine_agent_recognize_start_recorder(uid);
1272 stt_client_unset_current_recognition();
1273 sttd_recorder_unset_audio_session();
1275 sttd_engine_agent_recognize_cancel();
1276 if (NULL != g_recording_timer) {
1277 ecore_timer_del(g_recording_timer);
1278 g_recording_timer = NULL;
1280 sttd_client_set_state(uid, APP_STATE_READY);
1282 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1286 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1287 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid");
1288 return STTD_ERROR_INVALID_PARAMETER;
1290 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1291 /* Notify uid state change */
1292 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1295 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Start recognition");
1296 return STTD_RESULT_STATE_DONE;
1299 SLOG(LOG_INFO, TAG_STTD, "[Server] Wait sound finish");
1301 return STTD_RESULT_STATE_NOT_DONE;
1304 Eina_Bool __time_out_for_processing(void *data)
1306 if (NULL != g_processing_timer) {
1307 ecore_timer_del(g_processing_timer);
1308 g_processing_timer = NULL;
1311 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter __time_out_for_processing");
1314 int uid = stt_client_get_current_recognition();
1315 if (0 == uid) return EINA_FALSE;
1317 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by timeout for processing");
1320 int ret = sttd_engine_agent_recognize_cancel();
1322 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1325 if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1326 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1328 /* send error msg */
1329 int reason = (int)STTD_ERROR_TIMED_OUT;
1330 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1331 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1335 /* Change uid state */
1336 sttd_client_set_state(uid, APP_STATE_READY);
1338 stt_client_unset_current_recognition();
1340 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End __time_out_for_processing");
1345 void __sttd_server_engine_stop(void* data)
1347 intptr_t puid = (intptr_t)data;
1348 int uid = (int)puid;
1349 /* change uid state */
1350 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1352 /* Notify uid state change */
1353 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1355 /* Unset audio session */
1356 int ret = sttd_recorder_unset_audio_session();
1358 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1363 ret = sttd_engine_agent_recognize_stop_engine();
1365 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1369 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1372 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1374 SLOG(LOG_INFO, TAG_STTD, "===== Stop sound completed");
1376 /* After wav play callback, engine stop */
1377 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1379 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1380 SLOG(LOG_DEBUG, TAG_STTD, " ");
1384 int sttd_server_stop(int uid)
1386 /* check if uid is valid */
1388 if (0 != sttd_client_get_state(uid, &state)) {
1389 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1390 return STTD_ERROR_INVALID_PARAMETER;
1393 /* check uid state */
1394 if (APP_STATE_RECORDING != state) {
1395 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1396 return STTD_ERROR_INVALID_STATE;
1399 if (NULL != g_recording_timer) {
1400 ecore_timer_del(g_recording_timer);
1401 g_recording_timer = NULL;
1404 if (NULL != g_processing_timer) {
1405 ecore_timer_del(g_processing_timer);
1406 g_processing_timer = NULL;
1410 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1411 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1412 return STTD_ERROR_OPERATION_FAILED;
1415 SLOG(LOG_INFO, TAG_STTD, "[Server] stop sound path : %s", sound);
1418 /* 1. Stop recorder */
1419 ret = sttd_engine_agent_recognize_stop_recorder();
1421 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1422 if (0 != sttd_engine_agent_recognize_cancel()) {
1423 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1425 if (NULL != sound) free(sound);
1429 /* 2. Request wav play */
1430 if (NULL != sound) {
1432 intptr_t puid = (intptr_t)uid;
1433 sound_stream_info_h wav_stream_info_h;
1434 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1435 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1437 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1438 if (WAV_PLAYER_ERROR_NONE != ret) {
1439 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1441 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1444 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1445 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1450 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1452 return STTD_RESULT_STATE_NOT_DONE;
1454 SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
1456 /* Unset audio session */
1457 ret = sttd_recorder_unset_audio_session();
1459 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1464 ret = sttd_engine_agent_recognize_stop_engine();
1466 stt_client_unset_current_recognition();
1467 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1471 /* change uid state */
1472 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1474 /* Notify uid state change */
1475 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1477 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Stop recognition");
1479 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1481 return STTD_RESULT_STATE_DONE;
1484 return STTD_ERROR_NONE;
1487 int sttd_server_cancel(int uid)
1489 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel");
1491 /* check if uid is valid */
1493 if (0 != sttd_client_get_state(uid, &state)) {
1494 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1495 return STTD_ERROR_INVALID_PARAMETER;
1498 /* check uid state */
1499 if (APP_STATE_READY == state) {
1500 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1501 return STTD_ERROR_NONE;
1504 stt_client_unset_current_recognition();
1506 if (NULL != g_recording_timer) {
1507 ecore_timer_del(g_recording_timer);
1508 g_recording_timer = NULL;
1511 if (NULL != g_processing_timer) {
1512 ecore_timer_del(g_processing_timer);
1513 g_processing_timer = NULL;
1516 if (APP_STATE_RECORDING == state) {
1517 /* Unset audio session */
1518 int ret = sttd_recorder_unset_audio_session();
1520 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1525 /* change uid state */
1526 sttd_client_set_state(uid, APP_STATE_READY);
1528 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by app's request");
1530 /* cancel engine recognition */
1531 int ret = sttd_engine_agent_recognize_cancel();
1533 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1537 /* Notify uid state change */
1538 sttdc_send_set_state(uid, APP_STATE_READY);
1540 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel");
1542 return STTD_ERROR_NONE;
1545 int sttd_server_start_file(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential,
1546 const char* filepath, stte_audio_type_e audio_type, int sample_rate)
1548 if (NULL == lang || NULL == recognition_type || NULL == filepath) {
1549 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1550 return STTD_ERROR_INVALID_PARAMETER;
1553 /* check if uid is valid */
1555 if (0 != sttd_client_get_state(uid, &state)) {
1556 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1557 return STTD_ERROR_INVALID_PARAMETER;
1560 /* check uid state */
1561 if (APP_STATE_READY != state) {
1562 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1563 return STTD_ERROR_INVALID_STATE;
1567 if (false == stt_client_get_app_agreed(uid)) {
1569 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1571 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1575 if (false == temp) {
1576 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1577 return STTD_ERROR_PERMISSION_DENIED;
1580 stt_client_set_app_agreed(uid);
1583 /* check if engine use network */
1584 if (true == sttd_engine_agent_need_network()) {
1585 if (false == stt_network_is_connected()) {
1586 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1587 return STTD_ERROR_OUT_OF_NETWORK;
1591 if (0 != stt_client_set_current_recognition(uid)) {
1592 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1593 return STTD_ERROR_RECORDER_BUSY;
1596 /* engine start recognition */
1597 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);
1599 /* 1. Set audio session */
1600 ret = sttd_recorder_set_audio_session();
1602 stt_client_unset_current_recognition();
1603 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1607 /* 2. Start engine to recognize */
1608 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1610 stt_client_unset_current_recognition();
1611 sttd_recorder_unset_audio_session();
1612 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start engine : result(%d)", ret);
1616 sttd_client_set_state(uid, APP_STATE_RECORDING);
1617 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1619 /* 3. Start to send pcm from file to engine */
1620 ret = sttd_engine_agent_recognize_start_file(uid, filepath);
1622 stt_client_unset_current_recognition();
1623 sttd_recorder_unset_audio_session();
1624 sttd_engine_agent_recognize_cancel();
1625 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start file : result(%d)", ret);
1629 /* 4. Stop to send pcm from file */
1630 ret = sttd_engine_agent_recognize_stop_file();
1632 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1633 stt_client_unset_current_recognition();
1634 sttd_recorder_unset_audio_session();
1635 sttd_engine_agent_recognize_cancel();
1639 /* 5. change & notify uid state */
1640 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1641 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1643 /* 6. Unset audio session */
1644 ret = sttd_recorder_unset_audio_session();
1646 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1647 stt_client_unset_current_recognition();
1651 /* 7. Stop engine */
1652 ret = sttd_engine_agent_recognize_stop_engine();
1654 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1655 stt_client_unset_current_recognition();
1659 return STTD_ERROR_NONE;
1662 int sttd_server_cancel_file(int uid)
1664 /* check if uid is valid */
1666 if (0 != sttd_client_get_state(uid, &state)) {
1667 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1668 return STTD_ERROR_INVALID_PARAMETER;
1671 /* check uid state */
1672 if (APP_STATE_READY == state) {
1673 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1674 return STTD_ERROR_NONE;
1677 stt_client_unset_current_recognition();
1679 if (NULL != g_recording_timer) {
1680 ecore_timer_del(g_recording_timer);
1681 g_recording_timer = NULL;
1684 if (NULL != g_processing_timer) {
1685 ecore_timer_del(g_processing_timer);
1686 g_processing_timer = NULL;
1689 if (APP_STATE_RECORDING == state) {
1690 /* Unset audio session */
1691 int ret = sttd_recorder_unset_audio_session();
1693 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1698 /* change uid state */
1699 sttd_client_set_state(uid, APP_STATE_READY);
1701 /* cancel engine recognition */
1702 int ret = sttd_engine_agent_recognize_cancel();
1704 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1708 /* Notify uid state change */
1709 sttdc_send_set_state(uid, APP_STATE_READY);
1711 return STTD_ERROR_NONE;