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 sttpe_result_mutex = PTHREAD_MUTEX_INITIALIZER;
28 static pthread_mutex_t sttpe_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(uid);
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(uid, 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(sttp_result_event_e event, const char* type,
183 const char** data, int data_count, const char* msg, void *user_data)
186 pthread_mutex_lock(&sttpe_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(&sttpe_result_mutex);
202 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid (%d), event(%d)", uid, event);
204 /* send result to client */
205 if (STTP_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 (STTP_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 (STTP_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(&sttpe_result_mutex);
293 bool __server_result_time_callback(int index, sttp_result_time_event_e event, const char* text, long start_time, long end_time, void* user_data)
295 pthread_mutex_lock(&sttpe_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(&sttpe_result_time_mutex);
309 pthread_mutex_unlock(&sttpe_result_time_mutex);
313 pthread_mutex_unlock(&sttpe_result_time_mutex);
318 void __server_silence_dectection_callback(sttp_silence_type_e type, void *user_param)
320 SLOG(LOG_DEBUG, TAG_STTD, "===== Silence Detection 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 (STTP_SILENCE_TYPE_NO_RECORD_TIMEOUT == type) {
336 SLOG(LOG_DEBUG, TAG_STTD, "Silence Detection type - No Record");
337 ecore_main_loop_thread_safe_call_async(__cancel_by_no_record, NULL);
338 if (0 != sttdc_send_error_signal(uid, STTP_ERROR_NO_SPEECH, "No speech while recording")) {
339 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] No speech while recording");
341 } else if (STTP_SILENCE_TYPE_END_OF_SPEECH_DETECTED == type) {
342 SLOG(LOG_DEBUG, TAG_STTD, "Silence Detection type - End of Speech");
343 ecore_main_loop_thread_safe_call_async(__stop_by_silence, NULL);
346 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recogntion uid is not valid ");
349 SLOG(LOG_DEBUG, TAG_STTD, "=====");
350 SLOG(LOG_DEBUG, TAG_STTD, " ");
355 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
357 if (NULL == engine_id) {
358 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
361 SLOG(LOG_DEBUG, TAG_STTD, "[Server] New default engine : %s", engine_id);
364 /* need to change state of app to ready */
366 uid = stt_client_get_current_recognition();
369 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%d)", uid);
371 sttd_server_cancel(uid);
372 sttdc_send_set_state(uid, (int)APP_STATE_READY);
374 stt_client_unset_current_recognition();
378 int ret = sttd_engine_agent_set_default_engine(engine_id);
380 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
382 if (NULL != language) {
383 ret = sttd_engine_agent_set_default_language(language);
385 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
388 ret = sttd_engine_agent_set_silence_detection(support_silence);
390 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
395 void __sttd_server_language_changed_cb(const char* language, void* user_data)
397 if (NULL == language) {
398 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
401 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get language changed : %s", language);
404 int ret = sttd_engine_agent_set_default_language(language);
406 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
411 void __sttd_server_silence_changed_cb(bool value, void* user_data)
413 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
416 ret = sttd_engine_agent_set_silence_detection(value);
418 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
427 static void __sig_handler(int signo)
429 /* restore signal handler */
430 signal(signo, SIG_DFL);
432 /* Send error signal to clients */
433 int* client_list = NULL;
434 int client_count = 0;
436 if (0 != sttd_client_get_list(&client_list, &client_count)) {
437 if (NULL != client_list) {
443 if (NULL != client_list) {
444 for (i = 0; i < client_count; i++) {
445 sttdc_send_error_signal(client_list[i], STTD_ERROR_SERVICE_RESET, "Service Reset");
452 /* invoke signal again */
456 static void __register_sig_handler()
458 signal(SIGSEGV, __sig_handler);
459 signal(SIGABRT, __sig_handler);
460 signal(SIGTERM, __sig_handler);
461 signal(SIGINT, __sig_handler);
462 signal(SIGQUIT, __sig_handler);
465 int sttd_initialize()
469 __register_sig_handler();
471 if (0 != pthread_mutex_init(&sttpe_result_mutex, NULL)) {
472 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize sttpe result mutex.");
475 if (0 != pthread_mutex_init(&sttpe_result_time_mutex, NULL)) {
476 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize sttpe sttpe_result_time_mutex.");
479 if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
480 __sttd_server_silence_changed_cb, NULL)) {
481 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
484 ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
486 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
490 /* Engine Agent initialize */
491 ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
492 __server_silence_dectection_callback);
494 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
498 /* Update engine list */
499 ret = sttd_engine_agent_initialize_engine_list();
501 if (STTD_ERROR_ENGINE_NOT_FOUND == ret) {
502 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] There is no stt engine");
504 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to update engine list : %d", ret);
509 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] initialize");
516 if (0 != pthread_mutex_destroy(&sttpe_result_mutex)) {
517 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy sttpe result mutex.");
520 if (0 != pthread_mutex_destroy(&sttpe_result_time_mutex)) {
521 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy sttpe_result_time_mutex.");
525 if (0 < g_list_length(g_proc_list)) {
526 iter = g_list_first(g_proc_list);
527 while (NULL != iter) {
528 g_proc_list = g_list_remove_link(g_proc_list, iter);
529 iter = g_list_first(g_proc_list);
533 sttd_recorder_deinitialize();
535 sttd_config_finalize();
537 sttd_engine_agent_release();
539 return STTD_ERROR_NONE;
542 static void __read_proc()
546 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_remove_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");
564 ret = readdir_r(dp, &entry, &dirp);
566 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to readdir");
571 tmp = atoi(dirp->d_name);
572 if (0 >= tmp) continue;
573 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
575 } while (NULL != dirp);
581 Eina_Bool sttd_cleanup_client(void *data)
583 int* client_list = NULL;
584 int client_count = 0;
589 if (0 != sttd_client_get_list(&client_list, &client_count)) {
590 if (NULL != client_list)
596 if (NULL != client_list) {
597 SLOG(LOG_DEBUG, TAG_STTD, "===== Clean up client ");
601 for (i = 0; i < client_count; i++) {
602 int pid = sttd_client_get_pid(client_list[i]);
604 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
610 for (j = 0; j < g_list_length(g_proc_list); j++) {
611 iter = g_list_nth(g_proc_list, j);
613 if (pid == GPOINTER_TO_INT(iter->data)) {
614 SLOG(LOG_DEBUG, TAG_STTD, "uid (%d) is running", client_list[i]);
621 if (false == exist) {
622 SLOG(LOG_ERROR, TAG_STTD, "uid (%d) should be removed", client_list[i]);
623 sttd_server_finalize(client_list[i]);
626 result = sttdc_send_hello(client_list[i]);
629 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", client_list[i]);
630 sttd_server_finalize(client_list[i]);
631 } else if (-1 == result) {
632 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
637 SLOG(LOG_DEBUG, TAG_STTD, "=====");
638 SLOG(LOG_DEBUG, TAG_STTD, " ");
647 * STT Server Functions for Client
650 int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential)
652 if (false == sttd_engine_agent_is_default_engine()) {
653 /* Update installed engine */
654 sttd_engine_agent_initialize_engine_list();
656 if (false == sttd_engine_agent_is_default_engine()) {
657 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] No stt-engine");
658 return STTD_ERROR_ENGINE_NOT_FOUND;
662 int ret = STTD_ERROR_NONE;
664 /* check if uid is valid */
666 if (0 == sttd_client_get_state(uid, &state)) {
667 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
668 return STTD_ERROR_NONE;
672 ret = sttd_engine_agent_load_current_engine(uid, NULL);
674 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
678 ret = sttd_engine_agent_get_option_supported(uid, silence);
680 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
684 ret = sttd_engine_agent_is_credential_needed(uid, credential);
686 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
690 /* Add client information to client manager */
691 ret = sttd_client_add(pid, uid);
693 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
697 return STTD_ERROR_NONE;
700 static Eina_Bool __quit_ecore_loop(void *data)
702 ecore_main_loop_quit();
706 int sttd_server_finalize(int uid)
708 /* check if uid is valid */
710 if (0 != sttd_client_get_state(uid, &state)) {
711 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
712 return STTD_ERROR_INVALID_PARAMETER;
715 /* release recorder */
716 if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
717 if (APP_STATE_RECORDING == state) {
718 if (NULL != g_recording_timer) {
719 ecore_timer_del(g_recording_timer);
720 g_recording_timer = NULL;
724 if (APP_STATE_PROCESSING == state) {
725 if (NULL != g_processing_timer) {
726 ecore_timer_del(g_processing_timer);
727 g_processing_timer = NULL;
731 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
732 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
735 stt_client_unset_current_recognition();
738 if (0 != sttd_engine_agent_unload_current_engine(uid)) {
739 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unload engine");
742 /* Remove client information */
743 if (0 != sttd_client_delete(uid)) {
744 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
747 /* unload engine, if ref count of client is 0 */
748 if (0 == sttd_client_get_ref_count()) {
749 // sttd_dbus_close_connection();
750 ecore_timer_add(0, __quit_ecore_loop, NULL);
753 return STTD_ERROR_NONE;
756 int sttd_server_get_supported_engines(int uid, GSList** engine_list)
758 /* Check if uid is valid */
760 if (0 != sttd_client_get_state(uid, &state)) {
761 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
762 return STTD_ERROR_INVALID_PARAMETER;
765 /* Check state of uid */
766 if (APP_STATE_READY != state) {
767 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
768 return STTD_ERROR_INVALID_STATE;
772 ret = sttd_engine_agent_get_engine_list(engine_list);
774 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
778 return STTD_ERROR_NONE;
781 int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence, bool* credential)
783 /* Check if uid is valid */
785 if (0 != sttd_client_get_state(uid, &state)) {
786 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
787 return STTD_ERROR_INVALID_PARAMETER;
790 /* Check state of uid */
791 if (APP_STATE_READY != state) {
792 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
793 return STTD_ERROR_INVALID_STATE;
797 ret = sttd_engine_agent_load_current_engine(uid, engine_id);
799 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
803 ret = sttd_engine_agent_get_option_supported(uid, silence);
805 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
809 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
810 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
814 return STTD_ERROR_NONE;
817 int sttd_server_get_current_engine(int uid, char** engine_id)
819 /* Check if uid is valid */
821 if (0 != sttd_client_get_state(uid, &state)) {
822 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
823 return STTD_ERROR_INVALID_PARAMETER;
826 /* Check state of uid */
827 if (APP_STATE_READY != state) {
828 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
829 return STTD_ERROR_INVALID_STATE;
833 ret = sttd_engine_agent_get_current_engine(uid, 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(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(%d) is not Ready", uid);
854 return STTD_ERROR_INVALID_STATE;
857 /* Ask engine available */
860 ret = sttd_engine_agent_check_app_agreed(uid, appid, &temp);
862 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
867 stt_client_set_app_agreed(uid);
868 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
873 return STTD_ERROR_NONE;
877 int sttd_server_get_supported_languages(int uid, GSList** lang_list)
879 /* check if uid is valid */
881 if (0 != sttd_client_get_state(uid, &state)) {
882 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
883 return STTD_ERROR_INVALID_PARAMETER;
886 /* get language list from engine */
887 int ret = sttd_engine_agent_supported_langs(uid, lang_list);
889 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
893 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
895 return STTD_ERROR_NONE;
898 int sttd_server_get_current_langauage(int uid, char** current_lang)
900 /* check if uid is valid */
902 if (0 != sttd_client_get_state(uid, &state)) {
903 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
904 return STTD_ERROR_INVALID_PARAMETER;
907 if (NULL == current_lang) {
908 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
909 return STTD_ERROR_INVALID_PARAMETER;
912 /*get current language from engine */
913 int ret = sttd_engine_agent_get_default_lang(uid, current_lang);
915 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
919 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get default language");
921 return STTD_ERROR_NONE;
924 int sttd_server_set_private_data(int uid, const char* key, const char* data)
926 /* check if uid is valid */
928 if (0 != sttd_client_get_state(uid, &state)) {
929 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
930 return STTD_ERROR_INVALID_PARAMETER;
933 if (NULL == key || NULL == data) {
934 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
935 return STTD_ERROR_INVALID_PARAMETER;
938 /* set private data to engine */
940 ret = sttd_engine_agent_set_private_data(uid, key, data);
942 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
946 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Set private data");
948 return STTD_ERROR_NONE;
951 int sttd_server_get_private_data(int uid, const char* key, char** data)
953 /* check if uid is valid */
955 if (0 != sttd_client_get_state(uid, &state)) {
956 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
957 return STTD_ERROR_INVALID_PARAMETER;
960 if (NULL == key || NULL == data) {
961 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
962 return STTD_ERROR_INVALID_PARAMETER;
965 /* get private data to engine */
967 ret = sttd_engine_agent_get_private_data(uid, key, data);
969 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
973 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get private data");
975 return STTD_ERROR_NONE;
978 int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support)
980 /* check if uid is valid */
982 if (0 != sttd_client_get_state(uid, &state)) {
983 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
984 return STTD_ERROR_INVALID_PARAMETER;
987 if (NULL == type || NULL == support) {
988 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
989 return STTD_ERROR_INVALID_PARAMETER;
993 int ret = sttd_engine_agent_is_recognition_type_supported(uid, type, &temp);
995 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
999 *support = (int)temp;
1001 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
1003 return STTD_ERROR_NONE;
1006 int sttd_server_set_start_sound(int uid, const char* file)
1008 /* check if uid is valid */
1010 if (0 != sttd_client_get_state(uid, &state)) {
1011 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1012 return STTD_ERROR_INVALID_PARAMETER;
1015 int ret = sttd_client_set_start_sound(uid, file);
1017 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1024 int sttd_server_set_stop_sound(int uid, const char* file)
1026 /* check if uid is valid */
1028 if (0 != sttd_client_get_state(uid, &state)) {
1029 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1030 return STTD_ERROR_INVALID_PARAMETER;
1033 int ret = sttd_client_set_stop_sound(uid, file);
1035 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1043 Eina_Bool __check_recording_state(void *data)
1046 int uid = stt_client_get_current_recognition();
1051 if (0 != sttdc_send_get_state(uid, (int*)&state)) {
1052 /* client is removed */
1053 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", uid);
1054 sttd_server_finalize(uid);
1058 if (APP_STATE_READY == state) {
1060 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The daemon should cancel recording", uid);
1061 sttd_server_cancel(uid);
1062 } else if (APP_STATE_PROCESSING == state) {
1063 /* Cancel stt and send change state */
1064 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The daemon should cancel recording", uid);
1065 sttd_server_cancel(uid);
1066 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1069 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of daemon and client are identical");
1077 Eina_Bool __stop_by_recording_timeout(void *data)
1079 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by timeout");
1081 g_recording_timer = NULL;
1084 uid = stt_client_get_current_recognition();
1086 /* cancel engine recognition */
1087 int ret = sttd_server_stop(uid);
1089 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1093 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1094 SLOG(LOG_DEBUG, TAG_STTD, " ");
1099 void __sttd_server_recorder_start(void* data)
1101 intptr_t puid = (intptr_t)data;
1102 int uid = (int)puid;
1103 int current_uid = stt_client_get_current_recognition();
1105 if (uid != current_uid) {
1106 stt_client_unset_current_recognition();
1110 int ret = sttd_engine_agent_recognize_start_recorder(uid);
1112 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1116 /* Notify uid state change */
1117 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1119 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1122 void __sttd_start_sound_completed_cb(int id, void *user_data)
1124 SLOG(LOG_DEBUG, TAG_STTD, "===== Start sound completed");
1126 /* After wav play callback, recorder start */
1127 ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1129 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1130 SLOG(LOG_DEBUG, TAG_STTD, " ");
1134 int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
1136 if (NULL == lang || NULL == recognition_type) {
1137 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1138 return STTD_ERROR_INVALID_PARAMETER;
1141 /* check if uid is valid */
1143 if (0 != sttd_client_get_state(uid, &state)) {
1144 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1145 return STTD_ERROR_INVALID_PARAMETER;
1148 /* check uid state */
1149 if (APP_STATE_READY != state) {
1150 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1151 return STTD_ERROR_INVALID_STATE;
1155 if (false == stt_client_get_app_agreed(uid)) {
1157 ret = sttd_engine_agent_check_app_agreed(uid, appid, &temp);
1159 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1163 if (false == temp) {
1164 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1165 return STTD_ERROR_PERMISSION_DENIED;
1168 stt_client_set_app_agreed(uid);
1171 /* check if engine use network */
1172 if (true == sttd_engine_agent_need_network(uid)) {
1173 if (false == stt_network_is_connected()) {
1174 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1175 return STTD_ERROR_OUT_OF_NETWORK;
1180 ret = sttd_client_get_start_sound(uid, &sound);
1182 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1186 if (0 != stt_client_set_current_recognition(uid)) {
1187 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1188 if (NULL != sound) free(sound);
1189 return STTD_ERROR_RECORDER_BUSY;
1192 /* engine start recognition */
1193 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)",
1194 uid, lang, recognition_type);
1196 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start sound : %s", sound);
1198 /* 1. Set audio session */
1199 ret = sttd_recorder_set_audio_session();
1201 stt_client_unset_current_recognition();
1202 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1203 if (NULL != sound) free(sound);
1207 bool is_sound_done = false;
1209 /* 2. Request wav play */
1210 if (NULL != sound) {
1212 intptr_t puid = (intptr_t)uid;
1213 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_start_sound_completed_cb, (void*)puid, &id);
1214 if (WAV_PLAYER_ERROR_NONE != ret) {
1215 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1216 is_sound_done = true;
1221 is_sound_done = true;
1224 /* 3. Create recorder & engine initialize */
1225 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, credential, NULL);
1227 stt_client_unset_current_recognition();
1228 sttd_recorder_unset_audio_session();
1229 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1233 if (0 != strcmp(STTP_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1234 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1237 /* change uid state */
1238 sttd_client_set_state(uid, APP_STATE_RECORDING);
1240 g_recording_log_count = 0;
1242 if (true == is_sound_done) {
1243 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1245 ret = sttd_engine_agent_recognize_start_recorder(uid);
1247 stt_client_unset_current_recognition();
1248 sttd_recorder_unset_audio_session();
1250 sttd_engine_agent_recognize_cancel(uid);
1251 ecore_timer_del(g_recording_timer);
1252 sttd_client_set_state(uid, APP_STATE_READY);
1254 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1258 /* Notify uid state change */
1259 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1261 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1262 return STTD_RESULT_STATE_DONE;
1265 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Wait sound finish");
1267 return STTD_RESULT_STATE_NOT_DONE;
1270 Eina_Bool __time_out_for_processing(void *data)
1272 g_processing_timer = NULL;
1275 int uid = stt_client_get_current_recognition();
1276 if (0 == uid) return EINA_FALSE;
1279 int ret = sttd_engine_agent_recognize_cancel(uid);
1281 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1284 if (0 != sttdc_send_result(uid, STTP_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1285 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1287 /* send error msg */
1288 int reason = (int)STTD_ERROR_TIMED_OUT;
1289 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1290 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1294 /* Change uid state */
1295 sttd_client_set_state(uid, APP_STATE_READY);
1297 stt_client_unset_current_recognition();
1302 void __sttd_server_engine_stop(void* data)
1304 intptr_t puid = (intptr_t)data;
1305 int uid = (int)puid;
1306 /* change uid state */
1307 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1309 /* Notify uid state change */
1310 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1312 /* Unset audio session */
1313 int ret = sttd_recorder_unset_audio_session();
1315 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1320 ret = sttd_engine_agent_recognize_stop_engine(uid);
1322 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1326 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1329 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1331 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop sound completed");
1333 /* After wav play callback, engine stop */
1334 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1336 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1337 SLOG(LOG_DEBUG, TAG_STTD, " ");
1341 int sttd_server_stop(int uid)
1343 /* check if uid is valid */
1345 if (0 != sttd_client_get_state(uid, &state)) {
1346 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1347 return STTD_ERROR_INVALID_PARAMETER;
1350 /* check uid state */
1351 if (APP_STATE_RECORDING != state) {
1352 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1353 return STTD_ERROR_INVALID_STATE;
1356 if (NULL != g_recording_timer) {
1357 ecore_timer_del(g_recording_timer);
1358 g_recording_timer = NULL;
1362 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1363 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1364 return STTD_ERROR_OPERATION_FAILED;
1367 SLOG(LOG_DEBUG, TAG_STTD, "[Server] stop sound path : %s", sound);
1370 /* 1. Stop recorder */
1371 ret = sttd_engine_agent_recognize_stop_recorder(uid);
1373 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1374 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
1375 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1377 if (NULL != sound) free(sound);
1381 /* 2. Request wav play */
1382 if (NULL != sound) {
1384 intptr_t puid = (intptr_t)uid;
1385 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1386 if (WAV_PLAYER_ERROR_NONE != ret) {
1387 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1389 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1394 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1396 return STTD_RESULT_STATE_NOT_DONE;
1398 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1400 /* Unset audio session */
1401 ret = sttd_recorder_unset_audio_session();
1403 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1408 ret = sttd_engine_agent_recognize_stop_engine(uid);
1410 stt_client_unset_current_recognition();
1411 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1415 /* change uid state */
1416 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1418 /* Notify uid state change */
1419 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1421 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1423 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1425 return STTD_RESULT_STATE_DONE;
1428 return STTD_ERROR_NONE;
1431 int sttd_server_cancel(int uid)
1433 /* check if uid is valid */
1435 if (0 != sttd_client_get_state(uid, &state)) {
1436 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1437 return STTD_ERROR_INVALID_PARAMETER;
1440 /* check uid state */
1441 if (APP_STATE_READY == state) {
1442 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1443 return STTD_ERROR_NONE;
1446 stt_client_unset_current_recognition();
1448 if (NULL != g_recording_timer) {
1449 ecore_timer_del(g_recording_timer);
1450 g_recording_timer = NULL;
1453 if (NULL != g_processing_timer) {
1454 ecore_timer_del(g_processing_timer);
1455 g_processing_timer = NULL;
1458 if (APP_STATE_RECORDING == state) {
1459 /* Unset audio session */
1460 int ret = sttd_recorder_unset_audio_session();
1462 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1467 /* change uid state */
1468 sttd_client_set_state(uid, APP_STATE_READY);
1470 /* cancel engine recognition */
1471 int ret = sttd_engine_agent_recognize_cancel(uid);
1473 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1477 /* Notify uid state change */
1478 sttdc_send_set_state(uid, APP_STATE_READY);
1480 return STTD_ERROR_NONE;