2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
16 #include <sound_manager.h>
17 #include <wav_player.h>
19 #include "stt_network.h"
20 #include "sttd_client_data.h"
21 #include "sttd_config.h"
22 #include "sttd_dbus.h"
23 #include "sttd_engine_agent.h"
24 #include "sttd_main.h"
25 #include "sttd_recorder.h"
27 #include "sttd_server.h"
30 #define CLIENT_CLEAN_UP_TIME 500
33 static pthread_mutex_t stte_result_mutex = PTHREAD_MUTEX_INITIALIZER;
34 static pthread_mutex_t stte_result_time_mutex = PTHREAD_MUTEX_INITIALIZER;
38 * STT Server static variable
40 static double g_processing_timeout = 60;
42 static double g_recording_timeout = 60;
44 static Ecore_Timer *g_check_client_timer = NULL;
45 static Ecore_Timer *g_recording_timer = NULL;
46 static Ecore_Timer *g_processing_timer = NULL;
47 static Ecore_Timer *g_stop_by_silence_timer = NULL;
49 static int g_recording_log_count = 0;
51 static GList *g_proc_list = NULL;
54 * STT Server Callback Functions
56 static void __cancel_recognition_internal();
58 static Eina_Bool __stop_by_silence(void *data)
60 SLOG(LOG_INFO, TAG_STTD, "===== Stop by silence detection");
62 unsigned int uid = stt_client_get_current_recognition();
64 if (STT_INVALID_UID != uid) {
65 int ret = sttd_server_stop(uid);
66 if (STTD_ERROR_NONE != ret) {
67 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Fail to stop recording");
68 __cancel_recognition_internal();
71 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
74 SLOG(LOG_INFO, TAG_STTD, "=====");
75 SLOG(LOG_DEBUG, TAG_STTD, " ");
77 g_stop_by_silence_timer = NULL;
81 static void __cancel_recognition_internal()
83 if (NULL != g_recording_timer) {
84 ecore_timer_del(g_recording_timer);
85 g_recording_timer = NULL;
89 unsigned int uid = stt_client_get_current_recognition();
91 app_state_e state = 0;
92 ret = sttd_client_get_state(uid, &state);
95 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
99 if (0 != uid && (APP_STATE_PROCESSING == state || APP_STATE_RECORDING == state)) {
100 SLOG(LOG_INFO, TAG_STTD, "===== cancel by internal");
101 /* cancel engine recognition */
102 ret = sttd_server_cancel(uid);
104 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
107 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
111 static void __cancel_by_error(void *data)
113 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by error");
115 __cancel_recognition_internal();
117 SLOG(LOG_DEBUG, TAG_STTD, "=====");
118 SLOG(LOG_DEBUG, TAG_STTD, " ");
123 static int __server_audio_recorder_callback(const void* data, const unsigned int length)
127 if (NULL == data || 0 == length) {
128 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Recording data is not valid");
129 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
133 unsigned int uid = stt_client_get_current_recognition();
134 if (STT_INVALID_UID != uid) {
135 ret = sttd_engine_agent_set_recording_data(data, length);
137 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
140 g_recording_log_count++;
141 if (200 <= g_recording_log_count) {
142 SLOG(LOG_DEBUG, TAG_STTD, "=== Set recording data ===");
143 g_recording_log_count = 0;
146 if (NULL != g_recording_timer) {
147 ecore_timer_del(g_recording_timer);
148 g_recording_timer = NULL;
150 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current uid in recording is is not valid");
157 static void __server_audio_interrupt_callback()
159 SLOG(LOG_INFO, TAG_STTD, "===== Cancel by sound interrupt");
161 __cancel_recognition_internal();
163 SLOG(LOG_DEBUG, TAG_STTD, "=====");
164 SLOG(LOG_DEBUG, TAG_STTD, " ");
167 static int __server_recognition_result_callback(stte_result_event_e event, const char* type,
168 const char** data, int data_count, const char* msg, void *user_data)
171 pthread_mutex_lock(&stte_result_mutex);
173 SLOG(LOG_INFO, TAG_STTD, "===== RESULT event[%d] type[%s] data[%p] data_count[%d]", event, type, data, data_count);
176 unsigned int uid = stt_client_get_current_recognition();
179 if (STT_INVALID_UID == uid || 0 != sttd_client_get_state(uid, &state)) {
180 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid (%u)", uid);
181 SLOG(LOG_DEBUG, TAG_STTD, "=====");
182 SLOG(LOG_DEBUG, TAG_STTD, " ");
183 pthread_mutex_unlock(&stte_result_mutex);
184 return STTD_ERROR_OPERATION_FAILED;
187 SLOG(LOG_INFO, TAG_STTD, "[Server] uid (%u), event(%d)", uid, event);
189 /* send result to client */
190 if (STTE_RESULT_EVENT_FINAL_RESULT == event) {
191 if (APP_STATE_PROCESSING != state) {
192 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is NOT 'Processing'.");
194 SLOG(LOG_INFO, TAG_STTD, "[Server] the size of result from engine is '%d'", data_count);
196 /* Delete timer for processing time out */
197 ecore_thread_main_loop_begin();
198 if (NULL != g_processing_timer) {
199 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
200 ecore_timer_del(g_processing_timer);
201 g_processing_timer = NULL;
203 ecore_thread_main_loop_end();
205 sttd_config_time_save();
206 sttd_config_time_reset();
208 sttd_recorder_clear();
210 sttd_client_set_state(uid, APP_STATE_READY);
211 stt_client_unset_current_recognition();
213 if (NULL == data || 0 == data_count) {
214 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
215 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
216 int reason = (int)STTD_ERROR_OPERATION_FAILED;
218 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
219 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
223 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
224 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
225 int reason = (int)STTD_ERROR_OPERATION_FAILED;
227 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
228 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
233 } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) {
234 SLOG(LOG_INFO, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event, data_count);
236 sttd_config_time_save();
237 sttd_config_time_reset();
239 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
240 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
241 int reason = (int)STTD_ERROR_OPERATION_FAILED;
243 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
244 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
248 } else if (STTE_RESULT_EVENT_ERROR == event) {
249 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
251 /* Delete timer for processing time out */
252 ecore_thread_main_loop_begin();
253 if (NULL != g_processing_timer) {
254 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
255 ecore_timer_del(g_processing_timer);
256 g_processing_timer = NULL;
258 ecore_thread_main_loop_end();
259 sttd_config_time_reset();
262 if (APP_STATE_RECORDING == state) {
263 ret = sttd_engine_agent_recognize_cancel();
265 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel: result(%d)", ret);
269 sttd_client_set_state(uid, APP_STATE_READY);
270 stt_client_unset_current_recognition();
272 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
273 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
276 int reason = (int)STTD_ERROR_INVALID_STATE;
277 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
278 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
285 SLOG(LOG_INFO, TAG_STTD, "=====");
286 SLOG(LOG_DEBUG, TAG_STTD, " ");
287 pthread_mutex_unlock(&stte_result_mutex);
289 return STTD_ERROR_NONE;
292 static 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)
294 pthread_mutex_lock(&stte_result_time_mutex);
296 SECURE_SLOG(LOG_INFO, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)",
297 index, event, text, start_time, end_time);
300 ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
302 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
303 pthread_mutex_unlock(&stte_result_time_mutex);
307 pthread_mutex_unlock(&stte_result_time_mutex);
312 static int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
314 SLOG(LOG_INFO, TAG_STTD, "===== Speech status detected Callback");
316 unsigned int uid = stt_client_get_current_recognition();
317 if (STT_INVALID_UID != uid) {
319 if (0 != sttd_client_get_state(uid, &state)) {
320 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid ");
321 return STTD_ERROR_OPERATION_FAILED;
324 if (APP_STATE_RECORDING != state && APP_STATE_PROCESSING != state) {
325 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording, state(%d), status(%d)", state, status);
326 return STTD_ERROR_INVALID_STATE;
329 if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) {
330 SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected");
331 sttdc_send_speech_status(uid, status);
332 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
333 SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
334 ecore_thread_main_loop_begin();
335 if (NULL == g_stop_by_silence_timer) {
336 g_stop_by_silence_timer = ecore_timer_add(0, __stop_by_silence, NULL);
338 ecore_thread_main_loop_end();
341 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recognition uid is not valid ");
344 SLOG(LOG_INFO, TAG_STTD, "=====");
345 SLOG(LOG_DEBUG, TAG_STTD, " ");
347 return STTD_ERROR_NONE;
350 static int __server_error_callback(stte_error_e error, const char* msg)
352 SLOG(LOG_ERROR, TAG_STTD, "[Server] Error Callback is called, error(%d), err_msg(%s)", error, msg);
353 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
355 return STTD_ERROR_NONE;
358 static 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_INFO, TAG_STTD, "[Server] New default engine : %s", engine_id);
370 static void __sttd_server_language_changed_cb(const char* language, void* user_data)
372 if (NULL == language) {
373 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
376 SLOG(LOG_INFO, TAG_STTD, "[Server] Get language changed : %s", language);
379 int ret = sttd_engine_agent_set_default_language(language);
381 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
386 static void __sttd_server_silence_changed_cb(bool value, void* user_data)
388 SLOG(LOG_INFO, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
391 ret = sttd_engine_agent_set_silence_detection(value);
393 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
401 int sttd_initialize(stte_request_callback_s *callback)
405 if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
406 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
409 if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) {
410 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex.");
413 if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
414 __sttd_server_silence_changed_cb, NULL)) {
415 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
418 ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
420 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
424 /* Engine Agent initialize */
425 ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
426 __server_speech_status_callback, __server_error_callback);
428 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
433 ret = sttd_engine_agent_load_current_engine(callback);
435 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
439 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, sttd_cleanup_client, NULL);
440 if (NULL == g_check_client_timer) {
441 SLOG(LOG_WARN, TAG_STTD, "[Main Warning] Fail to create timer of client check");
444 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] initialize");
451 if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
452 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
455 if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) {
456 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex.");
460 if (0 < g_list_length(g_proc_list)) {
461 iter = g_list_first(g_proc_list);
462 while (NULL != iter) {
463 g_proc_list = g_list_delete_link(g_proc_list, iter);
464 iter = g_list_first(g_proc_list);
468 sttd_recorder_deinitialize();
470 sttd_config_finalize();
472 sttd_engine_agent_release();
474 if (NULL != g_check_client_timer) {
475 ecore_timer_del(g_check_client_timer);
476 g_check_client_timer = NULL;
478 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete ecore timer handle");
481 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] finalize");
483 return STTD_ERROR_NONE;
486 static void __read_proc()
489 struct dirent *dirp = NULL;
493 if (0 < g_list_length(g_proc_list)) {
494 iter = g_list_first(g_proc_list);
495 while (NULL != iter) {
496 g_proc_list = g_list_delete_link(g_proc_list, iter);
497 iter = g_list_first(g_proc_list);
501 dp = opendir("/proc");
503 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
509 tmp = atoi(dirp->d_name);
510 if (0 >= tmp) continue;
511 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
513 } while (NULL != dirp);
519 Eina_Bool sttd_cleanup_client(void *data)
521 unsigned int* client_list = NULL;
522 int client_count = 0;
527 if (0 != sttd_client_get_list(&client_list, &client_count)) {
528 if (NULL != client_list)
534 if (NULL != client_list) {
535 SLOG(LOG_INFO, TAG_STTD, "===== Clean up client ");
539 for (i = 0; i < client_count; i++) {
540 int pid = sttd_client_get_pid(client_list[i]);
542 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
548 for (j = 0; j < g_list_length(g_proc_list); j++) {
549 iter = g_list_nth(g_proc_list, j);
551 if (pid == GPOINTER_TO_INT(iter->data)) {
552 SLOG(LOG_DEBUG, TAG_STTD, "uid (%u) is running", client_list[i]);
559 if (false == exist) {
560 SLOG(LOG_ERROR, TAG_STTD, "uid (%u) should be removed", client_list[i]);
561 sttd_server_finalize(client_list[i]);
565 SLOG(LOG_INFO, TAG_STTD, "=====");
566 SLOG(LOG_DEBUG, TAG_STTD, " ");
575 * STT Server Functions for Client
578 int sttd_server_initialize(int pid, unsigned int uid, bool* silence, bool* credential)
580 int ret = STTD_ERROR_NONE;
582 SLOG(LOG_INFO, TAG_STTD, "[Server] server initialize");
584 /* check if uid is valid */
586 if (0 == sttd_client_get_state(uid, &state)) {
587 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
588 return STTD_ERROR_NONE;
591 ret = sttd_engine_agent_get_option_supported(silence);
593 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
597 ret = sttd_engine_agent_is_credential_needed(uid, credential);
599 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
603 /* Add client information to client manager */
604 ret = sttd_client_add(pid, uid);
606 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
610 SLOG(LOG_INFO, TAG_STTD, "[Server Success] server initialize");
612 return STTD_ERROR_NONE;
615 static Eina_Bool __quit_ecore_loop(void *data)
617 SLOG(LOG_INFO, TAG_STTD, "[Server] Quit");
619 stt_network_finalize();
621 sttd_dbus_close_connection();
622 ecore_main_loop_quit();
624 SLOG(LOG_INFO, TAG_STTD, "");
629 static void delete_timers_related_to_recording_session()
631 if (NULL != g_recording_timer) {
632 ecore_timer_del(g_recording_timer);
633 g_recording_timer = NULL;
636 if (NULL != g_processing_timer) {
637 SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
638 ecore_timer_del(g_processing_timer);
639 g_processing_timer = NULL;
642 if (NULL != g_stop_by_silence_timer) {
643 ecore_timer_del(g_stop_by_silence_timer);
644 g_stop_by_silence_timer = NULL;
648 int sttd_server_finalize(unsigned int uid)
650 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter Finalize");
652 /* check if uid is valid */
654 if (0 != sttd_client_get_state(uid, &state)) {
655 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
656 return STTD_ERROR_INVALID_PARAMETER;
659 /* release recorder */
660 if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
661 delete_timers_related_to_recording_session();
663 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] stt_cancel is invoked while state is (%d)", state);
664 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
665 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
668 stt_client_unset_current_recognition();
671 /* Remove client information */
672 if (0 != sttd_client_delete(uid)) {
673 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
676 /* unload engine, if ref count of client is 0 */
677 if (0 == sttd_client_get_ref_count()) {
678 ecore_timer_add(0, __quit_ecore_loop, NULL);
681 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End Finalize");
683 return STTD_ERROR_NONE;
686 int sttd_server_get_supported_engines(unsigned int uid, GSList** engine_list)
688 /* Check if uid is valid */
690 if (0 != sttd_client_get_state(uid, &state)) {
691 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
692 return STTD_ERROR_INVALID_PARAMETER;
695 /* Check state of uid */
696 if (APP_STATE_READY != state) {
697 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%u) is not Ready", uid);
698 return STTD_ERROR_INVALID_STATE;
701 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported egnines");
704 ret = sttd_engine_agent_get_engine_list(engine_list);
706 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
710 return STTD_ERROR_NONE;
713 int sttd_server_set_current_engine(unsigned int uid, const char* engine_id, bool* silence, bool* credential)
715 /* Check if uid is valid */
717 if (0 != sttd_client_get_state(uid, &state)) {
718 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
719 return STTD_ERROR_INVALID_PARAMETER;
722 /* Check state of uid */
723 if (APP_STATE_READY != state) {
724 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%u) is not Ready", uid);
725 return STTD_ERROR_INVALID_STATE;
728 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] set current egnines, uid(%u), engine_id(%s)", uid, engine_id);
731 ret = sttd_engine_agent_load_current_engine(NULL);
733 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
737 ret = sttd_engine_agent_get_option_supported(silence);
739 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
743 if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
744 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
748 return STTD_ERROR_NONE;
751 int sttd_server_get_current_engine(unsigned int uid, char** engine_id)
753 /* Check if uid is valid */
755 if (0 != sttd_client_get_state(uid, &state)) {
756 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
757 return STTD_ERROR_INVALID_PARAMETER;
760 /* Check state of uid */
761 if (APP_STATE_READY != state) {
762 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%u) is not Ready", uid);
763 return STTD_ERROR_INVALID_STATE;
766 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get current egnines, uid(%u)", uid);
769 ret = sttd_engine_agent_get_current_engine(engine_id);
771 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
775 return STTD_ERROR_NONE;
778 int sttd_server_check_app_agreed(unsigned int uid, const char* appid, bool* available)
780 /* Check if uid is valid */
782 if (0 != sttd_client_get_state(uid, &state)) {
783 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
784 return STTD_ERROR_INVALID_PARAMETER;
787 /* Check state of uid */
788 if (APP_STATE_READY != state) {
789 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%u) is not Ready", uid);
790 return STTD_ERROR_INVALID_STATE;
793 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] check app agreed");
795 /* Ask engine available */
798 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
800 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
805 stt_client_set_app_agreed(uid);
806 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
811 return STTD_ERROR_NONE;
815 int sttd_server_get_supported_languages(unsigned int uid, GSList** lang_list)
817 /* check if uid is valid */
819 if (0 != sttd_client_get_state(uid, &state)) {
820 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
821 return STTD_ERROR_INVALID_PARAMETER;
824 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] get supported languages");
826 /* get language list from engine */
827 int ret = sttd_engine_agent_supported_langs(lang_list);
829 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
833 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
835 return STTD_ERROR_NONE;
838 int sttd_server_get_current_langauage(unsigned int uid, char** current_lang)
840 /* check if uid is valid */
842 if (0 != sttd_client_get_state(uid, &state)) {
843 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
844 return STTD_ERROR_INVALID_PARAMETER;
847 if (NULL == current_lang) {
848 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
849 return STTD_ERROR_INVALID_PARAMETER;
852 /*get current language from engine */
853 int ret = sttd_engine_agent_get_default_lang(current_lang);
855 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
859 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get default language, current_lang(%s)", *current_lang);
861 return STTD_ERROR_NONE;
864 int sttd_server_set_private_data(unsigned int uid, const char* key, const char* data)
866 /* check if uid is valid */
868 if (0 != sttd_client_get_state(uid, &state)) {
869 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
870 return STTD_ERROR_INVALID_PARAMETER;
873 if (NULL == key || NULL == data) {
874 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
875 return STTD_ERROR_INVALID_PARAMETER;
878 /* set private data to engine */
880 ret = sttd_engine_agent_set_private_data(key, data);
882 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
886 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Set private data");
888 return STTD_ERROR_NONE;
891 int sttd_server_get_private_data(unsigned int uid, const char* key, char** data)
893 /* check if uid is valid */
895 if (0 != sttd_client_get_state(uid, &state)) {
896 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
897 return STTD_ERROR_INVALID_PARAMETER;
900 if (NULL == key || NULL == data) {
901 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
902 return STTD_ERROR_INVALID_PARAMETER;
905 /* get private data to engine */
907 ret = sttd_engine_agent_get_private_data(key, data);
909 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
913 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
915 return STTD_ERROR_NONE;
918 int sttd_server_is_recognition_type_supported(unsigned int uid, const char* type, int* support)
920 /* check if uid is valid */
922 if (0 != sttd_client_get_state(uid, &state)) {
923 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
924 return STTD_ERROR_INVALID_PARAMETER;
927 if (NULL == type || NULL == support) {
928 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
929 return STTD_ERROR_INVALID_PARAMETER;
933 int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
935 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
939 *support = (int)temp;
941 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
943 return STTD_ERROR_NONE;
946 int sttd_server_get_audio_format(unsigned int uid, stte_audio_type_e *type, int *rate, int *num_of_channels)
948 /* check if uid is valid */
949 app_state_e state = APP_STATE_CREATED;
950 if (0 != sttd_client_get_state(uid, &state)) {
951 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
952 return STTD_ERROR_INVALID_PARAMETER;
955 if (NULL == type || NULL == rate || NULL == num_of_channels) {
956 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
957 return STTD_ERROR_INVALID_PARAMETER;
960 int ret = sttd_engine_agent_get_audio_format(type, rate, num_of_channels);
962 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
966 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Audio format: type(%d), rate(%d), number of channels(%d)", *type, *rate, *num_of_channels);
967 return STTD_ERROR_NONE;
970 int sttd_server_set_start_sound(unsigned int uid, const char* file)
972 /* check if uid is valid */
974 if (0 != sttd_client_get_state(uid, &state)) {
975 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
976 return STTD_ERROR_INVALID_PARAMETER;
979 int ret = sttd_client_set_start_sound(uid, file);
981 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
988 int sttd_server_set_stop_sound(unsigned int uid, const char* file)
990 /* check if uid is valid */
992 if (0 != sttd_client_get_state(uid, &state)) {
993 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
994 return STTD_ERROR_INVALID_PARAMETER;
997 int ret = sttd_client_set_stop_sound(uid, file);
999 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
1006 static Eina_Bool __stop_by_recording_timeout(void *data)
1008 SLOG(LOG_INFO, TAG_STTD, "===== Stop by timeout");
1009 g_recording_timer = NULL;
1011 unsigned int uid = stt_client_get_current_recognition();
1012 if (STT_INVALID_UID != uid) {
1013 /* cancel engine recognition */
1014 int ret = sttd_server_stop(uid);
1015 if (STTD_ERROR_NONE != ret) {
1016 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1020 SLOG(LOG_INFO, TAG_STTD, "=====");
1021 SLOG(LOG_DEBUG, TAG_STTD, " ");
1026 static void __sttd_server_recorder_start(void* data)
1028 uintptr_t puid = (uintptr_t)data;
1029 unsigned int uid = (unsigned int)puid;
1030 int current_uid = stt_client_get_current_recognition();
1032 if (uid != current_uid) {
1033 stt_client_unset_current_recognition();
1037 char appid[1024] = {0, };
1038 int pid = sttd_client_get_pid(uid);
1040 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1);
1041 if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1042 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to get application ID");
1044 SLOG(LOG_DEBUG, TAG_STTD, "[DEBUG] Current app id is %s", appid);
1047 ret = sttd_engine_agent_recognize_start_recorder(uid, appid);
1049 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1053 app_state_e temp_app_state;
1054 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1055 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1058 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1059 /* Notify uid state change */
1060 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1061 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1065 static void __sttd_start_sound_completed_cb(int id, void *user_data)
1067 SLOG(LOG_INFO, TAG_STTD, "===== Start sound completed");
1069 /* After wav play callback, recorder start */
1070 ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1072 SLOG(LOG_INFO, TAG_STTD, "=====");
1073 SLOG(LOG_DEBUG, TAG_STTD, " ");
1077 static int __sttd_server_check_precondition_to_start(unsigned int uid, const char* appid)
1079 /* check if uid is valid */
1081 if (0 != sttd_client_get_state(uid, &state)) {
1082 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1083 return STTD_ERROR_INVALID_PARAMETER;
1086 /* check uid state */
1087 if (APP_STATE_READY != state) {
1088 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1089 return STTD_ERROR_INVALID_STATE;
1092 int ret = STTD_ERROR_NONE;
1093 if (false == stt_client_get_app_agreed(uid)) {
1095 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1097 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1101 if (false == temp) {
1102 SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1103 return STTD_ERROR_PERMISSION_DENIED;
1106 stt_client_set_app_agreed(uid);
1109 /* check if engine use network */
1110 if (true == sttd_engine_agent_need_network()) {
1111 if (false == stt_network_is_connected()) {
1112 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1113 return STTD_ERROR_OUT_OF_NETWORK;
1119 int sttd_server_start(unsigned int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential, const char* audio_id)
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 int ret = __sttd_server_check_precondition_to_start(uid, appid);
1128 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to satisfy preconditions, ret(0x%x)", ret);
1132 if (0 != stt_client_set_current_recognition(uid)) {
1133 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1134 return STTD_ERROR_RECORDER_BUSY;
1137 delete_timers_related_to_recording_session();
1140 ret = sttd_client_get_start_sound(uid, &sound);
1142 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1146 ret = sttd_client_set_audio_id(uid, audio_id);
1148 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set audio_id(%s)", audio_id);
1149 if (NULL != sound) free(sound);
1153 /* engine start recognition */
1154 SLOG(LOG_INFO, TAG_STTD, "[Server] start : uid(%u), lang(%s), recog_type(%s)",
1155 uid, lang, recognition_type);
1157 SLOG(LOG_INFO, TAG_STTD, "[Server] start sound : %s", sound);
1159 /* 1. Set audio session */
1160 ret = sttd_recorder_set_audio_session();
1162 stt_client_unset_current_recognition();
1163 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1164 if (NULL != sound) free(sound);
1168 bool is_sound_done = false;
1170 /* 2. Request wav play */
1171 if (NULL != sound) {
1173 uintptr_t puid = (uintptr_t)uid;
1174 sound_stream_info_h wav_stream_info_h;
1175 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1176 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1177 is_sound_done = true;
1179 ret = wav_player_start_new(sound, wav_stream_info_h, __sttd_start_sound_completed_cb, (void*)puid, &id);
1180 if (WAV_PLAYER_ERROR_NONE != ret) {
1181 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1182 is_sound_done = true;
1185 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1186 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1192 is_sound_done = true;
1195 /* 3. Create recorder & engine initialize */
1196 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1198 stt_client_unset_current_recognition();
1199 sttd_recorder_unset_audio_session();
1200 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1204 if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1205 if (NULL == g_processing_timer) {
1206 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1210 /* change uid state */
1211 sttd_client_set_state(uid, APP_STATE_RECORDING);
1213 g_recording_log_count = 0;
1215 app_state_e temp_app_state;
1217 if (true == is_sound_done) {
1218 SLOG(LOG_INFO, TAG_STTD, "[Server] No sound play");
1220 ret = sttd_engine_agent_recognize_start_recorder(uid, appid);
1222 stt_client_unset_current_recognition();
1223 sttd_recorder_unset_audio_session();
1225 sttd_engine_agent_recognize_cancel();
1226 if (NULL != g_recording_timer) {
1227 ecore_timer_del(g_recording_timer);
1228 g_recording_timer = NULL;
1230 sttd_client_set_state(uid, APP_STATE_READY);
1232 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1236 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1237 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid");
1238 return STTD_ERROR_INVALID_PARAMETER;
1240 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1241 /* Notify uid state change */
1242 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1245 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Start recognition");
1247 SLOG(LOG_INFO, TAG_STTD, "[Server] Wait sound finish");
1250 sttd_client_set_streaming(uid, false);
1251 return STTD_ERROR_NONE;
1254 static Eina_Bool __time_out_for_processing(void *data)
1256 g_processing_timer = NULL;
1257 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter __time_out_for_processing");
1260 unsigned int uid = stt_client_get_current_recognition();
1261 if (STT_INVALID_UID == uid) return EINA_FALSE;
1263 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by timeout for processing");
1266 int ret = sttd_engine_agent_recognize_cancel();
1268 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1271 if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1272 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1274 /* send error msg */
1275 int reason = (int)STTD_ERROR_TIMED_OUT;
1276 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1277 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1281 /* Change uid state */
1282 sttd_client_set_state(uid, APP_STATE_READY);
1284 stt_client_unset_current_recognition();
1286 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End __time_out_for_processing");
1291 static void __sttd_server_engine_stop(void* data)
1293 uintptr_t puid = (uintptr_t)data;
1294 unsigned int uid = (unsigned int)puid;
1295 /* change uid state */
1296 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1298 /* Notify uid state change */
1299 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1301 /* Unset audio session */
1302 int ret = sttd_recorder_unset_audio_session();
1304 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1309 ret = sttd_engine_agent_recognize_stop_engine();
1311 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1315 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1318 static void __sttd_stop_sound_completed_cb(int id, void *user_data)
1320 SLOG(LOG_INFO, TAG_STTD, "===== Stop sound completed");
1322 /* After wav play callback, engine stop */
1323 ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1325 SLOG(LOG_DEBUG, TAG_STTD, "=====");
1326 SLOG(LOG_DEBUG, TAG_STTD, " ");
1330 int sttd_server_stop(unsigned int uid)
1332 /* check if uid is valid */
1334 if (0 != sttd_client_get_state(uid, &state)) {
1335 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1336 return STTD_ERROR_INVALID_PARAMETER;
1339 /* check uid state */
1340 if (APP_STATE_PROCESSING == state) {
1341 SLOG(LOG_INFO, TAG_STTD, "[Server] State is already processing. Skip stop behavior");
1342 return STTD_ERROR_NONE;
1345 if (APP_STATE_RECORDING != state) {
1346 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1347 return STTD_ERROR_INVALID_STATE;
1350 if (sttd_client_is_streaming(uid)) {
1351 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current client is not started as default");
1352 return STTD_ERROR_OPERATION_FAILED;
1355 delete_timers_related_to_recording_session();
1358 if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1359 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1360 return STTD_ERROR_OPERATION_FAILED;
1363 SLOG(LOG_INFO, TAG_STTD, "[Server] stop sound path : %s", sound);
1366 /* 1. Stop recorder */
1367 ret = sttd_engine_agent_recognize_stop_recorder();
1369 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1370 if (0 != sttd_engine_agent_recognize_cancel()) {
1371 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1373 if (NULL != sound) free(sound);
1377 /* 2. Request wav play */
1378 if (NULL != sound) {
1380 uintptr_t puid = (uintptr_t)uid;
1381 sound_stream_info_h wav_stream_info_h;
1382 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1383 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1385 ret = wav_player_start_new(sound, wav_stream_info_h, __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);
1392 if (0 != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1393 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stream info for playing wav");
1398 SLOG(LOG_INFO, 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();
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_INFO, TAG_STTD, "[Server SUCCESS] Stop recognition");
1424 SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
1425 if (NULL == g_processing_timer) {
1426 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1429 return STTD_ERROR_NONE;
1432 static bool play_sound(const char *path, wav_player_playback_completed_cb callback, void *user_data)
1434 sound_stream_info_h wav_stream_info_h = NULL;
1435 if (SOUND_MANAGER_ERROR_NONE != sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &wav_stream_info_h)) {
1436 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to create stream info for playing wav");
1440 int ret = wav_player_start_new(path, wav_stream_info_h, callback, user_data, NULL);
1441 if (SOUND_MANAGER_ERROR_NONE != sound_manager_destroy_stream_information(wav_stream_info_h)) {
1442 SLOG(LOG_WARN, TAG_STTD, "[Server WARN] Fail to destroy stream info for playing wav");
1445 if (WAV_PLAYER_ERROR_NONE != ret) {
1446 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1453 static bool play_start_sound_for_uid(unsigned int uid)
1456 if (STTD_ERROR_NONE != sttd_client_get_start_sound(uid, &sound)) {
1457 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start sound");
1461 if (NULL == sound) {
1462 SLOG(LOG_INFO, TAG_STTD, "[Server] There is no start sound");
1466 SLOG(LOG_INFO, TAG_STTD, "[Server] start sound : %s", sound);
1467 bool ret = play_sound(sound, NULL, NULL);
1473 int sttd_server_start_audio_streaming(unsigned int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential, const char* audio_id)
1475 if (NULL == lang || NULL == recognition_type) {
1476 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1477 return STTD_ERROR_INVALID_PARAMETER;
1480 int ret = __sttd_server_check_precondition_to_start(uid, appid);
1481 if (STTD_ERROR_NONE != ret) {
1482 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to satisfy preconditions, ret(0x%x)", ret);
1486 if (STTD_ERROR_NONE != stt_client_set_current_recognition(uid)) {
1487 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1488 return STTD_ERROR_RECORDER_BUSY;
1491 delete_timers_related_to_recording_session();
1493 ret = sttd_client_set_audio_id(uid, audio_id);
1494 if (STTD_ERROR_NONE != ret) {
1495 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set audio_id(%s)", audio_id);
1499 /* engine start recognition */
1500 SLOG(LOG_INFO, TAG_STTD, "[Server] start : uid(%u), lang(%s), recog_type(%s)", uid, lang, recognition_type);
1502 play_start_sound_for_uid(uid);
1504 /* 3. Create recorder & engine initialize */
1505 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1506 if (STTD_ERROR_NONE != ret) {
1507 stt_client_unset_current_recognition();
1508 sttd_recorder_unset_audio_session();
1509 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d/%s)", ret, get_error_message(ret));
1513 if (STTD_ERROR_NONE != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1514 if (NULL == g_processing_timer) {
1515 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1519 /* change uid state */
1520 sttd_client_set_state(uid, APP_STATE_RECORDING);
1522 g_recording_log_count = 0;
1524 app_state_e temp_app_state = APP_STATE_CREATED;
1525 if (0 != sttd_client_get_state(uid, &temp_app_state)) {
1526 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid");
1527 return STTD_ERROR_INVALID_PARAMETER;
1530 if (APP_STATE_READY != temp_app_state && 0 != stt_client_get_current_recognition()) {
1531 /* Notify uid state change */
1532 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1535 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Start recognition");
1537 sttd_client_set_streaming(uid, true);
1538 return STTD_ERROR_NONE;
1541 int sttd_server_send_audio_streaming(unsigned int uid, const char *data, size_t data_size)
1543 app_state_e state = APP_STATE_CREATED;
1544 if (0 != sttd_client_get_state(uid, &state)) {
1545 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1546 return STTD_ERROR_INVALID_PARAMETER;
1549 /* check uid state */
1550 if (APP_STATE_PROCESSING == state) {
1551 SLOG(LOG_INFO, TAG_STTD, "[Server] State is already processing. Skip stop behavior");
1552 return STTD_ERROR_NONE;
1555 if (APP_STATE_RECORDING != state) {
1556 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1557 return STTD_ERROR_INVALID_STATE;
1560 if (false == sttd_client_is_streaming(uid)) {
1561 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current client is not started as audio streaming");
1562 return STTD_ERROR_OPERATION_FAILED;
1565 int ret = __server_audio_recorder_callback(data, data_size);
1567 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send audio data");
1568 return STTD_ERROR_OPERATION_FAILED;
1571 return STTD_ERROR_NONE;
1574 static bool play_stop_sound_for_uid(unsigned int uid)
1577 if (STTD_ERROR_NONE != sttd_client_get_stop_sound(uid, &sound)) {
1578 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start sound");
1582 if (NULL == sound) {
1583 SLOG(LOG_INFO, TAG_STTD, "[Server] There is no start sound");
1587 SLOG(LOG_INFO, TAG_STTD, "[Server] stop sound : %s", sound);
1588 bool ret = play_sound(sound, NULL, NULL);
1594 int sttd_server_stop_audio_streaming(unsigned int uid)
1596 /* check if uid is valid */
1597 app_state_e state = APP_STATE_CREATED;
1598 if (0 != sttd_client_get_state(uid, &state)) {
1599 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1600 return STTD_ERROR_INVALID_PARAMETER;
1603 /* check uid state */
1604 if (APP_STATE_PROCESSING == state) {
1605 SLOG(LOG_INFO, TAG_STTD, "[Server] State is already processing. Skip stop behavior");
1606 return STTD_ERROR_NONE;
1609 if (APP_STATE_RECORDING != state) {
1610 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1611 return STTD_ERROR_INVALID_STATE;
1614 if (false == sttd_client_is_streaming(uid)) {
1615 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current client is not started as audio streaming");
1616 return STTD_ERROR_OPERATION_FAILED;
1619 delete_timers_related_to_recording_session();
1621 /* 2. Request wav play */
1622 play_stop_sound_for_uid(uid);
1625 int ret = sttd_engine_agent_recognize_stop_engine();
1627 stt_client_unset_current_recognition();
1628 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1632 /* change uid state */
1633 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1635 /* Notify uid state change */
1636 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1638 SLOG(LOG_INFO, TAG_STTD, "[Server SUCCESS] Stop recognition");
1640 SLOG(LOG_INFO, TAG_STTD, "[INFO] Add g_processing_timer");
1641 if (NULL == g_processing_timer) {
1642 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1645 sttd_client_set_streaming(uid, false);
1646 return STTD_ERROR_NONE;
1649 static int __sttd_server_cancel(unsigned int uid)
1651 /* check if uid is valid */
1653 if (0 != sttd_client_get_state(uid, &state)) {
1654 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1655 return STTD_ERROR_INVALID_PARAMETER;
1658 /* check uid state */
1659 if (APP_STATE_READY == state) {
1660 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1661 return STTD_ERROR_NONE;
1664 stt_client_unset_current_recognition();
1666 delete_timers_related_to_recording_session();
1668 if (APP_STATE_RECORDING == state) {
1669 /* Unset audio session */
1670 int ret = sttd_recorder_unset_audio_session();
1672 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1677 /* change uid state */
1678 sttd_client_set_state(uid, APP_STATE_READY);
1680 SLOG(LOG_INFO, TAG_STTD, "[Server Info] stt cancel is invoked by app's request");
1682 /* cancel engine recognition */
1683 int ret = sttd_engine_agent_recognize_cancel();
1685 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1689 /* Notify uid state change */
1690 sttdc_send_set_state(uid, APP_STATE_READY);
1695 int sttd_server_cancel(unsigned int uid)
1697 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel");
1699 int ret = __sttd_server_cancel(uid);
1701 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(0x%x)", ret);
1704 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel");
1708 int sttd_server_start_file(unsigned int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential, const char* filepath)
1710 if (NULL == lang || NULL == recognition_type || NULL == filepath) {
1711 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1712 return STTD_ERROR_INVALID_PARAMETER;
1715 int ret = __sttd_server_check_precondition_to_start(uid, appid);
1717 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to satisfy preconditions, ret(0x%x)", ret);
1721 if (0 != stt_client_set_current_recognition(uid)) {
1722 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1723 return STTD_ERROR_RECORDER_BUSY;
1726 /* engine start recognition */
1727 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%u), lang(%s), recog_type(%s), appid(%s), file(%s)", uid, lang, recognition_type, appid, filepath);
1729 /* 1. Set audio session */
1730 ret = sttd_recorder_set_audio_session();
1732 stt_client_unset_current_recognition();
1733 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1737 /* 2. Start engine to recognize */
1738 ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1740 stt_client_unset_current_recognition();
1741 sttd_recorder_unset_audio_session();
1742 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start engine : result(%d)", ret);
1746 sttd_client_set_state(uid, APP_STATE_RECORDING);
1747 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1749 /* 3. Start to send pcm from file to engine */
1750 ret = sttd_engine_agent_recognize_start_file(uid, filepath);
1752 stt_client_unset_current_recognition();
1753 sttd_recorder_unset_audio_session();
1754 sttd_engine_agent_recognize_cancel();
1755 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start file : result(%d)", ret);
1759 /* 4. Stop to send pcm from file */
1760 ret = sttd_engine_agent_recognize_stop_file();
1762 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1763 stt_client_unset_current_recognition();
1764 sttd_recorder_unset_audio_session();
1765 sttd_engine_agent_recognize_cancel();
1769 /* 5. change & notify uid state */
1770 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1771 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1773 /* 6. Unset audio session */
1774 ret = sttd_recorder_unset_audio_session();
1776 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1777 stt_client_unset_current_recognition();
1781 /* 7. Stop engine */
1782 ret = sttd_engine_agent_recognize_stop_engine();
1784 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1785 stt_client_unset_current_recognition();
1789 return STTD_ERROR_NONE;
1792 int sttd_server_cancel_file(unsigned int uid)
1794 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel_file");
1796 int ret = __sttd_server_cancel(uid);
1798 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel file : result(0x%x)", ret);
1801 SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel_file");