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 int __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);
199 return STTD_ERROR_OPERATION_FAILED;
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 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_DEBUG, 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_timer_del(g_processing_timer);
268 g_processing_timer = NULL;
270 sttd_config_time_reset();
272 sttd_client_set_state(uid, APP_STATE_READY);
273 stt_client_unset_current_recognition();
275 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
276 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
279 int reason = (int)STTD_ERROR_INVALID_STATE;
280 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
281 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
285 /* change state of uid */
286 // sttd_client_set_state(uid, APP_STATE_READY);
287 // stt_client_unset_current_recognition();
292 SLOG(LOG_DEBUG, TAG_STTD, "=====");
293 SLOG(LOG_DEBUG, TAG_STTD, " ");
294 pthread_mutex_unlock(&stte_result_mutex);
296 return STTD_ERROR_NONE;
299 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)
301 pthread_mutex_lock(&stte_result_time_mutex);
303 SLOG(LOG_DEBUG, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)",
304 index, event, text, start_time, end_time);
307 ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
309 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
310 pthread_mutex_unlock(&stte_result_time_mutex);
314 pthread_mutex_unlock(&stte_result_time_mutex);
319 int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
321 SLOG(LOG_DEBUG, TAG_STTD, "===== Speech status detected Callback");
323 int uid = stt_client_get_current_recognition();
326 if (0 != sttd_client_get_state(uid, &state)) {
327 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid ");
328 return STTD_ERROR_OPERATION_FAILED;
331 if (APP_STATE_RECORDING != state) {
332 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
333 return STTD_ERROR_INVALID_STATE;
336 if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) {
337 SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected");
338 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
339 SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
340 ecore_main_loop_thread_safe_call_async(__stop_by_silence, NULL);
343 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recogntion uid is not valid ");
346 SLOG(LOG_DEBUG, TAG_STTD, "=====");
347 SLOG(LOG_DEBUG, TAG_STTD, " ");
349 return STTD_ERROR_NONE;
352 int __server_error_callback(stte_error_e error, const char* msg)
354 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Error Callback is called");
355 return STTD_ERROR_NONE;
358 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
360 if (NULL == engine_id) {
361 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
364 SLOG(LOG_DEBUG, TAG_STTD, "[Server] New default engine : %s", engine_id);
367 /* need to change state of app to ready */
369 uid = stt_client_get_current_recognition();
372 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%d)", uid);
374 sttd_server_cancel(uid);
375 sttdc_send_set_state(uid, (int)APP_STATE_READY);
377 stt_client_unset_current_recognition();
381 int ret = sttd_engine_agent_set_default_engine(engine_id);
383 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
385 if (NULL != language) {
386 ret = sttd_engine_agent_set_default_language(language);
388 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
391 ret = sttd_engine_agent_set_silence_detection(support_silence);
393 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
398 void __sttd_server_language_changed_cb(const char* language, void* user_data)
400 if (NULL == language) {
401 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
404 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get language changed : %s", language);
407 int ret = sttd_engine_agent_set_default_language(language);
409 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
414 void __sttd_server_silence_changed_cb(bool value, void* user_data)
416 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
419 ret = sttd_engine_agent_set_silence_detection(value);
421 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
430 static void __sig_handler(int signo)
432 /* restore signal handler */
433 signal(signo, SIG_DFL);
435 /* Send error signal to clients */
436 int* client_list = NULL;
437 int client_count = 0;
439 if (0 != sttd_client_get_list(&client_list, &client_count)) {
440 if (NULL != client_list) {
446 if (NULL != client_list) {
447 for (i = 0; i < client_count; i++) {
448 sttdc_send_error_signal(client_list[i], STTD_ERROR_SERVICE_RESET, "Service Reset");
455 /* invoke signal again */
459 static void __register_sig_handler()
461 signal(SIGSEGV, __sig_handler);
462 signal(SIGABRT, __sig_handler);
463 signal(SIGTERM, __sig_handler);
464 signal(SIGINT, __sig_handler);
465 signal(SIGQUIT, __sig_handler);
468 int sttd_initialize(stte_request_callback_s *callback)
472 __register_sig_handler();
474 if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
475 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
478 if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) {
479 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex.");
482 if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
483 __sttd_server_silence_changed_cb, NULL)) {
484 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
487 ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
489 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
493 /* Engine Agent initialize */
494 ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
495 __server_speech_status_callback, __server_error_callback);
497 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
502 ret = sttd_engine_agent_load_current_engine(callback);
504 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
508 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] initialize");
515 if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
516 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
519 if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) {
520 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex.");
524 if (0 < g_list_length(g_proc_list)) {
525 iter = g_list_first(g_proc_list);
526 while (NULL != iter) {
527 g_proc_list = g_list_remove_link(g_proc_list, iter);
528 iter = g_list_first(g_proc_list);
532 sttd_recorder_deinitialize();
534 sttd_config_finalize();
536 sttd_engine_agent_release();
538 return STTD_ERROR_NONE;
541 static void __read_proc()
545 struct dirent *dirp = NULL;
550 if (0 < g_list_length(g_proc_list)) {
551 iter = g_list_first(g_proc_list);
552 while (NULL != iter) {
553 g_proc_list = g_list_remove_link(g_proc_list, iter);
554 iter = g_list_first(g_proc_list);
558 dp = opendir("/proc");
560 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
563 ret = readdir_r(dp, &entry, &dirp);
565 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to readdir");
570 tmp = atoi(dirp->d_name);
571 if (0 >= tmp) continue;
572 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
574 } while (NULL != dirp);
580 Eina_Bool sttd_cleanup_client(void *data)
582 int* client_list = NULL;
583 int client_count = 0;
588 if (0 != sttd_client_get_list(&client_list, &client_count)) {
589 if (NULL != client_list)
595 if (NULL != client_list) {
596 SLOG(LOG_DEBUG, TAG_STTD, "===== Clean up client ");
600 for (i = 0; i < client_count; i++) {
601 int pid = sttd_client_get_pid(client_list[i]);
603 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
609 for (j = 0; j < g_list_length(g_proc_list); j++) {
610 iter = g_list_nth(g_proc_list, j);
612 if (pid == GPOINTER_TO_INT(iter->data)) {
613 SLOG(LOG_DEBUG, TAG_STTD, "uid (%d) is running", client_list[i]);
620 if (false == exist) {
621 SLOG(LOG_ERROR, TAG_STTD, "uid (%d) should be removed", client_list[i]);
622 sttd_server_finalize(client_list[i]);
625 result = sttdc_send_hello(client_list[i]);
628 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", client_list[i]);
629 sttd_server_finalize(client_list[i]);
630 } else if (-1 == result) {
631 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
636 SLOG(LOG_DEBUG, TAG_STTD, "=====");
637 SLOG(LOG_DEBUG, TAG_STTD, " ");
646 * STT Server Functions for Client
649 int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential)
651 int ret = STTD_ERROR_NONE;
653 /* check if uid is valid */
655 if (0 == sttd_client_get_state(uid, &state)) {
656 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
657 return STTD_ERROR_NONE;
660 ret = sttd_engine_agent_get_option_supported(silence);
662 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
666 ret = sttd_engine_agent_is_credential_needed(uid, credential);
668 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
672 /* Add client information to client manager */
673 ret = sttd_client_add(pid, uid);
675 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
679 return STTD_ERROR_NONE;
682 static Eina_Bool __quit_ecore_loop(void *data)
684 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Quit");
686 stt_network_finalize();
688 ecore_main_loop_quit();
690 SLOG(LOG_DEBUG, TAG_STTD, "");
695 int sttd_server_finalize(int uid)
697 /* check if uid is valid */
699 if (0 != sttd_client_get_state(uid, &state)) {
700 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
701 return STTD_ERROR_INVALID_PARAMETER;
704 /* release recorder */
705 if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
706 if (APP_STATE_RECORDING == state) {
707 if (NULL != g_recording_timer) {
708 ecore_timer_del(g_recording_timer);
709 g_recording_timer = NULL;
713 if (APP_STATE_PROCESSING == state) {
714 if (NULL != g_processing_timer) {
715 ecore_timer_del(g_processing_timer);
716 g_processing_timer = NULL;
720 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
721 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
724 stt_client_unset_current_recognition();
727 /* Remove client information */
728 if (0 != sttd_client_delete(uid)) {
729 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
732 /* unload engine, if ref count of client is 0 */
733 if (0 == sttd_client_get_ref_count()) {
734 // sttd_dbus_close_connection();
735 ecore_timer_add(0, __quit_ecore_loop, NULL);
738 return STTD_ERROR_NONE;
741 int sttd_server_get_supported_engines(int uid, GSList** engine_list)
743 /* Check if uid is valid */
745 if (0 != sttd_client_get_state(uid, &state)) {
746 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
747 return STTD_ERROR_INVALID_PARAMETER;
750 /* Check state of uid */
751 if (APP_STATE_READY != state) {
752 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
753 return STTD_ERROR_INVALID_STATE;
757 ret = sttd_engine_agent_get_engine_list(engine_list);
759 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
763 return STTD_ERROR_NONE;
766 int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence, bool* credential)
768 /* Check if uid is valid */
770 if (0 != sttd_client_get_state(uid, &state)) {
771 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
772 return STTD_ERROR_INVALID_PARAMETER;
775 /* Check state of uid */
776 if (APP_STATE_READY != state) {
777 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
778 return STTD_ERROR_INVALID_STATE;
782 ret = sttd_engine_agent_load_current_engine(NULL);
784 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
788 ret = sttd_engine_agent_get_option_supported(silence);
790 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
794 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
795 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
799 return STTD_ERROR_NONE;
802 int sttd_server_get_current_engine(int uid, char** engine_id)
804 /* Check if uid is valid */
806 if (0 != sttd_client_get_state(uid, &state)) {
807 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
808 return STTD_ERROR_INVALID_PARAMETER;
811 /* Check state of uid */
812 if (APP_STATE_READY != state) {
813 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
814 return STTD_ERROR_INVALID_STATE;
818 ret = sttd_engine_agent_get_current_engine(engine_id);
820 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
824 return STTD_ERROR_NONE;
827 int sttd_server_check_app_agreed(int uid, const char* appid, bool* available)
829 /* Check if uid is valid */
831 if (0 != sttd_client_get_state(uid, &state)) {
832 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
833 return STTD_ERROR_INVALID_PARAMETER;
836 /* Check state of uid */
837 if (APP_STATE_READY != state) {
838 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
839 return STTD_ERROR_INVALID_STATE;
842 /* Ask engine available */
845 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
847 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
852 stt_client_set_app_agreed(uid);
853 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
858 return STTD_ERROR_NONE;
862 int sttd_server_get_supported_languages(int uid, GSList** lang_list)
864 /* check if uid is valid */
866 if (0 != sttd_client_get_state(uid, &state)) {
867 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
868 return STTD_ERROR_INVALID_PARAMETER;
871 /* get language list from engine */
872 int ret = sttd_engine_agent_supported_langs(lang_list);
874 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
878 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
880 return STTD_ERROR_NONE;
883 int sttd_server_get_current_langauage(int uid, char** current_lang)
885 /* check if uid is valid */
887 if (0 != sttd_client_get_state(uid, &state)) {
888 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
889 return STTD_ERROR_INVALID_PARAMETER;
892 if (NULL == current_lang) {
893 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
894 return STTD_ERROR_INVALID_PARAMETER;
897 /*get current language from engine */
898 int ret = sttd_engine_agent_get_default_lang(current_lang);
900 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
904 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get default language");
906 return STTD_ERROR_NONE;
909 int sttd_server_set_private_data(int uid, const char* key, const char* data)
911 /* check if uid is valid */
913 if (0 != sttd_client_get_state(uid, &state)) {
914 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
915 return STTD_ERROR_INVALID_PARAMETER;
918 if (NULL == key || NULL == data) {
919 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
920 return STTD_ERROR_INVALID_PARAMETER;
923 /* set private data to engine */
925 ret = sttd_engine_agent_set_private_data(key, data);
927 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
931 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Set private data");
933 return STTD_ERROR_NONE;
936 int sttd_server_get_private_data(int uid, const char* key, char** data)
938 /* check if uid is valid */
940 if (0 != sttd_client_get_state(uid, &state)) {
941 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
942 return STTD_ERROR_INVALID_PARAMETER;
945 if (NULL == key || NULL == data) {
946 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
947 return STTD_ERROR_INVALID_PARAMETER;
950 /* get private data to engine */
952 ret = sttd_engine_agent_get_private_data(key, data);
954 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
958 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
960 return STTD_ERROR_NONE;
963 int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support)
965 /* check if uid is valid */
967 if (0 != sttd_client_get_state(uid, &state)) {
968 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
969 return STTD_ERROR_INVALID_PARAMETER;
972 if (NULL == type || NULL == support) {
973 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
974 return STTD_ERROR_INVALID_PARAMETER;
978 int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
980 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
984 *support = (int)temp;
986 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
988 return STTD_ERROR_NONE;
991 int sttd_server_set_start_sound(int uid, const char* file)
993 /* check if uid is valid */
995 if (0 != sttd_client_get_state(uid, &state)) {
996 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
997 return STTD_ERROR_INVALID_PARAMETER;
1000 int ret = sttd_client_set_start_sound(uid, file);
1002 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1009 int sttd_server_set_stop_sound(int uid, const char* file)
1011 /* check if uid is valid */
1013 if (0 != sttd_client_get_state(uid, &state)) {
1014 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1015 return STTD_ERROR_INVALID_PARAMETER;
1018 int ret = sttd_client_set_stop_sound(uid, file);
1020 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1028 Eina_Bool __check_recording_state(void *data)
1031 int uid = stt_client_get_current_recognition();
1036 if (0 != sttdc_send_get_state(uid, (int*)&state)) {
1037 /* client is removed */
1038 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", uid);
1039 sttd_server_finalize(uid);
1043 if (APP_STATE_READY == state) {
1045 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The STT service should cancel recording", uid);
1046 sttd_server_cancel(uid);
1047 } else if (APP_STATE_PROCESSING == state) {
1048 /* Cancel stt and send change state */
1049 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The STT service should cancel recording", uid);
1050 sttd_server_cancel(uid);
1051 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1054 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1062 Eina_Bool __stop_by_recording_timeout(void *data)
1064 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by timeout");
1066 g_recording_timer = NULL;
1069 uid = stt_client_get_current_recognition();
1071 /* cancel engine recognition */
1072 int ret = sttd_server_stop(uid);
1074 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1078 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1079 SLOG(LOG_DEBUG, TAG_STTD, " ");
1084 void __sttd_server_recorder_start(void* data)
1086 intptr_t puid = (intptr_t)data;
1087 int uid = (int)puid;
1088 int current_uid = stt_client_get_current_recognition();
1090 if (uid != current_uid) {
1091 stt_client_unset_current_recognition();
1095 int ret = sttd_engine_agent_recognize_start_recorder(uid);
1097 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1101 /* Notify uid state change */
1102 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1104 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1107 void __sttd_start_sound_completed_cb(int id, void *user_data)
1109 SLOG(LOG_DEBUG, TAG_STTD, "===== Start sound completed");
1111 /* After wav play callback, recorder start */
1112 ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1114 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1115 SLOG(LOG_DEBUG, TAG_STTD, " ");
1119 int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
1121 if (NULL == lang || NULL == recognition_type) {
1122 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1123 return STTD_ERROR_INVALID_PARAMETER;
1126 /* check if uid is valid */
1128 if (0 != sttd_client_get_state(uid, &state)) {
1129 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1130 return STTD_ERROR_INVALID_PARAMETER;
1133 /* check uid state */
1134 if (APP_STATE_READY != state) {
1135 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1136 return STTD_ERROR_INVALID_STATE;
1140 if (false == stt_client_get_app_agreed(uid)) {
1142 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1144 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1148 if (false == temp) {
1149 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1150 return STTD_ERROR_PERMISSION_DENIED;
1153 stt_client_set_app_agreed(uid);
1156 /* check if engine use network */
1157 if (true == sttd_engine_agent_need_network()) {
1158 if (false == stt_network_is_connected()) {
1159 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1160 return STTD_ERROR_OUT_OF_NETWORK;
1165 ret = sttd_client_get_start_sound(uid, &sound);
1167 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1171 if (0 != stt_client_set_current_recognition(uid)) {
1172 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1173 if (NULL != sound) free(sound);
1174 return STTD_ERROR_RECORDER_BUSY;
1177 /* engine start recognition */
1178 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)",
1179 uid, lang, recognition_type);
1181 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start sound : %s", sound);
1183 /* 1. Set audio session */
1184 ret = sttd_recorder_set_audio_session();
1186 stt_client_unset_current_recognition();
1187 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1188 if (NULL != sound) free(sound);
1192 bool is_sound_done = false;
1194 /* 2. Request wav play */
1195 if (NULL != sound) {
1197 intptr_t puid = (intptr_t)uid;
1198 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_start_sound_completed_cb, (void*)puid, &id);
1199 if (WAV_PLAYER_ERROR_NONE != ret) {
1200 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1201 is_sound_done = true;
1206 is_sound_done = true;
1209 /* 3. Create recorder & engine initialize */
1210 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1212 stt_client_unset_current_recognition();
1213 sttd_recorder_unset_audio_session();
1214 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1218 if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1219 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1222 /* change uid state */
1223 sttd_client_set_state(uid, APP_STATE_RECORDING);
1225 g_recording_log_count = 0;
1227 if (true == is_sound_done) {
1228 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1230 ret = sttd_engine_agent_recognize_start_recorder(uid);
1232 stt_client_unset_current_recognition();
1233 sttd_recorder_unset_audio_session();
1235 sttd_engine_agent_recognize_cancel();
1236 ecore_timer_del(g_recording_timer);
1237 sttd_client_set_state(uid, APP_STATE_READY);
1239 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1243 /* Notify uid state change */
1244 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1246 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1247 return STTD_RESULT_STATE_DONE;
1250 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Wait sound finish");
1252 return STTD_RESULT_STATE_NOT_DONE;
1255 Eina_Bool __time_out_for_processing(void *data)
1257 g_processing_timer = NULL;
1260 int uid = stt_client_get_current_recognition();
1261 if (0 == uid) return EINA_FALSE;
1264 int ret = sttd_engine_agent_recognize_cancel();
1266 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1269 if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1270 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1272 /* send error msg */
1273 int reason = (int)STTD_ERROR_TIMED_OUT;
1274 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1275 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1279 /* Change uid state */
1280 sttd_client_set_state(uid, APP_STATE_READY);
1282 stt_client_unset_current_recognition();
1287 void __sttd_server_engine_stop(void* data)
1289 intptr_t puid = (intptr_t)data;
1290 int uid = (int)puid;
1291 /* change uid state */
1292 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1294 /* Notify uid state change */
1295 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1297 /* Unset audio session */
1298 int ret = sttd_recorder_unset_audio_session();
1300 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1305 ret = sttd_engine_agent_recognize_stop_engine();
1307 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1311 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1314 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1316 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop sound completed");
1318 /* After wav play callback, engine stop */
1319 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1321 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1322 SLOG(LOG_DEBUG, TAG_STTD, " ");
1326 int sttd_server_stop(int uid)
1328 /* check if uid is valid */
1330 if (0 != sttd_client_get_state(uid, &state)) {
1331 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1332 return STTD_ERROR_INVALID_PARAMETER;
1335 /* check uid state */
1336 if (APP_STATE_RECORDING != state) {
1337 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1338 return STTD_ERROR_INVALID_STATE;
1341 if (NULL != g_recording_timer) {
1342 ecore_timer_del(g_recording_timer);
1343 g_recording_timer = NULL;
1347 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1348 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1349 return STTD_ERROR_OPERATION_FAILED;
1352 SLOG(LOG_DEBUG, TAG_STTD, "[Server] stop sound path : %s", sound);
1355 /* 1. Stop recorder */
1356 ret = sttd_engine_agent_recognize_stop_recorder();
1358 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1359 if (0 != sttd_engine_agent_recognize_cancel()) {
1360 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1362 if (NULL != sound) free(sound);
1366 /* 2. Request wav play */
1367 if (NULL != sound) {
1369 intptr_t puid = (intptr_t)uid;
1370 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1371 if (WAV_PLAYER_ERROR_NONE != ret) {
1372 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1374 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1379 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1381 return STTD_RESULT_STATE_NOT_DONE;
1383 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1385 /* Unset audio session */
1386 ret = sttd_recorder_unset_audio_session();
1388 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1393 ret = sttd_engine_agent_recognize_stop_engine();
1395 stt_client_unset_current_recognition();
1396 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1400 /* change uid state */
1401 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1403 /* Notify uid state change */
1404 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1406 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1408 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1410 return STTD_RESULT_STATE_DONE;
1413 return STTD_ERROR_NONE;
1416 int sttd_server_cancel(int uid)
1418 /* check if uid is valid */
1420 if (0 != sttd_client_get_state(uid, &state)) {
1421 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1422 return STTD_ERROR_INVALID_PARAMETER;
1425 /* check uid state */
1426 if (APP_STATE_READY == state) {
1427 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1428 return STTD_ERROR_NONE;
1431 stt_client_unset_current_recognition();
1433 if (NULL != g_recording_timer) {
1434 ecore_timer_del(g_recording_timer);
1435 g_recording_timer = NULL;
1438 if (NULL != g_processing_timer) {
1439 ecore_timer_del(g_processing_timer);
1440 g_processing_timer = NULL;
1443 if (APP_STATE_RECORDING == state) {
1444 /* Unset audio session */
1445 int ret = sttd_recorder_unset_audio_session();
1447 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1452 /* change uid state */
1453 sttd_client_set_state(uid, APP_STATE_READY);
1455 /* cancel engine recognition */
1456 int ret = sttd_engine_agent_recognize_cancel();
1458 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1462 /* Notify uid state change */
1463 sttdc_send_set_state(uid, APP_STATE_READY);
1465 return STTD_ERROR_NONE;