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 sttdc_send_speech_status(uid, status);
339 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
340 SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
341 ecore_main_loop_thread_safe_call_async(__stop_by_silence, NULL);
344 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recogntion uid is not valid ");
347 SLOG(LOG_DEBUG, TAG_STTD, "=====");
348 SLOG(LOG_DEBUG, TAG_STTD, " ");
350 return STTD_ERROR_NONE;
353 int __server_error_callback(stte_error_e error, const char* msg)
355 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Error Callback is called");
356 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
358 return STTD_ERROR_NONE;
361 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
363 if (NULL == engine_id) {
364 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
367 SLOG(LOG_DEBUG, TAG_STTD, "[Server] New default engine : %s", engine_id);
370 /* need to change state of app to ready */
372 uid = stt_client_get_current_recognition();
375 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%d)", uid);
377 sttd_server_cancel(uid);
378 sttdc_send_set_state(uid, (int)APP_STATE_READY);
380 stt_client_unset_current_recognition();
384 int ret = sttd_engine_agent_set_default_engine(engine_id);
386 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
388 if (NULL != language) {
389 ret = sttd_engine_agent_set_default_language(language);
391 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
394 ret = sttd_engine_agent_set_silence_detection(support_silence);
396 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
401 void __sttd_server_language_changed_cb(const char* language, void* user_data)
403 if (NULL == language) {
404 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
407 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get language changed : %s", language);
410 int ret = sttd_engine_agent_set_default_language(language);
412 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
417 void __sttd_server_silence_changed_cb(bool value, void* user_data)
419 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
422 ret = sttd_engine_agent_set_silence_detection(value);
424 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
432 int sttd_initialize(stte_request_callback_s *callback)
436 if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
437 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
440 if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) {
441 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex.");
444 if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
445 __sttd_server_silence_changed_cb, NULL)) {
446 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
449 ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
451 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
455 /* Engine Agent initialize */
456 ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
457 __server_speech_status_callback, __server_error_callback);
459 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
464 ret = sttd_engine_agent_load_current_engine(callback);
466 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
470 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] initialize");
477 if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
478 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
481 if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) {
482 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex.");
486 if (0 < g_list_length(g_proc_list)) {
487 iter = g_list_first(g_proc_list);
488 while (NULL != iter) {
489 g_proc_list = g_list_remove_link(g_proc_list, iter);
490 iter = g_list_first(g_proc_list);
494 sttd_recorder_deinitialize();
496 sttd_config_finalize();
498 sttd_engine_agent_release();
500 return STTD_ERROR_NONE;
503 static void __read_proc()
506 struct dirent *dirp = NULL;
510 if (0 < g_list_length(g_proc_list)) {
511 iter = g_list_first(g_proc_list);
512 while (NULL != iter) {
513 g_proc_list = g_list_remove_link(g_proc_list, iter);
514 iter = g_list_first(g_proc_list);
518 dp = opendir("/proc");
520 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
526 tmp = atoi(dirp->d_name);
527 if (0 >= tmp) continue;
528 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
530 } while (NULL != dirp);
536 Eina_Bool sttd_cleanup_client(void *data)
538 int* client_list = NULL;
539 int client_count = 0;
544 if (0 != sttd_client_get_list(&client_list, &client_count)) {
545 if (NULL != client_list)
551 if (NULL != client_list) {
552 SLOG(LOG_DEBUG, TAG_STTD, "===== Clean up client ");
556 for (i = 0; i < client_count; i++) {
557 int pid = sttd_client_get_pid(client_list[i]);
559 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
565 for (j = 0; j < g_list_length(g_proc_list); j++) {
566 iter = g_list_nth(g_proc_list, j);
568 if (pid == GPOINTER_TO_INT(iter->data)) {
569 SLOG(LOG_DEBUG, TAG_STTD, "uid (%d) is running", client_list[i]);
576 if (false == exist) {
577 SLOG(LOG_ERROR, TAG_STTD, "uid (%d) should be removed", client_list[i]);
578 sttd_server_finalize(client_list[i]);
581 result = sttdc_send_hello(client_list[i]);
584 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", client_list[i]);
585 sttd_server_finalize(client_list[i]);
586 } else if (-1 == result) {
587 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
592 SLOG(LOG_DEBUG, TAG_STTD, "=====");
593 SLOG(LOG_DEBUG, TAG_STTD, " ");
602 * STT Server Functions for Client
605 int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential)
607 int ret = STTD_ERROR_NONE;
609 /* check if uid is valid */
611 if (0 == sttd_client_get_state(uid, &state)) {
612 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
613 return STTD_ERROR_NONE;
616 ret = sttd_engine_agent_get_option_supported(silence);
618 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
622 ret = sttd_engine_agent_is_credential_needed(uid, credential);
624 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
628 /* Add client information to client manager */
629 ret = sttd_client_add(pid, uid);
631 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
635 return STTD_ERROR_NONE;
638 static Eina_Bool __quit_ecore_loop(void *data)
640 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Quit");
642 stt_network_finalize();
644 ecore_main_loop_quit();
646 SLOG(LOG_DEBUG, TAG_STTD, "");
651 int sttd_server_finalize(int uid)
653 /* check if uid is valid */
655 if (0 != sttd_client_get_state(uid, &state)) {
656 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
657 return STTD_ERROR_INVALID_PARAMETER;
660 /* release recorder */
661 if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
662 if (APP_STATE_RECORDING == state) {
663 if (NULL != g_recording_timer) {
664 ecore_timer_del(g_recording_timer);
665 g_recording_timer = NULL;
669 if (APP_STATE_PROCESSING == state) {
670 if (NULL != g_processing_timer) {
671 ecore_timer_del(g_processing_timer);
672 g_processing_timer = NULL;
676 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
677 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
680 stt_client_unset_current_recognition();
683 /* Remove client information */
684 if (0 != sttd_client_delete(uid)) {
685 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
688 /* unload engine, if ref count of client is 0 */
689 if (0 == sttd_client_get_ref_count()) {
690 // sttd_dbus_close_connection();
691 ecore_timer_add(0, __quit_ecore_loop, NULL);
694 return STTD_ERROR_NONE;
697 int sttd_server_get_supported_engines(int uid, GSList** engine_list)
699 /* Check if uid is valid */
701 if (0 != sttd_client_get_state(uid, &state)) {
702 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
703 return STTD_ERROR_INVALID_PARAMETER;
706 /* Check state of uid */
707 if (APP_STATE_READY != state) {
708 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
709 return STTD_ERROR_INVALID_STATE;
713 ret = sttd_engine_agent_get_engine_list(engine_list);
715 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
719 return STTD_ERROR_NONE;
722 int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence, bool* credential)
724 /* Check if uid is valid */
726 if (0 != sttd_client_get_state(uid, &state)) {
727 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
728 return STTD_ERROR_INVALID_PARAMETER;
731 /* Check state of uid */
732 if (APP_STATE_READY != state) {
733 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
734 return STTD_ERROR_INVALID_STATE;
738 ret = sttd_engine_agent_load_current_engine(NULL);
740 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
744 ret = sttd_engine_agent_get_option_supported(silence);
746 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
750 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
751 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
755 return STTD_ERROR_NONE;
758 int sttd_server_get_current_engine(int uid, char** engine_id)
760 /* Check if uid is valid */
762 if (0 != sttd_client_get_state(uid, &state)) {
763 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
764 return STTD_ERROR_INVALID_PARAMETER;
767 /* Check state of uid */
768 if (APP_STATE_READY != state) {
769 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
770 return STTD_ERROR_INVALID_STATE;
774 ret = sttd_engine_agent_get_current_engine(engine_id);
776 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
780 return STTD_ERROR_NONE;
783 int sttd_server_check_app_agreed(int uid, const char* appid, bool* available)
785 /* Check if uid is valid */
787 if (0 != sttd_client_get_state(uid, &state)) {
788 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
789 return STTD_ERROR_INVALID_PARAMETER;
792 /* Check state of uid */
793 if (APP_STATE_READY != state) {
794 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
795 return STTD_ERROR_INVALID_STATE;
798 /* Ask engine available */
801 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
803 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
808 stt_client_set_app_agreed(uid);
809 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
814 return STTD_ERROR_NONE;
818 int sttd_server_get_supported_languages(int uid, GSList** lang_list)
820 /* check if uid is valid */
822 if (0 != sttd_client_get_state(uid, &state)) {
823 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
824 return STTD_ERROR_INVALID_PARAMETER;
827 /* get language list from engine */
828 int ret = sttd_engine_agent_supported_langs(lang_list);
830 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
834 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
836 return STTD_ERROR_NONE;
839 int sttd_server_get_current_langauage(int uid, char** current_lang)
841 /* check if uid is valid */
843 if (0 != sttd_client_get_state(uid, &state)) {
844 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
845 return STTD_ERROR_INVALID_PARAMETER;
848 if (NULL == current_lang) {
849 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
850 return STTD_ERROR_INVALID_PARAMETER;
853 /*get current language from engine */
854 int ret = sttd_engine_agent_get_default_lang(current_lang);
856 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
860 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get default language");
862 return STTD_ERROR_NONE;
865 int sttd_server_set_private_data(int uid, const char* key, const char* data)
867 /* check if uid is valid */
869 if (0 != sttd_client_get_state(uid, &state)) {
870 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
871 return STTD_ERROR_INVALID_PARAMETER;
874 if (NULL == key || NULL == data) {
875 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
876 return STTD_ERROR_INVALID_PARAMETER;
879 /* set private data to engine */
881 ret = sttd_engine_agent_set_private_data(key, data);
883 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
887 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Set private data");
889 return STTD_ERROR_NONE;
892 int sttd_server_get_private_data(int uid, const char* key, char** data)
894 /* check if uid is valid */
896 if (0 != sttd_client_get_state(uid, &state)) {
897 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
898 return STTD_ERROR_INVALID_PARAMETER;
901 if (NULL == key || NULL == data) {
902 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
903 return STTD_ERROR_INVALID_PARAMETER;
906 /* get private data to engine */
908 ret = sttd_engine_agent_get_private_data(key, data);
910 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
914 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
916 return STTD_ERROR_NONE;
919 int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support)
921 /* check if uid is valid */
923 if (0 != sttd_client_get_state(uid, &state)) {
924 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
925 return STTD_ERROR_INVALID_PARAMETER;
928 if (NULL == type || NULL == support) {
929 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
930 return STTD_ERROR_INVALID_PARAMETER;
934 int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
936 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
940 *support = (int)temp;
942 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
944 return STTD_ERROR_NONE;
947 int sttd_server_set_start_sound(int uid, const char* file)
949 /* check if uid is valid */
951 if (0 != sttd_client_get_state(uid, &state)) {
952 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
953 return STTD_ERROR_INVALID_PARAMETER;
956 int ret = sttd_client_set_start_sound(uid, file);
958 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
965 int sttd_server_set_stop_sound(int uid, const char* file)
967 /* check if uid is valid */
969 if (0 != sttd_client_get_state(uid, &state)) {
970 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
971 return STTD_ERROR_INVALID_PARAMETER;
974 int ret = sttd_client_set_stop_sound(uid, file);
976 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
984 Eina_Bool __check_recording_state(void *data)
987 int uid = stt_client_get_current_recognition();
992 if (0 != sttdc_send_get_state(uid, (int*)&state)) {
993 /* client is removed */
994 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", uid);
995 sttd_server_finalize(uid);
999 if (APP_STATE_READY == state) {
1001 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The STT service should cancel recording", uid);
1002 sttd_server_cancel(uid);
1003 } else if (APP_STATE_PROCESSING == state) {
1004 /* Cancel stt and send change state */
1005 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The STT service should cancel recording", uid);
1006 sttd_server_cancel(uid);
1007 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1010 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1018 Eina_Bool __stop_by_recording_timeout(void *data)
1020 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by timeout");
1022 g_recording_timer = NULL;
1025 uid = stt_client_get_current_recognition();
1027 /* cancel engine recognition */
1028 int ret = sttd_server_stop(uid);
1030 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1034 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1035 SLOG(LOG_DEBUG, TAG_STTD, " ");
1040 void __sttd_server_recorder_start(void* data)
1042 intptr_t puid = (intptr_t)data;
1043 int uid = (int)puid;
1044 int current_uid = stt_client_get_current_recognition();
1046 if (uid != current_uid) {
1047 stt_client_unset_current_recognition();
1051 int ret = sttd_engine_agent_recognize_start_recorder(uid);
1053 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1057 /* Notify uid state change */
1058 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1060 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1063 void __sttd_start_sound_completed_cb(int id, void *user_data)
1065 SLOG(LOG_DEBUG, TAG_STTD, "===== Start sound completed");
1067 /* After wav play callback, recorder start */
1068 ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1070 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1071 SLOG(LOG_DEBUG, TAG_STTD, " ");
1075 int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
1077 if (NULL == lang || NULL == recognition_type) {
1078 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1079 return STTD_ERROR_INVALID_PARAMETER;
1082 /* check if uid is valid */
1084 if (0 != sttd_client_get_state(uid, &state)) {
1085 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1086 return STTD_ERROR_INVALID_PARAMETER;
1089 /* check uid state */
1090 if (APP_STATE_READY != state) {
1091 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1092 return STTD_ERROR_INVALID_STATE;
1096 if (false == stt_client_get_app_agreed(uid)) {
1098 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1100 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1104 if (false == temp) {
1105 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1106 return STTD_ERROR_PERMISSION_DENIED;
1109 stt_client_set_app_agreed(uid);
1112 /* check if engine use network */
1113 if (true == sttd_engine_agent_need_network()) {
1114 if (false == stt_network_is_connected()) {
1115 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1116 return STTD_ERROR_OUT_OF_NETWORK;
1121 ret = sttd_client_get_start_sound(uid, &sound);
1123 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1127 if (0 != stt_client_set_current_recognition(uid)) {
1128 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1129 if (NULL != sound) free(sound);
1130 return STTD_ERROR_RECORDER_BUSY;
1133 /* engine start recognition */
1134 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)",
1135 uid, lang, recognition_type);
1137 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start sound : %s", sound);
1139 /* 1. Set audio session */
1140 ret = sttd_recorder_set_audio_session();
1142 stt_client_unset_current_recognition();
1143 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1144 if (NULL != sound) free(sound);
1148 bool is_sound_done = false;
1150 /* 2. Request wav play */
1151 if (NULL != sound) {
1153 intptr_t puid = (intptr_t)uid;
1154 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_start_sound_completed_cb, (void*)puid, &id);
1155 if (WAV_PLAYER_ERROR_NONE != ret) {
1156 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1157 is_sound_done = true;
1162 is_sound_done = true;
1165 /* 3. Create recorder & engine initialize */
1166 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1168 stt_client_unset_current_recognition();
1169 sttd_recorder_unset_audio_session();
1170 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1174 if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1175 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1178 /* change uid state */
1179 sttd_client_set_state(uid, APP_STATE_RECORDING);
1181 g_recording_log_count = 0;
1183 if (true == is_sound_done) {
1184 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1186 ret = sttd_engine_agent_recognize_start_recorder(uid);
1188 stt_client_unset_current_recognition();
1189 sttd_recorder_unset_audio_session();
1191 sttd_engine_agent_recognize_cancel();
1192 ecore_timer_del(g_recording_timer);
1193 sttd_client_set_state(uid, APP_STATE_READY);
1195 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1199 /* Notify uid state change */
1200 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1202 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1203 return STTD_RESULT_STATE_DONE;
1206 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Wait sound finish");
1208 return STTD_RESULT_STATE_NOT_DONE;
1211 Eina_Bool __time_out_for_processing(void *data)
1213 g_processing_timer = NULL;
1216 int uid = stt_client_get_current_recognition();
1217 if (0 == uid) return EINA_FALSE;
1220 int ret = sttd_engine_agent_recognize_cancel();
1222 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1225 if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1226 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1228 /* send error msg */
1229 int reason = (int)STTD_ERROR_TIMED_OUT;
1230 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1231 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1235 /* Change uid state */
1236 sttd_client_set_state(uid, APP_STATE_READY);
1238 stt_client_unset_current_recognition();
1243 void __sttd_server_engine_stop(void* data)
1245 intptr_t puid = (intptr_t)data;
1246 int uid = (int)puid;
1247 /* change uid state */
1248 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1250 /* Notify uid state change */
1251 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1253 /* Unset audio session */
1254 int ret = sttd_recorder_unset_audio_session();
1256 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1261 ret = sttd_engine_agent_recognize_stop_engine();
1263 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1267 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1270 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1272 SLOG(LOG_DEBUG, TAG_STTD, "===== Stop sound completed");
1274 /* After wav play callback, engine stop */
1275 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1277 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1278 SLOG(LOG_DEBUG, TAG_STTD, " ");
1282 int sttd_server_stop(int uid)
1284 /* check if uid is valid */
1286 if (0 != sttd_client_get_state(uid, &state)) {
1287 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1288 return STTD_ERROR_INVALID_PARAMETER;
1291 /* check uid state */
1292 if (APP_STATE_RECORDING != state) {
1293 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1294 return STTD_ERROR_INVALID_STATE;
1297 if (NULL != g_recording_timer) {
1298 ecore_timer_del(g_recording_timer);
1299 g_recording_timer = NULL;
1303 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1304 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1305 return STTD_ERROR_OPERATION_FAILED;
1308 SLOG(LOG_DEBUG, TAG_STTD, "[Server] stop sound path : %s", sound);
1311 /* 1. Stop recorder */
1312 ret = sttd_engine_agent_recognize_stop_recorder();
1314 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1315 if (0 != sttd_engine_agent_recognize_cancel()) {
1316 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1318 if (NULL != sound) free(sound);
1322 /* 2. Request wav play */
1323 if (NULL != sound) {
1325 intptr_t puid = (intptr_t)uid;
1326 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1327 if (WAV_PLAYER_ERROR_NONE != ret) {
1328 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1330 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1335 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1337 return STTD_RESULT_STATE_NOT_DONE;
1339 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1341 /* Unset audio session */
1342 ret = sttd_recorder_unset_audio_session();
1344 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1349 ret = sttd_engine_agent_recognize_stop_engine();
1351 stt_client_unset_current_recognition();
1352 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1356 /* change uid state */
1357 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1359 /* Notify uid state change */
1360 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1362 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1364 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1366 return STTD_RESULT_STATE_DONE;
1369 return STTD_ERROR_NONE;
1372 int sttd_server_cancel(int uid)
1374 /* check if uid is valid */
1376 if (0 != sttd_client_get_state(uid, &state)) {
1377 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1378 return STTD_ERROR_INVALID_PARAMETER;
1381 /* check uid state */
1382 if (APP_STATE_READY == state) {
1383 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1384 return STTD_ERROR_NONE;
1387 stt_client_unset_current_recognition();
1389 if (NULL != g_recording_timer) {
1390 ecore_timer_del(g_recording_timer);
1391 g_recording_timer = NULL;
1394 if (NULL != g_processing_timer) {
1395 ecore_timer_del(g_processing_timer);
1396 g_processing_timer = NULL;
1399 if (APP_STATE_RECORDING == state) {
1400 /* Unset audio session */
1401 int ret = sttd_recorder_unset_audio_session();
1403 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1408 /* change uid state */
1409 sttd_client_set_state(uid, APP_STATE_READY);
1411 /* cancel engine recognition */
1412 int ret = sttd_engine_agent_recognize_cancel();
1414 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1418 /* Notify uid state change */
1419 sttdc_send_set_state(uid, APP_STATE_READY);
1421 return STTD_ERROR_NONE;