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();
64 if (STT_INVALID_UID != uid) {
65 ret = sttd_server_stop(uid);
67 __cancel_recognition_internal();
71 if (STTD_RESULT_STATE_DONE == ret) {
72 ret = sttdc_send_set_state(uid, (int)APP_STATE_PROCESSING);
74 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send state : result(%d)", ret);
77 sttd_server_finalize(uid);
78 stt_client_unset_current_recognition();
82 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
85 SLOG(LOG_INFO, TAG_STTD, "=====");
86 SLOG(LOG_DEBUG, TAG_STTD, " ");
91 static void __cancel_recognition_internal()
93 if (NULL != g_recording_timer) {
94 ecore_timer_del(g_recording_timer);
95 g_recording_timer = NULL;
99 unsigned int uid = stt_client_get_current_recognition();
101 app_state_e state = 0;
102 ret = sttd_client_get_state(uid, &state);
105 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
109 if (0 != uid && (APP_STATE_PROCESSING == state || APP_STATE_RECORDING == state)) {
110 SLOG(LOG_INFO, TAG_STTD, "===== cancel by internal");
111 /* cancel engine recognition */
112 ret = sttd_server_cancel(uid);
114 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
117 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
121 static void __cancel_by_error(void *data)
123 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by error");
125 __cancel_recognition_internal();
127 SLOG(LOG_DEBUG, TAG_STTD, "=====");
128 SLOG(LOG_DEBUG, TAG_STTD, " ");
133 int __server_audio_recorder_callback(const void* data, const unsigned int length)
137 if (NULL == data || 0 == length) {
138 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Recording data is not valid");
139 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
143 unsigned int uid = stt_client_get_current_recognition();
144 if (STT_INVALID_UID != uid) {
145 ret = sttd_engine_agent_set_recording_data(data, length);
147 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
150 g_recording_log_count++;
151 if (200 <= g_recording_log_count) {
152 SLOG(LOG_DEBUG, TAG_STTD, "=== Set recording data ===");
153 g_recording_log_count = 0;
156 if (NULL != g_recording_timer) {
157 ecore_timer_del(g_recording_timer);
158 g_recording_timer = NULL;
160 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current uid in recording is is not valid");
167 void __server_audio_interrupt_callback()
169 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by sound interrupt");
171 __cancel_recognition_internal();
173 SLOG(LOG_DEBUG, TAG_STTD, "=====");
174 SLOG(LOG_DEBUG, TAG_STTD, " ");
177 void __cancel_by_no_record(void *data)
179 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by no record");
181 __cancel_recognition_internal();
183 SLOG(LOG_DEBUG, TAG_STTD, "=====");
184 SLOG(LOG_DEBUG, TAG_STTD, " ");
189 int __server_recognition_result_callback(stte_result_event_e event, const char* type,
190 const char** data, int data_count, const char* msg, void *user_data)
193 pthread_mutex_lock(&stte_result_mutex);
195 SLOG(LOG_INFO, TAG_STTD, "===== RESULT event[%d] type[%s] data[%p] data_count[%d]", event, type, data, data_count);
198 unsigned int uid = stt_client_get_current_recognition();
201 if (STT_INVALID_UID == uid || 0 != sttd_client_get_state(uid, &state)) {
202 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid (%u)", uid);
203 SLOG(LOG_DEBUG, TAG_STTD, "=====");
204 SLOG(LOG_DEBUG, TAG_STTD, " ");
205 pthread_mutex_unlock(&stte_result_mutex);
206 return STTD_ERROR_OPERATION_FAILED;
209 SLOG(LOG_INFO, TAG_STTD, "[Server] uid (%u), event(%d)", uid, event);
211 /* send result to client */
212 if (STTE_RESULT_EVENT_FINAL_RESULT == event) {
213 if (APP_STATE_PROCESSING != state) {
214 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is NOT 'Processing'.");
216 SLOG(LOG_INFO, TAG_STTD, "[Server] the size of result from engine is '%d'", data_count);
218 /* Delete timer for processing time out */
219 if (NULL != g_processing_timer) {
220 ecore_thread_main_loop_begin();
221 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
222 ecore_timer_del(g_processing_timer);
223 g_processing_timer = NULL;
224 ecore_thread_main_loop_end();
227 sttd_config_time_save();
228 sttd_config_time_reset();
230 sttd_recorder_clear();
232 sttd_client_set_state(uid, APP_STATE_READY);
233 stt_client_unset_current_recognition();
235 if (NULL == data || 0 == data_count) {
236 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
237 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
238 int reason = (int)STTD_ERROR_OPERATION_FAILED;
240 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
241 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
245 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
246 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
247 int reason = (int)STTD_ERROR_OPERATION_FAILED;
249 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
250 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
255 /* change state of uid */
256 // sttd_client_set_state(uid, APP_STATE_READY);
257 // stt_client_unset_current_recognition();
259 } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) {
260 SLOG(LOG_INFO, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event, data_count);
262 sttd_config_time_save();
263 sttd_config_time_reset();
265 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
266 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
267 int reason = (int)STTD_ERROR_OPERATION_FAILED;
269 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
270 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
274 } else if (STTE_RESULT_EVENT_ERROR == event) {
275 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
277 /* Delete timer for processing time out */
278 if (NULL != g_processing_timer) {
279 ecore_thread_main_loop_begin();
280 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
281 ecore_timer_del(g_processing_timer);
282 g_processing_timer = NULL;
283 ecore_thread_main_loop_end();
285 sttd_config_time_reset();
288 if (APP_STATE_RECORDING == state) {
289 ret = sttd_engine_agent_recognize_cancel();
291 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel: result(%d)", ret);
295 sttd_client_set_state(uid, APP_STATE_READY);
296 stt_client_unset_current_recognition();
298 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
299 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
302 int reason = (int)STTD_ERROR_INVALID_STATE;
303 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
304 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
308 /* change state of uid */
309 // sttd_client_set_state(uid, APP_STATE_READY);
310 // stt_client_unset_current_recognition();
315 SLOG(LOG_INFO, TAG_STTD, "=====");
316 SLOG(LOG_DEBUG, TAG_STTD, " ");
317 pthread_mutex_unlock(&stte_result_mutex);
319 return STTD_ERROR_NONE;
322 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)
324 pthread_mutex_lock(&stte_result_time_mutex);
326 SECURE_SLOG(LOG_INFO, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)",
327 index, event, text, start_time, end_time);
330 ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
332 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
333 pthread_mutex_unlock(&stte_result_time_mutex);
337 pthread_mutex_unlock(&stte_result_time_mutex);
342 int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
344 SLOG(LOG_INFO, TAG_STTD, "===== Speech status detected Callback");
346 unsigned int uid = stt_client_get_current_recognition();
347 if (STT_INVALID_UID != uid) {
349 if (0 != sttd_client_get_state(uid, &state)) {
350 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid ");
351 return STTD_ERROR_OPERATION_FAILED;
354 if (APP_STATE_RECORDING != state && APP_STATE_PROCESSING != state) {
355 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording, state(%d), status(%d)", state, status);
356 return STTD_ERROR_INVALID_STATE;
359 if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) {
360 SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected");
361 sttdc_send_speech_status(uid, status);
362 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
363 SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
364 ecore_thread_main_loop_begin();
365 ecore_timer_add(0, __stop_by_silence, NULL);
366 ecore_thread_main_loop_end();
369 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recognition uid is not valid ");
372 SLOG(LOG_INFO, TAG_STTD, "=====");
373 SLOG(LOG_DEBUG, TAG_STTD, " ");
375 return STTD_ERROR_NONE;
378 int __server_error_callback(stte_error_e error, const char* msg)
380 SLOG(LOG_ERROR, TAG_STTD, "[Server] Error Callback is called, error(%d), err_msg(%s)", error, msg);
381 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
383 return STTD_ERROR_NONE;
386 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
388 if (NULL == engine_id) {
389 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
392 SLOG(LOG_INFO, TAG_STTD, "[Server] New default engine : %s", engine_id);
396 /* need to change state of app to ready */
398 uid = stt_client_get_current_recognition();
401 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%u)", uid);
403 sttd_server_cancel(uid);
404 sttdc_send_set_state(uid, (int)APP_STATE_READY);
406 stt_client_unset_current_recognition();
410 int ret = sttd_engine_agent_set_default_engine(engine_id);
412 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
414 if (NULL != language) {
415 ret = sttd_engine_agent_set_default_language(language);
417 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
420 ret = sttd_engine_agent_set_silence_detection(support_silence);
422 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
428 void __sttd_server_language_changed_cb(const char* language, void* user_data)
430 if (NULL == language) {
431 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
434 SLOG(LOG_INFO, TAG_STTD, "[Server] Get language changed : %s", language);
437 int ret = sttd_engine_agent_set_default_language(language);
439 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
444 void __sttd_server_silence_changed_cb(bool value, void* user_data)
446 SLOG(LOG_INFO, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
449 ret = sttd_engine_agent_set_silence_detection(value);
451 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
459 int sttd_initialize(stte_request_callback_s *callback)
463 if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
464 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
467 if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) {
468 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex.");
471 if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
472 __sttd_server_silence_changed_cb, NULL)) {
473 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
476 ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
478 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
482 /* Engine Agent initialize */
483 ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
484 __server_speech_status_callback, __server_error_callback);
486 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
491 ret = sttd_engine_agent_load_current_engine(callback);
493 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
497 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, sttd_cleanup_client, NULL);
498 if (NULL == g_check_client_timer) {
499 SLOG(LOG_WARN, TAG_STTD, "[Main Warning] Fail to create timer of client check");
502 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] initialize");
509 if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
510 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
513 if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) {
514 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex.");
518 if (0 < g_list_length(g_proc_list)) {
519 iter = g_list_first(g_proc_list);
520 while (NULL != iter) {
521 g_proc_list = g_list_delete_link(g_proc_list, iter);
522 iter = g_list_first(g_proc_list);
526 sttd_recorder_deinitialize();
528 sttd_config_finalize();
530 sttd_engine_agent_release();
532 if (NULL != g_check_client_timer) {
533 ecore_timer_del(g_check_client_timer);
534 g_check_client_timer = NULL;
536 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete ecore timer handle");
539 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] finalize");
541 return STTD_ERROR_NONE;
544 static void __read_proc()
547 struct dirent *dirp = NULL;
551 if (0 < g_list_length(g_proc_list)) {
552 iter = g_list_first(g_proc_list);
553 while (NULL != iter) {
554 g_proc_list = g_list_delete_link(g_proc_list, iter);
555 iter = g_list_first(g_proc_list);
559 dp = opendir("/proc");
561 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
567 tmp = atoi(dirp->d_name);
568 if (0 >= tmp) continue;
569 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
571 } while (NULL != dirp);
577 Eina_Bool sttd_cleanup_client(void *data)
579 unsigned int* client_list = NULL;
580 int client_count = 0;
585 if (0 != sttd_client_get_list(&client_list, &client_count)) {
586 if (NULL != client_list)
592 if (NULL != client_list) {
593 SLOG(LOG_INFO, TAG_STTD, "===== Clean up client ");
597 for (i = 0; i < client_count; i++) {
598 int pid = sttd_client_get_pid(client_list[i]);
600 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
606 for (j = 0; j < g_list_length(g_proc_list); j++) {
607 iter = g_list_nth(g_proc_list, j);
609 if (pid == GPOINTER_TO_INT(iter->data)) {
610 SLOG(LOG_DEBUG, TAG_STTD, "uid (%u) is running", client_list[i]);
617 if (false == exist) {
618 SLOG(LOG_ERROR, TAG_STTD, "uid (%u) should be removed", client_list[i]);
619 sttd_server_finalize(client_list[i]);
622 result = sttdc_send_hello(client_list[i]);
625 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%u) should be removed.", client_list[i]);
626 sttd_server_finalize(client_list[i]);
627 } else if (-1 == result) {
628 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
633 SLOG(LOG_INFO, TAG_STTD, "=====");
634 SLOG(LOG_DEBUG, TAG_STTD, " ");
643 * STT Server Functions for Client
646 int sttd_server_initialize(int pid, unsigned int uid, bool* silence, bool* credential)
648 int ret = STTD_ERROR_NONE;
650 SLOG(LOG_INFO, TAG_STTD, "[Server] server initialize");
652 /* check if uid is valid */
654 if (0 == sttd_client_get_state(uid, &state)) {
655 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
656 return STTD_ERROR_NONE;
659 ret = sttd_engine_agent_get_option_supported(silence);
661 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
665 ret = sttd_engine_agent_is_credential_needed(uid, credential);
667 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
671 /* Add client information to client manager */
672 ret = sttd_client_add(pid, uid);
674 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
678 SLOG(LOG_INFO, TAG_STTD, "[Server Success] server initialize");
680 return STTD_ERROR_NONE;
683 static Eina_Bool __quit_ecore_loop(void *data)
685 SLOG(LOG_INFO, TAG_STTD, "[Server] Quit");
687 stt_network_finalize();
689 sttd_dbus_close_connection();
690 ecore_main_loop_quit();
692 SLOG(LOG_INFO, TAG_STTD, "");
697 int sttd_server_finalize(unsigned int uid)
699 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter Finalize");
701 /* check if uid is valid */
703 if (0 != sttd_client_get_state(uid, &state)) {
704 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
705 return STTD_ERROR_INVALID_PARAMETER;
708 /* release recorder */
709 if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
710 if (APP_STATE_RECORDING == state) {
711 if (NULL != g_recording_timer) {
712 ecore_timer_del(g_recording_timer);
713 g_recording_timer = NULL;
717 if (APP_STATE_PROCESSING == state) {
718 if (NULL != g_processing_timer) {
719 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
720 ecore_timer_del(g_processing_timer);
721 g_processing_timer = NULL;
725 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] stt_cancel is invoked while state is (%d)", state);
727 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
728 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
731 stt_client_unset_current_recognition();
734 /* Remove client information */
735 if (0 != sttd_client_delete(uid)) {
736 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
739 /* unload engine, if ref count of client is 0 */
740 if (0 == sttd_client_get_ref_count()) {
741 // sttd_dbus_close_connection();
742 ecore_timer_add(0, __quit_ecore_loop, NULL);
745 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End Finalize");
747 return STTD_ERROR_NONE;
750 int sttd_server_get_supported_engines(unsigned int uid, GSList** engine_list)
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(%u) is not Ready", uid);
762 return STTD_ERROR_INVALID_STATE;
765 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported egnines");
768 ret = sttd_engine_agent_get_engine_list(engine_list);
770 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
774 return STTD_ERROR_NONE;
777 int sttd_server_set_current_engine(unsigned int uid, const char* engine_id, bool* silence, bool* credential)
779 /* Check if uid is valid */
781 if (0 != sttd_client_get_state(uid, &state)) {
782 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
783 return STTD_ERROR_INVALID_PARAMETER;
786 /* Check state of uid */
787 if (APP_STATE_READY != state) {
788 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%u) is not Ready", uid);
789 return STTD_ERROR_INVALID_STATE;
792 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] set current egnines, uid(%u), engine_id(%s)", uid, engine_id);
795 ret = sttd_engine_agent_load_current_engine(NULL);
797 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
801 ret = sttd_engine_agent_get_option_supported(silence);
803 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
807 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
808 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
812 return STTD_ERROR_NONE;
815 int sttd_server_get_current_engine(unsigned int uid, char** engine_id)
817 /* Check if uid is valid */
819 if (0 != sttd_client_get_state(uid, &state)) {
820 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
821 return STTD_ERROR_INVALID_PARAMETER;
824 /* Check state of uid */
825 if (APP_STATE_READY != state) {
826 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%u) is not Ready", uid);
827 return STTD_ERROR_INVALID_STATE;
830 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get current egnines, uid(%u)", uid);
833 ret = sttd_engine_agent_get_current_engine(engine_id);
835 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
839 return STTD_ERROR_NONE;
842 int sttd_server_check_app_agreed(unsigned int uid, const char* appid, bool* available)
844 /* Check if uid is valid */
846 if (0 != sttd_client_get_state(uid, &state)) {
847 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
848 return STTD_ERROR_INVALID_PARAMETER;
851 /* Check state of uid */
852 if (APP_STATE_READY != state) {
853 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%u) is not Ready", uid);
854 return STTD_ERROR_INVALID_STATE;
857 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] check app agreed");
859 /* Ask engine available */
862 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
864 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
869 stt_client_set_app_agreed(uid);
870 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
875 return STTD_ERROR_NONE;
879 int sttd_server_get_supported_languages(unsigned int uid, GSList** lang_list)
881 /* check if uid is valid */
883 if (0 != sttd_client_get_state(uid, &state)) {
884 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
885 return STTD_ERROR_INVALID_PARAMETER;
888 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported languages");
890 /* get language list from engine */
891 int ret = sttd_engine_agent_supported_langs(lang_list);
893 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
897 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
899 return STTD_ERROR_NONE;
902 int sttd_server_get_current_langauage(unsigned int uid, char** current_lang)
904 /* check if uid is valid */
906 if (0 != sttd_client_get_state(uid, &state)) {
907 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
908 return STTD_ERROR_INVALID_PARAMETER;
911 if (NULL == current_lang) {
912 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
913 return STTD_ERROR_INVALID_PARAMETER;
916 /*get current language from engine */
917 int ret = sttd_engine_agent_get_default_lang(current_lang);
919 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
923 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get default language, current_lang(%s)", *current_lang);
925 return STTD_ERROR_NONE;
928 int sttd_server_set_private_data(unsigned int uid, const char* key, const char* data)
930 /* check if uid is valid */
932 if (0 != sttd_client_get_state(uid, &state)) {
933 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
934 return STTD_ERROR_INVALID_PARAMETER;
937 if (NULL == key || NULL == data) {
938 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
939 return STTD_ERROR_INVALID_PARAMETER;
942 /* set private data to engine */
944 ret = sttd_engine_agent_set_private_data(key, data);
946 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
950 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Set private data");
952 return STTD_ERROR_NONE;
955 int sttd_server_get_private_data(unsigned int uid, const char* key, char** data)
957 /* check if uid is valid */
959 if (0 != sttd_client_get_state(uid, &state)) {
960 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
961 return STTD_ERROR_INVALID_PARAMETER;
964 if (NULL == key || NULL == data) {
965 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
966 return STTD_ERROR_INVALID_PARAMETER;
969 /* get private data to engine */
971 ret = sttd_engine_agent_get_private_data(key, data);
973 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
977 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
979 return STTD_ERROR_NONE;
982 int sttd_server_is_recognition_type_supported(unsigned int uid, const char* type, int* support)
984 /* check if uid is valid */
986 if (0 != sttd_client_get_state(uid, &state)) {
987 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
988 return STTD_ERROR_INVALID_PARAMETER;
991 if (NULL == type || NULL == support) {
992 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
993 return STTD_ERROR_INVALID_PARAMETER;
997 int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
999 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
1003 *support = (int)temp;
1005 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
1007 return STTD_ERROR_NONE;
1010 int sttd_server_set_start_sound(unsigned int uid, const char* file)
1012 /* check if uid is valid */
1014 if (0 != sttd_client_get_state(uid, &state)) {
1015 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1016 return STTD_ERROR_INVALID_PARAMETER;
1019 int ret = sttd_client_set_start_sound(uid, file);
1021 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1028 int sttd_server_set_stop_sound(unsigned int uid, const char* file)
1030 /* check if uid is valid */
1032 if (0 != sttd_client_get_state(uid, &state)) {
1033 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1034 return STTD_ERROR_INVALID_PARAMETER;
1037 int ret = sttd_client_set_stop_sound(uid, file);
1039 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1047 Eina_Bool __check_recording_state(void *data)
1050 unsigned int uid = stt_client_get_current_recognition();
1055 if (0 != sttdc_send_get_state(uid, (int*)&state)) {
1056 /* client is removed */
1057 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%u) should be removed.", uid);
1058 sttd_server_finalize(uid);
1062 if (APP_STATE_READY == state) {
1064 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%u) is 'Ready'. The STT service should cancel recording", uid);
1065 sttd_server_cancel(uid);
1066 } else if (APP_STATE_PROCESSING == state) {
1067 /* Cancel stt and send change state */
1068 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%u) is 'Processing'. The STT service should cancel recording", uid);
1069 sttd_server_cancel(uid);
1070 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1073 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1081 Eina_Bool __stop_by_recording_timeout(void *data)
1083 SLOG(LOG_INFO, TAG_STTD, "===== Stop by timeout");
1085 if (NULL != g_recording_timer) {
1086 ecore_timer_del(g_recording_timer);
1087 g_recording_timer = NULL;
1090 unsigned int uid = stt_client_get_current_recognition();
1091 if (STT_INVALID_UID != uid) {
1092 /* cancel engine recognition */
1093 int ret = sttd_server_stop(uid);
1095 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1099 SLOG(LOG_INFO, TAG_STTD, "=====");
1100 SLOG(LOG_DEBUG, TAG_STTD, " ");
1105 void __sttd_server_recorder_start(void* data)
1107 uintptr_t puid = (uintptr_t)data;
1108 unsigned int uid = (unsigned int)puid;
1109 int current_uid = stt_client_get_current_recognition();
1111 if (uid != current_uid) {
1112 stt_client_unset_current_recognition();
1116 char appid[1024] = {0, };
1117 int pid = sttd_client_get_pid(uid);
1119 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1);
1120 if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1121 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to get application ID");
1123 SLOG(LOG_DEBUG, TAG_STTD, "[DEBUG] Current app id is %s", appid);
1126 ret = sttd_engine_agent_recognize_start_recorder(uid, appid);
1128 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1132 app_state_e temp_app_state;
1133 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1134 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1137 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1138 /* Notify uid state change */
1139 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1140 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1144 void __sttd_start_sound_completed_cb(int id, void *user_data)
1146 SLOG(LOG_INFO, TAG_STTD, "===== Start sound completed");
1148 /* After wav play callback, recorder start */
1149 ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1151 SLOG(LOG_INFO, TAG_STTD, "=====");
1152 SLOG(LOG_DEBUG, TAG_STTD, " ");
1156 static int __sttd_server_check_precondition_to_start(unsigned int uid, const char* appid)
1158 /* check if uid is valid */
1160 if (0 != sttd_client_get_state(uid, &state)) {
1161 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1162 return STTD_ERROR_INVALID_PARAMETER;
1165 /* check uid state */
1166 if (APP_STATE_READY != state) {
1167 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1168 return STTD_ERROR_INVALID_STATE;
1171 int ret = STTD_ERROR_NONE;
1172 if (false == stt_client_get_app_agreed(uid)) {
1174 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1176 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1180 if (false == temp) {
1181 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1182 return STTD_ERROR_PERMISSION_DENIED;
1185 stt_client_set_app_agreed(uid);
1188 /* check if engine use network */
1189 if (true == sttd_engine_agent_need_network()) {
1190 if (false == stt_network_is_connected()) {
1191 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1192 return STTD_ERROR_OUT_OF_NETWORK;
1198 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)
1200 if (NULL == lang || NULL == recognition_type) {
1201 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1202 return STTD_ERROR_INVALID_PARAMETER;
1205 int ret = __sttd_server_check_precondition_to_start(uid, appid);
1207 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to satisfy preconditions, ret(0x%x)", ret);
1211 if (NULL != g_recording_timer) {
1212 ecore_timer_del(g_recording_timer);
1213 g_recording_timer = NULL;
1216 if (NULL != g_processing_timer) {
1217 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1218 ecore_timer_del(g_processing_timer);
1219 g_processing_timer = NULL;
1223 ret = sttd_client_get_start_sound(uid, &sound);
1225 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1229 ret = sttd_client_set_audio_id(uid, audio_id);
1231 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set audio_id(%s)", audio_id);
1232 if (NULL != sound) free(sound);
1236 if (0 != stt_client_set_current_recognition(uid)) {
1237 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1238 if (NULL != sound) free(sound);
1239 return STTD_ERROR_RECORDER_BUSY;
1242 /* engine start recognition */
1243 SLOG(LOG_INFO, TAG_STTD, "[Server] start : uid(%u), lang(%s), recog_type(%s)",
1244 uid, lang, recognition_type);
1246 SLOG(LOG_INFO, TAG_STTD, "[Server] start sound : %s", sound);
1248 /* 1. Set audio session */
1249 ret = sttd_recorder_set_audio_session();
1251 stt_client_unset_current_recognition();
1252 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1253 if (NULL != sound) free(sound);
1257 bool is_sound_done = false;
1259 /* 2. Request wav play */
1260 if (NULL != sound) {
1262 uintptr_t puid = (uintptr_t)uid;
1263 sound_stream_info_h wav_stream_info_h;
1264 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1265 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1266 is_sound_done = true;
1268 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_start_sound_completed_cb, (void*)puid, &id);
1269 if (WAV_PLAYER_ERROR_NONE != ret) {
1270 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1271 is_sound_done = true;
1274 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1275 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1281 is_sound_done = true;
1284 /* 3. Create recorder & engine initialize */
1285 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1287 stt_client_unset_current_recognition();
1288 sttd_recorder_unset_audio_session();
1289 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1293 if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1294 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1297 /* change uid state */
1298 sttd_client_set_state(uid, APP_STATE_RECORDING);
1300 g_recording_log_count = 0;
1302 app_state_e temp_app_state;
1304 if (true == is_sound_done) {
1305 SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
1307 ret = sttd_engine_agent_recognize_start_recorder(uid, appid);
1309 stt_client_unset_current_recognition();
1310 sttd_recorder_unset_audio_session();
1312 sttd_engine_agent_recognize_cancel();
1313 if (NULL != g_recording_timer) {
1314 ecore_timer_del(g_recording_timer);
1315 g_recording_timer = NULL;
1317 sttd_client_set_state(uid, APP_STATE_READY);
1319 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1323 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1324 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid");
1325 return STTD_ERROR_INVALID_PARAMETER;
1327 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1328 /* Notify uid state change */
1329 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1332 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Start recognition");
1333 return STTD_RESULT_STATE_DONE;
1336 SLOG(LOG_INFO, TAG_STTD, "[Server] Wait sound finish");
1338 return STTD_RESULT_STATE_NOT_DONE;
1341 Eina_Bool __time_out_for_processing(void *data)
1343 if (NULL != g_processing_timer) {
1344 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1345 ecore_timer_del(g_processing_timer);
1346 g_processing_timer = NULL;
1349 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter __time_out_for_processing");
1352 unsigned int uid = stt_client_get_current_recognition();
1353 if (STT_INVALID_UID == uid) return EINA_FALSE;
1355 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by timeout for processing");
1358 int ret = sttd_engine_agent_recognize_cancel();
1360 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1363 if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1364 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1366 /* send error msg */
1367 int reason = (int)STTD_ERROR_TIMED_OUT;
1368 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1369 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1373 /* Change uid state */
1374 sttd_client_set_state(uid, APP_STATE_READY);
1376 stt_client_unset_current_recognition();
1378 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End __time_out_for_processing");
1383 void __sttd_server_engine_stop(void* data)
1385 uintptr_t puid = (uintptr_t)data;
1386 unsigned int uid = (unsigned int)puid;
1387 /* change uid state */
1388 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1390 /* Notify uid state change */
1391 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1393 /* Unset audio session */
1394 int ret = sttd_recorder_unset_audio_session();
1396 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1401 ret = sttd_engine_agent_recognize_stop_engine();
1403 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1407 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1410 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1412 SLOG(LOG_INFO, TAG_STTD, "===== Stop sound completed");
1414 /* After wav play callback, engine stop */
1415 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1417 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1418 SLOG(LOG_DEBUG, TAG_STTD, " ");
1422 int sttd_server_stop(unsigned int uid)
1424 /* check if uid is valid */
1426 if (0 != sttd_client_get_state(uid, &state)) {
1427 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1428 return STTD_ERROR_INVALID_PARAMETER;
1431 /* check uid state */
1432 if (APP_STATE_RECORDING != state) {
1433 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1434 return STTD_ERROR_INVALID_STATE;
1437 if (NULL != g_recording_timer) {
1438 ecore_timer_del(g_recording_timer);
1439 g_recording_timer = NULL;
1442 if (NULL != g_processing_timer) {
1443 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1444 ecore_timer_del(g_processing_timer);
1445 g_processing_timer = NULL;
1449 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1450 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1451 return STTD_ERROR_OPERATION_FAILED;
1454 SLOG(LOG_INFO, TAG_STTD, "[Server] stop sound path : %s", sound);
1457 /* 1. Stop recorder */
1458 ret = sttd_engine_agent_recognize_stop_recorder();
1460 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1461 if (0 != sttd_engine_agent_recognize_cancel()) {
1462 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1464 if (NULL != sound) free(sound);
1468 /* 2. Request wav play */
1469 if (NULL != sound) {
1471 uintptr_t puid = (uintptr_t)uid;
1472 sound_stream_info_h wav_stream_info_h;
1473 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1474 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1476 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1477 if (WAV_PLAYER_ERROR_NONE != ret) {
1478 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1480 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1483 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1484 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1489 SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
1490 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1492 return STTD_RESULT_STATE_NOT_DONE;
1494 SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
1496 /* Unset audio session */
1497 ret = sttd_recorder_unset_audio_session();
1499 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1504 ret = sttd_engine_agent_recognize_stop_engine();
1506 stt_client_unset_current_recognition();
1507 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1511 /* change uid state */
1512 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1514 /* Notify uid state change */
1515 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1517 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Stop recognition");
1519 SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
1520 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1522 return STTD_RESULT_STATE_DONE;
1525 return STTD_ERROR_NONE;
1528 static int __sttd_server_cancel(unsigned int uid)
1530 /* check if uid is valid */
1532 if (0 != sttd_client_get_state(uid, &state)) {
1533 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1534 return STTD_ERROR_INVALID_PARAMETER;
1537 /* check uid state */
1538 if (APP_STATE_READY == state) {
1539 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1540 return STTD_ERROR_NONE;
1543 stt_client_unset_current_recognition();
1545 if (NULL != g_recording_timer) {
1546 ecore_timer_del(g_recording_timer);
1547 g_recording_timer = NULL;
1550 if (NULL != g_processing_timer) {
1551 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
1552 ecore_timer_del(g_processing_timer);
1553 g_processing_timer = NULL;
1556 if (APP_STATE_RECORDING == state) {
1557 /* Unset audio session */
1558 int ret = sttd_recorder_unset_audio_session();
1560 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1565 /* change uid state */
1566 sttd_client_set_state(uid, APP_STATE_READY);
1568 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by app's request");
1570 /* cancel engine recognition */
1571 int ret = sttd_engine_agent_recognize_cancel();
1573 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1577 /* Notify uid state change */
1578 sttdc_send_set_state(uid, APP_STATE_READY);
1583 int sttd_server_cancel(unsigned int uid)
1585 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel");
1587 int ret = __sttd_server_cancel(uid);
1589 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(0x%x)", ret);
1592 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel");
1596 int sttd_server_start_file(unsigned int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential,
1597 const char* filepath, stte_audio_type_e audio_type, int channels, int sample_rate)
1599 if (NULL == lang || NULL == recognition_type || NULL == filepath) {
1600 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1601 return STTD_ERROR_INVALID_PARAMETER;
1604 int ret = __sttd_server_check_precondition_to_start(uid, appid);
1606 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to satisfy preconditions, ret(0x%x)", ret);
1610 if (0 != stt_client_set_current_recognition(uid)) {
1611 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1612 return STTD_ERROR_RECORDER_BUSY;
1615 /* engine start recognition */
1616 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%u), lang(%s), recog_type(%s), appid(%s), file(%s), audio_type(%d), channels(%d), sample_rate(%d)", uid, lang, recognition_type, appid, filepath, audio_type, channels, sample_rate);
1618 /* 1. Set audio session */
1619 ret = sttd_recorder_set_audio_session();
1621 stt_client_unset_current_recognition();
1622 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1626 /* 2. Start engine to recognize */
1627 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1629 stt_client_unset_current_recognition();
1630 sttd_recorder_unset_audio_session();
1631 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start engine : result(%d)", ret);
1635 sttd_client_set_state(uid, APP_STATE_RECORDING);
1636 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1638 /* 3. Start to send pcm from file to engine */
1639 ret = sttd_engine_agent_recognize_start_file(uid, filepath, audio_type, channels, sample_rate);
1641 stt_client_unset_current_recognition();
1642 sttd_recorder_unset_audio_session();
1643 sttd_engine_agent_recognize_cancel();
1644 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start file : result(%d)", ret);
1648 /* 4. Stop to send pcm from file */
1649 ret = sttd_engine_agent_recognize_stop_file();
1651 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1652 stt_client_unset_current_recognition();
1653 sttd_recorder_unset_audio_session();
1654 sttd_engine_agent_recognize_cancel();
1658 /* 5. change & notify uid state */
1659 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1660 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1662 /* 6. Unset audio session */
1663 ret = sttd_recorder_unset_audio_session();
1665 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1666 stt_client_unset_current_recognition();
1670 /* 7. Stop engine */
1671 ret = sttd_engine_agent_recognize_stop_engine();
1673 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1674 stt_client_unset_current_recognition();
1678 return STTD_ERROR_NONE;
1681 int sttd_server_cancel_file(unsigned int uid)
1683 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel_file");
1685 int ret = __sttd_server_cancel(uid);
1687 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel file : result(0x%x)", ret);
1690 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel_file");