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.
16 #include <sound_manager.h>
17 #include <wav_player.h>
19 #include "stt_network.h"
20 #include "sttd_client_data.h"
21 #include "sttd_config.h"
22 #include "sttd_dbus.h"
23 #include "sttd_engine_agent.h"
24 #include "sttd_main.h"
25 #include "sttd_recorder.h"
26 #include "sttd_server.h"
29 #define CLIENT_CLEAN_UP_TIME 500
32 static pthread_mutex_t stte_result_mutex = PTHREAD_MUTEX_INITIALIZER;
33 static pthread_mutex_t stte_result_time_mutex = PTHREAD_MUTEX_INITIALIZER;
37 * STT Server static variable
39 static double g_processing_timeout = 30;
41 static double g_recording_timeout = 60;
43 static Ecore_Timer* g_check_client_timer = NULL;
44 Ecore_Timer* g_recording_timer = NULL;
45 Ecore_Timer* g_processing_timer = NULL;
47 static int g_recording_log_count = 0;
49 static GList *g_proc_list = NULL;
52 * STT Server Callback Functions
54 static void __cancel_recognition_internal();
56 Eina_Bool __stop_by_silence(void *data)
58 SLOG(LOG_INFO, TAG_STTD, "===== Stop by silence detection");
62 uid = stt_client_get_current_recognition();
66 ret = sttd_server_stop(uid);
68 __cancel_recognition_internal();
72 if (STTD_RESULT_STATE_DONE == ret) {
73 ret = sttdc_send_set_state(uid, (int)APP_STATE_PROCESSING);
75 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send state : result(%d)", ret);
78 sttd_server_finalize(uid);
79 stt_client_unset_current_recognition();
83 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
86 SLOG(LOG_INFO, TAG_STTD, "=====");
87 SLOG(LOG_DEBUG, TAG_STTD, " ");
92 static void __cancel_recognition_internal()
94 if (NULL != g_recording_timer) {
95 ecore_timer_del(g_recording_timer);
96 g_recording_timer = NULL;
101 uid = stt_client_get_current_recognition();
103 app_state_e state = 0;
104 ret = sttd_client_get_state(uid, &state);
107 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
111 if (0 != uid && (APP_STATE_PROCESSING == state || APP_STATE_RECORDING == state)) {
112 SLOG(LOG_INFO, TAG_STTD, "===== cancel by internal");
113 /* cancel engine recognition */
114 ret = sttd_server_cancel(uid);
116 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
119 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
123 static void __cancel_by_error(void *data)
125 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by error");
127 __cancel_recognition_internal();
129 SLOG(LOG_DEBUG, TAG_STTD, "=====");
130 SLOG(LOG_DEBUG, TAG_STTD, " ");
135 int __server_audio_recorder_callback(const void* data, const unsigned int length)
140 if (NULL == data || 0 == length) {
141 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Recording data is not valid");
142 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
146 uid = stt_client_get_current_recognition();
148 ret = sttd_engine_agent_set_recording_data(data, length);
150 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
153 g_recording_log_count++;
154 if (200 <= g_recording_log_count) {
155 SLOG(LOG_DEBUG, TAG_STTD, "=== Set recording data ===");
156 g_recording_log_count = 0;
159 if (NULL != g_recording_timer) {
160 ecore_timer_del(g_recording_timer);
161 g_recording_timer = NULL;
163 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current uid in recording is is not valid");
170 void __server_audio_interrupt_callback()
172 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by sound interrupt");
174 __cancel_recognition_internal();
176 SLOG(LOG_DEBUG, TAG_STTD, "=====");
177 SLOG(LOG_DEBUG, TAG_STTD, " ");
180 void __cancel_by_no_record(void *data)
182 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by no record");
184 __cancel_recognition_internal();
186 SLOG(LOG_DEBUG, TAG_STTD, "=====");
187 SLOG(LOG_DEBUG, TAG_STTD, " ");
192 int __server_recognition_result_callback(stte_result_event_e event, const char* type,
193 const char** data, int data_count, const char* msg, void *user_data)
196 pthread_mutex_lock(&stte_result_mutex);
198 SLOG(LOG_INFO, TAG_STTD, "===== RESULT event[%d] type[%s] data[%p] data_count[%d]", event, type, data, data_count);
201 int uid = stt_client_get_current_recognition();
204 if (0 == uid || 0 != sttd_client_get_state(uid, &state)) {
205 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
206 SLOG(LOG_DEBUG, TAG_STTD, "=====");
207 SLOG(LOG_DEBUG, TAG_STTD, " ");
208 pthread_mutex_unlock(&stte_result_mutex);
209 return STTD_ERROR_OPERATION_FAILED;
212 SLOG(LOG_INFO, TAG_STTD, "[Server] uid (%d), event(%d)", uid, event);
214 /* send result to client */
215 if (STTE_RESULT_EVENT_FINAL_RESULT == event) {
216 if (APP_STATE_PROCESSING != state) {
217 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is NOT 'Processing'.");
219 SLOG(LOG_INFO, TAG_STTD, "[Server] the size of result from engine is '%d'", data_count);
221 /* Delete timer for processing time out */
222 if (NULL != g_processing_timer) {
223 ecore_thread_main_loop_begin();
224 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
225 ecore_timer_del(g_processing_timer);
226 g_processing_timer = NULL;
227 ecore_thread_main_loop_end();
230 sttd_config_time_save();
231 sttd_config_time_reset();
233 sttd_recorder_clear();
235 sttd_client_set_state(uid, APP_STATE_READY);
236 stt_client_unset_current_recognition();
238 if (NULL == data || 0 == data_count) {
239 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
240 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
241 int reason = (int)STTD_ERROR_OPERATION_FAILED;
243 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
244 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
248 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
249 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
250 int reason = (int)STTD_ERROR_OPERATION_FAILED;
252 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
253 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
258 /* change state of uid */
259 // sttd_client_set_state(uid, APP_STATE_READY);
260 // stt_client_unset_current_recognition();
262 } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) {
263 SLOG(LOG_INFO, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event, data_count);
265 sttd_config_time_save();
266 sttd_config_time_reset();
268 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
269 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
270 int reason = (int)STTD_ERROR_OPERATION_FAILED;
272 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
273 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
277 } else if (STTE_RESULT_EVENT_ERROR == event) {
278 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
280 /* Delete timer for processing time out */
281 if (NULL != g_processing_timer) {
282 ecore_thread_main_loop_begin();
283 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
284 ecore_timer_del(g_processing_timer);
285 g_processing_timer = NULL;
286 ecore_thread_main_loop_end();
288 sttd_config_time_reset();
291 if (APP_STATE_RECORDING == state) {
292 ret = sttd_engine_agent_recognize_cancel();
294 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel: result(%d)", ret);
298 sttd_client_set_state(uid, APP_STATE_READY);
299 stt_client_unset_current_recognition();
301 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
302 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
305 int reason = (int)STTD_ERROR_INVALID_STATE;
306 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
307 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
311 /* change state of uid */
312 // sttd_client_set_state(uid, APP_STATE_READY);
313 // stt_client_unset_current_recognition();
318 SLOG(LOG_INFO, TAG_STTD, "=====");
319 SLOG(LOG_DEBUG, TAG_STTD, " ");
320 pthread_mutex_unlock(&stte_result_mutex);
322 return STTD_ERROR_NONE;
325 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)
327 pthread_mutex_lock(&stte_result_time_mutex);
329 SECURE_SLOG(LOG_INFO, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)",
330 index, event, text, start_time, end_time);
333 ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
335 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
336 pthread_mutex_unlock(&stte_result_time_mutex);
340 pthread_mutex_unlock(&stte_result_time_mutex);
345 int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
347 SLOG(LOG_INFO, TAG_STTD, "===== Speech status detected Callback");
349 int uid = stt_client_get_current_recognition();
352 if (0 != sttd_client_get_state(uid, &state)) {
353 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid ");
354 return STTD_ERROR_OPERATION_FAILED;
357 if (APP_STATE_RECORDING != state && APP_STATE_PROCESSING != state) {
358 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording, state(%d), status(%d)", state, status);
359 return STTD_ERROR_INVALID_STATE;
362 if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) {
363 SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected");
364 sttdc_send_speech_status(uid, status);
365 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
366 SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
367 ecore_thread_main_loop_begin();
368 ecore_timer_add(0, __stop_by_silence, NULL);
369 ecore_thread_main_loop_end();
372 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recognition uid is not valid ");
375 SLOG(LOG_INFO, TAG_STTD, "=====");
376 SLOG(LOG_DEBUG, TAG_STTD, " ");
378 return STTD_ERROR_NONE;
381 int __server_error_callback(stte_error_e error, const char* msg)
383 SLOG(LOG_INFO, TAG_STTD, "[Server] Error Callback is called, error(%d), err_msg(%s)", error, msg);
384 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
386 return STTD_ERROR_NONE;
389 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
391 if (NULL == engine_id) {
392 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
395 SLOG(LOG_INFO, TAG_STTD, "[Server] New default engine : %s", engine_id);
399 /* need to change state of app to ready */
401 uid = stt_client_get_current_recognition();
404 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%d)", uid);
406 sttd_server_cancel(uid);
407 sttdc_send_set_state(uid, (int)APP_STATE_READY);
409 stt_client_unset_current_recognition();
413 int ret = sttd_engine_agent_set_default_engine(engine_id);
415 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
417 if (NULL != language) {
418 ret = sttd_engine_agent_set_default_language(language);
420 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
423 ret = sttd_engine_agent_set_silence_detection(support_silence);
425 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
431 void __sttd_server_language_changed_cb(const char* language, void* user_data)
433 if (NULL == language) {
434 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
437 SLOG(LOG_INFO, TAG_STTD, "[Server] Get language changed : %s", language);
440 int ret = sttd_engine_agent_set_default_language(language);
442 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
447 void __sttd_server_silence_changed_cb(bool value, void* user_data)
449 SLOG(LOG_INFO, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
452 ret = sttd_engine_agent_set_silence_detection(value);
454 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
462 int sttd_initialize(stte_request_callback_s *callback)
466 if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
467 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
470 if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) {
471 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex.");
474 if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
475 __sttd_server_silence_changed_cb, NULL)) {
476 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
479 ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
481 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
485 /* Engine Agent initialize */
486 ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
487 __server_speech_status_callback, __server_error_callback);
489 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
494 ret = sttd_engine_agent_load_current_engine(callback);
496 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
500 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, sttd_cleanup_client, NULL);
501 if (NULL == g_check_client_timer) {
502 SLOG(LOG_WARN, TAG_STTD, "[Main Warning] Fail to create timer of client check");
505 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] initialize");
512 if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
513 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
516 if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) {
517 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex.");
521 if (0 < g_list_length(g_proc_list)) {
522 iter = g_list_first(g_proc_list);
523 while (NULL != iter) {
524 g_proc_list = g_list_delete_link(g_proc_list, iter);
525 iter = g_list_first(g_proc_list);
529 sttd_recorder_deinitialize();
531 sttd_config_finalize();
533 sttd_engine_agent_release();
535 if (NULL != g_check_client_timer) {
536 ecore_timer_del(g_check_client_timer);
537 g_check_client_timer = NULL;
539 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete ecore timer handle");
542 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] finalize");
544 return STTD_ERROR_NONE;
547 static void __read_proc()
550 struct dirent *dirp = NULL;
554 if (0 < g_list_length(g_proc_list)) {
555 iter = g_list_first(g_proc_list);
556 while (NULL != iter) {
557 g_proc_list = g_list_delete_link(g_proc_list, iter);
558 iter = g_list_first(g_proc_list);
562 dp = opendir("/proc");
564 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
570 tmp = atoi(dirp->d_name);
571 if (0 >= tmp) continue;
572 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
574 } while (NULL != dirp);
580 Eina_Bool sttd_cleanup_client(void *data)
582 int* client_list = NULL;
583 int client_count = 0;
588 if (0 != sttd_client_get_list(&client_list, &client_count)) {
589 if (NULL != client_list)
595 if (NULL != client_list) {
596 SLOG(LOG_INFO, TAG_STTD, "===== Clean up client ");
600 for (i = 0; i < client_count; i++) {
601 int pid = sttd_client_get_pid(client_list[i]);
603 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
609 for (j = 0; j < g_list_length(g_proc_list); j++) {
610 iter = g_list_nth(g_proc_list, j);
612 if (pid == GPOINTER_TO_INT(iter->data)) {
613 SLOG(LOG_DEBUG, TAG_STTD, "uid (%d) is running", client_list[i]);
620 if (false == exist) {
621 SLOG(LOG_ERROR, TAG_STTD, "uid (%d) should be removed", client_list[i]);
622 sttd_server_finalize(client_list[i]);
625 result = sttdc_send_hello(client_list[i]);
628 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", client_list[i]);
629 sttd_server_finalize(client_list[i]);
630 } else if (-1 == result) {
631 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
636 SLOG(LOG_INFO, TAG_STTD, "=====");
637 SLOG(LOG_DEBUG, TAG_STTD, " ");
646 * STT Server Functions for Client
649 int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential)
651 int ret = STTD_ERROR_NONE;
653 SLOG(LOG_INFO, TAG_STTD, "[Server] server initialize");
655 /* check if uid is valid */
657 if (0 == sttd_client_get_state(uid, &state)) {
658 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
659 return STTD_ERROR_NONE;
662 ret = sttd_engine_agent_get_option_supported(silence);
664 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
668 ret = sttd_engine_agent_is_credential_needed(uid, credential);
670 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
674 /* Add client information to client manager */
675 ret = sttd_client_add(pid, uid);
677 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
681 SLOG(LOG_INFO, TAG_STTD, "[Server Success] server initialize");
683 return STTD_ERROR_NONE;
686 static Eina_Bool __quit_ecore_loop(void *data)
688 SLOG(LOG_INFO, TAG_STTD, "[Server] Quit");
690 stt_network_finalize();
692 sttd_dbus_close_connection();
693 ecore_main_loop_quit();
695 SLOG(LOG_INFO, TAG_STTD, "");
700 int sttd_server_finalize(int uid)
702 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter Finalize");
704 /* check if uid is valid */
706 if (0 != sttd_client_get_state(uid, &state)) {
707 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
708 return STTD_ERROR_INVALID_PARAMETER;
711 /* release recorder */
712 if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
713 if (APP_STATE_RECORDING == state) {
714 if (NULL != g_recording_timer) {
715 ecore_timer_del(g_recording_timer);
716 g_recording_timer = NULL;
720 if (APP_STATE_PROCESSING == state) {
721 if (NULL != g_processing_timer) {
722 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
723 ecore_timer_del(g_processing_timer);
724 g_processing_timer = NULL;
728 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] stt_cancel is invoked while state is (%d)", state);
730 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
731 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
734 stt_client_unset_current_recognition();
737 /* Remove client information */
738 if (0 != sttd_client_delete(uid)) {
739 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
742 /* unload engine, if ref count of client is 0 */
743 if (0 == sttd_client_get_ref_count()) {
744 // sttd_dbus_close_connection();
745 ecore_timer_add(0, __quit_ecore_loop, NULL);
748 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End Finalize");
750 return STTD_ERROR_NONE;
753 int sttd_server_get_supported_engines(int uid, GSList** engine_list)
755 /* Check if uid is valid */
757 if (0 != sttd_client_get_state(uid, &state)) {
758 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
759 return STTD_ERROR_INVALID_PARAMETER;
762 /* Check state of uid */
763 if (APP_STATE_READY != state) {
764 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
765 return STTD_ERROR_INVALID_STATE;
768 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported egnines");
771 ret = sttd_engine_agent_get_engine_list(engine_list);
773 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
777 return STTD_ERROR_NONE;
780 int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence, bool* credential)
782 /* Check if uid is valid */
784 if (0 != sttd_client_get_state(uid, &state)) {
785 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
786 return STTD_ERROR_INVALID_PARAMETER;
789 /* Check state of uid */
790 if (APP_STATE_READY != state) {
791 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
792 return STTD_ERROR_INVALID_STATE;
795 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] set current egnines, uid(%d), engine_id(%s)", uid, engine_id);
798 ret = sttd_engine_agent_load_current_engine(NULL);
800 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
804 ret = sttd_engine_agent_get_option_supported(silence);
806 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
810 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
811 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
815 return STTD_ERROR_NONE;
818 int sttd_server_get_current_engine(int uid, char** engine_id)
820 /* Check if uid is valid */
822 if (0 != sttd_client_get_state(uid, &state)) {
823 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
824 return STTD_ERROR_INVALID_PARAMETER;
827 /* Check state of uid */
828 if (APP_STATE_READY != state) {
829 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
830 return STTD_ERROR_INVALID_STATE;
833 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get current egnines, uid(%d)", uid);
836 ret = sttd_engine_agent_get_current_engine(engine_id);
838 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
842 return STTD_ERROR_NONE;
845 int sttd_server_check_app_agreed(int uid, const char* appid, bool* available)
847 /* Check if uid is valid */
849 if (0 != sttd_client_get_state(uid, &state)) {
850 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
851 return STTD_ERROR_INVALID_PARAMETER;
854 /* Check state of uid */
855 if (APP_STATE_READY != state) {
856 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
857 return STTD_ERROR_INVALID_STATE;
860 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] check app agreed");
862 /* Ask engine available */
865 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
867 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
872 stt_client_set_app_agreed(uid);
873 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
878 return STTD_ERROR_NONE;
882 int sttd_server_get_supported_languages(int uid, GSList** lang_list)
884 /* check if uid is valid */
886 if (0 != sttd_client_get_state(uid, &state)) {
887 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
888 return STTD_ERROR_INVALID_PARAMETER;
891 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported languages");
893 /* get language list from engine */
894 int ret = sttd_engine_agent_supported_langs(lang_list);
896 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
900 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
902 return STTD_ERROR_NONE;
905 int sttd_server_get_current_langauage(int uid, char** current_lang)
907 /* check if uid is valid */
909 if (0 != sttd_client_get_state(uid, &state)) {
910 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
911 return STTD_ERROR_INVALID_PARAMETER;
914 if (NULL == current_lang) {
915 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
916 return STTD_ERROR_INVALID_PARAMETER;
919 /*get current language from engine */
920 int ret = sttd_engine_agent_get_default_lang(current_lang);
922 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
926 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get default language, current_lang(%s)", *current_lang);
928 return STTD_ERROR_NONE;
931 int sttd_server_set_private_data(int uid, const char* key, const char* data)
933 /* check if uid is valid */
935 if (0 != sttd_client_get_state(uid, &state)) {
936 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
937 return STTD_ERROR_INVALID_PARAMETER;
940 if (NULL == key || NULL == data) {
941 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
942 return STTD_ERROR_INVALID_PARAMETER;
945 /* set private data to engine */
947 ret = sttd_engine_agent_set_private_data(key, data);
949 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
953 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Set private data");
955 return STTD_ERROR_NONE;
958 int sttd_server_get_private_data(int uid, const char* key, char** data)
960 /* check if uid is valid */
962 if (0 != sttd_client_get_state(uid, &state)) {
963 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
964 return STTD_ERROR_INVALID_PARAMETER;
967 if (NULL == key || NULL == data) {
968 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
969 return STTD_ERROR_INVALID_PARAMETER;
972 /* get private data to engine */
974 ret = sttd_engine_agent_get_private_data(key, data);
976 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
980 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
982 return STTD_ERROR_NONE;
985 int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support)
987 /* check if uid is valid */
989 if (0 != sttd_client_get_state(uid, &state)) {
990 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
991 return STTD_ERROR_INVALID_PARAMETER;
994 if (NULL == type || NULL == support) {
995 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
996 return STTD_ERROR_INVALID_PARAMETER;
1000 int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
1002 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
1006 *support = (int)temp;
1008 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
1010 return STTD_ERROR_NONE;
1013 int sttd_server_set_start_sound(int uid, const char* file)
1015 /* check if uid is valid */
1017 if (0 != sttd_client_get_state(uid, &state)) {
1018 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1019 return STTD_ERROR_INVALID_PARAMETER;
1022 int ret = sttd_client_set_start_sound(uid, file);
1024 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1031 int sttd_server_set_stop_sound(int uid, const char* file)
1033 /* check if uid is valid */
1035 if (0 != sttd_client_get_state(uid, &state)) {
1036 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1037 return STTD_ERROR_INVALID_PARAMETER;
1040 int ret = sttd_client_set_stop_sound(uid, file);
1042 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1050 Eina_Bool __check_recording_state(void *data)
1053 int uid = stt_client_get_current_recognition();
1058 if (0 != sttdc_send_get_state(uid, (int*)&state)) {
1059 /* client is removed */
1060 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", uid);
1061 sttd_server_finalize(uid);
1065 if (APP_STATE_READY == state) {
1067 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The STT service should cancel recording", uid);
1068 sttd_server_cancel(uid);
1069 } else if (APP_STATE_PROCESSING == state) {
1070 /* Cancel stt and send change state */
1071 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The STT service should cancel recording", uid);
1072 sttd_server_cancel(uid);
1073 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1076 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1084 Eina_Bool __stop_by_recording_timeout(void *data)
1086 SLOG(LOG_INFO, TAG_STTD, "===== Stop by timeout");
1088 if (NULL != g_recording_timer) {
1089 ecore_timer_del(g_recording_timer);
1090 g_recording_timer = NULL;
1094 uid = stt_client_get_current_recognition();
1096 /* cancel engine recognition */
1097 int ret = sttd_server_stop(uid);
1099 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1103 SLOG(LOG_INFO, TAG_STTD, "=====");
1104 SLOG(LOG_DEBUG, TAG_STTD, " ");
1109 void __sttd_server_recorder_start(void* data)
1111 intptr_t puid = (intptr_t)data;
1112 int uid = (int)puid;
1113 int current_uid = stt_client_get_current_recognition();
1115 if (uid != current_uid) {
1116 stt_client_unset_current_recognition();
1120 char appid[1024] = {0, };
1121 int pid = sttd_client_get_pid(uid);
1123 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1);
1124 if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1125 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to get application ID");
1127 SLOG(LOG_DEBUG, TAG_STTD, "[DEBUG] Current app id is %s", appid);
1130 ret = sttd_engine_agent_recognize_start_recorder(uid, appid);
1132 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1136 app_state_e temp_app_state;
1137 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1138 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1141 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1142 /* Notify uid state change */
1143 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1144 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1148 void __sttd_start_sound_completed_cb(int id, void *user_data)
1150 SLOG(LOG_INFO, TAG_STTD, "===== Start sound completed");
1152 /* After wav play callback, recorder start */
1153 ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1155 SLOG(LOG_INFO, TAG_STTD, "=====");
1156 SLOG(LOG_DEBUG, TAG_STTD, " ");
1160 int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
1162 if (NULL == lang || NULL == recognition_type) {
1163 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1164 return STTD_ERROR_INVALID_PARAMETER;
1167 /* check if uid is valid */
1169 if (0 != sttd_client_get_state(uid, &state)) {
1170 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1171 return STTD_ERROR_INVALID_PARAMETER;
1174 /* check uid state */
1175 if (APP_STATE_READY != state) {
1176 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1177 return STTD_ERROR_INVALID_STATE;
1181 if (false == stt_client_get_app_agreed(uid)) {
1183 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1185 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1189 if (false == temp) {
1190 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1191 return STTD_ERROR_PERMISSION_DENIED;
1194 stt_client_set_app_agreed(uid);
1197 /* check if engine use network */
1198 if (true == sttd_engine_agent_need_network()) {
1199 if (false == stt_network_is_connected()) {
1200 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1201 return STTD_ERROR_OUT_OF_NETWORK;
1205 if (NULL != g_recording_timer) {
1206 ecore_timer_del(g_recording_timer);
1207 g_recording_timer = NULL;
1210 if (NULL != g_processing_timer) {
1211 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1212 ecore_timer_del(g_processing_timer);
1213 g_processing_timer = NULL;
1217 ret = sttd_client_get_start_sound(uid, &sound);
1219 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1223 if (0 != stt_client_set_current_recognition(uid)) {
1224 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1225 if (NULL != sound) free(sound);
1226 return STTD_ERROR_RECORDER_BUSY;
1229 /* engine start recognition */
1230 SLOG(LOG_INFO, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)",
1231 uid, lang, recognition_type);
1233 SLOG(LOG_INFO, TAG_STTD, "[Server] start sound : %s", sound);
1235 /* 1. Set audio session */
1236 ret = sttd_recorder_set_audio_session();
1238 stt_client_unset_current_recognition();
1239 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1240 if (NULL != sound) free(sound);
1244 bool is_sound_done = false;
1246 /* 2. Request wav play */
1247 if (NULL != sound) {
1249 intptr_t puid = (intptr_t)uid;
1250 sound_stream_info_h wav_stream_info_h;
1251 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1252 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1253 is_sound_done = true;
1255 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_start_sound_completed_cb, (void*)puid, &id);
1256 if (WAV_PLAYER_ERROR_NONE != ret) {
1257 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1258 is_sound_done = true;
1261 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1262 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1268 is_sound_done = true;
1271 /* 3. Create recorder & engine initialize */
1272 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1274 stt_client_unset_current_recognition();
1275 sttd_recorder_unset_audio_session();
1276 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1280 if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1281 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1284 /* change uid state */
1285 sttd_client_set_state(uid, APP_STATE_RECORDING);
1287 g_recording_log_count = 0;
1289 app_state_e temp_app_state;
1291 if (true == is_sound_done) {
1292 SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
1294 ret = sttd_engine_agent_recognize_start_recorder(uid, appid);
1296 stt_client_unset_current_recognition();
1297 sttd_recorder_unset_audio_session();
1299 sttd_engine_agent_recognize_cancel();
1300 if (NULL != g_recording_timer) {
1301 ecore_timer_del(g_recording_timer);
1302 g_recording_timer = NULL;
1304 sttd_client_set_state(uid, APP_STATE_READY);
1306 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1310 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1311 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid");
1312 return STTD_ERROR_INVALID_PARAMETER;
1314 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1315 /* Notify uid state change */
1316 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1319 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Start recognition");
1320 return STTD_RESULT_STATE_DONE;
1323 SLOG(LOG_INFO, TAG_STTD, "[Server] Wait sound finish");
1325 return STTD_RESULT_STATE_NOT_DONE;
1328 Eina_Bool __time_out_for_processing(void *data)
1330 if (NULL != g_processing_timer) {
1331 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1332 ecore_timer_del(g_processing_timer);
1333 g_processing_timer = NULL;
1336 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter __time_out_for_processing");
1339 int uid = stt_client_get_current_recognition();
1340 if (0 == uid) return EINA_FALSE;
1342 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by timeout for processing");
1345 int ret = sttd_engine_agent_recognize_cancel();
1347 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1350 if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1351 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1353 /* send error msg */
1354 int reason = (int)STTD_ERROR_TIMED_OUT;
1355 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1356 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1360 /* Change uid state */
1361 sttd_client_set_state(uid, APP_STATE_READY);
1363 stt_client_unset_current_recognition();
1365 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End __time_out_for_processing");
1370 void __sttd_server_engine_stop(void* data)
1372 intptr_t puid = (intptr_t)data;
1373 int uid = (int)puid;
1374 /* change uid state */
1375 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1377 /* Notify uid state change */
1378 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1380 /* Unset audio session */
1381 int ret = sttd_recorder_unset_audio_session();
1383 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1388 ret = sttd_engine_agent_recognize_stop_engine();
1390 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1394 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1397 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1399 SLOG(LOG_INFO, TAG_STTD, "===== Stop sound completed");
1401 /* After wav play callback, engine stop */
1402 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1404 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1405 SLOG(LOG_DEBUG, TAG_STTD, " ");
1409 int sttd_server_stop(int uid)
1411 /* check if uid is valid */
1413 if (0 != sttd_client_get_state(uid, &state)) {
1414 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1415 return STTD_ERROR_INVALID_PARAMETER;
1418 /* check uid state */
1419 if (APP_STATE_RECORDING != state) {
1420 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1421 return STTD_ERROR_INVALID_STATE;
1424 if (NULL != g_recording_timer) {
1425 ecore_timer_del(g_recording_timer);
1426 g_recording_timer = NULL;
1429 if (NULL != g_processing_timer) {
1430 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1431 ecore_timer_del(g_processing_timer);
1432 g_processing_timer = NULL;
1436 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1437 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1438 return STTD_ERROR_OPERATION_FAILED;
1441 SLOG(LOG_INFO, TAG_STTD, "[Server] stop sound path : %s", sound);
1444 /* 1. Stop recorder */
1445 ret = sttd_engine_agent_recognize_stop_recorder();
1447 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1448 if (0 != sttd_engine_agent_recognize_cancel()) {
1449 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1451 if (NULL != sound) free(sound);
1455 /* 2. Request wav play */
1456 if (NULL != sound) {
1458 intptr_t puid = (intptr_t)uid;
1459 sound_stream_info_h wav_stream_info_h;
1460 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1461 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1463 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1464 if (WAV_PLAYER_ERROR_NONE != ret) {
1465 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1467 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1470 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1471 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1476 SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
1477 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1479 return STTD_RESULT_STATE_NOT_DONE;
1481 SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
1483 /* Unset audio session */
1484 ret = sttd_recorder_unset_audio_session();
1486 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1491 ret = sttd_engine_agent_recognize_stop_engine();
1493 stt_client_unset_current_recognition();
1494 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1498 /* change uid state */
1499 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1501 /* Notify uid state change */
1502 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1504 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Stop recognition");
1506 SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
1507 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1509 return STTD_RESULT_STATE_DONE;
1512 return STTD_ERROR_NONE;
1515 int sttd_server_cancel(int uid)
1517 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel");
1519 /* check if uid is valid */
1521 if (0 != sttd_client_get_state(uid, &state)) {
1522 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1523 return STTD_ERROR_INVALID_PARAMETER;
1526 /* check uid state */
1527 if (APP_STATE_READY == state) {
1528 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1529 return STTD_ERROR_NONE;
1532 stt_client_unset_current_recognition();
1534 if (NULL != g_recording_timer) {
1535 ecore_timer_del(g_recording_timer);
1536 g_recording_timer = NULL;
1539 if (NULL != g_processing_timer) {
1540 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1541 ecore_timer_del(g_processing_timer);
1542 g_processing_timer = NULL;
1545 if (APP_STATE_RECORDING == state) {
1546 /* Unset audio session */
1547 int ret = sttd_recorder_unset_audio_session();
1549 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1554 /* change uid state */
1555 sttd_client_set_state(uid, APP_STATE_READY);
1557 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by app's request");
1559 /* cancel engine recognition */
1560 int ret = sttd_engine_agent_recognize_cancel();
1562 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1566 /* Notify uid state change */
1567 sttdc_send_set_state(uid, APP_STATE_READY);
1569 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel");
1571 return STTD_ERROR_NONE;
1574 int sttd_server_start_file(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential,
1575 const char* filepath, stte_audio_type_e audio_type, int sample_rate)
1577 if (NULL == lang || NULL == recognition_type || NULL == filepath) {
1578 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1579 return STTD_ERROR_INVALID_PARAMETER;
1582 /* check if uid is valid */
1584 if (0 != sttd_client_get_state(uid, &state)) {
1585 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1586 return STTD_ERROR_INVALID_PARAMETER;
1589 /* check uid state */
1590 if (APP_STATE_READY != state) {
1591 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1592 return STTD_ERROR_INVALID_STATE;
1596 if (false == stt_client_get_app_agreed(uid)) {
1598 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1600 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1604 if (false == temp) {
1605 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1606 return STTD_ERROR_PERMISSION_DENIED;
1609 stt_client_set_app_agreed(uid);
1612 /* check if engine use network */
1613 if (true == sttd_engine_agent_need_network()) {
1614 if (false == stt_network_is_connected()) {
1615 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1616 return STTD_ERROR_OUT_OF_NETWORK;
1620 if (0 != stt_client_set_current_recognition(uid)) {
1621 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1622 return STTD_ERROR_RECORDER_BUSY;
1625 /* engine start recognition */
1626 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);
1628 /* 1. Set audio session */
1629 ret = sttd_recorder_set_audio_session();
1631 stt_client_unset_current_recognition();
1632 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1636 /* 2. Start engine to recognize */
1637 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1639 stt_client_unset_current_recognition();
1640 sttd_recorder_unset_audio_session();
1641 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start engine : result(%d)", ret);
1645 sttd_client_set_state(uid, APP_STATE_RECORDING);
1646 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1648 /* 3. Start to send pcm from file to engine */
1649 ret = sttd_engine_agent_recognize_start_file(uid, filepath);
1651 stt_client_unset_current_recognition();
1652 sttd_recorder_unset_audio_session();
1653 sttd_engine_agent_recognize_cancel();
1654 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start file : result(%d)", ret);
1658 /* 4. Stop to send pcm from file */
1659 ret = sttd_engine_agent_recognize_stop_file();
1661 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1662 stt_client_unset_current_recognition();
1663 sttd_recorder_unset_audio_session();
1664 sttd_engine_agent_recognize_cancel();
1668 /* 5. change & notify uid state */
1669 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1670 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1672 /* 6. Unset audio session */
1673 ret = sttd_recorder_unset_audio_session();
1675 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1676 stt_client_unset_current_recognition();
1680 /* 7. Stop engine */
1681 ret = sttd_engine_agent_recognize_stop_engine();
1683 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1684 stt_client_unset_current_recognition();
1688 return STTD_ERROR_NONE;
1691 int sttd_server_cancel_file(int uid)
1693 /* check if uid is valid */
1695 if (0 != sttd_client_get_state(uid, &state)) {
1696 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1697 return STTD_ERROR_INVALID_PARAMETER;
1700 /* check uid state */
1701 if (APP_STATE_READY == state) {
1702 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1703 return STTD_ERROR_NONE;
1706 stt_client_unset_current_recognition();
1708 if (NULL != g_recording_timer) {
1709 ecore_timer_del(g_recording_timer);
1710 g_recording_timer = NULL;
1713 if (NULL != g_processing_timer) {
1714 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1715 ecore_timer_del(g_processing_timer);
1716 g_processing_timer = NULL;
1719 if (APP_STATE_RECORDING == state) {
1720 /* Unset audio session */
1721 int ret = sttd_recorder_unset_audio_session();
1723 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1728 /* change uid state */
1729 sttd_client_set_state(uid, APP_STATE_READY);
1731 /* cancel engine recognition */
1732 int ret = sttd_engine_agent_recognize_cancel();
1734 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1738 /* Notify uid state change */
1739 sttdc_send_set_state(uid, APP_STATE_READY);
1741 return STTD_ERROR_NONE;