2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
15 #include <sound_manager.h>
16 #include <wav_player.h>
18 #include "stt_network.h"
19 #include "sttd_client_data.h"
20 #include "sttd_config.h"
21 #include "sttd_dbus.h"
22 #include "sttd_engine_agent.h"
23 #include "sttd_main.h"
24 #include "sttd_recorder.h"
25 #include "sttd_server.h"
27 static pthread_mutex_t stte_result_mutex = PTHREAD_MUTEX_INITIALIZER;
28 static pthread_mutex_t stte_result_time_mutex = PTHREAD_MUTEX_INITIALIZER;
32 * STT Server static variable
34 static double g_processing_timeout = 30;
36 static double g_recording_timeout = 60;
38 Ecore_Timer* g_recording_timer = NULL;
39 Ecore_Timer* g_processing_timer = NULL;
41 static int g_recording_log_count = 0;
43 static GList *g_proc_list = NULL;
46 * STT Server Callback Functions
48 void __stop_by_silence(void *data)
50 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by silence detection");
54 uid = stt_client_get_current_recognition();
58 ret = sttd_server_stop(uid);
63 if (STTD_RESULT_STATE_DONE == ret) {
64 ret = sttdc_send_set_state(uid, (int)APP_STATE_PROCESSING);
66 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send state : result(%d)", ret);
69 sttd_server_finalize(uid);
70 stt_client_unset_current_recognition();
74 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
77 SLOG(LOG_DEBUG, TAG_STTD, "=====");
78 SLOG(LOG_DEBUG, TAG_STTD, " ");
83 static void __cancel_recognition_internal()
85 if (NULL != g_recording_timer) {
86 ecore_timer_del(g_recording_timer);
87 g_recording_timer = NULL;
91 uid = stt_client_get_current_recognition();
94 /* cancel engine recognition */
95 int ret = sttd_engine_agent_recognize_cancel();
97 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
100 /* change uid state */
101 sttd_client_set_state(uid, APP_STATE_READY);
102 stt_client_unset_current_recognition();
104 ret = sttdc_send_set_state(uid, (int)APP_STATE_READY);
106 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send state change : result(%d)", ret);
109 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
113 static void __cancel_by_error(void *data)
115 SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by error");
117 __cancel_recognition_internal();
119 SLOG(LOG_DEBUG, TAG_STTD, "=====");
120 SLOG(LOG_DEBUG, TAG_STTD, " ");
125 int __server_audio_recorder_callback(const void* data, const unsigned int length)
130 if (NULL == data || 0 == length) {
131 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Recording data is not valid");
132 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
136 uid = stt_client_get_current_recognition();
138 ret = sttd_engine_agent_set_recording_data(data, length);
140 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
143 g_recording_log_count++;
144 if (200 <= g_recording_log_count) {
145 SLOG(LOG_DEBUG, TAG_STTD, "=== Set recording data ===");
146 g_recording_log_count = 0;
149 if (NULL != g_recording_timer) {
150 ecore_timer_del(g_recording_timer);
151 g_recording_timer = NULL;
153 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current uid in recording is is not valid");
160 void __server_audio_interrupt_callback()
162 SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by sound interrupt");
164 __cancel_recognition_internal();
166 SLOG(LOG_DEBUG, TAG_STTD, "=====");
167 SLOG(LOG_DEBUG, TAG_STTD, " ");
170 void __cancel_by_no_record(void *data)
172 SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by no record");
174 __cancel_recognition_internal();
176 SLOG(LOG_DEBUG, TAG_STTD, "=====");
177 SLOG(LOG_DEBUG, TAG_STTD, " ");
182 void __server_recognition_result_callback(stte_result_event_e event, const char* type,
183 const char** data, int data_count, const char* msg, void *user_data)
186 pthread_mutex_lock(&stte_result_mutex);
188 SLOG(LOG_DEBUG, TAG_STTD, "===== RESULT event[%d] type[%s] data[%p] data_count[%d]", event, type, data, data_count);
191 int uid = stt_client_get_current_recognition();
194 if (0 == uid || 0 != sttd_client_get_state(uid, &state)) {
195 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
196 SLOG(LOG_DEBUG, TAG_STTD, "=====");
197 SLOG(LOG_DEBUG, TAG_STTD, " ");
198 pthread_mutex_unlock(&stte_result_mutex);
202 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid (%d), event(%d)", uid, event);
204 /* send result to client */
205 if (STTE_RESULT_EVENT_FINAL_RESULT == event) {
206 if (APP_STATE_PROCESSING != state) {
207 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is NOT 'Processing'.");
209 SLOG(LOG_DEBUG, TAG_STTD, "[Server] the size of result from engine is '%d'", data_count);
211 /* Delete timer for processing time out */
212 if (NULL != g_processing_timer) {
213 ecore_timer_del(g_processing_timer);
214 g_processing_timer = NULL;
217 sttd_config_time_save();
218 sttd_config_time_reset();
220 if (NULL == data || 0 == data_count) {
221 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
222 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
223 int reason = (int)STTD_ERROR_OPERATION_FAILED;
225 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
226 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
230 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
231 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
232 int reason = (int)STTD_ERROR_OPERATION_FAILED;
234 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
235 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
240 /* change state of uid */
241 sttd_client_set_state(uid, APP_STATE_READY);
242 stt_client_unset_current_recognition();
244 } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) {
245 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event, data_count);
247 sttd_config_time_save();
248 sttd_config_time_reset();
250 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
251 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
252 int reason = (int)STTD_ERROR_OPERATION_FAILED;
254 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
255 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
259 } else if (STTE_RESULT_EVENT_ERROR == event) {
260 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
262 /* Delete timer for processing time out */
263 if (NULL != g_processing_timer) {
264 ecore_timer_del(g_processing_timer);
265 g_processing_timer = NULL;
267 sttd_config_time_reset();
269 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
270 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
273 int reason = (int)STTD_ERROR_INVALID_STATE;
274 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
275 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
279 /* change state of uid */
280 sttd_client_set_state(uid, APP_STATE_READY);
281 stt_client_unset_current_recognition();
286 SLOG(LOG_DEBUG, TAG_STTD, "=====");
287 SLOG(LOG_DEBUG, TAG_STTD, " ");
288 pthread_mutex_unlock(&stte_result_mutex);
293 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)
295 pthread_mutex_lock(&stte_result_time_mutex);
297 SLOG(LOG_DEBUG, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)",
298 index, event, text, start_time, end_time);
302 ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
304 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
305 pthread_mutex_unlock(&stte_result_time_mutex);
309 pthread_mutex_unlock(&stte_result_time_mutex);
313 pthread_mutex_unlock(&stte_result_time_mutex);
318 void __server_speech_status_callback(stte_speech_status_e status, void *user_param)
320 SLOG(LOG_DEBUG, TAG_STTD, "===== Speech status detected Callback");
322 int uid = stt_client_get_current_recognition();
325 if (0 != sttd_client_get_state(uid, &state)) {
326 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid ");
330 if (APP_STATE_RECORDING != state) {
331 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
335 if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) {
336 SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected");
337 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
338 SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
339 ecore_main_loop_thread_safe_call_async(__stop_by_silence, NULL);
342 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recogntion uid is not valid ");
345 SLOG(LOG_DEBUG, TAG_STTD, "=====");
346 SLOG(LOG_DEBUG, TAG_STTD, " ");
351 void __server_error_callback(stte_error_e error, const char* msg)
353 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Error Callback is called");
357 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
359 if (NULL == engine_id) {
360 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
363 SLOG(LOG_DEBUG, TAG_STTD, "[Server] New default engine : %s", engine_id);
366 /* need to change state of app to ready */
368 uid = stt_client_get_current_recognition();
371 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%d)", uid);
373 sttd_server_cancel(uid);
374 sttdc_send_set_state(uid, (int)APP_STATE_READY);
376 stt_client_unset_current_recognition();
380 int ret = sttd_engine_agent_set_default_engine(engine_id);
382 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
384 if (NULL != language) {
385 ret = sttd_engine_agent_set_default_language(language);
387 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
390 ret = sttd_engine_agent_set_silence_detection(support_silence);
392 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
397 void __sttd_server_language_changed_cb(const char* language, void* user_data)
399 if (NULL == language) {
400 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
403 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get language changed : %s", language);
406 int ret = sttd_engine_agent_set_default_language(language);
408 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
413 void __sttd_server_silence_changed_cb(bool value, void* user_data)
415 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
418 ret = sttd_engine_agent_set_silence_detection(value);
420 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
429 static void __sig_handler(int signo)
431 /* restore signal handler */
432 signal(signo, SIG_DFL);
434 /* Send error signal to clients */
435 int* client_list = NULL;
436 int client_count = 0;
438 if (0 != sttd_client_get_list(&client_list, &client_count)) {
439 if (NULL != client_list) {
445 if (NULL != client_list) {
446 for (i = 0; i < client_count; i++) {
447 sttdc_send_error_signal(client_list[i], STTD_ERROR_SERVICE_RESET, "Service Reset");
454 /* invoke signal again */
458 static void __register_sig_handler()
460 signal(SIGSEGV, __sig_handler);
461 signal(SIGABRT, __sig_handler);
462 signal(SIGTERM, __sig_handler);
463 signal(SIGINT, __sig_handler);
464 signal(SIGQUIT, __sig_handler);
467 int sttd_initialize(stte_request_callback_s *callback)
471 __register_sig_handler();
473 if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
474 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
477 if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) {
478 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex.");
481 if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
482 __sttd_server_silence_changed_cb, NULL)) {
483 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
486 ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
488 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
492 /* Engine Agent initialize */
493 ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
494 __server_speech_status_callback, __server_error_callback);
496 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
501 ret = sttd_engine_agent_load_current_engine(callback);
503 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
507 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] initialize");
514 if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
515 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
518 if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) {
519 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex.");
523 if (0 < g_list_length(g_proc_list)) {
524 iter = g_list_first(g_proc_list);
525 while (NULL != iter) {
526 g_proc_list = g_list_remove_link(g_proc_list, iter);
527 iter = g_list_first(g_proc_list);
531 sttd_recorder_deinitialize();
533 sttd_config_finalize();
535 sttd_engine_agent_release();
537 return STTD_ERROR_NONE;
540 static void __read_proc()
544 struct dirent *dirp = NULL;
549 if (0 < g_list_length(g_proc_list)) {
550 iter = g_list_first(g_proc_list);
551 while (NULL != iter) {
552 g_proc_list = g_list_remove_link(g_proc_list, iter);
553 iter = g_list_first(g_proc_list);
557 dp = opendir("/proc");
559 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
562 ret = readdir_r(dp, &entry, &dirp);
564 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to readdir");
569 tmp = atoi(dirp->d_name);
570 if (0 >= tmp) continue;
571 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
573 } while (NULL != dirp);
579 Eina_Bool sttd_cleanup_client(void *data)
581 int* client_list = NULL;
582 int client_count = 0;
587 if (0 != sttd_client_get_list(&client_list, &client_count)) {
588 if (NULL != client_list)
594 if (NULL != client_list) {
595 SLOG(LOG_DEBUG, TAG_STTD, "===== Clean up client ");
599 for (i = 0; i < client_count; i++) {
600 int pid = sttd_client_get_pid(client_list[i]);
602 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
608 for (j = 0; j < g_list_length(g_proc_list); j++) {
609 iter = g_list_nth(g_proc_list, j);
611 if (pid == GPOINTER_TO_INT(iter->data)) {
612 SLOG(LOG_DEBUG, TAG_STTD, "uid (%d) is running", client_list[i]);
619 if (false == exist) {
620 SLOG(LOG_ERROR, TAG_STTD, "uid (%d) should be removed", client_list[i]);
621 sttd_server_finalize(client_list[i]);
624 result = sttdc_send_hello(client_list[i]);
627 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", client_list[i]);
628 sttd_server_finalize(client_list[i]);
629 } else if (-1 == result) {
630 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
635 SLOG(LOG_DEBUG, TAG_STTD, "=====");
636 SLOG(LOG_DEBUG, TAG_STTD, " ");
645 * STT Server Functions for Client
648 int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential)
650 int ret = STTD_ERROR_NONE;
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 return STTD_ERROR_NONE;
681 static Eina_Bool __quit_ecore_loop(void *data)
683 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Quit");
685 stt_network_finalize();
687 ecore_main_loop_quit();
689 SLOG(LOG_DEBUG, TAG_STTD, "");
694 int sttd_server_finalize(int uid)
696 /* check if uid is valid */
698 if (0 != sttd_client_get_state(uid, &state)) {
699 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
700 return STTD_ERROR_INVALID_PARAMETER;
703 /* release recorder */
704 if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
705 if (APP_STATE_RECORDING == state) {
706 if (NULL != g_recording_timer) {
707 ecore_timer_del(g_recording_timer);
708 g_recording_timer = NULL;
712 if (APP_STATE_PROCESSING == state) {
713 if (NULL != g_processing_timer) {
714 ecore_timer_del(g_processing_timer);
715 g_processing_timer = NULL;
719 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
720 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
723 stt_client_unset_current_recognition();
726 /* Remove client information */
727 if (0 != sttd_client_delete(uid)) {
728 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
731 /* unload engine, if ref count of client is 0 */
732 if (0 == sttd_client_get_ref_count()) {
733 // sttd_dbus_close_connection();
734 ecore_timer_add(0, __quit_ecore_loop, NULL);
737 return STTD_ERROR_NONE;
740 int sttd_server_get_supported_engines(int uid, GSList** engine_list)
742 /* Check if uid is valid */
744 if (0 != sttd_client_get_state(uid, &state)) {
745 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
746 return STTD_ERROR_INVALID_PARAMETER;
749 /* Check state of uid */
750 if (APP_STATE_READY != state) {
751 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
752 return STTD_ERROR_INVALID_STATE;
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(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(%d) is not Ready", uid);
777 return STTD_ERROR_INVALID_STATE;
781 ret = sttd_engine_agent_load_current_engine(NULL);
783 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
787 ret = sttd_engine_agent_get_option_supported(silence);
789 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
793 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
794 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
798 return STTD_ERROR_NONE;
801 int sttd_server_get_current_engine(int uid, char** engine_id)
803 /* Check if uid is valid */
805 if (0 != sttd_client_get_state(uid, &state)) {
806 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
807 return STTD_ERROR_INVALID_PARAMETER;
810 /* Check state of uid */
811 if (APP_STATE_READY != state) {
812 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
813 return STTD_ERROR_INVALID_STATE;
817 ret = sttd_engine_agent_get_current_engine(engine_id);
819 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
823 return STTD_ERROR_NONE;
826 int sttd_server_check_app_agreed(int uid, const char* appid, bool* available)
828 /* Check if uid is valid */
830 if (0 != sttd_client_get_state(uid, &state)) {
831 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
832 return STTD_ERROR_INVALID_PARAMETER;
835 /* Check state of uid */
836 if (APP_STATE_READY != state) {
837 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
838 return STTD_ERROR_INVALID_STATE;
841 /* Ask engine available */
844 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
846 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
851 stt_client_set_app_agreed(uid);
852 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
857 return STTD_ERROR_NONE;
861 int sttd_server_get_supported_languages(int uid, GSList** lang_list)
863 /* check if uid is valid */
865 if (0 != sttd_client_get_state(uid, &state)) {
866 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
867 return STTD_ERROR_INVALID_PARAMETER;
870 /* get language list from engine */
871 int ret = sttd_engine_agent_supported_langs(lang_list);
873 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
877 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
879 return STTD_ERROR_NONE;
882 int sttd_server_get_current_langauage(int uid, char** current_lang)
884 /* check if uid is valid */
886 if (0 != sttd_client_get_state(uid, &state)) {
887 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
888 return STTD_ERROR_INVALID_PARAMETER;
891 if (NULL == current_lang) {
892 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
893 return STTD_ERROR_INVALID_PARAMETER;
896 /*get current language from engine */
897 int ret = sttd_engine_agent_get_default_lang(current_lang);
899 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
903 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get default language");
905 return STTD_ERROR_NONE;
908 int sttd_server_set_private_data(int uid, const char* key, const char* data)
910 /* check if uid is valid */
912 if (0 != sttd_client_get_state(uid, &state)) {
913 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
914 return STTD_ERROR_INVALID_PARAMETER;
917 if (NULL == key || NULL == data) {
918 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
919 return STTD_ERROR_INVALID_PARAMETER;
922 /* set private data to engine */
924 ret = sttd_engine_agent_set_private_data(key, data);
926 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
930 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Set private data");
932 return STTD_ERROR_NONE;
935 int sttd_server_get_private_data(int uid, const char* key, char** data)
937 /* check if uid is valid */
939 if (0 != sttd_client_get_state(uid, &state)) {
940 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
941 return STTD_ERROR_INVALID_PARAMETER;
944 if (NULL == key || NULL == data) {
945 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
946 return STTD_ERROR_INVALID_PARAMETER;
949 /* get private data to engine */
951 ret = sttd_engine_agent_get_private_data(key, data);
953 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
957 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
959 return STTD_ERROR_NONE;
962 int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support)
964 /* check if uid is valid */
966 if (0 != sttd_client_get_state(uid, &state)) {
967 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
968 return STTD_ERROR_INVALID_PARAMETER;
971 if (NULL == type || NULL == support) {
972 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
973 return STTD_ERROR_INVALID_PARAMETER;
977 int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
979 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
983 *support = (int)temp;
985 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
987 return STTD_ERROR_NONE;
990 int sttd_server_set_start_sound(int uid, const char* file)
992 /* check if uid is valid */
994 if (0 != sttd_client_get_state(uid, &state)) {
995 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
996 return STTD_ERROR_INVALID_PARAMETER;
999 int ret = sttd_client_set_start_sound(uid, file);
1001 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1008 int sttd_server_set_stop_sound(int uid, const char* file)
1010 /* check if uid is valid */
1012 if (0 != sttd_client_get_state(uid, &state)) {
1013 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1014 return STTD_ERROR_INVALID_PARAMETER;
1017 int ret = sttd_client_set_stop_sound(uid, file);
1019 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1027 Eina_Bool __check_recording_state(void *data)
1030 int uid = stt_client_get_current_recognition();
1035 if (0 != sttdc_send_get_state(uid, (int*)&state)) {
1036 /* client is removed */
1037 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", uid);
1038 sttd_server_finalize(uid);
1042 if (APP_STATE_READY == state) {
1044 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The STT service should cancel recording", uid);
1045 sttd_server_cancel(uid);
1046 } else if (APP_STATE_PROCESSING == state) {
1047 /* Cancel stt and send change state */
1048 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The STT service should cancel recording", uid);
1049 sttd_server_cancel(uid);
1050 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1053 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1061 Eina_Bool __stop_by_recording_timeout(void *data)
1063 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by timeout");
1065 g_recording_timer = NULL;
1068 uid = stt_client_get_current_recognition();
1070 /* cancel engine recognition */
1071 int ret = sttd_server_stop(uid);
1073 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1077 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1078 SLOG(LOG_DEBUG, TAG_STTD, " ");
1083 void __sttd_server_recorder_start(void* data)
1085 intptr_t puid = (intptr_t)data;
1086 int uid = (int)puid;
1087 int current_uid = stt_client_get_current_recognition();
1089 if (uid != current_uid) {
1090 stt_client_unset_current_recognition();
1094 int ret = sttd_engine_agent_recognize_start_recorder(uid);
1096 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1100 /* Notify uid state change */
1101 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1103 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1106 void __sttd_start_sound_completed_cb(int id, void *user_data)
1108 SLOG(LOG_DEBUG, TAG_STTD, "===== Start sound completed");
1110 /* After wav play callback, recorder start */
1111 ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1113 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1114 SLOG(LOG_DEBUG, TAG_STTD, " ");
1118 int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
1120 if (NULL == lang || NULL == recognition_type) {
1121 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1122 return STTD_ERROR_INVALID_PARAMETER;
1125 /* check if uid is valid */
1127 if (0 != sttd_client_get_state(uid, &state)) {
1128 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1129 return STTD_ERROR_INVALID_PARAMETER;
1132 /* check uid state */
1133 if (APP_STATE_READY != state) {
1134 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1135 return STTD_ERROR_INVALID_STATE;
1139 if (false == stt_client_get_app_agreed(uid)) {
1141 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1143 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1147 if (false == temp) {
1148 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1149 return STTD_ERROR_PERMISSION_DENIED;
1152 stt_client_set_app_agreed(uid);
1155 /* check if engine use network */
1156 if (true == sttd_engine_agent_need_network()) {
1157 if (false == stt_network_is_connected()) {
1158 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1159 return STTD_ERROR_OUT_OF_NETWORK;
1164 ret = sttd_client_get_start_sound(uid, &sound);
1166 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1170 if (0 != stt_client_set_current_recognition(uid)) {
1171 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1172 if (NULL != sound) free(sound);
1173 return STTD_ERROR_RECORDER_BUSY;
1176 /* engine start recognition */
1177 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)",
1178 uid, lang, recognition_type);
1180 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start sound : %s", sound);
1182 /* 1. Set audio session */
1183 ret = sttd_recorder_set_audio_session();
1185 stt_client_unset_current_recognition();
1186 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1187 if (NULL != sound) free(sound);
1191 bool is_sound_done = false;
1193 /* 2. Request wav play */
1194 if (NULL != sound) {
1196 intptr_t puid = (intptr_t)uid;
1197 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_start_sound_completed_cb, (void*)puid, &id);
1198 if (WAV_PLAYER_ERROR_NONE != ret) {
1199 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1200 is_sound_done = true;
1205 is_sound_done = true;
1208 /* 3. Create recorder & engine initialize */
1209 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1211 stt_client_unset_current_recognition();
1212 sttd_recorder_unset_audio_session();
1213 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1217 if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1218 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1221 /* change uid state */
1222 sttd_client_set_state(uid, APP_STATE_RECORDING);
1224 g_recording_log_count = 0;
1226 if (true == is_sound_done) {
1227 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1229 ret = sttd_engine_agent_recognize_start_recorder(uid);
1231 stt_client_unset_current_recognition();
1232 sttd_recorder_unset_audio_session();
1234 sttd_engine_agent_recognize_cancel();
1235 ecore_timer_del(g_recording_timer);
1236 sttd_client_set_state(uid, APP_STATE_READY);
1238 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1242 /* Notify uid state change */
1243 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1245 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1246 return STTD_RESULT_STATE_DONE;
1249 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Wait sound finish");
1251 return STTD_RESULT_STATE_NOT_DONE;
1254 Eina_Bool __time_out_for_processing(void *data)
1256 g_processing_timer = NULL;
1259 int uid = stt_client_get_current_recognition();
1260 if (0 == uid) return EINA_FALSE;
1263 int ret = sttd_engine_agent_recognize_cancel();
1265 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1268 if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1269 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1271 /* send error msg */
1272 int reason = (int)STTD_ERROR_TIMED_OUT;
1273 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1274 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1278 /* Change uid state */
1279 sttd_client_set_state(uid, APP_STATE_READY);
1281 stt_client_unset_current_recognition();
1286 void __sttd_server_engine_stop(void* data)
1288 intptr_t puid = (intptr_t)data;
1289 int uid = (int)puid;
1290 /* change uid state */
1291 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1293 /* Notify uid state change */
1294 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1296 /* Unset audio session */
1297 int ret = sttd_recorder_unset_audio_session();
1299 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1304 ret = sttd_engine_agent_recognize_stop_engine();
1306 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1310 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1313 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1315 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop sound completed");
1317 /* After wav play callback, engine stop */
1318 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1320 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1321 SLOG(LOG_DEBUG, TAG_STTD, " ");
1325 int sttd_server_stop(int uid)
1327 /* check if uid is valid */
1329 if (0 != sttd_client_get_state(uid, &state)) {
1330 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1331 return STTD_ERROR_INVALID_PARAMETER;
1334 /* check uid state */
1335 if (APP_STATE_RECORDING != state) {
1336 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1337 return STTD_ERROR_INVALID_STATE;
1340 if (NULL != g_recording_timer) {
1341 ecore_timer_del(g_recording_timer);
1342 g_recording_timer = NULL;
1346 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1347 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1348 return STTD_ERROR_OPERATION_FAILED;
1351 SLOG(LOG_DEBUG, TAG_STTD, "[Server] stop sound path : %s", sound);
1354 /* 1. Stop recorder */
1355 ret = sttd_engine_agent_recognize_stop_recorder();
1357 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1358 if (0 != sttd_engine_agent_recognize_cancel()) {
1359 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1361 if (NULL != sound) free(sound);
1365 /* 2. Request wav play */
1366 if (NULL != sound) {
1368 intptr_t puid = (intptr_t)uid;
1369 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1370 if (WAV_PLAYER_ERROR_NONE != ret) {
1371 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1373 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1378 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1380 return STTD_RESULT_STATE_NOT_DONE;
1382 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1384 /* Unset audio session */
1385 ret = sttd_recorder_unset_audio_session();
1387 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1392 ret = sttd_engine_agent_recognize_stop_engine();
1394 stt_client_unset_current_recognition();
1395 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1399 /* change uid state */
1400 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1402 /* Notify uid state change */
1403 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1405 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1407 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1409 return STTD_RESULT_STATE_DONE;
1412 return STTD_ERROR_NONE;
1415 int sttd_server_cancel(int uid)
1417 /* check if uid is valid */
1419 if (0 != sttd_client_get_state(uid, &state)) {
1420 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1421 return STTD_ERROR_INVALID_PARAMETER;
1424 /* check uid state */
1425 if (APP_STATE_READY == state) {
1426 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1427 return STTD_ERROR_NONE;
1430 stt_client_unset_current_recognition();
1432 if (NULL != g_recording_timer) {
1433 ecore_timer_del(g_recording_timer);
1434 g_recording_timer = NULL;
1437 if (NULL != g_processing_timer) {
1438 ecore_timer_del(g_processing_timer);
1439 g_processing_timer = NULL;
1442 if (APP_STATE_RECORDING == state) {
1443 /* Unset audio session */
1444 int ret = sttd_recorder_unset_audio_session();
1446 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1451 /* change uid state */
1452 sttd_client_set_state(uid, APP_STATE_READY);
1454 /* cancel engine recognition */
1455 int ret = sttd_engine_agent_recognize_cancel();
1457 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1461 /* Notify uid state change */
1462 sttdc_send_set_state(uid, APP_STATE_READY);
1464 return STTD_ERROR_NONE;