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_DEBUG, 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_DEBUG, 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 /* cancel engine recognition */
109 ret = sttd_server_cancel(uid);
111 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
114 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
118 static void __cancel_by_error(void *data)
120 SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by error");
122 __cancel_recognition_internal();
124 SLOG(LOG_DEBUG, TAG_STTD, "=====");
125 SLOG(LOG_DEBUG, TAG_STTD, " ");
130 int __server_audio_recorder_callback(const void* data, const unsigned int length)
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);
141 uid = stt_client_get_current_recognition();
143 ret = sttd_engine_agent_set_recording_data(data, length);
145 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
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;
154 if (NULL != g_recording_timer) {
155 ecore_timer_del(g_recording_timer);
156 g_recording_timer = NULL;
158 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current uid in recording is is not valid");
165 void __server_audio_interrupt_callback()
167 SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by sound interrupt");
169 __cancel_recognition_internal();
171 SLOG(LOG_DEBUG, TAG_STTD, "=====");
172 SLOG(LOG_DEBUG, TAG_STTD, " ");
175 void __cancel_by_no_record(void *data)
177 SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by no record");
179 __cancel_recognition_internal();
181 SLOG(LOG_DEBUG, TAG_STTD, "=====");
182 SLOG(LOG_DEBUG, TAG_STTD, " ");
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)
191 pthread_mutex_lock(&stte_result_mutex);
193 SLOG(LOG_DEBUG, TAG_STTD, "===== RESULT event[%d] type[%s] data[%p] data_count[%d]", event, type, data, data_count);
196 int uid = stt_client_get_current_recognition();
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;
207 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid (%d), event(%d)", uid, event);
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'.");
214 SLOG(LOG_DEBUG, TAG_STTD, "[Server] the size of result from engine is '%d'", data_count);
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;
222 sttd_config_time_save();
223 sttd_config_time_reset();
225 sttd_recorder_clear();
227 sttd_client_set_state(uid, APP_STATE_READY);
228 stt_client_unset_current_recognition();
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;
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");
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;
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");
250 /* change state of uid */
251 // sttd_client_set_state(uid, APP_STATE_READY);
252 // stt_client_unset_current_recognition();
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);
257 sttd_config_time_save();
258 sttd_config_time_reset();
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;
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");
269 } else if (STTE_RESULT_EVENT_ERROR == event) {
270 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
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;
277 sttd_config_time_reset();
280 if (APP_STATE_RECORDING == state) {
281 ret = sttd_engine_agent_recognize_cancel();
283 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel: result(%d)", ret);
287 sttd_client_set_state(uid, APP_STATE_READY);
288 stt_client_unset_current_recognition();
290 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
291 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
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 ");
300 /* change state of uid */
301 // sttd_client_set_state(uid, APP_STATE_READY);
302 // stt_client_unset_current_recognition();
307 SLOG(LOG_DEBUG, TAG_STTD, "=====");
308 SLOG(LOG_DEBUG, TAG_STTD, " ");
309 pthread_mutex_unlock(&stte_result_mutex);
311 return STTD_ERROR_NONE;
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)
316 pthread_mutex_lock(&stte_result_time_mutex);
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);
322 ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
324 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
325 pthread_mutex_unlock(&stte_result_time_mutex);
329 pthread_mutex_unlock(&stte_result_time_mutex);
334 int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
336 SLOG(LOG_DEBUG, TAG_STTD, "===== Speech status detected Callback");
338 int uid = stt_client_get_current_recognition();
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;
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;
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);
359 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recognition uid is not valid ");
362 SLOG(LOG_DEBUG, TAG_STTD, "=====");
363 SLOG(LOG_DEBUG, TAG_STTD, " ");
365 return STTD_ERROR_NONE;
368 int __server_error_callback(stte_error_e error, const char* msg)
370 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Error Callback is called");
371 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
373 return STTD_ERROR_NONE;
376 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
378 if (NULL == engine_id) {
379 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
382 SLOG(LOG_DEBUG, TAG_STTD, "[Server] New default engine : %s", engine_id);
386 /* need to change state of app to ready */
388 uid = stt_client_get_current_recognition();
391 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%d)", uid);
393 sttd_server_cancel(uid);
394 sttdc_send_set_state(uid, (int)APP_STATE_READY);
396 stt_client_unset_current_recognition();
400 int ret = sttd_engine_agent_set_default_engine(engine_id);
402 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
404 if (NULL != language) {
405 ret = sttd_engine_agent_set_default_language(language);
407 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
410 ret = sttd_engine_agent_set_silence_detection(support_silence);
412 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
418 void __sttd_server_language_changed_cb(const char* language, void* user_data)
420 if (NULL == language) {
421 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
424 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get language changed : %s", language);
427 int ret = sttd_engine_agent_set_default_language(language);
429 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
434 void __sttd_server_silence_changed_cb(bool value, void* user_data)
436 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
439 ret = sttd_engine_agent_set_silence_detection(value);
441 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
449 int sttd_initialize(stte_request_callback_s *callback)
453 if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
454 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
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.");
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.");
466 ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
468 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
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);
476 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
481 ret = sttd_engine_agent_load_current_engine(callback);
483 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
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");
492 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] initialize");
499 if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
500 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
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.");
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);
516 sttd_recorder_deinitialize();
518 sttd_config_finalize();
520 sttd_engine_agent_release();
522 if (NULL != g_check_client_timer) {
523 ecore_timer_del(g_check_client_timer);
524 g_check_client_timer = NULL;
526 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete ecore timer handle");
529 return STTD_ERROR_NONE;
532 static void __read_proc()
535 struct dirent *dirp = 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);
547 dp = opendir("/proc");
549 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
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));
559 } while (NULL != dirp);
565 Eina_Bool sttd_cleanup_client(void *data)
567 int* client_list = NULL;
568 int client_count = 0;
573 if (0 != sttd_client_get_list(&client_list, &client_count)) {
574 if (NULL != client_list)
580 if (NULL != client_list) {
581 SLOG(LOG_DEBUG, TAG_STTD, "===== Clean up client ");
585 for (i = 0; i < client_count; i++) {
586 int pid = sttd_client_get_pid(client_list[i]);
588 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
594 for (j = 0; j < g_list_length(g_proc_list); j++) {
595 iter = g_list_nth(g_proc_list, j);
597 if (pid == GPOINTER_TO_INT(iter->data)) {
598 SLOG(LOG_DEBUG, TAG_STTD, "uid (%d) is running", client_list[i]);
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]);
610 result = sttdc_send_hello(client_list[i]);
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");
621 SLOG(LOG_DEBUG, TAG_STTD, "=====");
622 SLOG(LOG_DEBUG, TAG_STTD, " ");
631 * STT Server Functions for Client
634 int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential)
636 int ret = STTD_ERROR_NONE;
638 /* check if uid is valid */
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;
645 ret = sttd_engine_agent_get_option_supported(silence);
647 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
651 ret = sttd_engine_agent_is_credential_needed(uid, credential);
653 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
657 /* Add client information to client manager */
658 ret = sttd_client_add(pid, uid);
660 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
664 return STTD_ERROR_NONE;
667 static Eina_Bool __quit_ecore_loop(void *data)
669 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Quit");
671 stt_network_finalize();
673 sttd_dbus_close_connection();
674 ecore_main_loop_quit();
676 SLOG(LOG_DEBUG, TAG_STTD, "");
681 int sttd_server_finalize(int uid)
683 /* check if uid is valid */
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;
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;
699 if (APP_STATE_PROCESSING == state) {
700 if (NULL != g_processing_timer) {
701 ecore_timer_del(g_processing_timer);
702 g_processing_timer = NULL;
706 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
707 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
710 stt_client_unset_current_recognition();
713 /* Remove client information */
714 if (0 != sttd_client_delete(uid)) {
715 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
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);
724 return STTD_ERROR_NONE;
727 int sttd_server_get_supported_engines(int uid, GSList** engine_list)
729 /* Check if uid is valid */
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;
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;
743 ret = sttd_engine_agent_get_engine_list(engine_list);
745 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
749 return STTD_ERROR_NONE;
752 int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence, bool* credential)
754 /* Check if uid is valid */
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;
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;
768 ret = sttd_engine_agent_load_current_engine(NULL);
770 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
774 ret = sttd_engine_agent_get_option_supported(silence);
776 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
780 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
781 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
785 return STTD_ERROR_NONE;
788 int sttd_server_get_current_engine(int uid, char** engine_id)
790 /* Check if uid is valid */
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;
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;
804 ret = sttd_engine_agent_get_current_engine(engine_id);
806 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
810 return STTD_ERROR_NONE;
813 int sttd_server_check_app_agreed(int uid, const char* appid, bool* available)
815 /* Check if uid is valid */
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;
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;
828 /* Ask engine available */
831 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
833 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
838 stt_client_set_app_agreed(uid);
839 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
844 return STTD_ERROR_NONE;
848 int sttd_server_get_supported_languages(int uid, GSList** lang_list)
850 /* check if uid is valid */
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;
857 /* get language list from engine */
858 int ret = sttd_engine_agent_supported_langs(lang_list);
860 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
864 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
866 return STTD_ERROR_NONE;
869 int sttd_server_get_current_langauage(int uid, char** current_lang)
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 if (NULL == current_lang) {
879 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
880 return STTD_ERROR_INVALID_PARAMETER;
883 /*get current language from engine */
884 int ret = sttd_engine_agent_get_default_lang(current_lang);
886 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
890 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get default language");
892 return STTD_ERROR_NONE;
895 int sttd_server_set_private_data(int uid, const char* key, const char* data)
897 /* check if uid is valid */
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;
904 if (NULL == key || NULL == data) {
905 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
906 return STTD_ERROR_INVALID_PARAMETER;
909 /* set private data to engine */
911 ret = sttd_engine_agent_set_private_data(key, data);
913 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
917 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Set private data");
919 return STTD_ERROR_NONE;
922 int sttd_server_get_private_data(int uid, const char* key, char** data)
924 /* check if uid is valid */
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;
931 if (NULL == key || NULL == data) {
932 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
933 return STTD_ERROR_INVALID_PARAMETER;
936 /* get private data to engine */
938 ret = sttd_engine_agent_get_private_data(key, data);
940 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
944 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
946 return STTD_ERROR_NONE;
949 int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support)
951 /* check if uid is valid */
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;
958 if (NULL == type || NULL == support) {
959 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
960 return STTD_ERROR_INVALID_PARAMETER;
964 int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
966 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
970 *support = (int)temp;
972 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
974 return STTD_ERROR_NONE;
977 int sttd_server_set_start_sound(int uid, const char* file)
979 /* check if uid is valid */
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;
986 int ret = sttd_client_set_start_sound(uid, file);
988 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
995 int sttd_server_set_stop_sound(int uid, const char* file)
997 /* check if uid is valid */
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;
1004 int ret = sttd_client_set_stop_sound(uid, file);
1006 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1014 Eina_Bool __check_recording_state(void *data)
1017 int uid = stt_client_get_current_recognition();
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);
1029 if (APP_STATE_READY == state) {
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);
1040 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1048 Eina_Bool __stop_by_recording_timeout(void *data)
1050 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by timeout");
1052 if (NULL != g_recording_timer) {
1053 ecore_timer_del(g_recording_timer);
1054 g_recording_timer = NULL;
1058 uid = stt_client_get_current_recognition();
1060 /* cancel engine recognition */
1061 int ret = sttd_server_stop(uid);
1063 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1067 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1068 SLOG(LOG_DEBUG, TAG_STTD, " ");
1073 void __sttd_server_recorder_start(void* data)
1075 intptr_t puid = (intptr_t)data;
1076 int uid = (int)puid;
1077 int current_uid = stt_client_get_current_recognition();
1079 if (uid != current_uid) {
1080 stt_client_unset_current_recognition();
1084 int ret = sttd_engine_agent_recognize_start_recorder(uid);
1086 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1090 app_state_e temp_app_state;
1091 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1092 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1095 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1096 /* Notify uid state change */
1097 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1098 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1102 void __sttd_start_sound_completed_cb(int id, void *user_data)
1104 SLOG(LOG_DEBUG, TAG_STTD, "===== Start sound completed");
1106 /* After wav play callback, recorder start */
1107 ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1109 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1110 SLOG(LOG_DEBUG, TAG_STTD, " ");
1114 int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
1116 if (NULL == lang || NULL == recognition_type) {
1117 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1118 return STTD_ERROR_INVALID_PARAMETER;
1121 /* check if uid is valid */
1123 if (0 != sttd_client_get_state(uid, &state)) {
1124 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1125 return STTD_ERROR_INVALID_PARAMETER;
1128 /* check uid state */
1129 if (APP_STATE_READY != state) {
1130 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1131 return STTD_ERROR_INVALID_STATE;
1135 if (false == stt_client_get_app_agreed(uid)) {
1137 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1139 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1143 if (false == temp) {
1144 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1145 return STTD_ERROR_PERMISSION_DENIED;
1148 stt_client_set_app_agreed(uid);
1151 /* check if engine use network */
1152 if (true == sttd_engine_agent_need_network()) {
1153 if (false == stt_network_is_connected()) {
1154 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1155 return STTD_ERROR_OUT_OF_NETWORK;
1159 if (NULL != g_recording_timer) {
1160 ecore_timer_del(g_recording_timer);
1161 g_recording_timer = NULL;
1164 if (NULL != g_processing_timer) {
1165 ecore_timer_del(g_processing_timer);
1166 g_processing_timer = NULL;
1170 ret = sttd_client_get_start_sound(uid, &sound);
1172 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1176 if (0 != stt_client_set_current_recognition(uid)) {
1177 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1178 if (NULL != sound) free(sound);
1179 return STTD_ERROR_RECORDER_BUSY;
1182 /* engine start recognition */
1183 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)",
1184 uid, lang, recognition_type);
1186 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start sound : %s", sound);
1188 /* 1. Set audio session */
1189 ret = sttd_recorder_set_audio_session();
1191 stt_client_unset_current_recognition();
1192 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1193 if (NULL != sound) free(sound);
1197 bool is_sound_done = false;
1199 /* 2. Request wav play */
1200 if (NULL != sound) {
1202 intptr_t puid = (intptr_t)uid;
1203 sound_stream_info_h wav_stream_info_h;
1204 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1205 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1206 is_sound_done = true;
1208 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_start_sound_completed_cb, (void*)puid, &id);
1209 if (WAV_PLAYER_ERROR_NONE != ret) {
1210 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1211 is_sound_done = true;
1214 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1215 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1221 is_sound_done = true;
1224 /* 3. Create recorder & engine initialize */
1225 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1227 stt_client_unset_current_recognition();
1228 sttd_recorder_unset_audio_session();
1229 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1233 if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1234 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1237 /* change uid state */
1238 sttd_client_set_state(uid, APP_STATE_RECORDING);
1240 g_recording_log_count = 0;
1242 app_state_e temp_app_state;
1244 if (true == is_sound_done) {
1245 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1247 ret = sttd_engine_agent_recognize_start_recorder(uid);
1249 stt_client_unset_current_recognition();
1250 sttd_recorder_unset_audio_session();
1252 sttd_engine_agent_recognize_cancel();
1253 if (NULL != g_recording_timer) {
1254 ecore_timer_del(g_recording_timer);
1255 g_recording_timer = NULL;
1257 sttd_client_set_state(uid, APP_STATE_READY);
1259 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1263 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1264 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid");
1265 return STTD_ERROR_INVALID_PARAMETER;
1267 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1268 /* Notify uid state change */
1269 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1272 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1273 return STTD_RESULT_STATE_DONE;
1276 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Wait sound finish");
1278 return STTD_RESULT_STATE_NOT_DONE;
1281 Eina_Bool __time_out_for_processing(void *data)
1283 if (NULL != g_processing_timer) {
1284 ecore_timer_del(g_processing_timer);
1285 g_processing_timer = NULL;
1289 int uid = stt_client_get_current_recognition();
1290 if (0 == uid) return EINA_FALSE;
1293 int ret = sttd_engine_agent_recognize_cancel();
1295 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1298 if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1299 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1301 /* send error msg */
1302 int reason = (int)STTD_ERROR_TIMED_OUT;
1303 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1304 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1308 /* Change uid state */
1309 sttd_client_set_state(uid, APP_STATE_READY);
1311 stt_client_unset_current_recognition();
1316 void __sttd_server_engine_stop(void* data)
1318 intptr_t puid = (intptr_t)data;
1319 int uid = (int)puid;
1320 /* change uid state */
1321 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1323 /* Notify uid state change */
1324 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1326 /* Unset audio session */
1327 int ret = sttd_recorder_unset_audio_session();
1329 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1334 ret = sttd_engine_agent_recognize_stop_engine();
1336 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1340 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1343 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1345 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop sound completed");
1347 /* After wav play callback, engine stop */
1348 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1350 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1351 SLOG(LOG_DEBUG, TAG_STTD, " ");
1355 int sttd_server_stop(int uid)
1357 /* check if uid is valid */
1359 if (0 != sttd_client_get_state(uid, &state)) {
1360 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1361 return STTD_ERROR_INVALID_PARAMETER;
1364 /* check uid state */
1365 if (APP_STATE_RECORDING != state) {
1366 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1367 return STTD_ERROR_INVALID_STATE;
1370 if (NULL != g_recording_timer) {
1371 ecore_timer_del(g_recording_timer);
1372 g_recording_timer = NULL;
1375 if (NULL != g_processing_timer) {
1376 ecore_timer_del(g_processing_timer);
1377 g_processing_timer = NULL;
1381 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1382 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1383 return STTD_ERROR_OPERATION_FAILED;
1386 SLOG(LOG_DEBUG, TAG_STTD, "[Server] stop sound path : %s", sound);
1389 /* 1. Stop recorder */
1390 ret = sttd_engine_agent_recognize_stop_recorder();
1392 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1393 if (0 != sttd_engine_agent_recognize_cancel()) {
1394 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1396 if (NULL != sound) free(sound);
1400 /* 2. Request wav play */
1401 if (NULL != sound) {
1403 intptr_t puid = (intptr_t)uid;
1404 sound_stream_info_h wav_stream_info_h;
1405 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1406 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1408 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1409 if (WAV_PLAYER_ERROR_NONE != ret) {
1410 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1412 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1415 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1416 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1421 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1423 return STTD_RESULT_STATE_NOT_DONE;
1425 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1427 /* Unset audio session */
1428 ret = sttd_recorder_unset_audio_session();
1430 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1435 ret = sttd_engine_agent_recognize_stop_engine();
1437 stt_client_unset_current_recognition();
1438 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1442 /* change uid state */
1443 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1445 /* Notify uid state change */
1446 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1448 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1450 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1452 return STTD_RESULT_STATE_DONE;
1455 return STTD_ERROR_NONE;
1458 int sttd_server_cancel(int uid)
1460 /* check if uid is valid */
1462 if (0 != sttd_client_get_state(uid, &state)) {
1463 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1464 return STTD_ERROR_INVALID_PARAMETER;
1467 /* check uid state */
1468 if (APP_STATE_READY == state) {
1469 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1470 return STTD_ERROR_NONE;
1473 stt_client_unset_current_recognition();
1475 if (NULL != g_recording_timer) {
1476 ecore_timer_del(g_recording_timer);
1477 g_recording_timer = NULL;
1480 if (NULL != g_processing_timer) {
1481 ecore_timer_del(g_processing_timer);
1482 g_processing_timer = NULL;
1485 if (APP_STATE_RECORDING == state) {
1486 /* Unset audio session */
1487 int ret = sttd_recorder_unset_audio_session();
1489 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1494 /* change uid state */
1495 sttd_client_set_state(uid, APP_STATE_READY);
1497 /* cancel engine recognition */
1498 int ret = sttd_engine_agent_recognize_cancel();
1500 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1504 /* Notify uid state change */
1505 sttdc_send_set_state(uid, APP_STATE_READY);
1507 return STTD_ERROR_NONE;
1510 int sttd_server_start_file(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential,
1511 const char* filepath, stte_audio_type_e audio_type, int sample_rate)
1513 if (NULL == lang || NULL == recognition_type || NULL == filepath) {
1514 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1515 return STTD_ERROR_INVALID_PARAMETER;
1518 /* check if uid is valid */
1520 if (0 != sttd_client_get_state(uid, &state)) {
1521 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1522 return STTD_ERROR_INVALID_PARAMETER;
1525 /* check uid state */
1526 if (APP_STATE_READY != state) {
1527 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1528 return STTD_ERROR_INVALID_STATE;
1532 if (false == stt_client_get_app_agreed(uid)) {
1534 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1536 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1540 if (false == temp) {
1541 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1542 return STTD_ERROR_PERMISSION_DENIED;
1545 stt_client_set_app_agreed(uid);
1548 /* check if engine use network */
1549 if (true == sttd_engine_agent_need_network()) {
1550 if (false == stt_network_is_connected()) {
1551 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1552 return STTD_ERROR_OUT_OF_NETWORK;
1556 if (0 != stt_client_set_current_recognition(uid)) {
1557 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1558 return STTD_ERROR_RECORDER_BUSY;
1561 /* engine start recognition */
1562 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);
1564 /* 1. Set audio session */
1565 ret = sttd_recorder_set_audio_session();
1567 stt_client_unset_current_recognition();
1568 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1572 /* 2. Start engine to recognize */
1573 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1575 stt_client_unset_current_recognition();
1576 sttd_recorder_unset_audio_session();
1577 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start engine : result(%d)", ret);
1581 sttd_client_set_state(uid, APP_STATE_RECORDING);
1582 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1584 /* 3. Start to send pcm from file to engine */
1585 ret = sttd_engine_agent_recognize_start_file(uid, filepath);
1587 stt_client_unset_current_recognition();
1588 sttd_recorder_unset_audio_session();
1589 sttd_engine_agent_recognize_cancel();
1590 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start file : result(%d)", ret);
1594 /* 4. Stop to send pcm from file */
1595 ret = sttd_engine_agent_recognize_stop_file();
1597 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1598 stt_client_unset_current_recognition();
1599 sttd_recorder_unset_audio_session();
1600 sttd_engine_agent_recognize_cancel();
1604 /* 5. change & notify uid state */
1605 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1606 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1608 /* 6. Unset audio session */
1609 ret = sttd_recorder_unset_audio_session();
1611 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1612 stt_client_unset_current_recognition();
1616 /* 7. Stop engine */
1617 ret = sttd_engine_agent_recognize_stop_engine();
1619 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1620 stt_client_unset_current_recognition();
1624 return STTD_ERROR_NONE;
1627 int sttd_server_cancel_file(int uid)
1629 /* check if uid is valid */
1631 if (0 != sttd_client_get_state(uid, &state)) {
1632 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1633 return STTD_ERROR_INVALID_PARAMETER;
1636 /* check uid state */
1637 if (APP_STATE_READY == state) {
1638 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1639 return STTD_ERROR_NONE;
1642 stt_client_unset_current_recognition();
1644 if (NULL != g_recording_timer) {
1645 ecore_timer_del(g_recording_timer);
1646 g_recording_timer = NULL;
1649 if (NULL != g_processing_timer) {
1650 ecore_timer_del(g_processing_timer);
1651 g_processing_timer = NULL;
1654 if (APP_STATE_RECORDING == state) {
1655 /* Unset audio session */
1656 int ret = sttd_recorder_unset_audio_session();
1658 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1663 /* change uid state */
1664 sttd_client_set_state(uid, APP_STATE_READY);
1666 /* cancel engine recognition */
1667 int ret = sttd_engine_agent_recognize_cancel();
1669 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1673 /* Notify uid state change */
1674 sttdc_send_set_state(uid, APP_STATE_READY);
1676 return STTD_ERROR_NONE;