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 <cynara-client.h>
16 #include <cynara-error.h>
17 #include <cynara-session.h>
23 #include <sys/types.h>
25 #include <system_info.h>
28 #include <sound_manager.h>
29 #include <sound_manager_internal.h>
32 #include "stt_client.h"
34 #include "stt_config_mgr.h"
35 #include "stt_internal.h"
40 static void __stt_notify_state_changed(void *data);
41 static void __stt_notify_error(void *data);
43 static Ecore_Timer* g_connect_timer = NULL;
44 static float g_volume_db = 0;
46 static int g_feature_enabled = -1;
48 static pthread_mutex_t g_cynara_mutex = PTHREAD_MUTEX_INITIALIZER;
50 static cynara *p_cynara = NULL;
52 static bool g_err_callback_status = false;
54 /* for changing volume on each sound stream */
55 static sound_stream_ducking_h g_media_stream_ducking;
56 static sound_stream_ducking_h g_system_stream_ducking;
57 static sound_stream_ducking_h g_notification_stream_ducking;
58 static sound_stream_ducking_h g_alarm_stream_ducking;
60 #define STT_BG_VOLUME_RATIO_FARFIELD 0.0
61 #define STT_BG_VOLUME_RATIO_NEARFIELD 0.7
62 #define SND_MGR_DUCKING_DURATION 500
65 static int __stt_get_feature_enabled()
67 if (0 == g_feature_enabled) {
69 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] STT NOT supported");
70 return STT_ERROR_NOT_SUPPORTED;
72 } else if (-1 == g_feature_enabled) {
73 bool stt_supported = false;
74 bool mic_supported = false;
76 if (SYSTEM_INFO_ERROR_NONE != system_info_get_platform_bool(STT_FEATURE_PATH, &stt_supported)) {
78 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get feature value");
79 return STT_ERROR_NOT_SUPPORTED;
83 if (SYSTEM_INFO_ERROR_NONE != system_info_get_platform_bool(STT_MIC_FEATURE_PATH, &mic_supported)) {
85 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get feature value");
86 return STT_ERROR_NOT_SUPPORTED;
90 if (false == stt_supported || false == mic_supported) {
92 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] STT NOT supported");
93 g_feature_enabled = 0;
94 return STT_ERROR_NOT_SUPPORTED;
98 g_feature_enabled = 1;
101 return STT_ERROR_NONE;
104 static int __check_privilege_initialize()
106 int ret = cynara_initialize(&p_cynara, NULL);
107 if (CYNARA_API_SUCCESS != ret)
108 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to initialize"); //LCOV_EXCL_LINE
110 return ret == CYNARA_API_SUCCESS;
113 static bool __check_privilege(const char* uid, const char * privilege)
116 char label_path[1024] = "/proc/self/attr/current";
117 char smack_label[1024] = {'\0',};
123 fp = fopen(label_path, "r");
125 if (0 >= fread(smack_label, 1, sizeof(smack_label), fp))
126 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to fread"); //LCOV_EXCL_LINE
131 pid_t pid = getpid();
132 char *session = cynara_session_from_pid(pid);
133 int ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
137 if (ret != CYNARA_API_ACCESS_ALLOWED) {
138 SLOG(LOG_DEBUG, TAG_STTC, "[Client]cynara_check returned %d(Denied)", ret);
144 static void __check_privilege_deinitialize()
147 cynara_finish(p_cynara);
151 static int __stt_check_privilege()
153 pthread_mutex_lock(&g_cynara_mutex);
156 ret = __check_privilege_initialize();
158 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] privilege initialize is failed"); //LCOV_EXCL_LINE
159 pthread_mutex_unlock(&g_cynara_mutex);
160 return STT_ERROR_PERMISSION_DENIED;
164 snprintf(uid, 16, "%d", getuid());
166 ret = __check_privilege(uid, STT_PRIVILEGE_RECORDER);
167 __check_privilege_deinitialize();
169 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied"); //LCOV_EXCL_LINE
170 pthread_mutex_unlock(&g_cynara_mutex);
171 return STT_ERROR_PERMISSION_DENIED;
174 pthread_mutex_unlock(&g_cynara_mutex);
175 return STT_ERROR_NONE;
178 static int __stt_check_privilege_for_applaunch()
180 pthread_mutex_lock(&g_cynara_mutex);
183 ret = __check_privilege_initialize();
185 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] privilege initialize is failed (applaunch)"); //LCOV_EXCL_LINE
186 pthread_mutex_unlock(&g_cynara_mutex);
187 return STT_ERROR_PERMISSION_DENIED;
191 snprintf(uid, 16, "%d", getuid());
193 ret = __check_privilege(uid, STT_PRIVILEGE_APPLAUNCH);
194 __check_privilege_deinitialize();
196 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied : appmanager.launch"); //LCOV_EXCL_LINE
197 pthread_mutex_unlock(&g_cynara_mutex);
198 return STT_ERROR_PERMISSION_DENIED;
201 pthread_mutex_unlock(&g_cynara_mutex);
202 return STT_ERROR_NONE;
206 static const char* __stt_get_error_code(stt_error_e err)
209 case STT_ERROR_NONE: return "STT_ERROR_NONE";
210 case STT_ERROR_OUT_OF_MEMORY: return "STT_ERROR_OUT_OF_MEMORY";
211 case STT_ERROR_IO_ERROR: return "STT_ERROR_IO_ERROR";
212 case STT_ERROR_INVALID_PARAMETER: return "STT_ERROR_INVALID_PARAMETER";
213 case STT_ERROR_TIMED_OUT: return "STT_ERROR_TIMED_OUT";
214 case STT_ERROR_RECORDER_BUSY: return "STT_ERROR_RECORDER_BUSY";
215 case STT_ERROR_OUT_OF_NETWORK: return "STT_ERROR_OUT_OF_NETWORK";
216 case STT_ERROR_PERMISSION_DENIED: return "STT_ERROR_PERMISSION_DENIED";
217 case STT_ERROR_NOT_SUPPORTED: return "STT_ERROR_NOT_SUPPORTED";
218 case STT_ERROR_INVALID_STATE: return "STT_ERROR_INVALID_STATE";
219 case STT_ERROR_INVALID_LANGUAGE: return "STT_ERROR_INVALID_LANGUAGE";
220 case STT_ERROR_ENGINE_NOT_FOUND: return "STT_ERROR_ENGINE_NOT_FOUND";
221 case STT_ERROR_OPERATION_FAILED: return "STT_ERROR_OPERATION_FAILED";
222 case STT_ERROR_NOT_SUPPORTED_FEATURE: return "STT_ERROR_NOT_SUPPORTED_FEATURE";
223 case STT_ERROR_SERVICE_RESET: return "STT_ERROR_SERVICE_RESET";
225 return "Invalid error code";
229 static int __stt_convert_config_error_code(stt_config_error_e code)
231 if (code == STT_CONFIG_ERROR_NONE) return STT_ERROR_NONE;
232 if (code == STT_CONFIG_ERROR_OUT_OF_MEMORY) return STT_ERROR_OUT_OF_MEMORY;
233 if (code == STT_CONFIG_ERROR_IO_ERROR) return STT_ERROR_IO_ERROR;
234 if (code == STT_CONFIG_ERROR_INVALID_PARAMETER) return STT_ERROR_INVALID_PARAMETER;
235 if (code == STT_CONFIG_ERROR_PERMISSION_DENIED) return STT_ERROR_PERMISSION_DENIED;
236 if (code == STT_CONFIG_ERROR_NOT_SUPPORTED) return STT_ERROR_NOT_SUPPORTED;
237 if (code == STT_CONFIG_ERROR_INVALID_STATE) return STT_ERROR_INVALID_STATE;
238 if (code == STT_CONFIG_ERROR_INVALID_LANGUAGE) return STT_ERROR_INVALID_LANGUAGE;
239 if (code == STT_CONFIG_ERROR_ENGINE_NOT_FOUND) return STT_ERROR_ENGINE_NOT_FOUND;
240 if (code == STT_CONFIG_ERROR_OPERATION_FAILED) return STT_ERROR_OPERATION_FAILED;
245 void __stt_config_lang_changed_cb(const char* before_language, const char* current_language, void* user_data)
247 SLOG(LOG_DEBUG, TAG_STTC, "Language changed : Before lang(%s) Current lang(%s)",
248 before_language, current_language);
250 if (0 == strcmp(before_language, current_language)) {
254 GList* client_list = NULL;
255 client_list = stt_client_get_client_list();
258 stt_client_s *data = NULL;
260 if (g_list_length(client_list) > 0) {
261 /* Get a first item */
262 iter = g_list_first(client_list);
264 while (NULL != iter) {
266 if (NULL != data->default_lang_changed_cb) {
267 SLOG(LOG_DEBUG, TAG_STTC, "Call default language changed callback : uid(%u)", data->uid);
268 data->default_lang_changed_cb(data->stt, before_language, current_language,
269 data->default_lang_changed_user_data);
273 iter = g_list_next(iter);
280 static Eina_Bool __reconnect_by_engine_changed(void *data)
282 stt_h stt = (stt_h)data;
284 stt_client_s* client = stt_client_get(stt);
285 if (NULL == client) {
286 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
290 if (STT_STATE_READY != client->current_state) {
295 int ret = stt_unprepare(stt);
297 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
299 ret = stt_prepare(stt);
301 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
307 void __stt_config_engine_changed_cb(const char* engine_id, const char* setting, const char* language, bool support_silence, bool need_credential, void* user_data)
309 stt_h stt = (stt_h)user_data;
311 stt_client_s* client = stt_client_get(stt);
312 if (NULL == client) {
313 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
317 if (NULL != engine_id) SLOG(LOG_DEBUG, TAG_STTC, "Engine id(%s)", engine_id);
318 if (NULL != setting) SLOG(LOG_DEBUG, TAG_STTC, "Engine setting(%s)", setting);
319 if (NULL != language) SLOG(LOG_DEBUG, TAG_STTC, "Language(%s)", language);
320 SLOG(LOG_DEBUG, TAG_STTC, "Silence(%s), Credential(%s)", support_silence ? "on" : "off", need_credential ? "need" : "no need");
322 /* When the default engine is changed, please unload the old engine and load the new one. */
325 if (NULL == client->current_engine_id) {
326 if (STT_STATE_RECORDING == client->current_state || STT_STATE_PROCESSING == client->current_state) {
327 SLOG(LOG_INFO, TAG_STTC, "[INFO] stt cancel is invoked by engine_changed_cb, state(%d)", client->current_state);
328 ret = stt_cancel(stt);
330 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] STT client cancelling...");
333 ecore_idler_add(__reconnect_by_engine_changed, (void*)stt);
334 } else if (STT_STATE_READY == client->current_state) {
335 ret = stt_unprepare(stt);
337 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
339 ret = stt_prepare(stt);
341 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
346 /* call callback function */
347 if (NULL != client->engine_changed_cb) {
348 client->engine_changed_cb(stt, engine_id, language, support_silence, need_credential, client->engine_changed_user_data);
350 SLOG(LOG_WARN, TAG_STTC, "No registered callback function for engine change");
355 static const char* __stt_get_state_string(stt_state_e state)
358 case STT_STATE_CREATED: return "STT_STATE_CREATED";
359 case STT_STATE_READY: return "STT_STATE_READY";
360 case STT_STATE_RECORDING: return "STT_STATE_RECORDING";
361 case STT_STATE_PROCESSING: return "STT_STATE_PROCESSING";
363 return "Invalid State";
368 static int __stt_check_handle(stt_h stt, stt_client_s** client)
370 RETVM_IF(NULL == stt, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input handle is null");
372 stt_client_s* temp = NULL;
373 temp = stt_client_get(stt);
376 RETVM_IF(NULL == temp, STT_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not available");
379 return STT_ERROR_NONE;
382 static int __stt_check_precondition(stt_h stt, stt_client_s** client)
384 RETV_IF(0 != __stt_get_feature_enabled(), STT_ERROR_NOT_SUPPORTED);
385 RETV_IF(0 != __stt_check_privilege(), STT_ERROR_PERMISSION_DENIED);
386 RETV_IF(0 != __stt_check_handle(stt, client), STT_ERROR_INVALID_PARAMETER);
387 return STT_ERROR_NONE;
390 static int __stt_check_precondition_with_state(stt_h stt, stt_client_s** client, stt_state_e state)
392 int ret = __stt_check_precondition(stt, client);
393 if (STT_ERROR_NONE != ret)
395 const char* state_string = __stt_get_state_string(state);
396 RETVM_IF(state != (*client)->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not %s", (*client)->current_state, state_string);
397 return STT_ERROR_NONE;
400 int stt_create(stt_h* stt)
402 RETV_IF(0 != __stt_get_feature_enabled(), STT_ERROR_NOT_SUPPORTED);
403 RETV_IF(0 != __stt_check_privilege(), STT_ERROR_PERMISSION_DENIED);
405 SLOG(LOG_INFO, TAG_STTC, "===== Create STT");
406 RETVM_IF(NULL == stt, STT_ERROR_INVALID_PARAMETER, "[ERROR] A handle is null");
408 if (0 == stt_client_get_size()) {
409 if (0 != stt_dbus_open_connection()) {
411 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to open connection");
412 return STT_ERROR_OPERATION_FAILED;
417 stt_h new_stt = NULL;
418 if (STT_ERROR_NONE != stt_client_new(&new_stt)) {
420 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create client!");
421 return STT_ERROR_OUT_OF_MEMORY;
425 stt_client_s* client = stt_client_get(new_stt);
426 if (NULL == client) {
427 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create client"); //LCOV_EXCL_LINE
428 stt_client_destroy(new_stt);
429 return STT_ERROR_OPERATION_FAILED;
432 int ret = stt_config_mgr_initialize(client->uid);
433 ret = __stt_convert_config_error_code(ret);
435 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to init config manager : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
436 stt_client_destroy(new_stt);
440 ret = stt_config_mgr_set_callback(client->uid, __stt_config_engine_changed_cb, __stt_config_lang_changed_cb, NULL, client->stt);
441 ret = __stt_convert_config_error_code(ret);
443 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set config changed : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
444 stt_client_destroy(new_stt);
449 SLOG(LOG_INFO, TAG_STTC, "[Success] uid(%u)", client->uid);
450 SLOG(LOG_DEBUG, TAG_STTC, "=====");
451 return STT_ERROR_NONE;
454 int stt_destroy(stt_h stt)
456 stt_client_s* client = NULL;
457 int temp = __stt_check_precondition(stt, &client);
458 if (STT_ERROR_NONE != temp)
460 SLOG(LOG_INFO, TAG_STTC, "===== Destroy STT");
462 /* check used callback */
463 if (0 != stt_client_get_use_callback(client)) {
464 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Cannot destroy in Callback function");
465 return STT_ERROR_OPERATION_FAILED;
468 stt_config_mgr_finalize(client->uid);
473 switch (client->current_state) {
474 case STT_STATE_PROCESSING:
475 case STT_STATE_RECORDING:
476 case STT_STATE_READY:
477 ret = stt_dbus_request_finalize(client->uid);
479 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request finalize : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
481 case STT_STATE_CREATED:
482 if (NULL != g_connect_timer) {
483 SLOG(LOG_DEBUG, TAG_STTC, "Connect Timer is deleted");
484 ecore_timer_del(g_connect_timer);
485 g_connect_timer = NULL;
489 stt_client_destroy(stt);
495 if (0 == stt_client_get_size()) {
496 if (0 != stt_dbus_close_connection()) {
497 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to close connection"); //LCOV_EXCL_LINE
503 SLOG(LOG_INFO, TAG_STTC, "=====");
504 SLOG(LOG_DEBUG, TAG_STTC, " ");
506 return STT_ERROR_NONE;
509 static bool __stt_config_supported_engine_cb(const char* engine_id, const char* engine_name,
510 const char* setting, bool support_silence, void* user_data)
512 stt_h stt = (stt_h)user_data;
514 stt_client_s* client = stt_client_get(stt);
515 if (NULL == client) {
517 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
522 /* call callback function */
523 if (NULL != client->supported_engine_cb) {
524 return client->supported_engine_cb(stt, engine_id, engine_name, client->supported_engine_user_data);
526 SLOG(LOG_WARN, TAG_STTC, "No registered callback function of supported engine"); //LCOV_EXCL_LINE
532 int stt_foreach_supported_engines(stt_h stt, stt_supported_engine_cb callback, void* user_data)
534 stt_client_s* client = NULL;
535 int temp = __stt_check_precondition(stt, &client);
536 if (STT_ERROR_NONE != temp)
538 RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
539 RETVM_IF(client->current_state != STT_STATE_CREATED, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
541 SLOG(LOG_INFO, TAG_STTC, "===== Foreach Supported engine");
543 client->supported_engine_cb = callback;
544 client->supported_engine_user_data = user_data;
547 ret = stt_config_mgr_get_engine_list(__stt_config_supported_engine_cb, client->stt);
548 ret = __stt_convert_config_error_code(ret);
550 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get engines : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
553 client->supported_engine_cb = NULL;
554 client->supported_engine_user_data = NULL;
556 SLOG(LOG_INFO, TAG_STTC, "=====");
557 SLOG(LOG_DEBUG, TAG_STTC, " ");
562 int stt_get_engine(stt_h stt, char** engine_id)
564 stt_client_s* client = NULL;
565 int temp = __stt_check_precondition(stt, &client);
566 if (STT_ERROR_NONE != temp)
569 RETVM_IF(NULL == engine_id, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
570 RETVM_IF(client->current_state != STT_STATE_CREATED, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
572 SLOG(LOG_INFO, TAG_STTC, "===== Get current engine");
576 if (NULL != client->current_engine_id) {
577 *engine_id = strdup(client->current_engine_id);
578 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", *engine_id);
581 ret = stt_config_mgr_get_engine(engine_id);
582 ret = __stt_convert_config_error_code(ret);
584 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request get current engine : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
586 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", *engine_id);
590 SLOG(LOG_INFO, TAG_STTC, "=====");
591 SLOG(LOG_DEBUG, TAG_STTC, " ");
596 int __stt_set_buxtonkey(const char* engine_id)
599 struct buxton_client * bux_cli;
600 struct buxton_layer * bux_layer;
601 struct buxton_value * bux_val;
603 int ret = buxton_open(&bux_cli, NULL, NULL);
605 SLOG(LOG_ERROR, TAG_STTC, "[DBUS-BUXTON2] Fail to open buxton client"); //LCOV_EXCL_LINE
606 return STT_ERROR_OPERATION_FAILED;
608 SLOG(LOG_DEBUG, TAG_STTC, "[DBUS-BUXTON2] buxton_open: %d", ret);
609 bux_layer = buxton_create_layer("system");
610 if (NULL == bux_layer) {
612 SLOG(LOG_ERROR, TAG_STTC, "[DBUS-BUXTON2] buxton_create_layer FAIL");
613 buxton_close(bux_cli);
615 return STT_ERROR_OPERATION_FAILED;
618 bux_val = buxton_value_create_string(engine_id);
619 if (NULL == bux_val) {
621 SLOG(LOG_ERROR, TAG_STTC, "[DBUS-BUXTON2] buxton_value_create_string FAIL");
622 buxton_free_layer(bux_layer);
623 buxton_close(bux_cli);
626 return STT_ERROR_OPERATION_FAILED;
630 ret = buxton_set_value_sync(bux_cli, bux_layer, STT_ENGINE_DB_CUSTOM, bux_val);
633 SLOG(LOG_ERROR, TAG_STTC, "[DBUS-BUXTON2] Fail to set value sync");
634 buxton_value_free(bux_val);
635 buxton_free_layer(bux_layer);
636 buxton_close(bux_cli);
641 return STT_ERROR_OPERATION_FAILED;
644 SLOG(LOG_DEBUG, TAG_STTC, "[DBUS-BUXTON2] buxton_set_value_sync: %d, %s", ret, STT_ENGINE_DB_CUSTOM);
646 buxton_value_free(bux_val);
647 buxton_free_layer(bux_layer);
648 buxton_close(bux_cli);
654 return STT_ERROR_NONE;
657 int stt_set_engine(stt_h stt, const char* engine_id)
659 stt_client_s* client = NULL;
660 if (0 != __stt_get_feature_enabled()) {
661 return STT_ERROR_NOT_SUPPORTED;
663 if (0 != __stt_check_privilege()) {
664 return STT_ERROR_PERMISSION_DENIED;
666 if (0 != __stt_check_privilege_for_applaunch()) {
667 return STT_ERROR_PERMISSION_DENIED;
669 if (0 != __stt_check_handle(stt, &client)) {
670 return STT_ERROR_INVALID_PARAMETER;
673 SLOG(LOG_INFO, TAG_STTC, "===== Set current engine");
675 RETVM_IF(NULL == engine_id, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
676 RETVM_IF(client->current_state != STT_STATE_CREATED, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
678 SLOG(LOG_INFO, TAG_STTC, "===== engined_id(%s)", engine_id);
680 free(client->current_engine_id);
681 client->current_engine_id = strdup(engine_id);
684 int ret = __stt_set_buxtonkey(engine_id);
686 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] set buxtonkey Failed!!!"); //LCOV_EXCL_LINE
690 SLOG(LOG_INFO, TAG_STTC, "=====");
691 SLOG(LOG_DEBUG, TAG_STTC, " ");
696 int stt_set_credential(stt_h stt, const char* credential)
698 stt_client_s* client = NULL;
699 int temp = __stt_check_precondition(stt, &client);
700 if (STT_ERROR_NONE != temp)
703 SLOG(LOG_INFO, TAG_STTC, "===== Set credential");
705 RETVM_IF(NULL == credential, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
706 RETVM_IF(client->current_state != STT_STATE_CREATED && client->current_state != STT_STATE_READY, STT_ERROR_INVALID_STATE,
707 "[ERROR] Invalid State: Current state(%d) is not CREATED or READY", client->current_state);
709 free(client->credential);
710 client->credential = strdup(credential);
712 SLOG(LOG_INFO, TAG_STTC, "=====");
713 SLOG(LOG_DEBUG, TAG_STTC, " ");
715 return STT_ERROR_NONE;
719 static bool __stt_is_necessary_to_retry_dbus_request(int count, int return_value)
722 if (STT_ERROR_TIMED_OUT != return_value) {
725 SLOG(LOG_WARN, TAG_STTC, "[WARNING][%d] retry : %s", count, __stt_get_error_code(ret));
727 if (STT_RETRY_COUNT == count) {
728 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
736 int stt_set_private_data(stt_h stt, const char* key, const char* data)
738 stt_client_s* client = NULL;
739 int temp = __stt_check_precondition(stt, &client);
740 if (STT_ERROR_NONE != temp)
743 RETVM_IF(NULL == key || NULL == data, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter");
744 RETVM_IF(STT_STATE_READY != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
746 SLOG(LOG_INFO, TAG_STTC, "===== Set private data");
748 if (true != client->internal && (0 == strcmp(key, "server") || 0 == strcmp(key, "rampcode") || 0 == strcmp(key, "epd"))) {
749 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] This is not an internal app");
750 return STT_ERROR_INVALID_PARAMETER;
756 ret = stt_dbus_request_set_private_data(client->uid, key, data);
759 if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
760 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set private data : %s", __stt_get_error_code(ret));
768 SLOG(LOG_INFO, TAG_STTC, "=====");
769 SLOG(LOG_DEBUG, TAG_STTC, "");
771 return STT_ERROR_NONE;
774 int stt_get_private_data(stt_h stt, const char* key, char** data)
776 stt_client_s* client = NULL;
777 int temp = __stt_check_precondition(stt, &client);
778 if (STT_ERROR_NONE != temp)
781 RETVM_IF(NULL == key || NULL == data, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter");
782 RETVM_IF(STT_STATE_READY != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
784 SLOG(LOG_INFO, TAG_STTC, "===== Get private data");
789 ret = stt_dbus_request_get_private_data(client->uid, key, data);
792 if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
793 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get private data : %s", __stt_get_error_code(ret));
801 if (0 == strncmp(*data, "NULL", strlen(*data))) {
806 SLOG(LOG_INFO, TAG_STTC, "=====");
807 SLOG(LOG_DEBUG, TAG_STTC, "");
809 return STT_ERROR_NONE;
812 int stt_set_server_stt(stt_h stt, const char* key, char* user_data)
815 stt_client_s* client = NULL;
817 int temp = __stt_check_precondition(stt, &client);
818 if (STT_ERROR_NONE != temp)
821 SLOG(LOG_INFO, TAG_STTC, "===== Set STT server");
823 RETVM_IF(NULL == key || NULL == user_data, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter");
825 if (STT_STATE_READY != client->current_state) {
826 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] The current state is invalid (%d).", client->current_state);
827 return STT_ERROR_INVALID_STATE;
830 client->internal = true;
831 ret = stt_set_private_data(stt, key, user_data);
833 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set private data, ret(%d), key(%s)", ret, key);
837 SLOG(LOG_INFO, TAG_STTC, "======");
838 SLOG(LOG_DEBUG, TAG_STTC, " ");
843 static Eina_Bool __stt_connect_daemon(void *data)
845 stt_client_s* client = (stt_client_s*)data;
848 if (NULL == client) {
849 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] A handle is not available"); //LCOV_EXCL_LINE
850 g_connect_timer = NULL;
854 if (0 == stt_client_get_size() || NULL == stt_client_get_by_uid(client->uid)) {
855 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Client has been already destroyed"); //LCOV_EXCL_LINE
859 /* Check and Set vconfkey of custom engine before sending hello */
860 if (NULL != client->current_engine_id && 0 == __stt_check_privilege_for_applaunch()) {
862 ret = __stt_set_buxtonkey(client->current_engine_id);
865 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] set buxtonkey Failed!!! (inside __stt_connect_daemon)");
872 ret = stt_dbus_request_hello(client->uid);
874 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to send hello, ret(0x%x)", ret);
878 g_connect_timer = NULL;
879 SLOG(LOG_INFO, TAG_STTC, "===== Connect stt-service");
881 /* request initialization */
882 bool silence_supported = false;
883 bool credential_needed = false;
885 ret = stt_dbus_request_initialize(client->uid, &silence_supported, &credential_needed);
887 if (STT_ERROR_ENGINE_NOT_FOUND == ret) {
888 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to initialize : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
890 client->reason = STT_ERROR_ENGINE_NOT_FOUND; //LCOV_EXCL_LINE
891 ecore_main_loop_thread_safe_call_async(__stt_notify_error, (void*)client); //LCOV_EXCL_LINE
893 return EINA_FALSE; //LCOV_EXCL_LINE
894 } else if (STT_ERROR_NONE != ret) {
895 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] Fail to connection. Retry to connect"); //LCOV_EXCL_LINE
898 /* success to connect stt-service */
899 client->silence_supported = silence_supported;
900 client->credential_needed = credential_needed;
901 SLOG(LOG_DEBUG, TAG_STTC, "Supported options : silence(%s), credential(%s)", silence_supported ? "support" : "no support", credential_needed ? "need" : "no need");
904 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] uid(%u)", client->uid);
906 client->before_state = client->current_state;
907 client->current_state = STT_STATE_READY;
909 if (NULL != client->state_changed_cb) {
910 stt_client_use_callback(client);
911 client->state_changed_cb(client->stt, client->before_state,
912 client->current_state, client->state_changed_user_data);
913 stt_client_not_use_callback(client);
914 SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called");
916 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
919 SLOG(LOG_INFO, TAG_STTC, "=====");
920 SLOG(LOG_DEBUG, TAG_STTC, " ");
925 int stt_prepare(stt_h stt)
927 stt_client_s* client = NULL;
928 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
929 if (STT_ERROR_NONE != temp)
932 SLOG(LOG_INFO, TAG_STTC, "===== Prepare STT");
934 ecore_thread_main_loop_begin();
935 g_connect_timer = ecore_timer_add(0.02, __stt_connect_daemon, (void*)client);
936 ecore_thread_main_loop_end();
938 SLOG(LOG_INFO, TAG_STTC, "=====");
939 SLOG(LOG_DEBUG, TAG_STTC, " ");
941 return STT_ERROR_NONE;
944 int stt_unprepare(stt_h stt)
946 stt_client_s* client = NULL;
947 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
948 if (STT_ERROR_NONE != temp)
951 SLOG(LOG_INFO, TAG_STTC, "===== Unprepare STT");
956 ret = stt_dbus_request_finalize(client->uid);
959 if (STT_ERROR_TIMED_OUT != ret) {
960 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request finalize : %s", __stt_get_error_code(ret));
963 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
966 if (STT_RETRY_COUNT == count) {
967 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
975 client->internal_state = STT_INTERNAL_STATE_NONE;
977 client->before_state = client->current_state;
978 client->current_state = STT_STATE_CREATED;
980 if (NULL != client->state_changed_cb) {
981 stt_client_use_callback(client);
982 client->state_changed_cb(client->stt, client->before_state,
983 client->current_state, client->state_changed_user_data);
984 stt_client_not_use_callback(client);
986 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
989 if (g_connect_timer) {
990 ecore_timer_del(g_connect_timer);
991 g_connect_timer = NULL;
994 SLOG(LOG_INFO, TAG_STTC, "=====");
995 SLOG(LOG_DEBUG, TAG_STTC, " ");
997 return STT_ERROR_NONE;
1000 static bool __stt_config_supported_language_cb(const char* engine_id, const char* language, void* user_data)
1002 stt_h stt = (stt_h)user_data;
1004 stt_client_s* client = stt_client_get(stt);
1005 if (NULL == client) {
1006 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid"); //LCOV_EXCL_LINE
1010 SLOG(LOG_INFO, TAG_STTC, "===== supported language callback");
1012 /* call callback function */
1013 if (NULL != client->supported_lang_cb) {
1014 return client->supported_lang_cb(stt, language, client->supported_lang_user_data);
1016 SLOG(LOG_WARN, TAG_STTC, "No registered callback function of supported languages"); //LCOV_EXCL_LINE
1022 int stt_foreach_supported_languages(stt_h stt, stt_supported_language_cb callback, void* user_data)
1024 stt_client_s* client = NULL;
1025 int temp = __stt_check_precondition(stt, &client);
1026 if (STT_ERROR_NONE != temp)
1029 RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1030 SLOG(LOG_INFO, TAG_STTC, "===== Foreach Supported Language");
1033 char* current_engine_id = NULL;
1035 if (NULL == client->current_engine_id) {
1036 ret = stt_config_mgr_get_engine(¤t_engine_id);
1037 if (NULL == current_engine_id) {
1038 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory or Engine id is NULL");
1039 return STT_ERROR_OPERATION_FAILED;
1041 ret = __stt_convert_config_error_code(ret);
1043 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default engine id : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1044 free(current_engine_id);
1045 current_engine_id = NULL;
1049 current_engine_id = strdup(client->current_engine_id);
1051 if (NULL == current_engine_id) {
1052 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
1053 return STT_ERROR_OUT_OF_MEMORY;
1058 client->supported_lang_cb = callback;
1059 client->supported_lang_user_data = user_data;
1061 ret = stt_config_mgr_get_language_list(current_engine_id, __stt_config_supported_language_cb, client->stt);
1062 ret = __stt_convert_config_error_code(ret);
1064 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get languages : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1067 free(current_engine_id);
1068 current_engine_id = NULL;
1070 client->supported_lang_cb = NULL;
1071 client->supported_lang_user_data = NULL;
1073 SLOG(LOG_INFO, TAG_STTC, "=====");
1074 SLOG(LOG_DEBUG, TAG_STTC, " ");
1079 int stt_get_default_language(stt_h stt, char** language)
1081 stt_client_s* client = NULL;
1082 int temp = __stt_check_precondition(stt, &client);
1083 if (STT_ERROR_NONE != temp)
1086 RETVM_IF(NULL == language, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1087 SLOG(LOG_INFO, TAG_STTC, "===== Get Default Language");
1090 ret = stt_config_mgr_get_default_language(language);
1091 ret = __stt_convert_config_error_code(ret);
1093 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default language : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1095 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current language = %s", *language);
1098 SLOG(LOG_INFO, TAG_STTC, "=====");
1099 SLOG(LOG_DEBUG, TAG_STTC, " ");
1104 int stt_get_state(stt_h stt, stt_state_e* state)
1106 stt_client_s* client = NULL;
1107 int temp = __stt_check_precondition(stt, &client);
1108 if (STT_ERROR_NONE != temp)
1111 RETVM_IF(NULL == state, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1113 *state = client->current_state;
1115 SLOG(LOG_INFO, TAG_STTC, "===== Get state(%d)", *state);
1118 case STT_STATE_CREATED: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'CREATED'"); break;
1119 case STT_STATE_READY: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Ready'"); break;
1120 case STT_STATE_RECORDING: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Recording'"); break;
1121 case STT_STATE_PROCESSING: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Processing'"); break;
1122 default: SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid value"); break; //LCOV_EXCL_LINE
1125 return STT_ERROR_NONE;
1128 int stt_get_error_message(stt_h stt, char** err_msg)
1130 stt_client_s* client = NULL;
1131 int temp = __stt_check_precondition(stt, &client);
1132 if (STT_ERROR_NONE != temp)
1135 RETVM_IF(NULL == err_msg, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1136 RETVM_IF(false == g_err_callback_status, STT_ERROR_OPERATION_FAILED, "[ERROR] This callback should be called during an err_callback");
1138 if (NULL != client->err_msg) {
1139 *err_msg = strdup(client->err_msg);
1140 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Error msg (%s)", *err_msg);
1142 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Error msg (NULL)"); //LCOV_EXCL_LINE
1145 SLOG(LOG_INFO, TAG_STTC, "=====");
1146 SLOG(LOG_DEBUG, TAG_STTC, " ");
1148 return STT_ERROR_NONE;
1151 int stt_is_recognition_type_supported(stt_h stt, const char* type, bool* support)
1153 stt_client_s* client = NULL;
1154 int temp = __stt_check_precondition(stt, &client);
1155 if (STT_ERROR_NONE != temp)
1158 RETVM_IF(NULL == type || NULL == support, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1159 RETVM_IF(client->current_state != STT_STATE_READY, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1164 ret = stt_dbus_request_is_recognition_type_supported(client->uid, type, support);
1167 if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1168 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get recognition type supported : %s", __stt_get_error_code(ret));
1174 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] recognition type is %s", *support ? "true " : "false");
1179 return STT_ERROR_NONE;
1182 int stt_get_audio_format(stt_h stt, stt_audio_type_e* type, int* rate, int* num_of_channels)
1184 stt_client_s* client = NULL;
1185 int temp = __stt_check_precondition(stt, &client);
1186 if (STT_ERROR_NONE != temp)
1189 RETVM_IF(NULL == type || NULL == rate || NULL == num_of_channels, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1190 RETVM_IF(client->current_state == STT_STATE_CREATED, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is CREATED", client->current_state);
1192 int ret = STT_ERROR_OPERATION_FAILED;
1194 while (STT_ERROR_NONE != ret) {
1195 ret = stt_dbus_request_get_audio_format(client->uid, type, rate, num_of_channels);
1196 if (STT_ERROR_NONE != ret) {
1198 if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1199 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get audio format : %d/%s", ret, get_error_message(ret));
1205 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Audio format: type(%d), rate(%d), number of channels(%d)",
1206 *type, *rate, *num_of_channels);
1211 return STT_ERROR_NONE;
1214 int stt_set_silence_detection(stt_h stt, stt_option_silence_detection_e type)
1216 stt_client_s* client = NULL;
1217 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
1218 if (STT_ERROR_NONE != temp)
1221 SLOG(LOG_INFO, TAG_STTC, "===== Set silence detection, supported(%d), type(%d)", client->silence_supported, type);
1223 if (true == client->silence_supported) {
1224 if (type >= STT_OPTION_SILENCE_DETECTION_FALSE && type <= STT_OPTION_SILENCE_DETECTION_AUTO) {
1225 client->silence = type;
1227 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Type is invalid");
1228 return STT_ERROR_INVALID_PARAMETER;
1231 return STT_ERROR_NOT_SUPPORTED_FEATURE;
1234 return STT_ERROR_NONE;
1237 int stt_set_start_sound(stt_h stt, const char* filename)
1240 stt_client_s* client = NULL;
1241 int temp = __stt_check_precondition(stt, &client);
1242 if (STT_ERROR_NONE != temp)
1245 RETVM_IF(NULL == filename, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1246 RETVM_IF(0 != access(filename, F_OK), STT_ERROR_INVALID_PARAMETER, "[ERROR] File does not exist");
1247 RETVM_IF(STT_STATE_READY != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1249 SLOG(LOG_INFO, TAG_STTC, "===== STT SET START SOUND");
1254 ret = stt_dbus_request_set_start_sound(client->uid, filename);
1257 if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1258 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set start sound : %s", __stt_get_error_code(ret));
1264 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Set start sound : %s", filename);
1269 return STT_ERROR_NONE;
1272 int stt_unset_start_sound(stt_h stt)
1274 stt_client_s* client = NULL;
1275 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
1276 if (STT_ERROR_NONE != temp)
1279 SLOG(LOG_INFO, TAG_STTC, "===== STT UNSET START SOUND");
1284 ret = stt_dbus_request_unset_start_sound(client->uid);
1287 if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1288 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset start sound : %s", __stt_get_error_code(ret));
1294 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Unset start sound");
1299 return STT_ERROR_NONE;
1302 int stt_set_stop_sound(stt_h stt, const char* filename)
1305 stt_client_s* client = NULL;
1306 int temp = __stt_check_precondition(stt, &client);
1307 if (STT_ERROR_NONE != temp)
1310 RETVM_IF(NULL == filename, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1311 RETVM_IF(0 != access(filename, F_OK), STT_ERROR_INVALID_PARAMETER, "[ERROR] File does not exist");
1312 RETVM_IF(STT_STATE_READY != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1314 SLOG(LOG_INFO, TAG_STTC, "===== STT SET STOP SOUND");
1319 ret = stt_dbus_request_set_stop_sound(client->uid, filename);
1322 if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1323 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set stop sound : %s", __stt_get_error_code(ret));
1329 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Set stop sound : %s", filename);
1334 return STT_ERROR_NONE;
1337 int stt_unset_stop_sound(stt_h stt)
1339 stt_client_s* client = NULL;
1340 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
1341 if (STT_ERROR_NONE != temp)
1344 SLOG(LOG_INFO, TAG_STTC, "===== STT UNSET STOP SOUND");
1349 ret = stt_dbus_request_unset_stop_sound(client->uid);
1352 if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1353 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset stop sound : %s", __stt_get_error_code(ret));
1359 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Unset stop sound");
1364 return STT_ERROR_NONE;
1367 int stt_start(stt_h stt, const char* language, const char* type)
1369 stt_client_s* client = NULL;
1370 int tmp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
1371 if (STT_ERROR_NONE != tmp)
1374 RETVM_IF(STT_INTERNAL_STATE_NONE != client->internal_state, STT_ERROR_IN_PROGRESS_TO_RECORDING, "[ERROR] Invalid State : Internal state is NOT none : %d", client->current_state);
1375 SLOG(LOG_INFO, TAG_STTC, "===== STT START");
1378 char appid[1024] = {0, };
1379 ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
1381 if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1382 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID"); //LCOV_EXCL_LINE
1384 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
1387 const char* temp = NULL;
1388 if (NULL == language) {
1394 RETVM_IF(NULL == temp, STT_ERROR_OUT_OF_MEMORY, "[ERROR] Fail to allocate memory");
1396 if (true == client->credential_needed && NULL == client->credential) {
1397 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id); //LCOV_EXCL_LINE
1398 return STT_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1401 client->internal_state = STT_INTERNAL_STATE_STARTING;
1402 ret = stt_dbus_request_start(client->uid, temp, type, client->silence, appid, client->credential, client->audio_id);
1404 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1405 client->internal_state = STT_INTERNAL_STATE_NONE;
1407 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start is successful but not done");
1408 client->is_streaming = false;
1411 SLOG(LOG_INFO, TAG_STTC, "=====");
1412 SLOG(LOG_DEBUG, TAG_STTC, " ");
1417 int stt_stop(stt_h stt)
1419 stt_client_s* client = NULL;
1420 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_RECORDING);
1421 if (STT_ERROR_NONE != temp)
1424 SLOG(LOG_INFO, TAG_STTC, "===== STT STOP");
1426 if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1427 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1428 return STT_ERROR_IN_PROGRESS_TO_READY;
1429 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1430 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1431 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1434 if (client->is_streaming) {
1435 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Audio streaming is activated.");
1436 return STT_ERROR_OPERATION_FAILED;
1439 client->internal_state = STT_INTERNAL_STATE_STOPPING;
1440 int ret = stt_dbus_request_stop(client->uid);
1442 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to stop : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1443 client->internal_state = STT_INTERNAL_STATE_NONE;
1445 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Stop is successful but not done");
1448 SLOG(LOG_DEBUG, TAG_STTC, "=====");
1449 SLOG(LOG_DEBUG, TAG_STTC, " ");
1454 int stt_start_audio_streaming(stt_h stt, const char* language, const char* type)
1456 stt_client_s* client = NULL;
1457 int tmp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
1458 if (STT_ERROR_NONE != tmp)
1461 RETVM_IF(STT_INTERNAL_STATE_NONE != client->internal_state, STT_ERROR_IN_PROGRESS_TO_RECORDING, "[ERROR] Invalid State : Internal state is NOT none : %d", client->current_state);
1462 SLOG(LOG_INFO, TAG_STTC, "===== STT START AUDIO STREAMING");
1464 int ret = STT_ERROR_NONE;
1465 char appid[1024] = {0, };
1466 ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
1468 if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1469 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID"); //LCOV_EXCL_LINE
1471 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
1474 const char* language_param = NULL == language ? "default" : language;
1475 if (true == client->credential_needed && NULL == client->credential) {
1476 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id); //LCOV_EXCL_LINE
1477 return STT_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1480 client->internal_state = STT_INTERNAL_STATE_STARTING;
1481 ret = stt_dbus_request_start_audio_streaming(client->uid, language_param, type, client->silence, appid, client->credential, client->audio_id);
1483 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : ret(%d/%s)", ret, get_error_message(ret)); //LCOV_EXCL_LINE
1484 client->internal_state = STT_INTERNAL_STATE_NONE;
1488 client->is_streaming = true;
1489 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start audio streaming is successful but not done");
1490 SLOG(LOG_INFO, TAG_STTC, "=====");
1491 return STT_ERROR_NONE;
1494 int stt_send_audio_streaming(stt_h stt, const char* data, size_t data_size)
1496 stt_client_s* client = NULL;
1497 int tmp = __stt_check_precondition_with_state(stt, &client, STT_STATE_RECORDING);
1498 if (STT_ERROR_NONE != tmp)
1501 SLOG(LOG_INFO, TAG_STTC, "===== STT SEND AUDIO STREAMING");
1503 if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1504 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1505 return STT_ERROR_IN_PROGRESS_TO_READY;
1506 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1507 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1508 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1511 if (false == client->is_streaming) {
1512 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Audio streaming is not activated.");
1513 return STT_ERROR_OPERATION_FAILED;
1516 int ret = stt_dbus_request_send_audio_streaming(client->uid, data, data_size);
1517 if (STT_ERROR_NONE != ret) {
1518 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : ret(%d/%s)", ret, get_error_message(ret)); //LCOV_EXCL_LINE
1522 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Send audio streaming is successful");
1523 SLOG(LOG_INFO, TAG_STTC, "=====");
1524 return STT_ERROR_NONE;
1527 int stt_stop_audio_streaming(stt_h stt)
1529 stt_client_s* client = NULL;
1530 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_RECORDING);
1531 if (STT_ERROR_NONE != temp)
1534 SLOG(LOG_INFO, TAG_STTC, "===== STT STOP AUDIO STREAMING");
1536 if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1537 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1538 return STT_ERROR_IN_PROGRESS_TO_READY;
1539 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1540 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1541 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1544 if (false == client->is_streaming) {
1545 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Audio streaming is not activated.");
1546 return STT_ERROR_OPERATION_FAILED;
1549 client->internal_state = STT_INTERNAL_STATE_STOPPING;
1550 int ret = stt_dbus_request_stop_audio_streaming(client->uid);
1552 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to stop : ret(%d/%s)", ret, get_error_message(ret)); //LCOV_EXCL_LINE
1553 client->internal_state = STT_INTERNAL_STATE_NONE;
1557 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Stop is successful but not done");
1558 SLOG(LOG_DEBUG, TAG_STTC, "=====");
1559 return STT_ERROR_NONE;
1562 int stt_cancel(stt_h stt)
1564 stt_client_s* client = NULL;
1565 int temp = __stt_check_precondition(stt, &client);
1566 if (STT_ERROR_NONE != temp)
1569 SLOG(LOG_INFO, TAG_STTC, "===== STT CANCEL");
1572 if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
1573 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state); //LCOV_EXCL_LINE
1574 return STT_ERROR_INVALID_STATE;
1577 if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1578 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1579 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1580 } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1581 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1582 return STT_ERROR_IN_PROGRESS_TO_READY;
1585 client->internal_state = STT_INTERNAL_STATE_CANCELING;
1586 int ret = stt_dbus_request_cancel(client->uid);
1588 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1589 client->internal_state = STT_INTERNAL_STATE_NONE;
1591 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Cancel is successful but not done");
1594 SLOG(LOG_DEBUG, TAG_STTC, "=====");
1595 SLOG(LOG_DEBUG, TAG_STTC, " ");
1601 int __stt_cb_set_volume(unsigned int uid, float volume)
1603 stt_client_s* client = NULL;
1605 client = stt_client_get_by_uid(uid);
1606 if (NULL == client) {
1607 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
1608 return STT_ERROR_INVALID_PARAMETER;
1611 if (STT_STATE_RECORDING != client->current_state) {
1612 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1613 return STT_ERROR_INVALID_STATE;
1616 g_volume_db = volume;
1617 SLOG(LOG_INFO, TAG_STTC, "Set volume (%f)", g_volume_db);
1623 int stt_get_recording_volume(stt_h stt, float* volume)
1625 stt_client_s* client = NULL;
1626 int temp = __stt_check_precondition(stt, &client);
1627 if (STT_ERROR_NONE != temp)
1630 RETVM_IF(NULL == volume, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1631 RETVM_IF(STT_STATE_RECORDING != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1633 *volume = g_volume_db;
1635 SLOG(LOG_INFO, TAG_STTC, "Get recording volume (%f)", *volume);
1637 return STT_ERROR_NONE;
1641 bool __stt_result_time_cb(int index, int event, const char* text, long start_time, long end_time, void* user_data)
1643 stt_client_s* client = (stt_client_s*)user_data;
1646 if (NULL == client) {
1647 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1651 if (NULL != client->result_time_cb) {
1652 SLOG(LOG_INFO, TAG_STTC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
1653 index, event, text, start_time, end_time);
1654 client->result_time_cb(client->stt, index, (stt_result_time_event_e)event,
1655 text, start_time, end_time, client->result_time_user_data);
1657 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Callback is NULL");
1665 int stt_foreach_detailed_result(stt_h stt, stt_result_time_cb callback, void* user_data)
1667 stt_client_s* client = NULL;
1668 int temp = __stt_check_precondition(stt, &client);
1669 if (STT_ERROR_NONE != temp)
1672 SLOG(LOG_INFO, TAG_STTC, "===== STT FOREACH DETAILED RESULT");
1673 RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1675 client->result_time_cb = callback;
1676 client->result_time_user_data = user_data;
1679 ret = stt_config_mgr_foreach_time_info(__stt_result_time_cb, client);
1680 ret = __stt_convert_config_error_code(ret);
1682 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to foreach time info : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1685 client->result_time_cb = NULL;
1686 client->result_time_user_data = NULL;
1688 SLOG(LOG_INFO, TAG_STTC, "=====");
1689 SLOG(LOG_DEBUG, TAG_STTC, " ");
1694 static void __stt_notify_error(void *data)
1696 stt_client_s* client = (stt_client_s*)data;
1698 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error from sttd");
1699 RETM_IF(NULL == client, "[ERROR] Fail to notify error : A handle is not valid");
1701 if (NULL == stt_client_get_by_uid(client->uid))
1704 if (NULL != client->error_cb) {
1705 stt_client_use_callback(client);
1706 g_err_callback_status = true;
1707 client->error_cb(client->stt, client->reason, client->error_user_data);
1708 g_err_callback_status = false;
1709 stt_client_not_use_callback(client);
1710 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is called : reason [%d]", client->reason);
1712 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null"); //LCOV_EXCL_LINE
1716 int __stt_cb_error(unsigned int uid, int reason, char* err_msg)
1719 GList* client_list = NULL;
1720 client_list = stt_client_get_client_list();
1723 stt_client_s *data = NULL;
1725 if (g_list_length(client_list) > 0) {
1726 /* Get a first item */
1727 iter = g_list_first(client_list);
1729 while (NULL != iter) {
1732 data->reason = reason;
1733 data->internal_state = STT_INTERNAL_STATE_NONE;
1735 free(data->err_msg);
1736 data->err_msg = NULL;
1738 if (NULL != err_msg)
1739 data->err_msg = strdup(err_msg);
1741 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
1743 if (NULL != data->error_cb) {
1744 ecore_main_loop_thread_safe_call_async(__stt_notify_error, data);
1746 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1749 if (STT_ERROR_SERVICE_RESET == reason) {
1750 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
1752 data->current_state = STT_STATE_CREATED;
1753 if (0 != stt_prepare(data->stt)) {
1754 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
1759 iter = g_list_next(iter);
1764 stt_client_s* client = stt_client_get_by_uid(uid);
1765 if (NULL == client) {
1766 SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
1770 client->reason = reason;
1771 client->internal_state = STT_INTERNAL_STATE_NONE;
1773 free(client->err_msg);
1774 client->err_msg = NULL;
1776 if (NULL != err_msg)
1777 client->err_msg = strdup(err_msg);
1779 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
1781 if (NULL != client->error_cb) {
1782 ecore_main_loop_thread_safe_call_async(__stt_notify_error, client);
1784 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1787 if (STT_ERROR_SERVICE_RESET == reason) {
1788 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset"); //LCOV_EXCL_LINE
1790 client->current_state = STT_STATE_CREATED;
1791 if (0 != stt_prepare(client->stt)) {
1792 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare"); //LCOV_EXCL_LINE
1801 static void __stt_notify_state_changed(void *data)
1803 stt_client_s* client = (stt_client_s*)data;
1806 RETM_IF(NULL == client, "[ERROR] Fail to notify error : A handle is not valid");
1807 RET_IF(NULL == stt_client_get_by_uid(client->uid));
1809 if (STT_INTERNAL_STATE_STARTING == client->internal_state && STT_STATE_RECORDING == client->current_state) {
1810 client->internal_state = STT_INTERNAL_STATE_NONE;
1811 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
1812 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state && STT_STATE_PROCESSING == client->current_state) {
1813 client->internal_state = STT_INTERNAL_STATE_NONE;
1814 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
1815 } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state && STT_STATE_READY == client->current_state) {
1816 client->internal_state = STT_INTERNAL_STATE_NONE;
1817 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
1820 if (NULL != client->state_changed_cb) {
1821 stt_client_use_callback(client);
1822 client->state_changed_cb(client->stt, client->before_state,
1823 client->current_state, client->state_changed_user_data);
1824 stt_client_not_use_callback(client);
1825 SLOG(LOG_INFO, TAG_STTC, "State changed callback is called, State(%d)", client->current_state);
1827 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null, State(%d)", client->current_state); //LCOV_EXCL_LINE
1833 int __stt_cb_result(unsigned int uid, int event, char** data, int data_count, const char* msg)
1835 stt_client_s* client = stt_client_get_by_uid(uid);
1836 RETVM_IF(NULL == client, STT_ERROR_INVALID_PARAMETER, "Handle is NOT valid. uid(%u)", uid);
1839 SECURE_SLOG(LOG_INFO, TAG_STTC, "Recognition Result Message = %s", msg);
1841 for (int i = 0; i < data_count; i++) {
1842 if (NULL != data[i])
1843 SECURE_SLOG(LOG_INFO, TAG_STTC, "Recognition Result[%d] = %s", i, data[i]);
1846 if (NULL != client->recognition_result_cb) {
1847 stt_client_use_callback(client);
1848 client->recognition_result_cb(client->stt, event, (const char**)data, data_count,
1849 msg, client->recognition_result_user_data);
1850 stt_client_not_use_callback(client);
1851 SLOG(LOG_INFO, TAG_STTC, "client recognition result callback called");
1853 SLOG(LOG_WARN, TAG_STTC, "[WARNING] User recognition result callback is NULL");
1856 stt_config_mgr_remove_time_info_file();
1858 if (STT_RESULT_EVENT_FINAL_RESULT == event || STT_RESULT_EVENT_ERROR == event) {
1859 client->before_state = client->current_state;
1860 client->current_state = STT_STATE_READY;
1862 if (NULL != client->state_changed_cb) {
1863 ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
1865 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null"); //LCOV_EXCL_LINE
1869 return STT_ERROR_NONE;
1872 int __stt_cb_set_state(unsigned int uid, int state)
1874 stt_client_s* client = stt_client_get_by_uid(uid);
1875 if (NULL == client) {
1876 SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
1880 stt_state_e state_from_daemon = (stt_state_e)state;
1882 if (client->current_state == state_from_daemon) {
1883 SLOG(LOG_DEBUG, TAG_STTC, "Current state has already been %d", client->current_state);
1887 client->before_state = client->current_state;
1888 client->current_state = state_from_daemon;
1890 ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
1895 static void __stt_notify_speech_status(void *data)
1897 stt_client_s* client = (stt_client_s*)data;
1900 RETM_IF(NULL == client, "[ERROR] Fail to notify speech status : A handle is not valid");
1901 RET_IF(NULL == stt_client_get_by_uid(client->uid));
1903 if (NULL != client->speech_status_cb) {
1904 stt_client_use_callback(client);
1905 client->speech_status_cb(client->stt, client->speech_status, client->speech_status_user_data);
1906 stt_client_not_use_callback(client);
1907 SLOG(LOG_INFO, TAG_STTC, "Speech status callback is called"); //LCOV_EXCL_LINE
1909 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Speech status callback is null"); //LCOV_EXCL_LINE
1915 int __stt_cb_speech_status(unsigned int uid, int status)
1917 stt_client_s* client = stt_client_get_by_uid(uid);
1918 if (NULL == client) {
1919 SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
1923 client->speech_status = status;
1925 ecore_main_loop_thread_safe_call_async(__stt_notify_speech_status, client);
1930 int stt_set_recognition_result_cb(stt_h stt, stt_recognition_result_cb callback, void* user_data)
1932 stt_client_s* client = NULL;
1933 int temp = __stt_check_precondition(stt, &client);
1934 if (STT_ERROR_NONE != temp)
1937 RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1938 RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
1940 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set recognition result cb");
1942 client->recognition_result_cb = callback;
1943 client->recognition_result_user_data = user_data;
1948 int stt_unset_recognition_result_cb(stt_h stt)
1950 stt_client_s* client = NULL;
1951 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
1952 if (STT_ERROR_NONE != temp)
1955 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset recognition result cb");
1957 client->recognition_result_cb = NULL;
1958 client->recognition_result_user_data = NULL;
1963 int stt_set_state_changed_cb(stt_h stt, stt_state_changed_cb callback, void* user_data)
1965 stt_client_s* client = NULL;
1966 int temp = __stt_check_precondition(stt, &client);
1967 if (STT_ERROR_NONE != temp)
1970 RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1971 RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
1973 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set state changed cb");
1975 client->state_changed_cb = callback;
1976 client->state_changed_user_data = user_data;
1981 int stt_unset_state_changed_cb(stt_h stt)
1983 stt_client_s* client = NULL;
1984 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
1985 if (STT_ERROR_NONE != temp)
1988 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset state changed cb");
1990 client->state_changed_cb = NULL;
1991 client->state_changed_user_data = NULL;
1996 int stt_set_error_cb(stt_h stt, stt_error_cb callback, void* user_data)
1998 stt_client_s* client = NULL;
1999 int temp = __stt_check_precondition(stt, &client);
2000 if (STT_ERROR_NONE != temp)
2003 RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2004 RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2006 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set error cb");
2008 client->error_cb = callback;
2009 client->error_user_data = user_data;
2014 int stt_unset_error_cb(stt_h stt)
2016 stt_client_s* client = NULL;
2017 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
2018 if (STT_ERROR_NONE != temp)
2021 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset error cb");
2023 client->error_cb = NULL;
2024 client->error_user_data = NULL;
2029 int stt_set_default_language_changed_cb(stt_h stt, stt_default_language_changed_cb callback, void* user_data)
2031 stt_client_s* client = NULL;
2032 int temp = __stt_check_precondition(stt, &client);
2033 if (STT_ERROR_NONE != temp)
2036 RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2037 RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2039 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set default language changed cb");
2041 client->default_lang_changed_cb = callback;
2042 client->default_lang_changed_user_data = user_data;
2047 int stt_unset_default_language_changed_cb(stt_h stt)
2049 stt_client_s* client = NULL;
2050 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
2051 if (STT_ERROR_NONE != temp)
2054 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset default language changed cb");
2056 client->default_lang_changed_cb = NULL;
2057 client->default_lang_changed_user_data = NULL;
2062 int stt_set_engine_changed_cb(stt_h stt, stt_engine_changed_cb callback, void* user_data)
2064 stt_client_s* client = NULL;
2065 int temp = __stt_check_precondition(stt, &client);
2066 if (STT_ERROR_NONE != temp)
2069 RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2070 RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2072 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set engine changed cb");
2074 client->engine_changed_cb = callback;
2075 client->engine_changed_user_data = user_data;
2080 int stt_unset_engine_changed_cb(stt_h stt)
2082 stt_client_s* client = NULL;
2083 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
2084 if (STT_ERROR_NONE != temp)
2087 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset engine changed cb");
2089 client->engine_changed_cb = NULL;
2090 client->engine_changed_user_data = NULL;
2095 int stt_set_speech_status_cb(stt_h stt, stt_speech_status_cb callback, void* user_data)
2097 stt_client_s* client = NULL;
2098 int temp = __stt_check_precondition(stt, &client);
2099 if (STT_ERROR_NONE != temp)
2102 RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2103 RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2105 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set speech status cb");
2107 client->speech_status_cb = callback;
2108 client->speech_status_user_data = user_data;
2113 int stt_unset_speech_status_cb(stt_h stt)
2115 stt_client_s* client = NULL;
2116 int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
2117 if (STT_ERROR_NONE != temp)
2120 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset speech status cb");
2122 client->speech_status_cb = NULL;
2123 client->speech_status_user_data = NULL;
2129 int stt_start_file(stt_h stt, const char* language, const char* type, const char* filepath)
2131 stt_client_s* client = NULL;
2132 int tmp = __stt_check_precondition(stt, &client);
2133 if (STT_ERROR_NONE != tmp)
2136 RETVM_IF(NULL == filepath, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2137 RETVM_IF(client->current_state != STT_STATE_READY, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
2139 SLOG(LOG_INFO, TAG_STTC, "===== STT START FILE");
2141 if (STT_INTERNAL_STATE_NONE != client->internal_state) {
2142 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is NOT none : %d", client->internal_state);
2143 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2147 char appid[1024] = {0, };
2148 ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
2150 if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
2151 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID");
2153 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Current app id is %s", appid);
2157 if (NULL == language) {
2158 temp = strdup("default");
2160 temp = strdup(language);
2163 RETVM_IF(NULL == temp, STT_ERROR_OUT_OF_MEMORY, "[ERROR] Fail to allocate memory");
2165 if (true == client->credential_needed && NULL == client->credential) {
2166 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
2169 return STT_ERROR_PERMISSION_DENIED;
2172 client->internal_state = STT_INTERNAL_STATE_STARTING;
2173 ret = stt_dbus_request_start_file(client->uid, temp, type, client->silence, appid, client->credential, filepath);
2175 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start file : %s", __stt_get_error_code(ret));
2176 client->internal_state = STT_INTERNAL_STATE_NONE;
2178 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start is successful but not done");
2184 SLOG(LOG_DEBUG, TAG_STTC, "=====");
2185 SLOG(LOG_DEBUG, TAG_STTC, " ");
2190 int stt_cancel_file(stt_h stt)
2192 stt_client_s* client = NULL;
2193 int temp = __stt_check_precondition(stt, &client);
2194 if (STT_ERROR_NONE != temp)
2197 SLOG(LOG_INFO, TAG_STTC, "===== STT CANCEL FILE");
2200 if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
2201 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state);
2202 return STT_ERROR_INVALID_STATE;
2205 if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
2206 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
2207 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2208 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
2209 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
2210 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
2211 } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
2212 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
2213 return STT_ERROR_IN_PROGRESS_TO_READY;
2216 client->internal_state = STT_INTERNAL_STATE_CANCELING;
2217 int ret = stt_dbus_request_cancel_file(client->uid);
2219 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel file : %s", __stt_get_error_code(ret));
2220 client->internal_state = STT_INTERNAL_STATE_NONE;
2222 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Cancel file is successful but not done");
2225 SLOG(LOG_DEBUG, TAG_STTC, "=====");
2226 SLOG(LOG_DEBUG, TAG_STTC, " ");
2232 void __sound_stream_ducking_state_changed_cb(sound_stream_ducking_h stream_ducking, bool is_ducked, void *user_data)
2234 SLOG(LOG_DEBUG, TAG_STTC, "@@@ ducking state changed cb");
2235 SLOG(LOG_DEBUG, TAG_STTC, "[Volume] is ducked : %d", is_ducked);
2236 // ducking_flag = true;
2240 static char* __get_ducking_stream(sound_stream_type_e stream_type)
2242 if (SOUND_STREAM_TYPE_MEDIA == stream_type)
2243 return "Media stream";
2244 else if (SOUND_STREAM_TYPE_SYSTEM == stream_type)
2245 return "System stream";
2246 else if (SOUND_STREAM_TYPE_NOTIFICATION == stream_type)
2247 return "Notification stream";
2248 else if (SOUND_STREAM_TYPE_ALARM == stream_type)
2249 return "Alarm stream";
2251 return "Non matched stream";
2254 static int __activate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h, double bg_volume_ratio)
2256 bool is_ducked = false;
2257 int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
2259 SLOG(LOG_DEBUG, TAG_STTC, "[Volume] The %s is already ducked", __get_ducking_stream(stream_type));
2261 ret = sound_manager_activate_ducking(stream_ducking_h, SND_MGR_DUCKING_DURATION, bg_volume_ratio);
2262 if (SOUND_MANAGER_ERROR_NONE != ret) {
2263 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to activate ducking for %s", __get_ducking_stream(stream_type));
2265 SLOG(LOG_INFO, TAG_STTC, "[Volume SUCCESS] Activate ducking for %s", __get_ducking_stream(stream_type));
2271 static void __change_background_volume(stt_system_volume_event_e volume_event)
2273 double bg_volume_ratio = 0.0;
2275 if (STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_FARFIELD == volume_event) {
2276 bg_volume_ratio = STT_BG_VOLUME_RATIO_FARFIELD;
2277 } else if (STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_NEARFIELD == volume_event) {
2278 bg_volume_ratio = STT_BG_VOLUME_RATIO_NEARFIELD;
2281 SLOG(LOG_INFO, TAG_STTC, "[Volume] volume ratio(%lf)", bg_volume_ratio);
2283 if (1.0 > bg_volume_ratio) {
2284 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking, bg_volume_ratio);
2285 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking, bg_volume_ratio);
2286 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking, bg_volume_ratio);
2287 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking, bg_volume_ratio);
2291 static int __deactivate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h)
2293 bool is_ducked = false;
2294 int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
2296 SLOG(LOG_DEBUG, TAG_STTC, "[Volume] The %s is already recovered from ducking", __get_ducking_stream(stream_type));
2298 ret = sound_manager_deactivate_ducking(stream_ducking_h);
2299 if (SOUND_MANAGER_ERROR_NONE != ret) {
2300 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to deactivate ducking for %s", __get_ducking_stream(stream_type));
2302 SLOG(LOG_INFO, TAG_STTC, "[Volume SUCCESS] Deactivate ducking for %s", __get_ducking_stream(stream_type));
2308 static void __recover_background_volume()
2310 SLOG(LOG_INFO, TAG_STTC, "[Volume] background volume recover");
2312 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking);
2313 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking);
2314 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking);
2315 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking);
2318 int __create_ducking_handle(void)
2321 if (NULL == g_media_stream_ducking) {
2322 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_MEDIA, __sound_stream_ducking_state_changed_cb, NULL, &g_media_stream_ducking);
2323 if (SOUND_MANAGER_ERROR_NONE != ret)
2324 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for type media, ret(%d)", ret);
2326 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for media stream is already created");
2329 if (NULL == g_system_stream_ducking) {
2330 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_SYSTEM, __sound_stream_ducking_state_changed_cb, NULL, &g_system_stream_ducking);
2331 if (SOUND_MANAGER_ERROR_NONE != ret)
2332 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for system type, ret(%d)", ret);
2334 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for system stream is already created");
2337 if (NULL == g_notification_stream_ducking) {
2338 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_NOTIFICATION, __sound_stream_ducking_state_changed_cb, NULL, &g_notification_stream_ducking);
2339 if (SOUND_MANAGER_ERROR_NONE != ret)
2340 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for notification type, ret(%d)", ret);
2342 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for notification stream is already created");
2345 if (NULL == g_alarm_stream_ducking) {
2346 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_ALARM, __sound_stream_ducking_state_changed_cb, NULL, &g_alarm_stream_ducking);
2347 if (SOUND_MANAGER_ERROR_NONE != ret)
2348 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for alarm type, ret(%d)", ret);
2350 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for alarm stream is already created");
2356 int stt_change_system_volume(stt_h stt, stt_system_volume_event_e volume_event)
2358 SLOG(LOG_DEBUG, TAG_STTC, "[STT] Change system volume, volume_event(%d)", volume_event);
2360 stt_client_s* client = NULL;
2361 int temp = __stt_check_precondition(stt, &client);
2362 if (STT_ERROR_NONE != temp)
2365 RETVM_IF((STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_NEARFIELD > volume_event || STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_FARFIELD < volume_event),
2366 STT_ERROR_INVALID_PARAMETER,
2367 "[ERROR] Invalid volume event (%d)",
2371 if (client->current_state != STT_STATE_READY && client->current_state != STT_STATE_CREATED) {
2372 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY nor CREATED", client->current_state);
2373 return STT_ERROR_INVALID_STATE;
2376 /* change system volume */
2377 int ret = __create_ducking_handle();
2379 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create volume handle");
2381 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Success to create volume handle");
2384 __change_background_volume(volume_event);
2385 return STT_ERROR_NONE;
2388 int __destroy_ducking_handle(void)
2391 if (g_media_stream_ducking) {
2392 ret = sound_manager_destroy_stream_ducking(g_media_stream_ducking);
2393 if (SOUND_MANAGER_ERROR_NONE != ret)
2394 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy media stream ducking, ret(%d)", ret);
2395 g_media_stream_ducking = NULL;
2397 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for media stream is already created");
2400 if (g_system_stream_ducking) {
2401 ret = sound_manager_destroy_stream_ducking(g_system_stream_ducking);
2402 if (SOUND_MANAGER_ERROR_NONE != ret)
2403 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy system stream ducking, ret(%d)", ret);
2404 g_system_stream_ducking = NULL;
2406 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for system stream is already created");
2409 if (g_notification_stream_ducking) {
2410 ret = sound_manager_destroy_stream_ducking(g_notification_stream_ducking);
2411 if (SOUND_MANAGER_ERROR_NONE != ret)
2412 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy notification stream ducking, ret(%d)", ret);
2413 g_notification_stream_ducking = NULL;
2415 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for notification stream is already created");
2418 if (g_alarm_stream_ducking) {
2419 ret = sound_manager_destroy_stream_ducking(g_alarm_stream_ducking);
2420 if (SOUND_MANAGER_ERROR_NONE != ret)
2421 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy alarm stream ducking, ret(%d)", ret);
2422 g_alarm_stream_ducking = NULL;
2424 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for alarm stream is already created");
2429 int stt_recover_system_volume(stt_h stt)
2431 SLOG(LOG_DEBUG, TAG_STTC, "[STT] recover system volume");
2433 stt_client_s* client = NULL;
2434 int temp = __stt_check_precondition(stt, &client);
2435 if (STT_ERROR_NONE != temp)
2439 if (client->current_state != STT_STATE_READY && client->current_state != STT_STATE_CREATED) {
2440 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY nor CREATED", client->current_state);
2441 return STT_ERROR_INVALID_STATE;
2444 /* recover volume */
2445 __recover_background_volume();
2446 int ret = __destroy_ducking_handle();
2448 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to destroy volume handle");
2450 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Success to destroy volume handle");
2453 return STT_ERROR_NONE;
2456 int stt_set_audio_type(stt_h stt, const char *audio_id)
2458 stt_client_s* client = NULL;
2459 int temp = __stt_check_precondition(stt, &client);
2460 if (STT_ERROR_NONE != temp)
2463 RETVM_IF(NULL == audio_id, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2464 RETVM_IF(STT_STATE_CREATED != client->current_state && client->current_state != STT_STATE_READY, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created' or 'Ready'", client->current_state);
2466 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set audio type(%s)", audio_id);
2468 free(client->audio_id);
2469 client->audio_id = strdup(audio_id);
2471 return STT_ERROR_NONE;
2474 int stt_get_audio_type(stt_h stt, char **audio_id)
2476 stt_client_s* client = NULL;
2477 int temp = __stt_check_precondition(stt, &client);
2478 if (STT_ERROR_NONE != temp)
2481 RETVM_IF(NULL == audio_id, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2482 RETVM_IF(STT_STATE_CREATED != client->current_state && client->current_state != STT_STATE_READY, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created' or 'Ready'", client->current_state);
2484 *audio_id = strdup(client->audio_id);
2485 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Get audio type(%s)", *audio_id);
2487 return STT_ERROR_NONE;