2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
15 #include <sound_manager.h>
16 #include <wav_player.h>
18 #include "stt_network.h"
19 #include "sttd_client_data.h"
20 #include "sttd_config.h"
21 #include "sttd_dbus.h"
22 #include "sttd_engine_agent.h"
23 #include "sttd_main.h"
24 #include "sttd_recorder.h"
25 #include "sttd_server.h"
28 #define CLIENT_CLEAN_UP_TIME 500
31 static pthread_mutex_t stte_result_mutex = PTHREAD_MUTEX_INITIALIZER;
32 static pthread_mutex_t stte_result_time_mutex = PTHREAD_MUTEX_INITIALIZER;
36 * STT Server static variable
38 static double g_processing_timeout = 30;
40 static double g_recording_timeout = 60;
42 static Ecore_Timer* g_check_client_timer = NULL;
43 Ecore_Timer* g_recording_timer = NULL;
44 Ecore_Timer* g_processing_timer = NULL;
46 static int g_recording_log_count = 0;
48 static GList *g_proc_list = NULL;
51 * STT Server Callback Functions
53 void __stop_by_silence(void *data)
55 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by silence detection");
59 uid = stt_client_get_current_recognition();
63 ret = sttd_server_stop(uid);
68 if (STTD_RESULT_STATE_DONE == ret) {
69 ret = sttdc_send_set_state(uid, (int)APP_STATE_PROCESSING);
71 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send state : result(%d)", ret);
74 sttd_server_finalize(uid);
75 stt_client_unset_current_recognition();
79 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
82 SLOG(LOG_DEBUG, TAG_STTD, "=====");
83 SLOG(LOG_DEBUG, TAG_STTD, " ");
88 static void __cancel_recognition_internal()
90 if (NULL != g_recording_timer) {
91 ecore_timer_del(g_recording_timer);
92 g_recording_timer = NULL;
97 uid = stt_client_get_current_recognition();
99 app_state_e state = 0;
100 ret = sttd_client_get_state(uid, &state);
103 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
107 if (0 != uid && (APP_STATE_PROCESSING == state || APP_STATE_RECORDING == state)) {
108 /* cancel engine recognition */
109 ret = sttd_server_cancel(uid);
111 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
114 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
118 static void __cancel_by_error(void *data)
120 SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by error");
122 __cancel_recognition_internal();
124 SLOG(LOG_DEBUG, TAG_STTD, "=====");
125 SLOG(LOG_DEBUG, TAG_STTD, " ");
130 int __server_audio_recorder_callback(const void* data, const unsigned int length)
135 if (NULL == data || 0 == length) {
136 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Recording data is not valid");
137 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
141 uid = stt_client_get_current_recognition();
143 ret = sttd_engine_agent_set_recording_data(data, length);
145 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
148 g_recording_log_count++;
149 if (200 <= g_recording_log_count) {
150 SLOG(LOG_DEBUG, TAG_STTD, "=== Set recording data ===");
151 g_recording_log_count = 0;
154 if (NULL != g_recording_timer) {
155 ecore_timer_del(g_recording_timer);
156 g_recording_timer = NULL;
158 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current uid in recording is is not valid");
165 void __server_audio_interrupt_callback()
167 SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by sound interrupt");
169 __cancel_recognition_internal();
171 SLOG(LOG_DEBUG, TAG_STTD, "=====");
172 SLOG(LOG_DEBUG, TAG_STTD, " ");
175 void __cancel_by_no_record(void *data)
177 SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by no record");
179 __cancel_recognition_internal();
181 SLOG(LOG_DEBUG, TAG_STTD, "=====");
182 SLOG(LOG_DEBUG, TAG_STTD, " ");
187 int __server_recognition_result_callback(stte_result_event_e event, const char* type,
188 const char** data, int data_count, const char* msg, void *user_data)
191 pthread_mutex_lock(&stte_result_mutex);
193 SLOG(LOG_DEBUG, TAG_STTD, "===== RESULT event[%d] type[%s] data[%p] data_count[%d]", event, type, data, data_count);
196 int uid = stt_client_get_current_recognition();
199 if (0 == uid || 0 != sttd_client_get_state(uid, &state)) {
200 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
201 SLOG(LOG_DEBUG, TAG_STTD, "=====");
202 SLOG(LOG_DEBUG, TAG_STTD, " ");
203 pthread_mutex_unlock(&stte_result_mutex);
204 return STTD_ERROR_OPERATION_FAILED;
207 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid (%d), event(%d)", uid, event);
209 /* send result to client */
210 if (STTE_RESULT_EVENT_FINAL_RESULT == event) {
211 if (APP_STATE_PROCESSING != state) {
212 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is NOT 'Processing'.");
214 SLOG(LOG_DEBUG, TAG_STTD, "[Server] the size of result from engine is '%d'", data_count);
216 /* Delete timer for processing time out */
217 if (NULL != g_processing_timer) {
218 ecore_timer_del(g_processing_timer);
219 g_processing_timer = NULL;
222 sttd_config_time_save();
223 sttd_config_time_reset();
225 sttd_client_set_state(uid, APP_STATE_READY);
226 stt_client_unset_current_recognition();
228 if (NULL == data || 0 == data_count) {
229 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
230 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
231 int reason = (int)STTD_ERROR_OPERATION_FAILED;
233 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
234 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
238 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
239 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
240 int reason = (int)STTD_ERROR_OPERATION_FAILED;
242 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
243 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
248 /* change state of uid */
249 // sttd_client_set_state(uid, APP_STATE_READY);
250 // stt_client_unset_current_recognition();
252 } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) {
253 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event, data_count);
255 sttd_config_time_save();
256 sttd_config_time_reset();
258 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
259 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
260 int reason = (int)STTD_ERROR_OPERATION_FAILED;
262 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
263 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
267 } else if (STTE_RESULT_EVENT_ERROR == event) {
268 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
270 /* Delete timer for processing time out */
271 if (NULL != g_processing_timer) {
272 ecore_timer_del(g_processing_timer);
273 g_processing_timer = NULL;
275 sttd_config_time_reset();
278 if (APP_STATE_RECORDING == state) {
279 ret = sttd_engine_agent_recognize_cancel();
281 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel: result(%d)", ret);
285 sttd_client_set_state(uid, APP_STATE_READY);
286 stt_client_unset_current_recognition();
288 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
289 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
292 int reason = (int)STTD_ERROR_INVALID_STATE;
293 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
294 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
298 /* change state of uid */
299 // sttd_client_set_state(uid, APP_STATE_READY);
300 // stt_client_unset_current_recognition();
305 SLOG(LOG_DEBUG, TAG_STTD, "=====");
306 SLOG(LOG_DEBUG, TAG_STTD, " ");
307 pthread_mutex_unlock(&stte_result_mutex);
309 return STTD_ERROR_NONE;
312 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)
314 pthread_mutex_lock(&stte_result_time_mutex);
316 SLOG(LOG_DEBUG, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)",
317 index, event, text, start_time, end_time);
320 ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
322 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
323 pthread_mutex_unlock(&stte_result_time_mutex);
327 pthread_mutex_unlock(&stte_result_time_mutex);
332 int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
334 SLOG(LOG_DEBUG, TAG_STTD, "===== Speech status detected Callback");
336 int uid = stt_client_get_current_recognition();
339 if (0 != sttd_client_get_state(uid, &state)) {
340 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid ");
341 return STTD_ERROR_OPERATION_FAILED;
344 if (APP_STATE_RECORDING != state && APP_STATE_PROCESSING != state) {
345 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording, state(%d), status(%d)", state, status);
346 return STTD_ERROR_INVALID_STATE;
349 if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) {
350 SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected");
351 sttdc_send_speech_status(uid, status);
352 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
353 SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
354 ecore_main_loop_thread_safe_call_async(__stop_by_silence, NULL);
357 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recogntion uid is not valid ");
360 SLOG(LOG_DEBUG, TAG_STTD, "=====");
361 SLOG(LOG_DEBUG, TAG_STTD, " ");
363 return STTD_ERROR_NONE;
366 int __server_error_callback(stte_error_e error, const char* msg)
368 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Error Callback is called");
369 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
371 return STTD_ERROR_NONE;
374 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
376 if (NULL == engine_id) {
377 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
380 SLOG(LOG_DEBUG, TAG_STTD, "[Server] New default engine : %s", engine_id);
384 /* need to change state of app to ready */
386 uid = stt_client_get_current_recognition();
389 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%d)", uid);
391 sttd_server_cancel(uid);
392 sttdc_send_set_state(uid, (int)APP_STATE_READY);
394 stt_client_unset_current_recognition();
398 int ret = sttd_engine_agent_set_default_engine(engine_id);
400 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
402 if (NULL != language) {
403 ret = sttd_engine_agent_set_default_language(language);
405 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
408 ret = sttd_engine_agent_set_silence_detection(support_silence);
410 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
416 void __sttd_server_language_changed_cb(const char* language, void* user_data)
418 if (NULL == language) {
419 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
422 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get language changed : %s", language);
425 int ret = sttd_engine_agent_set_default_language(language);
427 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
432 void __sttd_server_silence_changed_cb(bool value, void* user_data)
434 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
437 ret = sttd_engine_agent_set_silence_detection(value);
439 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
447 int sttd_initialize(stte_request_callback_s *callback)
451 if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
452 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
455 if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) {
456 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex.");
459 if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
460 __sttd_server_silence_changed_cb, NULL)) {
461 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
464 ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
466 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
470 /* Engine Agent initialize */
471 ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
472 __server_speech_status_callback, __server_error_callback);
474 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
479 ret = sttd_engine_agent_load_current_engine(callback);
481 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
485 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, sttd_cleanup_client, NULL);
486 if (NULL == g_check_client_timer) {
487 SLOG(LOG_WARN, TAG_STTD, "[Main Warning] Fail to create timer of client check");
490 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] initialize");
497 if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
498 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
501 if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) {
502 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex.");
506 if (0 < g_list_length(g_proc_list)) {
507 iter = g_list_first(g_proc_list);
508 while (NULL != iter) {
509 g_proc_list = g_list_remove_link(g_proc_list, iter);
510 iter = g_list_first(g_proc_list);
514 sttd_recorder_deinitialize();
516 sttd_config_finalize();
518 sttd_engine_agent_release();
520 if (NULL != g_check_client_timer) {
521 ecore_timer_del(g_check_client_timer);
522 g_check_client_timer = NULL;
524 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete ecore timer handle");
527 return STTD_ERROR_NONE;
530 static void __read_proc()
533 struct dirent *dirp = NULL;
537 if (0 < g_list_length(g_proc_list)) {
538 iter = g_list_first(g_proc_list);
539 while (NULL != iter) {
540 g_proc_list = g_list_remove_link(g_proc_list, iter);
541 iter = g_list_first(g_proc_list);
545 dp = opendir("/proc");
547 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
553 tmp = atoi(dirp->d_name);
554 if (0 >= tmp) continue;
555 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
557 } while (NULL != dirp);
563 Eina_Bool sttd_cleanup_client(void *data)
565 int* client_list = NULL;
566 int client_count = 0;
571 if (0 != sttd_client_get_list(&client_list, &client_count)) {
572 if (NULL != client_list)
578 if (NULL != client_list) {
579 SLOG(LOG_DEBUG, TAG_STTD, "===== Clean up client ");
583 for (i = 0; i < client_count; i++) {
584 int pid = sttd_client_get_pid(client_list[i]);
586 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
592 for (j = 0; j < g_list_length(g_proc_list); j++) {
593 iter = g_list_nth(g_proc_list, j);
595 if (pid == GPOINTER_TO_INT(iter->data)) {
596 SLOG(LOG_DEBUG, TAG_STTD, "uid (%d) is running", client_list[i]);
603 if (false == exist) {
604 SLOG(LOG_ERROR, TAG_STTD, "uid (%d) should be removed", client_list[i]);
605 sttd_server_finalize(client_list[i]);
608 result = sttdc_send_hello(client_list[i]);
611 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", client_list[i]);
612 sttd_server_finalize(client_list[i]);
613 } else if (-1 == result) {
614 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
619 SLOG(LOG_DEBUG, TAG_STTD, "=====");
620 SLOG(LOG_DEBUG, TAG_STTD, " ");
629 * STT Server Functions for Client
632 int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential)
634 int ret = STTD_ERROR_NONE;
636 /* check if uid is valid */
638 if (0 == sttd_client_get_state(uid, &state)) {
639 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
640 return STTD_ERROR_NONE;
643 ret = sttd_engine_agent_get_option_supported(silence);
645 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
649 ret = sttd_engine_agent_is_credential_needed(uid, credential);
651 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
655 /* Add client information to client manager */
656 ret = sttd_client_add(pid, uid);
658 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
662 return STTD_ERROR_NONE;
665 static Eina_Bool __quit_ecore_loop(void *data)
667 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Quit");
669 stt_network_finalize();
671 sttd_dbus_close_connection();
672 ecore_main_loop_quit();
674 SLOG(LOG_DEBUG, TAG_STTD, "");
679 int sttd_server_finalize(int uid)
681 /* check if uid is valid */
683 if (0 != sttd_client_get_state(uid, &state)) {
684 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
685 return STTD_ERROR_INVALID_PARAMETER;
688 /* release recorder */
689 if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
690 if (APP_STATE_RECORDING == state) {
691 if (NULL != g_recording_timer) {
692 ecore_timer_del(g_recording_timer);
693 g_recording_timer = NULL;
697 if (APP_STATE_PROCESSING == state) {
698 if (NULL != g_processing_timer) {
699 ecore_timer_del(g_processing_timer);
700 g_processing_timer = NULL;
704 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
705 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
708 stt_client_unset_current_recognition();
711 /* Remove client information */
712 if (0 != sttd_client_delete(uid)) {
713 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
716 /* unload engine, if ref count of client is 0 */
717 if (0 == sttd_client_get_ref_count()) {
718 // sttd_dbus_close_connection();
719 ecore_timer_add(0, __quit_ecore_loop, NULL);
722 return STTD_ERROR_NONE;
725 int sttd_server_get_supported_engines(int uid, GSList** engine_list)
727 /* Check if uid is valid */
729 if (0 != sttd_client_get_state(uid, &state)) {
730 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
731 return STTD_ERROR_INVALID_PARAMETER;
734 /* Check state of uid */
735 if (APP_STATE_READY != state) {
736 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
737 return STTD_ERROR_INVALID_STATE;
741 ret = sttd_engine_agent_get_engine_list(engine_list);
743 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
747 return STTD_ERROR_NONE;
750 int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence, bool* credential)
752 /* Check if uid is valid */
754 if (0 != sttd_client_get_state(uid, &state)) {
755 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
756 return STTD_ERROR_INVALID_PARAMETER;
759 /* Check state of uid */
760 if (APP_STATE_READY != state) {
761 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
762 return STTD_ERROR_INVALID_STATE;
766 ret = sttd_engine_agent_load_current_engine(NULL);
768 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
772 ret = sttd_engine_agent_get_option_supported(silence);
774 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
778 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
779 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
783 return STTD_ERROR_NONE;
786 int sttd_server_get_current_engine(int uid, char** engine_id)
788 /* Check if uid is valid */
790 if (0 != sttd_client_get_state(uid, &state)) {
791 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
792 return STTD_ERROR_INVALID_PARAMETER;
795 /* Check state of uid */
796 if (APP_STATE_READY != state) {
797 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
798 return STTD_ERROR_INVALID_STATE;
802 ret = sttd_engine_agent_get_current_engine(engine_id);
804 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
808 return STTD_ERROR_NONE;
811 int sttd_server_check_app_agreed(int uid, const char* appid, bool* available)
813 /* Check if uid is valid */
815 if (0 != sttd_client_get_state(uid, &state)) {
816 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
817 return STTD_ERROR_INVALID_PARAMETER;
820 /* Check state of uid */
821 if (APP_STATE_READY != state) {
822 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
823 return STTD_ERROR_INVALID_STATE;
826 /* Ask engine available */
829 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
831 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
836 stt_client_set_app_agreed(uid);
837 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
842 return STTD_ERROR_NONE;
846 int sttd_server_get_supported_languages(int uid, GSList** lang_list)
848 /* check if uid is valid */
850 if (0 != sttd_client_get_state(uid, &state)) {
851 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
852 return STTD_ERROR_INVALID_PARAMETER;
855 /* get language list from engine */
856 int ret = sttd_engine_agent_supported_langs(lang_list);
858 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
862 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
864 return STTD_ERROR_NONE;
867 int sttd_server_get_current_langauage(int uid, char** current_lang)
869 /* check if uid is valid */
871 if (0 != sttd_client_get_state(uid, &state)) {
872 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
873 return STTD_ERROR_INVALID_PARAMETER;
876 if (NULL == current_lang) {
877 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
878 return STTD_ERROR_INVALID_PARAMETER;
881 /*get current language from engine */
882 int ret = sttd_engine_agent_get_default_lang(current_lang);
884 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
888 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get default language");
890 return STTD_ERROR_NONE;
893 int sttd_server_set_private_data(int uid, const char* key, const char* data)
895 /* check if uid is valid */
897 if (0 != sttd_client_get_state(uid, &state)) {
898 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
899 return STTD_ERROR_INVALID_PARAMETER;
902 if (NULL == key || NULL == data) {
903 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
904 return STTD_ERROR_INVALID_PARAMETER;
907 /* set private data to engine */
909 ret = sttd_engine_agent_set_private_data(key, data);
911 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
915 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Set private data");
917 return STTD_ERROR_NONE;
920 int sttd_server_get_private_data(int uid, const char* key, char** data)
922 /* check if uid is valid */
924 if (0 != sttd_client_get_state(uid, &state)) {
925 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
926 return STTD_ERROR_INVALID_PARAMETER;
929 if (NULL == key || NULL == data) {
930 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
931 return STTD_ERROR_INVALID_PARAMETER;
934 /* get private data to engine */
936 ret = sttd_engine_agent_get_private_data(key, data);
938 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
942 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
944 return STTD_ERROR_NONE;
947 int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support)
949 /* check if uid is valid */
951 if (0 != sttd_client_get_state(uid, &state)) {
952 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
953 return STTD_ERROR_INVALID_PARAMETER;
956 if (NULL == type || NULL == support) {
957 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
958 return STTD_ERROR_INVALID_PARAMETER;
962 int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
964 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
968 *support = (int)temp;
970 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
972 return STTD_ERROR_NONE;
975 int sttd_server_set_start_sound(int uid, const char* file)
977 /* check if uid is valid */
979 if (0 != sttd_client_get_state(uid, &state)) {
980 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
981 return STTD_ERROR_INVALID_PARAMETER;
984 int ret = sttd_client_set_start_sound(uid, file);
986 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
993 int sttd_server_set_stop_sound(int uid, const char* file)
995 /* check if uid is valid */
997 if (0 != sttd_client_get_state(uid, &state)) {
998 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
999 return STTD_ERROR_INVALID_PARAMETER;
1002 int ret = sttd_client_set_stop_sound(uid, file);
1004 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1012 Eina_Bool __check_recording_state(void *data)
1015 int uid = stt_client_get_current_recognition();
1020 if (0 != sttdc_send_get_state(uid, (int*)&state)) {
1021 /* client is removed */
1022 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", uid);
1023 sttd_server_finalize(uid);
1027 if (APP_STATE_READY == state) {
1029 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The STT service should cancel recording", uid);
1030 sttd_server_cancel(uid);
1031 } else if (APP_STATE_PROCESSING == state) {
1032 /* Cancel stt and send change state */
1033 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The STT service should cancel recording", uid);
1034 sttd_server_cancel(uid);
1035 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1038 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1046 Eina_Bool __stop_by_recording_timeout(void *data)
1048 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by timeout");
1050 g_recording_timer = NULL;
1053 uid = stt_client_get_current_recognition();
1055 /* cancel engine recognition */
1056 int ret = sttd_server_stop(uid);
1058 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1062 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1063 SLOG(LOG_DEBUG, TAG_STTD, " ");
1068 void __sttd_server_recorder_start(void* data)
1070 intptr_t puid = (intptr_t)data;
1071 int uid = (int)puid;
1072 int current_uid = stt_client_get_current_recognition();
1074 if (uid != current_uid) {
1075 stt_client_unset_current_recognition();
1079 int ret = sttd_engine_agent_recognize_start_recorder(uid);
1081 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1085 /* Notify uid state change */
1086 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1088 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1091 void __sttd_start_sound_completed_cb(int id, void *user_data)
1093 SLOG(LOG_DEBUG, TAG_STTD, "===== Start sound completed");
1095 /* After wav play callback, recorder start */
1096 ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1098 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1099 SLOG(LOG_DEBUG, TAG_STTD, " ");
1103 int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
1105 if (NULL == lang || NULL == recognition_type) {
1106 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1107 return STTD_ERROR_INVALID_PARAMETER;
1110 /* check if uid is valid */
1112 if (0 != sttd_client_get_state(uid, &state)) {
1113 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1114 return STTD_ERROR_INVALID_PARAMETER;
1117 /* check uid state */
1118 if (APP_STATE_READY != state) {
1119 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1120 return STTD_ERROR_INVALID_STATE;
1124 if (false == stt_client_get_app_agreed(uid)) {
1126 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1128 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1132 if (false == temp) {
1133 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1134 return STTD_ERROR_PERMISSION_DENIED;
1137 stt_client_set_app_agreed(uid);
1140 /* check if engine use network */
1141 if (true == sttd_engine_agent_need_network()) {
1142 if (false == stt_network_is_connected()) {
1143 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1144 return STTD_ERROR_OUT_OF_NETWORK;
1149 ret = sttd_client_get_start_sound(uid, &sound);
1151 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1155 if (0 != stt_client_set_current_recognition(uid)) {
1156 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1157 if (NULL != sound) free(sound);
1158 return STTD_ERROR_RECORDER_BUSY;
1161 /* engine start recognition */
1162 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)",
1163 uid, lang, recognition_type);
1165 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start sound : %s", sound);
1167 /* 1. Set audio session */
1168 ret = sttd_recorder_set_audio_session();
1170 stt_client_unset_current_recognition();
1171 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1172 if (NULL != sound) free(sound);
1176 bool is_sound_done = false;
1178 /* 2. Request wav play */
1179 if (NULL != sound) {
1181 intptr_t puid = (intptr_t)uid;
1182 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_start_sound_completed_cb, (void*)puid, &id);
1183 if (WAV_PLAYER_ERROR_NONE != ret) {
1184 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1185 is_sound_done = true;
1190 is_sound_done = true;
1193 /* 3. Create recorder & engine initialize */
1194 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1196 stt_client_unset_current_recognition();
1197 sttd_recorder_unset_audio_session();
1198 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1202 if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1203 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1206 /* change uid state */
1207 sttd_client_set_state(uid, APP_STATE_RECORDING);
1209 g_recording_log_count = 0;
1211 if (true == is_sound_done) {
1212 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1214 ret = sttd_engine_agent_recognize_start_recorder(uid);
1216 stt_client_unset_current_recognition();
1217 sttd_recorder_unset_audio_session();
1219 sttd_engine_agent_recognize_cancel();
1220 ecore_timer_del(g_recording_timer);
1221 sttd_client_set_state(uid, APP_STATE_READY);
1223 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1227 /* Notify uid state change */
1228 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1230 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1231 return STTD_RESULT_STATE_DONE;
1234 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Wait sound finish");
1236 return STTD_RESULT_STATE_NOT_DONE;
1239 Eina_Bool __time_out_for_processing(void *data)
1241 g_processing_timer = NULL;
1244 int uid = stt_client_get_current_recognition();
1245 if (0 == uid) return EINA_FALSE;
1248 int ret = sttd_engine_agent_recognize_cancel();
1250 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1253 if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1254 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1256 /* send error msg */
1257 int reason = (int)STTD_ERROR_TIMED_OUT;
1258 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1259 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1263 /* Change uid state */
1264 sttd_client_set_state(uid, APP_STATE_READY);
1266 stt_client_unset_current_recognition();
1271 void __sttd_server_engine_stop(void* data)
1273 intptr_t puid = (intptr_t)data;
1274 int uid = (int)puid;
1275 /* change uid state */
1276 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1278 /* Notify uid state change */
1279 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1281 /* Unset audio session */
1282 int ret = sttd_recorder_unset_audio_session();
1284 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1289 ret = sttd_engine_agent_recognize_stop_engine();
1291 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1295 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1298 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1300 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop sound completed");
1302 /* After wav play callback, engine stop */
1303 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1305 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1306 SLOG(LOG_DEBUG, TAG_STTD, " ");
1310 int sttd_server_stop(int uid)
1312 /* check if uid is valid */
1314 if (0 != sttd_client_get_state(uid, &state)) {
1315 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1316 return STTD_ERROR_INVALID_PARAMETER;
1319 /* check uid state */
1320 if (APP_STATE_RECORDING != state) {
1321 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1322 return STTD_ERROR_INVALID_STATE;
1325 if (NULL != g_recording_timer) {
1326 ecore_timer_del(g_recording_timer);
1327 g_recording_timer = NULL;
1331 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1332 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1333 return STTD_ERROR_OPERATION_FAILED;
1336 SLOG(LOG_DEBUG, TAG_STTD, "[Server] stop sound path : %s", sound);
1339 /* 1. Stop recorder */
1340 ret = sttd_engine_agent_recognize_stop_recorder();
1342 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1343 if (0 != sttd_engine_agent_recognize_cancel()) {
1344 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1346 if (NULL != sound) free(sound);
1350 /* 2. Request wav play */
1351 if (NULL != sound) {
1353 intptr_t puid = (intptr_t)uid;
1354 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1355 if (WAV_PLAYER_ERROR_NONE != ret) {
1356 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1358 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1363 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1365 return STTD_RESULT_STATE_NOT_DONE;
1367 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1369 /* Unset audio session */
1370 ret = sttd_recorder_unset_audio_session();
1372 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1377 ret = sttd_engine_agent_recognize_stop_engine();
1379 stt_client_unset_current_recognition();
1380 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1384 /* change uid state */
1385 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1387 /* Notify uid state change */
1388 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1390 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1392 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1394 return STTD_RESULT_STATE_DONE;
1397 return STTD_ERROR_NONE;
1400 int sttd_server_cancel(int uid)
1402 /* check if uid is valid */
1404 if (0 != sttd_client_get_state(uid, &state)) {
1405 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1406 return STTD_ERROR_INVALID_PARAMETER;
1409 /* check uid state */
1410 if (APP_STATE_READY == state) {
1411 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1412 return STTD_ERROR_NONE;
1415 stt_client_unset_current_recognition();
1417 if (NULL != g_recording_timer) {
1418 ecore_timer_del(g_recording_timer);
1419 g_recording_timer = NULL;
1422 if (NULL != g_processing_timer) {
1423 ecore_timer_del(g_processing_timer);
1424 g_processing_timer = NULL;
1427 if (APP_STATE_RECORDING == state) {
1428 /* Unset audio session */
1429 int ret = sttd_recorder_unset_audio_session();
1431 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1436 /* change uid state */
1437 sttd_client_set_state(uid, APP_STATE_READY);
1439 /* cancel engine recognition */
1440 int ret = sttd_engine_agent_recognize_cancel();
1442 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1446 /* Notify uid state change */
1447 sttdc_send_set_state(uid, APP_STATE_READY);
1449 return STTD_ERROR_NONE;
1452 int sttd_server_start_file(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential,
1453 const char* filepath, stte_audio_type_e audio_type, int sample_rate)
1455 if (NULL == lang || NULL == recognition_type || NULL == filepath) {
1456 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1457 return STTD_ERROR_INVALID_PARAMETER;
1460 /* check if uid is valid */
1462 if (0 != sttd_client_get_state(uid, &state)) {
1463 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1464 return STTD_ERROR_INVALID_PARAMETER;
1467 /* check uid state */
1468 if (APP_STATE_READY != state) {
1469 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1470 return STTD_ERROR_INVALID_STATE;
1474 if (false == stt_client_get_app_agreed(uid)) {
1476 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1478 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1482 if (false == temp) {
1483 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1484 return STTD_ERROR_PERMISSION_DENIED;
1487 stt_client_set_app_agreed(uid);
1490 /* check if engine use network */
1491 if (true == sttd_engine_agent_need_network()) {
1492 if (false == stt_network_is_connected()) {
1493 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1494 return STTD_ERROR_OUT_OF_NETWORK;
1498 if (0 != stt_client_set_current_recognition(uid)) {
1499 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1500 return STTD_ERROR_RECORDER_BUSY;
1503 /* engine start recognition */
1504 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);
1506 /* 1. Set audio session */
1507 ret = sttd_recorder_set_audio_session();
1509 stt_client_unset_current_recognition();
1510 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1514 /* 2. Start engine to recognize */
1515 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1517 stt_client_unset_current_recognition();
1518 sttd_recorder_unset_audio_session();
1519 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start engine : result(%d)", ret);
1523 sttd_client_set_state(uid, APP_STATE_RECORDING);
1524 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1526 /* 3. Start to send pcm from file to engine */
1527 ret = sttd_engine_agent_recognize_start_file(uid, filepath);
1529 stt_client_unset_current_recognition();
1530 sttd_recorder_unset_audio_session();
1531 sttd_engine_agent_recognize_cancel();
1532 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start file : result(%d)", ret);
1536 /* 4. Stop to send pcm from file */
1537 ret = sttd_engine_agent_recognize_stop_file();
1539 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1540 stt_client_unset_current_recognition();
1541 sttd_recorder_unset_audio_session();
1542 sttd_engine_agent_recognize_cancel();
1546 /* 5. change & notify uid state */
1547 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1548 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1550 /* 6. Unset audio session */
1551 ret = sttd_recorder_unset_audio_session();
1553 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1554 stt_client_unset_current_recognition();
1558 /* 7. Stop engine */
1559 ret = sttd_engine_agent_recognize_stop_engine();
1561 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1562 stt_client_unset_current_recognition();
1566 return STTD_ERROR_NONE;
1569 int sttd_server_cancel_file(int uid)
1571 /* check if uid is valid */
1573 if (0 != sttd_client_get_state(uid, &state)) {
1574 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1575 return STTD_ERROR_INVALID_PARAMETER;
1578 /* check uid state */
1579 if (APP_STATE_READY == state) {
1580 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1581 return STTD_ERROR_NONE;
1584 stt_client_unset_current_recognition();
1586 if (NULL != g_recording_timer) {
1587 ecore_timer_del(g_recording_timer);
1588 g_recording_timer = NULL;
1591 if (NULL != g_processing_timer) {
1592 ecore_timer_del(g_processing_timer);
1593 g_processing_timer = NULL;
1596 if (APP_STATE_RECORDING == state) {
1597 /* Unset audio session */
1598 int ret = sttd_recorder_unset_audio_session();
1600 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1605 /* change uid state */
1606 sttd_client_set_state(uid, APP_STATE_READY);
1608 /* cancel engine recognition */
1609 int ret = sttd_engine_agent_recognize_cancel();
1611 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1615 /* Notify uid state change */
1616 sttdc_send_set_state(uid, APP_STATE_READY);
1618 return STTD_ERROR_NONE;