X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=server%2Fsttd_server.c;h=0213b5e2f80148b2db753599859aaf78cab1d998;hb=refs%2Fchanges%2F27%2F167927%2F1;hp=62c1d0e81123e7de9f4fca59b72fb70f3d6b5b57;hpb=93fa86f0b8a33a4ea84e2f5761717b3e07a46e7e;p=platform%2Fcore%2Fuifw%2Fstt.git diff --git a/server/sttd_server.c b/server/sttd_server.c index 62c1d0e..0213b5e 100644 --- a/server/sttd_server.c +++ b/server/sttd_server.c @@ -1,5 +1,5 @@ /* -* Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved +* Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -24,8 +24,12 @@ #include "sttd_recorder.h" #include "sttd_server.h" -static pthread_mutex_t sttpe_result_mutex = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t sttpe_result_time_mutex = PTHREAD_MUTEX_INITIALIZER; + +#define CLIENT_CLEAN_UP_TIME 500 + + +static pthread_mutex_t stte_result_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t stte_result_time_mutex = PTHREAD_MUTEX_INITIALIZER; /* @@ -35,6 +39,7 @@ static double g_processing_timeout = 30; static double g_recording_timeout = 60; +static Ecore_Timer* g_check_client_timer = NULL; Ecore_Timer* g_recording_timer = NULL; Ecore_Timer* g_processing_timer = NULL; @@ -87,23 +92,23 @@ static void __cancel_recognition_internal() g_recording_timer = NULL; } + int ret = 0; int uid = 0; uid = stt_client_get_current_recognition(); - if (0 != uid) { - /* cancel engine recognition */ - int ret = sttd_engine_agent_recognize_cancel(uid); - if (0 != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret); - } + app_state_e state = 0; + ret = sttd_client_get_state(uid, &state); - /* change uid state */ - sttd_client_set_state(uid, APP_STATE_READY); - stt_client_unset_current_recognition(); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); + return; + } - ret = sttdc_send_set_state(uid, (int)APP_STATE_READY); + if (0 != uid && (APP_STATE_PROCESSING == state || APP_STATE_RECORDING == state)) { + /* cancel engine recognition */ + ret = sttd_server_cancel(uid); if (0 != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send state change : result(%d)", ret); + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret); } } else { SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid"); @@ -135,7 +140,7 @@ int __server_audio_recorder_callback(const void* data, const unsigned int length uid = stt_client_get_current_recognition(); if (0 != uid) { - ret = sttd_engine_agent_set_recording_data(uid, data, length); + ret = sttd_engine_agent_set_recording_data(data, length); if (ret < 0) { ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL); return -1; @@ -179,11 +184,11 @@ void __cancel_by_no_record(void *data) return; } -void __server_recognition_result_callback(sttp_result_event_e event, const char* type, +int __server_recognition_result_callback(stte_result_event_e event, const char* type, const char** data, int data_count, const char* msg, void *user_data) { // critical section - pthread_mutex_lock(&sttpe_result_mutex); + pthread_mutex_lock(&stte_result_mutex); SLOG(LOG_DEBUG, TAG_STTD, "===== RESULT event[%d] type[%s] data[%p] data_count[%d]", event, type, data, data_count); @@ -195,14 +200,14 @@ void __server_recognition_result_callback(sttp_result_event_e event, const char* SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); SLOG(LOG_DEBUG, TAG_STTD, "====="); SLOG(LOG_DEBUG, TAG_STTD, " "); - pthread_mutex_unlock(&sttpe_result_mutex); - return; + pthread_mutex_unlock(&stte_result_mutex); + return STTD_ERROR_OPERATION_FAILED; } SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid (%d), event(%d)", uid, event); /* send result to client */ - if (STTP_RESULT_EVENT_FINAL_RESULT == event) { + if (STTE_RESULT_EVENT_FINAL_RESULT == event) { if (APP_STATE_PROCESSING != state) { SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is NOT 'Processing'."); } @@ -217,6 +222,11 @@ void __server_recognition_result_callback(sttp_result_event_e event, const char* sttd_config_time_save(); sttd_config_time_reset(); + sttd_recorder_reset(); + + sttd_client_set_state(uid, APP_STATE_READY); + stt_client_unset_current_recognition(); + if (NULL == data || 0 == data_count) { if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result"); @@ -238,10 +248,10 @@ void __server_recognition_result_callback(sttp_result_event_e event, const char* } /* change state of uid */ - sttd_client_set_state(uid, APP_STATE_READY); - stt_client_unset_current_recognition(); +// sttd_client_set_state(uid, APP_STATE_READY); +// stt_client_unset_current_recognition(); - } else if (STTP_RESULT_EVENT_PARTIAL_RESULT == event) { + } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) { SLOG(LOG_DEBUG, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event, data_count); sttd_config_time_save(); @@ -256,7 +266,7 @@ void __server_recognition_result_callback(sttp_result_event_e event, const char* } } - } else if (STTP_RESULT_EVENT_ERROR == event) { + } else if (STTE_RESULT_EVENT_ERROR == event) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR"); /* Delete timer for processing time out */ @@ -266,6 +276,17 @@ void __server_recognition_result_callback(sttp_result_event_e event, const char* } sttd_config_time_reset(); + int ret = 0; + if (APP_STATE_RECORDING == state) { + ret = sttd_engine_agent_recognize_cancel(); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel: result(%d)", ret); + } + } + + sttd_client_set_state(uid, APP_STATE_READY); + stt_client_unset_current_recognition(); + if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result "); @@ -277,66 +298,61 @@ void __server_recognition_result_callback(sttp_result_event_e event, const char* } /* change state of uid */ - sttd_client_set_state(uid, APP_STATE_READY); - stt_client_unset_current_recognition(); +// sttd_client_set_state(uid, APP_STATE_READY); +// stt_client_unset_current_recognition(); } else { /* nothing */ } SLOG(LOG_DEBUG, TAG_STTD, "====="); SLOG(LOG_DEBUG, TAG_STTD, " "); - pthread_mutex_unlock(&sttpe_result_mutex); + pthread_mutex_unlock(&stte_result_mutex); - return; + return STTD_ERROR_NONE; } -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) +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) { - pthread_mutex_lock(&sttpe_result_time_mutex); + pthread_mutex_lock(&stte_result_time_mutex); - SLOG(LOG_DEBUG, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)", + SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)", index, event, text, start_time, end_time); - if (0 == index) { - int ret; - ret = sttd_config_time_add(index, (int)event, text, start_time, end_time); - if (0 != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info"); - pthread_mutex_unlock(&sttpe_result_time_mutex); - return false; - } - } else { - pthread_mutex_unlock(&sttpe_result_time_mutex); + int ret; + ret = sttd_config_time_add(index, (int)event, text, start_time, end_time); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info"); + pthread_mutex_unlock(&stte_result_time_mutex); return false; } - pthread_mutex_unlock(&sttpe_result_time_mutex); + pthread_mutex_unlock(&stte_result_time_mutex); return true; } -void __server_silence_dectection_callback(sttp_silence_type_e type, void *user_param) +int __server_speech_status_callback(stte_speech_status_e status, void *user_param) { - SLOG(LOG_DEBUG, TAG_STTD, "===== Silence Detection Callback"); + SLOG(LOG_DEBUG, TAG_STTD, "===== Speech status detected Callback"); int uid = stt_client_get_current_recognition(); if (0 != uid) { app_state_e state; if (0 != sttd_client_get_state(uid, &state)) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid "); - return; + return STTD_ERROR_OPERATION_FAILED; } - if (APP_STATE_RECORDING != state) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording"); - return; + if (APP_STATE_RECORDING != state && APP_STATE_PROCESSING != state) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording, state(%d), status(%d)", state, status); + return STTD_ERROR_INVALID_STATE; } - if (STTP_SILENCE_TYPE_NO_RECORD_TIMEOUT == type) { - SLOG(LOG_DEBUG, TAG_STTD, "Silence Detection type - No Record"); - ecore_main_loop_thread_safe_call_async(__cancel_by_no_record, NULL); - } else if (STTP_SILENCE_TYPE_END_OF_SPEECH_DETECTED == type) { - SLOG(LOG_DEBUG, TAG_STTD, "Silence Detection type - End of Speech"); + if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) { + SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected"); + sttdc_send_speech_status(uid, status); + } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) { + SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected"); ecore_main_loop_thread_safe_call_async(__stop_by_silence, NULL); } } else { @@ -346,10 +362,18 @@ void __server_silence_dectection_callback(sttp_silence_type_e type, void *user_p SLOG(LOG_DEBUG, TAG_STTD, "====="); SLOG(LOG_DEBUG, TAG_STTD, " "); - return; + return STTD_ERROR_NONE; +} + +int __server_error_callback(stte_error_e error, const char* msg) +{ + SLOG(LOG_DEBUG, TAG_STTD, "[Server] Error Callback is called"); + ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL); + + return STTD_ERROR_NONE; } -void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, void* user_data) +void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data) { if (NULL == engine_id) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL"); @@ -358,6 +382,7 @@ void __sttd_server_engine_changed_cb(const char* engine_id, const char* language SLOG(LOG_DEBUG, TAG_STTD, "[Server] New default engine : %s", engine_id); } +#if 0 /* need to change state of app to ready */ int uid; uid = stt_client_get_current_recognition(); @@ -385,6 +410,7 @@ void __sttd_server_engine_changed_cb(const char* engine_id, const char* language ret = sttd_engine_agent_set_silence_detection(support_silence); if (0 != ret) SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret); +#endif return; } @@ -420,17 +446,16 @@ void __sttd_server_silence_changed_cb(bool value, void* user_data) /* * Daemon function */ - -int sttd_initialize() +int sttd_initialize(stte_request_callback_s *callback) { int ret = 0; - if( 0 != pthread_mutex_init(&sttpe_result_mutex, NULL)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize sttpe result mutex."); + if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex."); } - if( 0 != pthread_mutex_init(&sttpe_result_time_mutex, NULL)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize sttpe sttpe_result_time_mutex."); + if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex."); } if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb, @@ -441,25 +466,27 @@ int sttd_initialize() ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret); + return ret; } /* Engine Agent initialize */ ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback, - __server_silence_dectection_callback); + __server_speech_status_callback, __server_error_callback); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret); return ret; } - /* Update engine list */ - ret = sttd_engine_agent_initialize_engine_list(); + /* load engine */ + ret = sttd_engine_agent_load_current_engine(callback); if (0 != ret) { - if (STTD_ERROR_ENGINE_NOT_FOUND == ret) { - SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] There is no stt engine"); - } else { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to update engine list : %d", ret); - return ret; - } + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine"); + return ret; + } + + g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, sttd_cleanup_client, NULL); + if (NULL == g_check_client_timer) { + SLOG(LOG_WARN, TAG_STTD, "[Main Warning] Fail to create timer of client check"); } SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] initialize"); @@ -469,12 +496,12 @@ int sttd_initialize() int sttd_finalize() { - if( 0 != pthread_mutex_destroy(&sttpe_result_mutex)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy sttpe result mutex."); + if (0 != pthread_mutex_destroy(&stte_result_mutex)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex."); } - if( 0 != pthread_mutex_destroy(&sttpe_result_time_mutex)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy sttpe_result_time_mutex."); + if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex."); } GList *iter = NULL; @@ -492,15 +519,20 @@ int sttd_finalize() sttd_engine_agent_release(); + if (NULL != g_check_client_timer) { + ecore_timer_del(g_check_client_timer); + g_check_client_timer = NULL; + + SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete ecore timer handle"); + } + return STTD_ERROR_NONE; } static void __read_proc() { DIR *dp = NULL; - struct dirent entry; struct dirent *dirp = NULL; - int ret = -1; int tmp; GList *iter = NULL; @@ -517,11 +549,7 @@ static void __read_proc() SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc"); } else { do { - ret = readdir_r(dp, &entry, &dirp); - if (0 != ret) { - SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to readdir"); - break; - } + dirp = readdir(dp); if (NULL != dirp) { tmp = atoi(dirp->d_name); @@ -551,10 +579,10 @@ Eina_Bool sttd_cleanup_client(void *data) if (NULL != client_list) { SLOG(LOG_DEBUG, TAG_STTD, "===== Clean up client "); - + __read_proc(); - for (i = 0;i < client_count;i++) { + for (i = 0; i < client_count; i++) { int pid = sttd_client_get_pid(client_list[i]); if (0 > pid) { SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid"); @@ -603,17 +631,9 @@ Eina_Bool sttd_cleanup_client(void *data) * STT Server Functions for Client */ -int sttd_server_initialize(int pid, int uid, bool* silence) +int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential) { - if (false == sttd_engine_agent_is_default_engine()) { - /* Update installed engine */ - sttd_engine_agent_initialize_engine_list(); - - if (false == sttd_engine_agent_is_default_engine()) { - SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] No stt-engine"); - return STTD_ERROR_ENGINE_NOT_FOUND; - } - } + int ret = STTD_ERROR_NONE; /* check if uid is valid */ app_state_e state; @@ -622,21 +642,23 @@ int sttd_server_initialize(int pid, int uid, bool* silence) return STTD_ERROR_NONE; } - /* load engine */ - if (0 != sttd_engine_agent_load_current_engine(uid, NULL)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine"); - return STTD_ERROR_OPERATION_FAILED; + ret = sttd_engine_agent_get_option_supported(silence); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported"); + return ret; } - if (0 != sttd_engine_agent_get_option_supported(uid, silence)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported"); - return STTD_ERROR_OPERATION_FAILED; + ret = sttd_engine_agent_is_credential_needed(uid, credential); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity"); + return ret; } /* Add client information to client manager */ - if (0 != sttd_client_add(pid, uid)) { + ret = sttd_client_add(pid, uid); + if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info"); - return STTD_ERROR_OPERATION_FAILED; + return ret; } return STTD_ERROR_NONE; @@ -644,7 +666,15 @@ int sttd_server_initialize(int pid, int uid, bool* silence) static Eina_Bool __quit_ecore_loop(void *data) { + SLOG(LOG_DEBUG, TAG_STTD, "[Server] Quit"); + + stt_network_finalize(); + sttd_finalize(); + sttd_dbus_close_connection(); ecore_main_loop_quit(); + + SLOG(LOG_DEBUG, TAG_STTD, ""); + return EINA_FALSE; } @@ -680,10 +710,6 @@ int sttd_server_finalize(int uid) stt_client_unset_current_recognition(); } - if (0 != sttd_engine_agent_unload_current_engine(uid)) { - SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unload engine"); - } - /* Remove client information */ if (0 != sttd_client_delete(uid)) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client"); @@ -691,7 +717,7 @@ int sttd_server_finalize(int uid) /* unload engine, if ref count of client is 0 */ if (0 == sttd_client_get_ref_count()) { - sttd_dbus_close_connection(); +// sttd_dbus_close_connection(); ecore_timer_add(0, __quit_ecore_loop, NULL); } @@ -717,13 +743,13 @@ int sttd_server_get_supported_engines(int uid, GSList** engine_list) ret = sttd_engine_agent_get_engine_list(engine_list); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list"); - return STTD_ERROR_OPERATION_FAILED; + return ret; } return STTD_ERROR_NONE; } -int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence) +int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence, bool* credential) { /* Check if uid is valid */ app_state_e state; @@ -739,16 +765,21 @@ int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence } int ret; - ret = sttd_engine_agent_load_current_engine(uid, engine_id); + ret = sttd_engine_agent_load_current_engine(NULL); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } - ret = sttd_engine_agent_get_option_supported(uid, silence); + ret = sttd_engine_agent_get_option_supported(silence); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; + } + + if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity"); + return ret; } return STTD_ERROR_NONE; @@ -770,10 +801,10 @@ int sttd_server_get_current_engine(int uid, char** engine_id) } int ret; - ret = sttd_engine_agent_get_current_engine(uid, engine_id); + ret = sttd_engine_agent_get_current_engine(engine_id); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } return STTD_ERROR_NONE; @@ -797,10 +828,10 @@ int sttd_server_check_app_agreed(int uid, const char* appid, bool* available) /* Ask engine available */ int ret; bool temp = false; - ret = sttd_engine_agent_check_app_agreed(uid, appid, &temp); + ret = sttd_engine_agent_check_app_agreed(appid, &temp); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } if (true == temp) { @@ -824,10 +855,10 @@ int sttd_server_get_supported_languages(int uid, GSList** lang_list) } /* get language list from engine */ - int ret = sttd_engine_agent_supported_langs(uid, lang_list); + int ret = sttd_engine_agent_supported_langs(lang_list); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages"); - return STTD_ERROR_OPERATION_FAILED; + return ret; } SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages"); @@ -850,10 +881,10 @@ int sttd_server_get_current_langauage(int uid, char** current_lang) } /*get current language from engine */ - int ret = sttd_engine_agent_get_default_lang(uid, current_lang); + int ret = sttd_engine_agent_get_default_lang(current_lang); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get default language"); @@ -861,6 +892,60 @@ int sttd_server_get_current_langauage(int uid, char** current_lang) return STTD_ERROR_NONE; } +int sttd_server_set_private_data(int uid, const char* key, const char* data) +{ + /* check if uid is valid */ + app_state_e state; + if (0 != sttd_client_get_state(uid, &state)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); + return STTD_ERROR_INVALID_PARAMETER; + } + + if (NULL == key || NULL == data) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); + return STTD_ERROR_INVALID_PARAMETER; + } + + /* set private data to engine */ + int ret = -1; + ret = sttd_engine_agent_set_private_data(key, data); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret); + return ret; + } + + SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Set private data"); + + return STTD_ERROR_NONE; +} + +int sttd_server_get_private_data(int uid, const char* key, char** data) +{ + /* check if uid is valid */ + app_state_e state; + if (0 != sttd_client_get_state(uid, &state)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); + return STTD_ERROR_INVALID_PARAMETER; + } + + if (NULL == key || NULL == data) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); + return STTD_ERROR_INVALID_PARAMETER; + } + + /* get private data to engine */ + int ret = -1; + ret = sttd_engine_agent_get_private_data(key, data); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret); + return ret; + } + + SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data); + + return STTD_ERROR_NONE; +} + int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support) { /* check if uid is valid */ @@ -876,10 +961,10 @@ int sttd_server_is_recognition_type_supported(int uid, const char* type, int* su } bool temp; - int ret = sttd_engine_agent_is_recognition_type_supported(uid, type, &temp); + int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } *support = (int)temp; @@ -901,7 +986,7 @@ int sttd_server_set_start_sound(int uid, const char* file) int ret = sttd_client_set_start_sound(uid, file); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } return 0; @@ -919,7 +1004,7 @@ int sttd_server_set_stop_sound(int uid, const char* file) int ret = sttd_client_set_stop_sound(uid, file); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } return 0; @@ -943,16 +1028,16 @@ Eina_Bool __check_recording_state(void *data) if (APP_STATE_READY == state) { /* Cancel stt */ - SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The daemon should cancel recording", uid); + SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The STT service should cancel recording", uid); sttd_server_cancel(uid); } else if (APP_STATE_PROCESSING == state) { /* Cancel stt and send change state */ - SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The daemon should cancel recording", uid); + SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The STT service should cancel recording", uid); sttd_server_cancel(uid); sttdc_send_set_state(uid, (int)APP_STATE_READY); } else { /* Normal state */ - SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of daemon and client are identical"); + SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical"); return EINA_TRUE; } @@ -984,7 +1069,8 @@ Eina_Bool __stop_by_recording_timeout(void *data) void __sttd_server_recorder_start(void* data) { - int uid = (int)data; + intptr_t puid = (intptr_t)data; + int uid = (int)puid; int current_uid = stt_client_get_current_recognition(); if (uid != current_uid) { @@ -1016,7 +1102,7 @@ void __sttd_start_sound_completed_cb(int id, void *user_data) return; } -int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid) +int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential) { if (NULL == lang || NULL == recognition_type) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); @@ -1039,10 +1125,10 @@ int sttd_server_start(int uid, const char* lang, const char* recognition_type, i int ret = 0; if (false == stt_client_get_app_agreed(uid)) { bool temp = false; - ret = sttd_engine_agent_check_app_agreed(uid, appid, &temp); + ret = sttd_engine_agent_check_app_agreed(appid, &temp); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } if (false == temp) { @@ -1054,7 +1140,7 @@ int sttd_server_start(int uid, const char* lang, const char* recognition_type, i } /* check if engine use network */ - if (true == sttd_engine_agent_need_network(uid)) { + if (true == sttd_engine_agent_need_network()) { if (false == stt_network_is_connected()) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection."); return STTD_ERROR_OUT_OF_NETWORK; @@ -1062,9 +1148,10 @@ int sttd_server_start(int uid, const char* lang, const char* recognition_type, i } char* sound = NULL; - if (0 != sttd_client_get_start_sound(uid, &sound)) { + ret = sttd_client_get_start_sound(uid, &sound); + if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound"); - return STTD_ERROR_OPERATION_FAILED; + return ret; } if (0 != stt_client_set_current_recognition(uid)) { @@ -1093,7 +1180,8 @@ int sttd_server_start(int uid, const char* lang, const char* recognition_type, i /* 2. Request wav play */ if (NULL != sound) { int id = 0; - ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_start_sound_completed_cb, (void*)uid, &id); + intptr_t puid = (intptr_t)uid; + ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_start_sound_completed_cb, (void*)puid, &id); if (WAV_PLAYER_ERROR_NONE != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav"); is_sound_done = true; @@ -1105,7 +1193,7 @@ int sttd_server_start(int uid, const char* lang, const char* recognition_type, i } /* 3. Create recorder & engine initialize */ - ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, NULL); + ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL); if (0 != ret) { stt_client_unset_current_recognition(); sttd_recorder_unset_audio_session(); @@ -1113,7 +1201,7 @@ int sttd_server_start(int uid, const char* lang, const char* recognition_type, i return ret; } - if (0 != strcmp(STTP_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) { + if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) { g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL); } @@ -1130,12 +1218,12 @@ int sttd_server_start(int uid, const char* lang, const char* recognition_type, i stt_client_unset_current_recognition(); sttd_recorder_unset_audio_session(); - sttd_engine_agent_recognize_cancel(uid); + sttd_engine_agent_recognize_cancel(); ecore_timer_del(g_recording_timer); sttd_client_set_state(uid, APP_STATE_READY); SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } /* Notify uid state change */ @@ -1159,12 +1247,12 @@ Eina_Bool __time_out_for_processing(void *data) if (0 == uid) return EINA_FALSE; /* Cancel engine */ - int ret = sttd_engine_agent_recognize_cancel(uid); + int ret = sttd_engine_agent_recognize_cancel(); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret); } - if (0 != sttdc_send_result(uid, STTP_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) { + if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result "); /* send error msg */ @@ -1184,7 +1272,8 @@ Eina_Bool __time_out_for_processing(void *data) void __sttd_server_engine_stop(void* data) { - int uid = (int)data; + intptr_t puid = (intptr_t)data; + int uid = (int)puid; /* change uid state */ sttd_client_set_state(uid, APP_STATE_PROCESSING); @@ -1199,7 +1288,7 @@ void __sttd_server_engine_stop(void* data) } /* Stop engine */ - ret = sttd_engine_agent_recognize_stop_engine(uid); + ret = sttd_engine_agent_recognize_stop_engine(); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret); return; @@ -1250,20 +1339,21 @@ int sttd_server_stop(int uid) int ret; /* 1. Stop recorder */ - ret = sttd_engine_agent_recognize_stop_recorder(uid); + ret = sttd_engine_agent_recognize_stop_recorder(); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret); - if (0 != sttd_engine_agent_recognize_cancel(uid)) { + if (0 != sttd_engine_agent_recognize_cancel()) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize"); } if (NULL != sound) free(sound); - return STTD_ERROR_OPERATION_FAILED; + return ret; } /* 2. Request wav play */ if (NULL != sound) { int id = 0; - ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_stop_sound_completed_cb, (void*)uid, &id); + intptr_t puid = (intptr_t)uid; + ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_stop_sound_completed_cb, (void*)puid, &id); if (WAV_PLAYER_ERROR_NONE != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav"); } else { @@ -1282,15 +1372,15 @@ int sttd_server_stop(int uid) ret = sttd_recorder_unset_audio_session(); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } /* Stop engine */ - ret = sttd_engine_agent_recognize_stop_engine(uid); + ret = sttd_engine_agent_recognize_stop_engine(); if (0 != ret) { stt_client_unset_current_recognition(); SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } /* change uid state */ @@ -1341,7 +1431,7 @@ int sttd_server_cancel(int uid) int ret = sttd_recorder_unset_audio_session(); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; } } @@ -1349,10 +1439,179 @@ int sttd_server_cancel(int uid) sttd_client_set_state(uid, APP_STATE_READY); /* cancel engine recognition */ - int ret = sttd_engine_agent_recognize_cancel(uid); + int ret = sttd_engine_agent_recognize_cancel(); if (0 != ret) { SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret); - return STTD_ERROR_OPERATION_FAILED; + return ret; + } + + /* Notify uid state change */ + sttdc_send_set_state(uid, APP_STATE_READY); + + return STTD_ERROR_NONE; +} + +int sttd_server_start_file(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential, + const char* filepath, stte_audio_type_e audio_type, int sample_rate) +{ + if (NULL == lang || NULL == recognition_type || NULL == filepath) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); + return STTD_ERROR_INVALID_PARAMETER; + } + + /* check if uid is valid */ + app_state_e state; + if (0 != sttd_client_get_state(uid, &state)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); + return STTD_ERROR_INVALID_PARAMETER; + } + + /* check uid state */ + if (APP_STATE_READY != state) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready"); + return STTD_ERROR_INVALID_STATE; + } + + int ret = 0; + if (false == stt_client_get_app_agreed(uid)) { + bool temp = false; + ret = sttd_engine_agent_check_app_agreed(appid, &temp); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret); + return ret; + } + + if (false == temp) { + SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid); + return STTD_ERROR_PERMISSION_DENIED; + } + + stt_client_set_app_agreed(uid); + } + + /* check if engine use network */ + if (true == sttd_engine_agent_need_network()) { + if (false == stt_network_is_connected()) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection."); + return STTD_ERROR_OUT_OF_NETWORK; + } + } + + if (0 != stt_client_set_current_recognition(uid)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing"); + return STTD_ERROR_RECORDER_BUSY; + } + + /* engine start recognition */ + SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s), appid(%s), file(%s), audio_type(%d), sample_rate(%d)", uid, lang, recognition_type, appid, filepath, audio_type, sample_rate); + + /* 1. Set audio session */ + ret = sttd_recorder_set_audio_session(); + if (0 != ret) { + stt_client_unset_current_recognition(); + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret); + return ret; + } + + /* 2. Start engine to recognize */ + ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL); + if (0 != ret) { + stt_client_unset_current_recognition(); + sttd_recorder_unset_audio_session(); + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start engine : result(%d)", ret); + return ret; + } + + sttd_client_set_state(uid, APP_STATE_RECORDING); + sttdc_send_set_state(uid, APP_STATE_RECORDING); + + /* 3. Start to send pcm from file to engine */ + ret = sttd_engine_agent_recognize_start_file(uid, filepath); + if (0 != ret) { + stt_client_unset_current_recognition(); + sttd_recorder_unset_audio_session(); + sttd_engine_agent_recognize_cancel(); + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start file : result(%d)", ret); + return ret; + } + + /* 4. Stop to send pcm from file */ + ret = sttd_engine_agent_recognize_stop_file(); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret); + stt_client_unset_current_recognition(); + sttd_recorder_unset_audio_session(); + sttd_engine_agent_recognize_cancel(); + return ret; + } + + /* 5. change & notify uid state */ + sttd_client_set_state(uid, APP_STATE_PROCESSING); + sttdc_send_set_state(uid, APP_STATE_PROCESSING); + + /* 6. Unset audio session */ + ret = sttd_recorder_unset_audio_session(); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret); + stt_client_unset_current_recognition(); + return ret; + } + + /* 7. Stop engine */ + ret = sttd_engine_agent_recognize_stop_engine(); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret); + stt_client_unset_current_recognition(); + return ret; + } + + return STTD_ERROR_NONE; +} + +int sttd_server_cancel_file(int uid) +{ + /* check if uid is valid */ + app_state_e state; + if (0 != sttd_client_get_state(uid, &state)) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); + return STTD_ERROR_INVALID_PARAMETER; + } + + /* check uid state */ + if (APP_STATE_READY == state) { + SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready"); + return STTD_ERROR_NONE; + } + + stt_client_unset_current_recognition(); + + if (NULL != g_recording_timer) { + ecore_timer_del(g_recording_timer); + g_recording_timer = NULL; + } + + if (NULL != g_processing_timer) { + ecore_timer_del(g_processing_timer); + g_processing_timer = NULL; + } + + if (APP_STATE_RECORDING == state) { + /* Unset audio session */ + int ret = sttd_recorder_unset_audio_session(); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret); + return ret; + } + } + + /* change uid state */ + sttd_client_set_state(uid, APP_STATE_READY); + + /* cancel engine recognition */ + int ret = sttd_engine_agent_recognize_cancel(); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret); + return ret; } /* Notify uid state change */