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_thread_main_loop_begin();
220 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
221 ecore_timer_del(g_processing_timer);
222 g_processing_timer = NULL;
223 ecore_thread_main_loop_end();
226 sttd_config_time_save();
227 sttd_config_time_reset();
229 sttd_recorder_clear();
231 sttd_client_set_state(uid, APP_STATE_READY);
232 stt_client_unset_current_recognition();
234 if (NULL == data || 0 == data_count) {
235 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
236 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
237 int reason = (int)STTD_ERROR_OPERATION_FAILED;
239 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
240 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
244 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
245 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
246 int reason = (int)STTD_ERROR_OPERATION_FAILED;
248 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
249 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
254 /* change state of uid */
255 // sttd_client_set_state(uid, APP_STATE_READY);
256 // stt_client_unset_current_recognition();
258 } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) {
259 SLOG(LOG_INFO, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event, data_count);
261 sttd_config_time_save();
262 sttd_config_time_reset();
264 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
265 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
266 int reason = (int)STTD_ERROR_OPERATION_FAILED;
268 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
269 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
273 } else if (STTE_RESULT_EVENT_ERROR == event) {
274 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
276 /* Delete timer for processing time out */
277 if (NULL != g_processing_timer) {
278 ecore_thread_main_loop_begin();
279 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
280 ecore_timer_del(g_processing_timer);
281 g_processing_timer = NULL;
282 ecore_thread_main_loop_end();
284 sttd_config_time_reset();
287 if (APP_STATE_RECORDING == state) {
288 ret = sttd_engine_agent_recognize_cancel();
290 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel: result(%d)", ret);
294 sttd_client_set_state(uid, APP_STATE_READY);
295 stt_client_unset_current_recognition();
297 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
298 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
301 int reason = (int)STTD_ERROR_INVALID_STATE;
302 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
303 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
307 /* change state of uid */
308 // sttd_client_set_state(uid, APP_STATE_READY);
309 // stt_client_unset_current_recognition();
314 SLOG(LOG_INFO, TAG_STTD, "=====");
315 SLOG(LOG_DEBUG, TAG_STTD, " ");
316 pthread_mutex_unlock(&stte_result_mutex);
318 return STTD_ERROR_NONE;
321 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)
323 pthread_mutex_lock(&stte_result_time_mutex);
325 SECURE_SLOG(LOG_INFO, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)",
326 index, event, text, start_time, end_time);
329 ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
331 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
332 pthread_mutex_unlock(&stte_result_time_mutex);
336 pthread_mutex_unlock(&stte_result_time_mutex);
341 int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
343 SLOG(LOG_INFO, TAG_STTD, "===== Speech status detected Callback");
345 int uid = stt_client_get_current_recognition();
348 if (0 != sttd_client_get_state(uid, &state)) {
349 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid ");
350 return STTD_ERROR_OPERATION_FAILED;
353 if (APP_STATE_RECORDING != state && APP_STATE_PROCESSING != state) {
354 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording, state(%d), status(%d)", state, status);
355 return STTD_ERROR_INVALID_STATE;
358 if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) {
359 SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected");
360 sttdc_send_speech_status(uid, status);
361 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
362 SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
363 ecore_main_loop_thread_safe_call_async(__stop_by_silence, NULL);
366 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recognition uid is not valid ");
369 SLOG(LOG_INFO, TAG_STTD, "=====");
370 SLOG(LOG_DEBUG, TAG_STTD, " ");
372 return STTD_ERROR_NONE;
375 int __server_error_callback(stte_error_e error, const char* msg)
377 SLOG(LOG_INFO, TAG_STTD, "[Server] Error Callback is called");
378 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
380 return STTD_ERROR_NONE;
383 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
385 if (NULL == engine_id) {
386 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
389 SLOG(LOG_INFO, TAG_STTD, "[Server] New default engine : %s", engine_id);
393 /* need to change state of app to ready */
395 uid = stt_client_get_current_recognition();
398 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%d)", uid);
400 sttd_server_cancel(uid);
401 sttdc_send_set_state(uid, (int)APP_STATE_READY);
403 stt_client_unset_current_recognition();
407 int ret = sttd_engine_agent_set_default_engine(engine_id);
409 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
411 if (NULL != language) {
412 ret = sttd_engine_agent_set_default_language(language);
414 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
417 ret = sttd_engine_agent_set_silence_detection(support_silence);
419 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
425 void __sttd_server_language_changed_cb(const char* language, void* user_data)
427 if (NULL == language) {
428 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
431 SLOG(LOG_INFO, TAG_STTD, "[Server] Get language changed : %s", language);
434 int ret = sttd_engine_agent_set_default_language(language);
436 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
441 void __sttd_server_silence_changed_cb(bool value, void* user_data)
443 SLOG(LOG_INFO, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
446 ret = sttd_engine_agent_set_silence_detection(value);
448 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
456 int sttd_initialize(stte_request_callback_s *callback)
460 if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
461 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
464 if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) {
465 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex.");
468 if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
469 __sttd_server_silence_changed_cb, NULL)) {
470 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
473 ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
475 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
479 /* Engine Agent initialize */
480 ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
481 __server_speech_status_callback, __server_error_callback);
483 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
488 ret = sttd_engine_agent_load_current_engine(callback);
490 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
494 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, sttd_cleanup_client, NULL);
495 if (NULL == g_check_client_timer) {
496 SLOG(LOG_WARN, TAG_STTD, "[Main Warning] Fail to create timer of client check");
499 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] initialize");
506 if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
507 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
510 if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) {
511 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex.");
515 if (0 < g_list_length(g_proc_list)) {
516 iter = g_list_first(g_proc_list);
517 while (NULL != iter) {
518 g_proc_list = g_list_remove_link(g_proc_list, iter);
519 iter = g_list_first(g_proc_list);
523 sttd_recorder_deinitialize();
525 sttd_config_finalize();
527 sttd_engine_agent_release();
529 if (NULL != g_check_client_timer) {
530 ecore_timer_del(g_check_client_timer);
531 g_check_client_timer = NULL;
533 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete ecore timer handle");
536 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] finalize");
538 return STTD_ERROR_NONE;
541 static void __read_proc()
544 struct dirent *dirp = NULL;
548 if (0 < g_list_length(g_proc_list)) {
549 iter = g_list_first(g_proc_list);
550 while (NULL != iter) {
551 g_proc_list = g_list_remove_link(g_proc_list, iter);
552 iter = g_list_first(g_proc_list);
556 dp = opendir("/proc");
558 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
564 tmp = atoi(dirp->d_name);
565 if (0 >= tmp) continue;
566 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
568 } while (NULL != dirp);
574 Eina_Bool sttd_cleanup_client(void *data)
576 int* client_list = NULL;
577 int client_count = 0;
582 if (0 != sttd_client_get_list(&client_list, &client_count)) {
583 if (NULL != client_list)
589 if (NULL != client_list) {
590 SLOG(LOG_INFO, TAG_STTD, "===== Clean up client ");
594 for (i = 0; i < client_count; i++) {
595 int pid = sttd_client_get_pid(client_list[i]);
597 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
603 for (j = 0; j < g_list_length(g_proc_list); j++) {
604 iter = g_list_nth(g_proc_list, j);
606 if (pid == GPOINTER_TO_INT(iter->data)) {
607 SLOG(LOG_DEBUG, TAG_STTD, "uid (%d) is running", client_list[i]);
614 if (false == exist) {
615 SLOG(LOG_ERROR, TAG_STTD, "uid (%d) should be removed", client_list[i]);
616 sttd_server_finalize(client_list[i]);
619 result = sttdc_send_hello(client_list[i]);
622 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", client_list[i]);
623 sttd_server_finalize(client_list[i]);
624 } else if (-1 == result) {
625 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
630 SLOG(LOG_INFO, TAG_STTD, "=====");
631 SLOG(LOG_DEBUG, TAG_STTD, " ");
640 * STT Server Functions for Client
643 int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential)
645 int ret = STTD_ERROR_NONE;
647 SLOG(LOG_INFO, TAG_STTD, "[Server] server initialize");
649 /* check if uid is valid */
651 if (0 == sttd_client_get_state(uid, &state)) {
652 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
653 return STTD_ERROR_NONE;
656 ret = sttd_engine_agent_get_option_supported(silence);
658 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
662 ret = sttd_engine_agent_is_credential_needed(uid, credential);
664 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
668 /* Add client information to client manager */
669 ret = sttd_client_add(pid, uid);
671 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
675 SLOG(LOG_INFO, TAG_STTD, "[Server Success] server initialize");
677 return STTD_ERROR_NONE;
680 static Eina_Bool __quit_ecore_loop(void *data)
682 SLOG(LOG_INFO, TAG_STTD, "[Server] Quit");
684 stt_network_finalize();
686 sttd_dbus_close_connection();
687 ecore_main_loop_quit();
689 SLOG(LOG_INFO, TAG_STTD, "");
694 int sttd_server_finalize(int uid)
696 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter Finalize");
698 /* check if uid is valid */
700 if (0 != sttd_client_get_state(uid, &state)) {
701 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
702 return STTD_ERROR_INVALID_PARAMETER;
705 /* release recorder */
706 if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
707 if (APP_STATE_RECORDING == state) {
708 if (NULL != g_recording_timer) {
709 ecore_timer_del(g_recording_timer);
710 g_recording_timer = NULL;
714 if (APP_STATE_PROCESSING == state) {
715 if (NULL != g_processing_timer) {
716 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
717 ecore_timer_del(g_processing_timer);
718 g_processing_timer = NULL;
722 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] stt_cancel is invoked while state is (%d)", state);
724 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
725 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
728 stt_client_unset_current_recognition();
731 /* Remove client information */
732 if (0 != sttd_client_delete(uid)) {
733 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
736 /* unload engine, if ref count of client is 0 */
737 if (0 == sttd_client_get_ref_count()) {
738 // sttd_dbus_close_connection();
739 ecore_timer_add(0, __quit_ecore_loop, NULL);
742 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End Finalize");
744 return STTD_ERROR_NONE;
747 int sttd_server_get_supported_engines(int uid, GSList** engine_list)
749 /* Check if uid is valid */
751 if (0 != sttd_client_get_state(uid, &state)) {
752 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
753 return STTD_ERROR_INVALID_PARAMETER;
756 /* Check state of uid */
757 if (APP_STATE_READY != state) {
758 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
759 return STTD_ERROR_INVALID_STATE;
762 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported egnines");
765 ret = sttd_engine_agent_get_engine_list(engine_list);
767 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
771 return STTD_ERROR_NONE;
774 int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence, bool* credential)
776 /* Check if uid is valid */
778 if (0 != sttd_client_get_state(uid, &state)) {
779 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
780 return STTD_ERROR_INVALID_PARAMETER;
783 /* Check state of uid */
784 if (APP_STATE_READY != state) {
785 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
786 return STTD_ERROR_INVALID_STATE;
789 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] set current egnines, uid(%d), engine_id(%s)", uid, engine_id);
792 ret = sttd_engine_agent_load_current_engine(NULL);
794 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
798 ret = sttd_engine_agent_get_option_supported(silence);
800 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
804 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
805 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
809 return STTD_ERROR_NONE;
812 int sttd_server_get_current_engine(int uid, char** engine_id)
814 /* Check if uid is valid */
816 if (0 != sttd_client_get_state(uid, &state)) {
817 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
818 return STTD_ERROR_INVALID_PARAMETER;
821 /* Check state of uid */
822 if (APP_STATE_READY != state) {
823 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
824 return STTD_ERROR_INVALID_STATE;
827 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get current egnines, uid(%d)", uid);
830 ret = sttd_engine_agent_get_current_engine(engine_id);
832 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
836 return STTD_ERROR_NONE;
839 int sttd_server_check_app_agreed(int uid, const char* appid, bool* available)
841 /* Check if uid is valid */
843 if (0 != sttd_client_get_state(uid, &state)) {
844 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
845 return STTD_ERROR_INVALID_PARAMETER;
848 /* Check state of uid */
849 if (APP_STATE_READY != state) {
850 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
851 return STTD_ERROR_INVALID_STATE;
854 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] check app agreed");
856 /* Ask engine available */
859 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
861 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
866 stt_client_set_app_agreed(uid);
867 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
872 return STTD_ERROR_NONE;
876 int sttd_server_get_supported_languages(int uid, GSList** lang_list)
878 /* check if uid is valid */
880 if (0 != sttd_client_get_state(uid, &state)) {
881 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
882 return STTD_ERROR_INVALID_PARAMETER;
885 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported languages");
887 /* get language list from engine */
888 int ret = sttd_engine_agent_supported_langs(lang_list);
890 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
894 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
896 return STTD_ERROR_NONE;
899 int sttd_server_get_current_langauage(int uid, char** current_lang)
901 /* check if uid is valid */
903 if (0 != sttd_client_get_state(uid, &state)) {
904 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
905 return STTD_ERROR_INVALID_PARAMETER;
908 if (NULL == current_lang) {
909 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
910 return STTD_ERROR_INVALID_PARAMETER;
913 /*get current language from engine */
914 int ret = sttd_engine_agent_get_default_lang(current_lang);
916 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
920 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get default language, current_lang(%s)", *current_lang);
922 return STTD_ERROR_NONE;
925 int sttd_server_set_private_data(int uid, const char* key, const char* data)
927 /* check if uid is valid */
929 if (0 != sttd_client_get_state(uid, &state)) {
930 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
931 return STTD_ERROR_INVALID_PARAMETER;
934 if (NULL == key || NULL == data) {
935 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
936 return STTD_ERROR_INVALID_PARAMETER;
939 /* set private data to engine */
941 ret = sttd_engine_agent_set_private_data(key, data);
943 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
947 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Set private data");
949 return STTD_ERROR_NONE;
952 int sttd_server_get_private_data(int uid, const char* key, char** data)
954 /* check if uid is valid */
956 if (0 != sttd_client_get_state(uid, &state)) {
957 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
958 return STTD_ERROR_INVALID_PARAMETER;
961 if (NULL == key || NULL == data) {
962 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
963 return STTD_ERROR_INVALID_PARAMETER;
966 /* get private data to engine */
968 ret = sttd_engine_agent_get_private_data(key, data);
970 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
974 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
976 return STTD_ERROR_NONE;
979 int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support)
981 /* check if uid is valid */
983 if (0 != sttd_client_get_state(uid, &state)) {
984 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
985 return STTD_ERROR_INVALID_PARAMETER;
988 if (NULL == type || NULL == support) {
989 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
990 return STTD_ERROR_INVALID_PARAMETER;
994 int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
996 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
1000 *support = (int)temp;
1002 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
1004 return STTD_ERROR_NONE;
1007 int sttd_server_set_start_sound(int uid, const char* file)
1009 /* check if uid is valid */
1011 if (0 != sttd_client_get_state(uid, &state)) {
1012 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1013 return STTD_ERROR_INVALID_PARAMETER;
1016 int ret = sttd_client_set_start_sound(uid, file);
1018 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1025 int sttd_server_set_stop_sound(int uid, const char* file)
1027 /* check if uid is valid */
1029 if (0 != sttd_client_get_state(uid, &state)) {
1030 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1031 return STTD_ERROR_INVALID_PARAMETER;
1034 int ret = sttd_client_set_stop_sound(uid, file);
1036 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1044 Eina_Bool __check_recording_state(void *data)
1047 int uid = stt_client_get_current_recognition();
1052 if (0 != sttdc_send_get_state(uid, (int*)&state)) {
1053 /* client is removed */
1054 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", uid);
1055 sttd_server_finalize(uid);
1059 if (APP_STATE_READY == state) {
1061 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The STT service should cancel recording", uid);
1062 sttd_server_cancel(uid);
1063 } else if (APP_STATE_PROCESSING == state) {
1064 /* Cancel stt and send change state */
1065 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The STT service should cancel recording", uid);
1066 sttd_server_cancel(uid);
1067 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1070 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1078 Eina_Bool __stop_by_recording_timeout(void *data)
1080 SLOG(LOG_INFO, TAG_STTD, "===== Stop by timeout");
1082 if (NULL != g_recording_timer) {
1083 ecore_timer_del(g_recording_timer);
1084 g_recording_timer = NULL;
1088 uid = stt_client_get_current_recognition();
1090 /* cancel engine recognition */
1091 int ret = sttd_server_stop(uid);
1093 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1097 SLOG(LOG_INFO, TAG_STTD, "=====");
1098 SLOG(LOG_DEBUG, TAG_STTD, " ");
1103 void __sttd_server_recorder_start(void* data)
1105 intptr_t puid = (intptr_t)data;
1106 int uid = (int)puid;
1107 int current_uid = stt_client_get_current_recognition();
1109 if (uid != current_uid) {
1110 stt_client_unset_current_recognition();
1114 int ret = sttd_engine_agent_recognize_start_recorder(uid);
1116 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1120 app_state_e temp_app_state;
1121 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1122 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1125 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1126 /* Notify uid state change */
1127 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1128 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1132 void __sttd_start_sound_completed_cb(int id, void *user_data)
1134 SLOG(LOG_INFO, TAG_STTD, "===== Start sound completed");
1136 /* After wav play callback, recorder start */
1137 ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1139 SLOG(LOG_INFO, TAG_STTD, "=====");
1140 SLOG(LOG_DEBUG, TAG_STTD, " ");
1144 int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
1146 if (NULL == lang || NULL == recognition_type) {
1147 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1148 return STTD_ERROR_INVALID_PARAMETER;
1151 /* check if uid is valid */
1153 if (0 != sttd_client_get_state(uid, &state)) {
1154 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1155 return STTD_ERROR_INVALID_PARAMETER;
1158 /* check uid state */
1159 if (APP_STATE_READY != state) {
1160 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1161 return STTD_ERROR_INVALID_STATE;
1165 if (false == stt_client_get_app_agreed(uid)) {
1167 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1169 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1173 if (false == temp) {
1174 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1175 return STTD_ERROR_PERMISSION_DENIED;
1178 stt_client_set_app_agreed(uid);
1181 /* check if engine use network */
1182 if (true == sttd_engine_agent_need_network()) {
1183 if (false == stt_network_is_connected()) {
1184 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1185 return STTD_ERROR_OUT_OF_NETWORK;
1189 if (NULL != g_recording_timer) {
1190 ecore_timer_del(g_recording_timer);
1191 g_recording_timer = NULL;
1194 if (NULL != g_processing_timer) {
1195 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1196 ecore_timer_del(g_processing_timer);
1197 g_processing_timer = NULL;
1201 ret = sttd_client_get_start_sound(uid, &sound);
1203 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1207 if (0 != stt_client_set_current_recognition(uid)) {
1208 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1209 if (NULL != sound) free(sound);
1210 return STTD_ERROR_RECORDER_BUSY;
1213 /* engine start recognition */
1214 SLOG(LOG_INFO, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)",
1215 uid, lang, recognition_type);
1217 SLOG(LOG_INFO, TAG_STTD, "[Server] start sound : %s", sound);
1219 /* 1. Set audio session */
1220 ret = sttd_recorder_set_audio_session();
1222 stt_client_unset_current_recognition();
1223 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1224 if (NULL != sound) free(sound);
1228 bool is_sound_done = false;
1230 /* 2. Request wav play */
1231 if (NULL != sound) {
1233 intptr_t puid = (intptr_t)uid;
1234 sound_stream_info_h wav_stream_info_h;
1235 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1236 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1237 is_sound_done = true;
1239 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_start_sound_completed_cb, (void*)puid, &id);
1240 if (WAV_PLAYER_ERROR_NONE != ret) {
1241 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1242 is_sound_done = true;
1245 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1246 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1252 is_sound_done = true;
1255 /* 3. Create recorder & engine initialize */
1256 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1258 stt_client_unset_current_recognition();
1259 sttd_recorder_unset_audio_session();
1260 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1264 if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1265 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1268 /* change uid state */
1269 sttd_client_set_state(uid, APP_STATE_RECORDING);
1271 g_recording_log_count = 0;
1273 app_state_e temp_app_state;
1275 if (true == is_sound_done) {
1276 SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
1278 ret = sttd_engine_agent_recognize_start_recorder(uid);
1280 stt_client_unset_current_recognition();
1281 sttd_recorder_unset_audio_session();
1283 sttd_engine_agent_recognize_cancel();
1284 if (NULL != g_recording_timer) {
1285 ecore_timer_del(g_recording_timer);
1286 g_recording_timer = NULL;
1288 sttd_client_set_state(uid, APP_STATE_READY);
1290 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1294 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1295 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid");
1296 return STTD_ERROR_INVALID_PARAMETER;
1298 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1299 /* Notify uid state change */
1300 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1303 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Start recognition");
1304 return STTD_RESULT_STATE_DONE;
1307 SLOG(LOG_INFO, TAG_STTD, "[Server] Wait sound finish");
1309 return STTD_RESULT_STATE_NOT_DONE;
1312 Eina_Bool __time_out_for_processing(void *data)
1314 if (NULL != g_processing_timer) {
1315 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1316 ecore_timer_del(g_processing_timer);
1317 g_processing_timer = NULL;
1320 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter __time_out_for_processing");
1323 int uid = stt_client_get_current_recognition();
1324 if (0 == uid) return EINA_FALSE;
1326 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by timeout for processing");
1329 int ret = sttd_engine_agent_recognize_cancel();
1331 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1334 if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1335 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1337 /* send error msg */
1338 int reason = (int)STTD_ERROR_TIMED_OUT;
1339 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1340 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1344 /* Change uid state */
1345 sttd_client_set_state(uid, APP_STATE_READY);
1347 stt_client_unset_current_recognition();
1349 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End __time_out_for_processing");
1354 void __sttd_server_engine_stop(void* data)
1356 intptr_t puid = (intptr_t)data;
1357 int uid = (int)puid;
1358 /* change uid state */
1359 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1361 /* Notify uid state change */
1362 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1364 /* Unset audio session */
1365 int ret = sttd_recorder_unset_audio_session();
1367 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1372 ret = sttd_engine_agent_recognize_stop_engine();
1374 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1378 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1381 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1383 SLOG(LOG_INFO, TAG_STTD, "===== Stop sound completed");
1385 /* After wav play callback, engine stop */
1386 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1388 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1389 SLOG(LOG_DEBUG, TAG_STTD, " ");
1393 int sttd_server_stop(int uid)
1395 /* check if uid is valid */
1397 if (0 != sttd_client_get_state(uid, &state)) {
1398 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1399 return STTD_ERROR_INVALID_PARAMETER;
1402 /* check uid state */
1403 if (APP_STATE_RECORDING != state) {
1404 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1405 return STTD_ERROR_INVALID_STATE;
1408 if (NULL != g_recording_timer) {
1409 ecore_timer_del(g_recording_timer);
1410 g_recording_timer = NULL;
1413 if (NULL != g_processing_timer) {
1414 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1415 ecore_timer_del(g_processing_timer);
1416 g_processing_timer = NULL;
1420 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1421 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1422 return STTD_ERROR_OPERATION_FAILED;
1425 SLOG(LOG_INFO, TAG_STTD, "[Server] stop sound path : %s", sound);
1428 /* 1. Stop recorder */
1429 ret = sttd_engine_agent_recognize_stop_recorder();
1431 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1432 if (0 != sttd_engine_agent_recognize_cancel()) {
1433 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1435 if (NULL != sound) free(sound);
1439 /* 2. Request wav play */
1440 if (NULL != sound) {
1442 intptr_t puid = (intptr_t)uid;
1443 sound_stream_info_h wav_stream_info_h;
1444 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1445 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1447 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1448 if (WAV_PLAYER_ERROR_NONE != ret) {
1449 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1451 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1454 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1455 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1460 SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
1461 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1463 return STTD_RESULT_STATE_NOT_DONE;
1465 SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
1467 /* Unset audio session */
1468 ret = sttd_recorder_unset_audio_session();
1470 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1475 ret = sttd_engine_agent_recognize_stop_engine();
1477 stt_client_unset_current_recognition();
1478 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1482 /* change uid state */
1483 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1485 /* Notify uid state change */
1486 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1488 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Stop recognition");
1490 SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
1491 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1493 return STTD_RESULT_STATE_DONE;
1496 return STTD_ERROR_NONE;
1499 int sttd_server_cancel(int uid)
1501 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel");
1503 /* check if uid is valid */
1505 if (0 != sttd_client_get_state(uid, &state)) {
1506 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1507 return STTD_ERROR_INVALID_PARAMETER;
1510 /* check uid state */
1511 if (APP_STATE_READY == state) {
1512 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1513 return STTD_ERROR_NONE;
1516 stt_client_unset_current_recognition();
1518 if (NULL != g_recording_timer) {
1519 ecore_timer_del(g_recording_timer);
1520 g_recording_timer = NULL;
1523 if (NULL != g_processing_timer) {
1524 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1525 ecore_timer_del(g_processing_timer);
1526 g_processing_timer = NULL;
1529 if (APP_STATE_RECORDING == state) {
1530 /* Unset audio session */
1531 int ret = sttd_recorder_unset_audio_session();
1533 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1538 /* change uid state */
1539 sttd_client_set_state(uid, APP_STATE_READY);
1541 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by app's request");
1543 /* cancel engine recognition */
1544 int ret = sttd_engine_agent_recognize_cancel();
1546 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1550 /* Notify uid state change */
1551 sttdc_send_set_state(uid, APP_STATE_READY);
1553 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel");
1555 return STTD_ERROR_NONE;
1558 int sttd_server_start_file(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential,
1559 const char* filepath, stte_audio_type_e audio_type, int sample_rate)
1561 if (NULL == lang || NULL == recognition_type || NULL == filepath) {
1562 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1563 return STTD_ERROR_INVALID_PARAMETER;
1566 /* check if uid is valid */
1568 if (0 != sttd_client_get_state(uid, &state)) {
1569 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1570 return STTD_ERROR_INVALID_PARAMETER;
1573 /* check uid state */
1574 if (APP_STATE_READY != state) {
1575 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1576 return STTD_ERROR_INVALID_STATE;
1580 if (false == stt_client_get_app_agreed(uid)) {
1582 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1584 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1588 if (false == temp) {
1589 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1590 return STTD_ERROR_PERMISSION_DENIED;
1593 stt_client_set_app_agreed(uid);
1596 /* check if engine use network */
1597 if (true == sttd_engine_agent_need_network()) {
1598 if (false == stt_network_is_connected()) {
1599 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1600 return STTD_ERROR_OUT_OF_NETWORK;
1604 if (0 != stt_client_set_current_recognition(uid)) {
1605 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1606 return STTD_ERROR_RECORDER_BUSY;
1609 /* engine start recognition */
1610 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);
1612 /* 1. Set audio session */
1613 ret = sttd_recorder_set_audio_session();
1615 stt_client_unset_current_recognition();
1616 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1620 /* 2. Start engine to recognize */
1621 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1623 stt_client_unset_current_recognition();
1624 sttd_recorder_unset_audio_session();
1625 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start engine : result(%d)", ret);
1629 sttd_client_set_state(uid, APP_STATE_RECORDING);
1630 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1632 /* 3. Start to send pcm from file to engine */
1633 ret = sttd_engine_agent_recognize_start_file(uid, filepath);
1635 stt_client_unset_current_recognition();
1636 sttd_recorder_unset_audio_session();
1637 sttd_engine_agent_recognize_cancel();
1638 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start file : result(%d)", ret);
1642 /* 4. Stop to send pcm from file */
1643 ret = sttd_engine_agent_recognize_stop_file();
1645 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1646 stt_client_unset_current_recognition();
1647 sttd_recorder_unset_audio_session();
1648 sttd_engine_agent_recognize_cancel();
1652 /* 5. change & notify uid state */
1653 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1654 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1656 /* 6. Unset audio session */
1657 ret = sttd_recorder_unset_audio_session();
1659 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1660 stt_client_unset_current_recognition();
1664 /* 7. Stop engine */
1665 ret = sttd_engine_agent_recognize_stop_engine();
1667 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1668 stt_client_unset_current_recognition();
1672 return STTD_ERROR_NONE;
1675 int sttd_server_cancel_file(int uid)
1677 /* check if uid is valid */
1679 if (0 != sttd_client_get_state(uid, &state)) {
1680 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1681 return STTD_ERROR_INVALID_PARAMETER;
1684 /* check uid state */
1685 if (APP_STATE_READY == state) {
1686 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1687 return STTD_ERROR_NONE;
1690 stt_client_unset_current_recognition();
1692 if (NULL != g_recording_timer) {
1693 ecore_timer_del(g_recording_timer);
1694 g_recording_timer = NULL;
1697 if (NULL != g_processing_timer) {
1698 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1699 ecore_timer_del(g_processing_timer);
1700 g_processing_timer = NULL;
1703 if (APP_STATE_RECORDING == state) {
1704 /* Unset audio session */
1705 int ret = sttd_recorder_unset_audio_session();
1707 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1712 /* change uid state */
1713 sttd_client_set_state(uid, APP_STATE_READY);
1715 /* cancel engine recognition */
1716 int ret = sttd_engine_agent_recognize_cancel();
1718 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1722 /* Notify uid state change */
1723 sttdc_send_set_state(uid, APP_STATE_READY);
1725 return STTD_ERROR_NONE;