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"
27 #include "sttd_server.h"
30 #define CLIENT_CLEAN_UP_TIME 500
33 static pthread_mutex_t stte_result_mutex = PTHREAD_MUTEX_INITIALIZER;
34 static pthread_mutex_t stte_result_time_mutex = PTHREAD_MUTEX_INITIALIZER;
38 * STT Server static variable
40 static double g_processing_timeout = 60;
42 static double g_recording_timeout = 60;
44 static Ecore_Timer* g_check_client_timer = NULL;
45 Ecore_Timer* g_recording_timer = NULL;
46 Ecore_Timer* g_processing_timer = NULL;
48 static int g_recording_log_count = 0;
50 static GList *g_proc_list = NULL;
53 * STT Server Callback Functions
55 static void __cancel_recognition_internal();
57 Eina_Bool __stop_by_silence(void *data)
59 SLOG(LOG_INFO, TAG_STTD, "===== Stop by silence detection");
61 unsigned int uid = stt_client_get_current_recognition();
63 if (STT_INVALID_UID != uid) {
64 int ret = sttd_server_stop(uid);
65 if (STTD_ERROR_NONE != ret) {
66 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Fail to stop recording");
67 __cancel_recognition_internal();
70 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
73 SLOG(LOG_INFO, TAG_STTD, "=====");
74 SLOG(LOG_DEBUG, TAG_STTD, " ");
79 static void __cancel_recognition_internal()
81 if (NULL != g_recording_timer) {
82 ecore_timer_del(g_recording_timer);
83 g_recording_timer = NULL;
87 unsigned int uid = stt_client_get_current_recognition();
89 app_state_e state = 0;
90 ret = sttd_client_get_state(uid, &state);
93 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
97 if (0 != uid && (APP_STATE_PROCESSING == state || APP_STATE_RECORDING == state)) {
98 SLOG(LOG_INFO, TAG_STTD, "===== cancel by internal");
99 /* cancel engine recognition */
100 ret = sttd_server_cancel(uid);
102 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
105 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
109 static void __cancel_by_error(void *data)
111 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by error");
113 __cancel_recognition_internal();
115 SLOG(LOG_DEBUG, TAG_STTD, "=====");
116 SLOG(LOG_DEBUG, TAG_STTD, " ");
121 int __server_audio_recorder_callback(const void* data, const unsigned int length)
125 if (NULL == data || 0 == length) {
126 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Recording data is not valid");
127 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
131 unsigned int uid = stt_client_get_current_recognition();
132 if (STT_INVALID_UID != uid) {
133 ret = sttd_engine_agent_set_recording_data(data, length);
135 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
138 g_recording_log_count++;
139 if (200 <= g_recording_log_count) {
140 SLOG(LOG_DEBUG, TAG_STTD, "=== Set recording data ===");
141 g_recording_log_count = 0;
144 if (NULL != g_recording_timer) {
145 ecore_timer_del(g_recording_timer);
146 g_recording_timer = NULL;
148 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current uid in recording is is not valid");
155 void __server_audio_interrupt_callback()
157 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by sound interrupt");
159 __cancel_recognition_internal();
161 SLOG(LOG_DEBUG, TAG_STTD, "=====");
162 SLOG(LOG_DEBUG, TAG_STTD, " ");
165 void __cancel_by_no_record(void *data)
167 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by no record");
169 __cancel_recognition_internal();
171 SLOG(LOG_DEBUG, TAG_STTD, "=====");
172 SLOG(LOG_DEBUG, TAG_STTD, " ");
177 int __server_recognition_result_callback(stte_result_event_e event, const char* type,
178 const char** data, int data_count, const char* msg, void *user_data)
181 pthread_mutex_lock(&stte_result_mutex);
183 SLOG(LOG_INFO, TAG_STTD, "===== RESULT event[%d] type[%s] data[%p] data_count[%d]", event, type, data, data_count);
186 unsigned int uid = stt_client_get_current_recognition();
189 if (STT_INVALID_UID == uid || 0 != sttd_client_get_state(uid, &state)) {
190 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid (%u)", uid);
191 SLOG(LOG_DEBUG, TAG_STTD, "=====");
192 SLOG(LOG_DEBUG, TAG_STTD, " ");
193 pthread_mutex_unlock(&stte_result_mutex);
194 return STTD_ERROR_OPERATION_FAILED;
197 SLOG(LOG_INFO, TAG_STTD, "[Server] uid (%u), event(%d)", uid, event);
199 /* send result to client */
200 if (STTE_RESULT_EVENT_FINAL_RESULT == event) {
201 if (APP_STATE_PROCESSING != state) {
202 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is NOT 'Processing'.");
204 SLOG(LOG_INFO, TAG_STTD, "[Server] the size of result from engine is '%d'", data_count);
206 /* Delete timer for processing time out */
207 if (NULL != g_processing_timer) {
208 ecore_thread_main_loop_begin();
209 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
210 ecore_timer_del(g_processing_timer);
211 g_processing_timer = NULL;
212 ecore_thread_main_loop_end();
215 sttd_config_time_save();
216 sttd_config_time_reset();
218 sttd_recorder_clear();
220 sttd_client_set_state(uid, APP_STATE_READY);
221 stt_client_unset_current_recognition();
223 if (NULL == data || 0 == data_count) {
224 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
225 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
226 int reason = (int)STTD_ERROR_OPERATION_FAILED;
228 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
229 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
233 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
234 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
235 int reason = (int)STTD_ERROR_OPERATION_FAILED;
237 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
238 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
243 /* change state of uid */
244 // sttd_client_set_state(uid, APP_STATE_READY);
245 // stt_client_unset_current_recognition();
247 } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) {
248 SLOG(LOG_INFO, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event, data_count);
250 sttd_config_time_save();
251 sttd_config_time_reset();
253 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
254 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
255 int reason = (int)STTD_ERROR_OPERATION_FAILED;
257 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
258 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
262 } else if (STTE_RESULT_EVENT_ERROR == event) {
263 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
265 /* Delete timer for processing time out */
266 if (NULL != g_processing_timer) {
267 ecore_thread_main_loop_begin();
268 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
269 ecore_timer_del(g_processing_timer);
270 g_processing_timer = NULL;
271 ecore_thread_main_loop_end();
273 sttd_config_time_reset();
276 if (APP_STATE_RECORDING == state) {
277 ret = sttd_engine_agent_recognize_cancel();
279 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel: result(%d)", ret);
283 sttd_client_set_state(uid, APP_STATE_READY);
284 stt_client_unset_current_recognition();
286 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
287 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
290 int reason = (int)STTD_ERROR_INVALID_STATE;
291 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
292 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
296 /* change state of uid */
297 // sttd_client_set_state(uid, APP_STATE_READY);
298 // stt_client_unset_current_recognition();
303 SLOG(LOG_INFO, TAG_STTD, "=====");
304 SLOG(LOG_DEBUG, TAG_STTD, " ");
305 pthread_mutex_unlock(&stte_result_mutex);
307 return STTD_ERROR_NONE;
310 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)
312 pthread_mutex_lock(&stte_result_time_mutex);
314 SECURE_SLOG(LOG_INFO, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)",
315 index, event, text, start_time, end_time);
318 ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
320 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
321 pthread_mutex_unlock(&stte_result_time_mutex);
325 pthread_mutex_unlock(&stte_result_time_mutex);
330 int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
332 SLOG(LOG_INFO, TAG_STTD, "===== Speech status detected Callback");
334 unsigned int uid = stt_client_get_current_recognition();
335 if (STT_INVALID_UID != uid) {
337 if (0 != sttd_client_get_state(uid, &state)) {
338 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid ");
339 return STTD_ERROR_OPERATION_FAILED;
342 if (APP_STATE_RECORDING != state && APP_STATE_PROCESSING != state) {
343 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording, state(%d), status(%d)", state, status);
344 return STTD_ERROR_INVALID_STATE;
347 if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) {
348 SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected");
349 sttdc_send_speech_status(uid, status);
350 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
351 SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
352 ecore_thread_main_loop_begin();
353 ecore_timer_add(0, __stop_by_silence, NULL);
354 ecore_thread_main_loop_end();
357 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recognition uid is not valid ");
360 SLOG(LOG_INFO, 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_ERROR, TAG_STTD, "[Server] Error Callback is called, error(%d), err_msg(%s)", error, msg);
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_INFO, 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(%u)", 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_INFO, 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_INFO, 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_INFO, 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_delete_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 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] finalize");
529 return STTD_ERROR_NONE;
532 static void __read_proc()
535 struct dirent *dirp = NULL;
539 if (0 < g_list_length(g_proc_list)) {
540 iter = g_list_first(g_proc_list);
541 while (NULL != iter) {
542 g_proc_list = g_list_delete_link(g_proc_list, iter);
543 iter = g_list_first(g_proc_list);
547 dp = opendir("/proc");
549 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
555 tmp = atoi(dirp->d_name);
556 if (0 >= tmp) continue;
557 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
559 } while (NULL != dirp);
565 Eina_Bool sttd_cleanup_client(void *data)
567 unsigned int* client_list = NULL;
568 int client_count = 0;
573 if (0 != sttd_client_get_list(&client_list, &client_count)) {
574 if (NULL != client_list)
580 if (NULL != client_list) {
581 SLOG(LOG_INFO, TAG_STTD, "===== Clean up client ");
585 for (i = 0; i < client_count; i++) {
586 int pid = sttd_client_get_pid(client_list[i]);
588 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
594 for (j = 0; j < g_list_length(g_proc_list); j++) {
595 iter = g_list_nth(g_proc_list, j);
597 if (pid == GPOINTER_TO_INT(iter->data)) {
598 SLOG(LOG_DEBUG, TAG_STTD, "uid (%u) is running", client_list[i]);
605 if (false == exist) {
606 SLOG(LOG_ERROR, TAG_STTD, "uid (%u) should be removed", client_list[i]);
607 sttd_server_finalize(client_list[i]);
610 result = sttdc_send_hello(client_list[i]);
613 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%u) should be removed.", client_list[i]);
614 sttd_server_finalize(client_list[i]);
615 } else if (-1 == result) {
616 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
621 SLOG(LOG_INFO, TAG_STTD, "=====");
622 SLOG(LOG_DEBUG, TAG_STTD, " ");
631 * STT Server Functions for Client
634 int sttd_server_initialize(int pid, unsigned int uid, bool* silence, bool* credential)
636 int ret = STTD_ERROR_NONE;
638 SLOG(LOG_INFO, TAG_STTD, "[Server] server initialize");
640 /* check if uid is valid */
642 if (0 == sttd_client_get_state(uid, &state)) {
643 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
644 return STTD_ERROR_NONE;
647 ret = sttd_engine_agent_get_option_supported(silence);
649 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
653 ret = sttd_engine_agent_is_credential_needed(uid, credential);
655 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
659 /* Add client information to client manager */
660 ret = sttd_client_add(pid, uid);
662 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
666 SLOG(LOG_INFO, TAG_STTD, "[Server Success] server initialize");
668 return STTD_ERROR_NONE;
671 static Eina_Bool __quit_ecore_loop(void *data)
673 SLOG(LOG_INFO, TAG_STTD, "[Server] Quit");
675 stt_network_finalize();
677 sttd_dbus_close_connection();
678 ecore_main_loop_quit();
680 SLOG(LOG_INFO, TAG_STTD, "");
685 int sttd_server_finalize(unsigned int uid)
687 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter Finalize");
689 /* check if uid is valid */
691 if (0 != sttd_client_get_state(uid, &state)) {
692 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
693 return STTD_ERROR_INVALID_PARAMETER;
696 /* release recorder */
697 if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
698 if (APP_STATE_RECORDING == state) {
699 if (NULL != g_recording_timer) {
700 ecore_timer_del(g_recording_timer);
701 g_recording_timer = NULL;
705 if (APP_STATE_PROCESSING == state) {
706 if (NULL != g_processing_timer) {
707 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
708 ecore_timer_del(g_processing_timer);
709 g_processing_timer = NULL;
713 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] stt_cancel is invoked while state is (%d)", state);
715 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
716 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
719 stt_client_unset_current_recognition();
722 /* Remove client information */
723 if (0 != sttd_client_delete(uid)) {
724 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
727 /* unload engine, if ref count of client is 0 */
728 if (0 == sttd_client_get_ref_count()) {
729 // sttd_dbus_close_connection();
730 ecore_timer_add(0, __quit_ecore_loop, NULL);
733 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End Finalize");
735 return STTD_ERROR_NONE;
738 int sttd_server_get_supported_engines(unsigned int uid, GSList** engine_list)
740 /* Check if uid is valid */
742 if (0 != sttd_client_get_state(uid, &state)) {
743 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
744 return STTD_ERROR_INVALID_PARAMETER;
747 /* Check state of uid */
748 if (APP_STATE_READY != state) {
749 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%u) is not Ready", uid);
750 return STTD_ERROR_INVALID_STATE;
753 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported egnines");
756 ret = sttd_engine_agent_get_engine_list(engine_list);
758 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
762 return STTD_ERROR_NONE;
765 int sttd_server_set_current_engine(unsigned int uid, const char* engine_id, bool* silence, bool* credential)
767 /* Check if uid is valid */
769 if (0 != sttd_client_get_state(uid, &state)) {
770 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
771 return STTD_ERROR_INVALID_PARAMETER;
774 /* Check state of uid */
775 if (APP_STATE_READY != state) {
776 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%u) is not Ready", uid);
777 return STTD_ERROR_INVALID_STATE;
780 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] set current egnines, uid(%u), engine_id(%s)", uid, engine_id);
783 ret = sttd_engine_agent_load_current_engine(NULL);
785 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
789 ret = sttd_engine_agent_get_option_supported(silence);
791 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
795 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
796 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
800 return STTD_ERROR_NONE;
803 int sttd_server_get_current_engine(unsigned int uid, char** engine_id)
805 /* Check if uid is valid */
807 if (0 != sttd_client_get_state(uid, &state)) {
808 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
809 return STTD_ERROR_INVALID_PARAMETER;
812 /* Check state of uid */
813 if (APP_STATE_READY != state) {
814 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%u) is not Ready", uid);
815 return STTD_ERROR_INVALID_STATE;
818 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get current egnines, uid(%u)", uid);
821 ret = sttd_engine_agent_get_current_engine(engine_id);
823 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
827 return STTD_ERROR_NONE;
830 int sttd_server_check_app_agreed(unsigned int uid, const char* appid, bool* available)
832 /* Check if uid is valid */
834 if (0 != sttd_client_get_state(uid, &state)) {
835 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
836 return STTD_ERROR_INVALID_PARAMETER;
839 /* Check state of uid */
840 if (APP_STATE_READY != state) {
841 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%u) is not Ready", uid);
842 return STTD_ERROR_INVALID_STATE;
845 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] check app agreed");
847 /* Ask engine available */
850 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
852 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
857 stt_client_set_app_agreed(uid);
858 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
863 return STTD_ERROR_NONE;
867 int sttd_server_get_supported_languages(unsigned int uid, GSList** lang_list)
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 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported languages");
878 /* get language list from engine */
879 int ret = sttd_engine_agent_supported_langs(lang_list);
881 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
885 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
887 return STTD_ERROR_NONE;
890 int sttd_server_get_current_langauage(unsigned int uid, char** current_lang)
892 /* check if uid is valid */
894 if (0 != sttd_client_get_state(uid, &state)) {
895 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
896 return STTD_ERROR_INVALID_PARAMETER;
899 if (NULL == current_lang) {
900 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
901 return STTD_ERROR_INVALID_PARAMETER;
904 /*get current language from engine */
905 int ret = sttd_engine_agent_get_default_lang(current_lang);
907 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
911 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get default language, current_lang(%s)", *current_lang);
913 return STTD_ERROR_NONE;
916 int sttd_server_set_private_data(unsigned int uid, const char* key, const char* data)
918 /* check if uid is valid */
920 if (0 != sttd_client_get_state(uid, &state)) {
921 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
922 return STTD_ERROR_INVALID_PARAMETER;
925 if (NULL == key || NULL == data) {
926 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
927 return STTD_ERROR_INVALID_PARAMETER;
930 /* set private data to engine */
932 ret = sttd_engine_agent_set_private_data(key, data);
934 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
938 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Set private data");
940 return STTD_ERROR_NONE;
943 int sttd_server_get_private_data(unsigned int uid, const char* key, char** data)
945 /* check if uid is valid */
947 if (0 != sttd_client_get_state(uid, &state)) {
948 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
949 return STTD_ERROR_INVALID_PARAMETER;
952 if (NULL == key || NULL == data) {
953 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
954 return STTD_ERROR_INVALID_PARAMETER;
957 /* get private data to engine */
959 ret = sttd_engine_agent_get_private_data(key, data);
961 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
965 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
967 return STTD_ERROR_NONE;
970 int sttd_server_is_recognition_type_supported(unsigned int uid, const char* type, int* support)
972 /* check if uid is valid */
974 if (0 != sttd_client_get_state(uid, &state)) {
975 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
976 return STTD_ERROR_INVALID_PARAMETER;
979 if (NULL == type || NULL == support) {
980 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
981 return STTD_ERROR_INVALID_PARAMETER;
985 int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
987 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
991 *support = (int)temp;
993 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
995 return STTD_ERROR_NONE;
998 int sttd_server_set_start_sound(unsigned int uid, const char* file)
1000 /* check if uid is valid */
1002 if (0 != sttd_client_get_state(uid, &state)) {
1003 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1004 return STTD_ERROR_INVALID_PARAMETER;
1007 int ret = sttd_client_set_start_sound(uid, file);
1009 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1016 int sttd_server_set_stop_sound(unsigned int uid, const char* file)
1018 /* check if uid is valid */
1020 if (0 != sttd_client_get_state(uid, &state)) {
1021 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1022 return STTD_ERROR_INVALID_PARAMETER;
1025 int ret = sttd_client_set_stop_sound(uid, file);
1027 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1035 Eina_Bool __check_recording_state(void *data)
1038 unsigned int uid = stt_client_get_current_recognition();
1043 if (0 != sttdc_send_get_state(uid, (int*)&state)) {
1044 /* client is removed */
1045 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%u) should be removed.", uid);
1046 sttd_server_finalize(uid);
1050 if (APP_STATE_READY == state) {
1052 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%u) is 'Ready'. The STT service should cancel recording", uid);
1053 sttd_server_cancel(uid);
1054 } else if (APP_STATE_PROCESSING == state) {
1055 /* Cancel stt and send change state */
1056 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%u) is 'Processing'. The STT service should cancel recording", uid);
1057 sttd_server_cancel(uid);
1058 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1061 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1069 Eina_Bool __stop_by_recording_timeout(void *data)
1071 SLOG(LOG_INFO, TAG_STTD, "===== Stop by timeout");
1073 if (NULL != g_recording_timer) {
1074 ecore_timer_del(g_recording_timer);
1075 g_recording_timer = NULL;
1078 unsigned int uid = stt_client_get_current_recognition();
1079 if (STT_INVALID_UID != uid) {
1080 /* cancel engine recognition */
1081 int ret = sttd_server_stop(uid);
1083 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1087 SLOG(LOG_INFO, TAG_STTD, "=====");
1088 SLOG(LOG_DEBUG, TAG_STTD, " ");
1093 void __sttd_server_recorder_start(void* data)
1095 uintptr_t puid = (uintptr_t)data;
1096 unsigned int uid = (unsigned int)puid;
1097 int current_uid = stt_client_get_current_recognition();
1099 if (uid != current_uid) {
1100 stt_client_unset_current_recognition();
1104 char appid[1024] = {0, };
1105 int pid = sttd_client_get_pid(uid);
1107 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1);
1108 if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1109 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to get application ID");
1111 SLOG(LOG_DEBUG, TAG_STTD, "[DEBUG] Current app id is %s", appid);
1114 ret = sttd_engine_agent_recognize_start_recorder(uid, appid);
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 static int __sttd_server_check_precondition_to_start(unsigned int uid, const char* appid)
1146 /* check if uid is valid */
1148 if (0 != sttd_client_get_state(uid, &state)) {
1149 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1150 return STTD_ERROR_INVALID_PARAMETER;
1153 /* check uid state */
1154 if (APP_STATE_READY != state) {
1155 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1156 return STTD_ERROR_INVALID_STATE;
1159 int ret = STTD_ERROR_NONE;
1160 if (false == stt_client_get_app_agreed(uid)) {
1162 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1164 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1168 if (false == temp) {
1169 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1170 return STTD_ERROR_PERMISSION_DENIED;
1173 stt_client_set_app_agreed(uid);
1176 /* check if engine use network */
1177 if (true == sttd_engine_agent_need_network()) {
1178 if (false == stt_network_is_connected()) {
1179 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1180 return STTD_ERROR_OUT_OF_NETWORK;
1186 int sttd_server_start(unsigned int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential, const char* audio_id)
1188 if (NULL == lang || NULL == recognition_type) {
1189 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1190 return STTD_ERROR_INVALID_PARAMETER;
1193 int ret = __sttd_server_check_precondition_to_start(uid, appid);
1195 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to satisfy preconditions, ret(0x%x)", ret);
1199 if (NULL != g_recording_timer) {
1200 ecore_timer_del(g_recording_timer);
1201 g_recording_timer = NULL;
1204 if (NULL != g_processing_timer) {
1205 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1206 ecore_timer_del(g_processing_timer);
1207 g_processing_timer = NULL;
1211 ret = sttd_client_get_start_sound(uid, &sound);
1213 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1217 ret = sttd_client_set_audio_id(uid, audio_id);
1219 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set audio_id(%s)", audio_id);
1220 if (NULL != sound) free(sound);
1224 if (0 != stt_client_set_current_recognition(uid)) {
1225 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1226 if (NULL != sound) free(sound);
1227 return STTD_ERROR_RECORDER_BUSY;
1230 /* engine start recognition */
1231 SLOG(LOG_INFO, TAG_STTD, "[Server] start : uid(%u), lang(%s), recog_type(%s)",
1232 uid, lang, recognition_type);
1234 SLOG(LOG_INFO, TAG_STTD, "[Server] start sound : %s", sound);
1236 /* 1. Set audio session */
1237 ret = sttd_recorder_set_audio_session();
1239 stt_client_unset_current_recognition();
1240 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1241 if (NULL != sound) free(sound);
1245 bool is_sound_done = false;
1247 /* 2. Request wav play */
1248 if (NULL != sound) {
1250 uintptr_t puid = (uintptr_t)uid;
1251 sound_stream_info_h wav_stream_info_h;
1252 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1253 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1254 is_sound_done = true;
1256 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_start_sound_completed_cb, (void*)puid, &id);
1257 if (WAV_PLAYER_ERROR_NONE != ret) {
1258 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1259 is_sound_done = true;
1262 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1263 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1269 is_sound_done = true;
1272 /* 3. Create recorder & engine initialize */
1273 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1275 stt_client_unset_current_recognition();
1276 sttd_recorder_unset_audio_session();
1277 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1281 if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1282 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1285 /* change uid state */
1286 sttd_client_set_state(uid, APP_STATE_RECORDING);
1288 g_recording_log_count = 0;
1290 app_state_e temp_app_state;
1292 if (true == is_sound_done) {
1293 SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
1295 ret = sttd_engine_agent_recognize_start_recorder(uid, appid);
1297 stt_client_unset_current_recognition();
1298 sttd_recorder_unset_audio_session();
1300 sttd_engine_agent_recognize_cancel();
1301 if (NULL != g_recording_timer) {
1302 ecore_timer_del(g_recording_timer);
1303 g_recording_timer = NULL;
1305 sttd_client_set_state(uid, APP_STATE_READY);
1307 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1311 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1312 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid");
1313 return STTD_ERROR_INVALID_PARAMETER;
1315 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1316 /* Notify uid state change */
1317 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1320 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Start recognition");
1321 return STTD_RESULT_STATE_DONE;
1324 SLOG(LOG_INFO, TAG_STTD, "[Server] Wait sound finish");
1326 return STTD_RESULT_STATE_NOT_DONE;
1329 Eina_Bool __time_out_for_processing(void *data)
1331 if (NULL != g_processing_timer) {
1332 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1333 ecore_timer_del(g_processing_timer);
1334 g_processing_timer = NULL;
1337 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter __time_out_for_processing");
1340 unsigned int uid = stt_client_get_current_recognition();
1341 if (STT_INVALID_UID == uid) return EINA_FALSE;
1343 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by timeout for processing");
1346 int ret = sttd_engine_agent_recognize_cancel();
1348 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1351 if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1352 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1354 /* send error msg */
1355 int reason = (int)STTD_ERROR_TIMED_OUT;
1356 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1357 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1361 /* Change uid state */
1362 sttd_client_set_state(uid, APP_STATE_READY);
1364 stt_client_unset_current_recognition();
1366 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End __time_out_for_processing");
1371 void __sttd_server_engine_stop(void* data)
1373 uintptr_t puid = (uintptr_t)data;
1374 unsigned int uid = (unsigned int)puid;
1375 /* change uid state */
1376 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1378 /* Notify uid state change */
1379 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1381 /* Unset audio session */
1382 int ret = sttd_recorder_unset_audio_session();
1384 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1389 ret = sttd_engine_agent_recognize_stop_engine();
1391 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1395 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1398 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1400 SLOG(LOG_INFO, TAG_STTD, "===== Stop sound completed");
1402 /* After wav play callback, engine stop */
1403 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1405 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1406 SLOG(LOG_DEBUG, TAG_STTD, " ");
1410 int sttd_server_stop(unsigned int uid)
1412 /* check if uid is valid */
1414 if (0 != sttd_client_get_state(uid, &state)) {
1415 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1416 return STTD_ERROR_INVALID_PARAMETER;
1419 /* check uid state */
1420 if (APP_STATE_RECORDING != state) {
1421 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1422 return STTD_ERROR_INVALID_STATE;
1425 if (NULL != g_recording_timer) {
1426 ecore_timer_del(g_recording_timer);
1427 g_recording_timer = NULL;
1430 if (NULL != g_processing_timer) {
1431 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1432 ecore_timer_del(g_processing_timer);
1433 g_processing_timer = NULL;
1437 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1438 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1439 return STTD_ERROR_OPERATION_FAILED;
1442 SLOG(LOG_INFO, TAG_STTD, "[Server] stop sound path : %s", sound);
1445 /* 1. Stop recorder */
1446 ret = sttd_engine_agent_recognize_stop_recorder();
1448 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1449 if (0 != sttd_engine_agent_recognize_cancel()) {
1450 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1452 if (NULL != sound) free(sound);
1456 /* 2. Request wav play */
1457 if (NULL != sound) {
1459 uintptr_t puid = (uintptr_t)uid;
1460 sound_stream_info_h wav_stream_info_h;
1461 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1462 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1464 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1465 if (WAV_PLAYER_ERROR_NONE != ret) {
1466 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1468 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1471 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1472 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1477 SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
1478 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1480 return STTD_RESULT_STATE_NOT_DONE;
1482 SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
1484 /* Unset audio session */
1485 ret = sttd_recorder_unset_audio_session();
1487 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1492 ret = sttd_engine_agent_recognize_stop_engine();
1494 stt_client_unset_current_recognition();
1495 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1499 /* change uid state */
1500 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1502 /* Notify uid state change */
1503 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1505 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Stop recognition");
1507 SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
1508 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1510 return STTD_RESULT_STATE_DONE;
1513 return STTD_ERROR_NONE;
1516 static int __sttd_server_cancel(unsigned int uid)
1518 /* check if uid is valid */
1520 if (0 != sttd_client_get_state(uid, &state)) {
1521 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1522 return STTD_ERROR_INVALID_PARAMETER;
1525 /* check uid state */
1526 if (APP_STATE_READY == state) {
1527 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1528 return STTD_ERROR_NONE;
1531 stt_client_unset_current_recognition();
1533 if (NULL != g_recording_timer) {
1534 ecore_timer_del(g_recording_timer);
1535 g_recording_timer = NULL;
1538 if (NULL != g_processing_timer) {
1539 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1540 ecore_timer_del(g_processing_timer);
1541 g_processing_timer = NULL;
1544 if (APP_STATE_RECORDING == state) {
1545 /* Unset audio session */
1546 int ret = sttd_recorder_unset_audio_session();
1548 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1553 /* change uid state */
1554 sttd_client_set_state(uid, APP_STATE_READY);
1556 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by app's request");
1558 /* cancel engine recognition */
1559 int ret = sttd_engine_agent_recognize_cancel();
1561 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1565 /* Notify uid state change */
1566 sttdc_send_set_state(uid, APP_STATE_READY);
1571 int sttd_server_cancel(unsigned int uid)
1573 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel");
1575 int ret = __sttd_server_cancel(uid);
1577 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(0x%x)", ret);
1580 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel");
1584 int sttd_server_start_file(unsigned int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential, const char* filepath)
1586 if (NULL == lang || NULL == recognition_type || NULL == filepath) {
1587 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1588 return STTD_ERROR_INVALID_PARAMETER;
1591 int ret = __sttd_server_check_precondition_to_start(uid, appid);
1593 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to satisfy preconditions, ret(0x%x)", ret);
1597 if (0 != stt_client_set_current_recognition(uid)) {
1598 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1599 return STTD_ERROR_RECORDER_BUSY;
1602 /* engine start recognition */
1603 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%u), lang(%s), recog_type(%s), appid(%s), file(%s)", uid, lang, recognition_type, appid, filepath);
1605 /* 1. Set audio session */
1606 ret = sttd_recorder_set_audio_session();
1608 stt_client_unset_current_recognition();
1609 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1613 /* 2. Start engine to recognize */
1614 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1616 stt_client_unset_current_recognition();
1617 sttd_recorder_unset_audio_session();
1618 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start engine : result(%d)", ret);
1622 sttd_client_set_state(uid, APP_STATE_RECORDING);
1623 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1625 /* 3. Start to send pcm from file to engine */
1626 ret = sttd_engine_agent_recognize_start_file(uid, filepath);
1628 stt_client_unset_current_recognition();
1629 sttd_recorder_unset_audio_session();
1630 sttd_engine_agent_recognize_cancel();
1631 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start file : result(%d)", ret);
1635 /* 4. Stop to send pcm from file */
1636 ret = sttd_engine_agent_recognize_stop_file();
1638 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1639 stt_client_unset_current_recognition();
1640 sttd_recorder_unset_audio_session();
1641 sttd_engine_agent_recognize_cancel();
1645 /* 5. change & notify uid state */
1646 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1647 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1649 /* 6. Unset audio session */
1650 ret = sttd_recorder_unset_audio_session();
1652 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1653 stt_client_unset_current_recognition();
1657 /* 7. Stop engine */
1658 ret = sttd_engine_agent_recognize_stop_engine();
1660 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1661 stt_client_unset_current_recognition();
1665 return STTD_ERROR_NONE;
1668 int sttd_server_cancel_file(unsigned int uid)
1670 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel_file");
1672 int ret = __sttd_server_cancel(uid);
1674 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel file : result(0x%x)", ret);
1677 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel_file");