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"
39 static void __stt_notify_state_changed(void *data);
40 static void __stt_notify_error(void *data);
42 static Ecore_Timer* g_connect_timer = NULL;
43 static float g_volume_db = 0;
45 static int g_feature_enabled = -1;
47 static pthread_mutex_t g_cynara_mutex = PTHREAD_MUTEX_INITIALIZER;
49 static cynara *p_cynara = NULL;
51 static bool g_err_callback_status = false;
53 /* for changing volume on each sound stream */
54 static sound_stream_ducking_h g_media_stream_ducking;
55 static sound_stream_ducking_h g_system_stream_ducking;
56 static sound_stream_ducking_h g_notification_stream_ducking;
57 static sound_stream_ducking_h g_alarm_stream_ducking;
59 #define STT_BG_VOLUME_RATIO_FARFIELD 0.0
60 #define STT_BG_VOLUME_RATIO_NEARFIELD 0.7
61 #define SND_MGR_DUCKING_DURATION 500
70 static int __stt_get_feature_enabled()
72 if (0 == g_feature_enabled) {
74 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] STT NOT supported");
75 return STT_ERROR_NOT_SUPPORTED;
77 } else if (-1 == g_feature_enabled) {
78 bool stt_supported = false;
79 bool mic_supported = false;
80 if (0 == system_info_get_platform_bool(STT_FEATURE_PATH, &stt_supported)) {
81 if (0 == system_info_get_platform_bool(STT_MIC_FEATURE_PATH, &mic_supported)) {
82 if (false == stt_supported || false == mic_supported) {
84 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] STT NOT supported");
85 g_feature_enabled = 0;
86 return STT_ERROR_NOT_SUPPORTED;
90 g_feature_enabled = 1;
93 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get feature value");
94 return STT_ERROR_NOT_SUPPORTED;
99 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get feature value");
100 return STT_ERROR_NOT_SUPPORTED;
108 static int __check_privilege_initialize()
110 int ret = cynara_initialize(&p_cynara, NULL);
111 if (CYNARA_API_SUCCESS != ret)
112 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to initialize"); //LCOV_EXCL_LINE
114 return ret == CYNARA_API_SUCCESS;
117 static bool __check_privilege(const char* uid, const char * privilege)
120 char label_path[1024] = "/proc/self/attr/current";
121 char smack_label[1024] = {'\0',};
127 fp = fopen(label_path, "r");
129 if (0 >= fread(smack_label, 1, sizeof(smack_label), fp))
130 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to fread"); //LCOV_EXCL_LINE
135 pid_t pid = getpid();
136 char *session = cynara_session_from_pid(pid);
137 int ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
143 if (ret != CYNARA_API_ACCESS_ALLOWED) {
144 SLOG(LOG_DEBUG, TAG_STTC, "[Client]cynara_check returned %d(Denied)", ret);
150 static void __check_privilege_deinitialize()
153 cynara_finish(p_cynara);
157 static int __stt_check_privilege()
159 pthread_mutex_lock(&g_cynara_mutex);
162 ret = __check_privilege_initialize();
164 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] privilege initialize is failed"); //LCOV_EXCL_LINE
165 pthread_mutex_unlock(&g_cynara_mutex);
166 return STT_ERROR_PERMISSION_DENIED;
170 snprintf(uid, 16, "%d", getuid());
172 ret = __check_privilege(uid, STT_PRIVILEGE_RECORDER);
173 __check_privilege_deinitialize();
175 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied"); //LCOV_EXCL_LINE
176 pthread_mutex_unlock(&g_cynara_mutex);
177 return STT_ERROR_PERMISSION_DENIED;
180 pthread_mutex_unlock(&g_cynara_mutex);
181 return STT_ERROR_NONE;
184 static int __stt_check_privilege_for_applaunch()
186 pthread_mutex_lock(&g_cynara_mutex);
189 ret = __check_privilege_initialize();
191 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] privilege initialize is failed (applaunch)"); //LCOV_EXCL_LINE
192 pthread_mutex_unlock(&g_cynara_mutex);
193 return STT_ERROR_PERMISSION_DENIED;
197 snprintf(uid, 16, "%d", getuid());
199 ret = __check_privilege(uid, STT_PRIVILEGE_APPLAUNCH);
200 __check_privilege_deinitialize();
202 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied : appmanager.launch"); //LCOV_EXCL_LINE
203 pthread_mutex_unlock(&g_cynara_mutex);
204 return STT_ERROR_PERMISSION_DENIED;
207 pthread_mutex_unlock(&g_cynara_mutex);
208 return STT_ERROR_NONE;
211 static const char* __stt_get_error_code(stt_error_e err)
215 case STT_ERROR_NONE: return "STT_ERROR_NONE";
216 case STT_ERROR_OUT_OF_MEMORY: return "STT_ERROR_OUT_OF_MEMORY";
217 case STT_ERROR_IO_ERROR: return "STT_ERROR_IO_ERROR";
218 case STT_ERROR_INVALID_PARAMETER: return "STT_ERROR_INVALID_PARAMETER";
219 case STT_ERROR_TIMED_OUT: return "STT_ERROR_TIMED_OUT";
220 case STT_ERROR_RECORDER_BUSY: return "STT_ERROR_RECORDER_BUSY";
221 case STT_ERROR_OUT_OF_NETWORK: return "STT_ERROR_OUT_OF_NETWORK";
222 case STT_ERROR_PERMISSION_DENIED: return "STT_ERROR_PERMISSION_DENIED";
223 case STT_ERROR_NOT_SUPPORTED: return "STT_ERROR_NOT_SUPPORTED";
224 case STT_ERROR_INVALID_STATE: return "STT_ERROR_INVALID_STATE";
225 case STT_ERROR_INVALID_LANGUAGE: return "STT_ERROR_INVALID_LANGUAGE";
226 case STT_ERROR_ENGINE_NOT_FOUND: return "STT_ERROR_ENGINE_NOT_FOUND";
227 case STT_ERROR_OPERATION_FAILED: return "STT_ERROR_OPERATION_FAILED";
228 case STT_ERROR_NOT_SUPPORTED_FEATURE: return "STT_ERROR_NOT_SUPPORTED_FEATURE";
229 case STT_ERROR_SERVICE_RESET: return "STT_ERROR_SERVICE_RESET";
231 return "Invalid error code";
236 static int __stt_convert_config_error_code(stt_config_error_e code)
239 if (code == STT_CONFIG_ERROR_NONE) return STT_ERROR_NONE;
240 if (code == STT_CONFIG_ERROR_OUT_OF_MEMORY) return STT_ERROR_OUT_OF_MEMORY;
241 if (code == STT_CONFIG_ERROR_IO_ERROR) return STT_ERROR_IO_ERROR;
242 if (code == STT_CONFIG_ERROR_INVALID_PARAMETER) return STT_ERROR_INVALID_PARAMETER;
243 if (code == STT_CONFIG_ERROR_PERMISSION_DENIED) return STT_ERROR_PERMISSION_DENIED;
244 if (code == STT_CONFIG_ERROR_NOT_SUPPORTED) return STT_ERROR_NOT_SUPPORTED;
245 if (code == STT_CONFIG_ERROR_INVALID_STATE) return STT_ERROR_INVALID_STATE;
246 if (code == STT_CONFIG_ERROR_INVALID_LANGUAGE) return STT_ERROR_INVALID_LANGUAGE;
247 if (code == STT_CONFIG_ERROR_ENGINE_NOT_FOUND) return STT_ERROR_ENGINE_NOT_FOUND;
248 if (code == STT_CONFIG_ERROR_OPERATION_FAILED) return STT_ERROR_OPERATION_FAILED;
254 void __stt_config_lang_changed_cb(const char* before_language, const char* current_language, void* user_data)
256 SLOG(LOG_DEBUG, TAG_STTC, "Language changed : Before lang(%s) Current lang(%s)",
257 before_language, current_language);
259 if (0 == strcmp(before_language, current_language)) {
263 GList* client_list = NULL;
264 client_list = stt_client_get_client_list();
267 stt_client_s *data = NULL;
269 if (g_list_length(client_list) > 0) {
270 /* Get a first item */
271 iter = g_list_first(client_list);
273 while (NULL != iter) {
275 if (NULL != data->default_lang_changed_cb) {
276 SLOG(LOG_DEBUG, TAG_STTC, "Call default language changed callback : uid(%d)", data->uid);
277 data->default_lang_changed_cb(data->stt, before_language, current_language,
278 data->default_lang_changed_user_data);
282 iter = g_list_next(iter);
289 static Eina_Bool __reconnect_by_engine_changed(void *data)
291 stt_h stt = (stt_h)data;
293 stt_client_s* client = stt_client_get(stt);
294 if (NULL == client) {
295 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
299 if (STT_STATE_READY != client->current_state) {
304 int ret = stt_unprepare(stt);
306 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
308 ret = stt_prepare(stt);
310 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
316 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)
318 stt_h stt = (stt_h)user_data;
320 stt_client_s* client = stt_client_get(stt);
321 if (NULL == client) {
322 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
326 if (NULL != engine_id) SLOG(LOG_DEBUG, TAG_STTC, "Engine id(%s)", engine_id);
327 if (NULL != setting) SLOG(LOG_DEBUG, TAG_STTC, "Engine setting(%s)", setting);
328 if (NULL != language) SLOG(LOG_DEBUG, TAG_STTC, "Language(%s)", language);
329 SLOG(LOG_DEBUG, TAG_STTC, "Silence(%s), Credential(%s)", support_silence ? "on" : "off", need_credential ? "need" : "no need");
331 /* When the default engine is changed, please unload the old engine and load the new one. */
334 if (NULL == client->current_engine_id) {
335 if (STT_STATE_RECORDING == client->current_state || STT_STATE_PROCESSING == client->current_state) {
336 SLOG(LOG_INFO, TAG_STTC, "[INFO] stt cancel is invoked by engine_changed_cb, state(%d)", client->current_state);
337 ret = stt_cancel(stt);
339 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] STT client cancelling...");
342 ecore_idler_add(__reconnect_by_engine_changed, (void*)stt);
343 } else if (STT_STATE_READY == client->current_state) {
344 ret = stt_unprepare(stt);
346 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
348 ret = stt_prepare(stt);
350 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
355 /* call callback function */
356 if (NULL != client->engine_changed_cb) {
357 client->engine_changed_cb(stt, engine_id, language, support_silence, need_credential, client->engine_changed_user_data);
359 SLOG(LOG_WARN, TAG_STTC, "No registered callback function for engine change");
364 static int __stt_check_handle(stt_h stt, stt_client_s** client)
367 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input handle is null");
368 return STT_ERROR_INVALID_PARAMETER;
371 stt_client_s* temp = NULL;
372 temp = stt_client_get(stt);
376 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] A handle is not available");
377 return STT_ERROR_INVALID_PARAMETER;
381 return STT_ERROR_NONE;
384 int stt_create(stt_h* stt)
386 if (0 != __stt_get_feature_enabled()) {
387 return STT_ERROR_NOT_SUPPORTED;
389 if (0 != __stt_check_privilege()) {
390 return STT_ERROR_PERMISSION_DENIED;
393 SLOG(LOG_INFO, TAG_STTC, "===== Create STT");
396 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] A handle is null");
397 return STT_ERROR_INVALID_PARAMETER;
400 if (0 == stt_client_get_size()) {
401 if (0 != stt_dbus_open_connection()) {
403 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to open connection");
404 return STT_ERROR_OPERATION_FAILED;
409 if (0 != stt_client_new(stt)) {
411 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create client!");
412 return STT_ERROR_OUT_OF_MEMORY;
416 stt_client_s* client = stt_client_get(*stt);
417 if (NULL == client) {
418 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create client"); //LCOV_EXCL_LINE
419 stt_client_destroy(*stt);
420 return STT_ERROR_OPERATION_FAILED;
423 int ret = stt_config_mgr_initialize(client->uid);
424 ret = __stt_convert_config_error_code(ret);
426 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to init config manager : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
427 stt_client_destroy(*stt);
431 ret = stt_config_mgr_set_callback(client->uid, __stt_config_engine_changed_cb, __stt_config_lang_changed_cb, NULL, client->stt);
432 ret = __stt_convert_config_error_code(ret);
434 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set config changed : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
435 stt_client_destroy(*stt);
439 SLOG(LOG_INFO, TAG_STTC, "[Success] uid(%d)", (*stt)->handle);
441 SLOG(LOG_DEBUG, TAG_STTC, "=====");
442 SLOG(LOG_DEBUG, TAG_STTC, " ");
444 return STT_ERROR_NONE;
447 int stt_destroy(stt_h stt)
449 stt_client_s* client = NULL;
450 if (0 != __stt_get_feature_enabled()) {
451 return STT_ERROR_NOT_SUPPORTED;
453 if (0 != __stt_check_privilege()) {
454 return STT_ERROR_PERMISSION_DENIED;
456 if (0 != __stt_check_handle(stt, &client)) {
457 return STT_ERROR_INVALID_PARAMETER;
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 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 if (0 != __stt_get_feature_enabled()) {
536 return STT_ERROR_NOT_SUPPORTED;
538 if (0 != __stt_check_privilege()) {
539 return STT_ERROR_PERMISSION_DENIED;
541 if (0 != __stt_check_handle(stt, &client)) {
542 return STT_ERROR_INVALID_PARAMETER;
545 SLOG(LOG_INFO, TAG_STTC, "===== Foreach Supported engine");
547 if (NULL == callback) {
548 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
549 return STT_ERROR_INVALID_PARAMETER;
552 if (client->current_state != STT_STATE_CREATED) {
553 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
554 return STT_ERROR_INVALID_STATE;
557 client->supported_engine_cb = callback;
558 client->supported_engine_user_data = user_data;
561 ret = stt_config_mgr_get_engine_list(__stt_config_supported_engine_cb, client->stt);
562 ret = __stt_convert_config_error_code(ret);
564 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get engines : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
567 client->supported_engine_cb = NULL;
568 client->supported_engine_user_data = NULL;
570 SLOG(LOG_INFO, TAG_STTC, "=====");
571 SLOG(LOG_DEBUG, TAG_STTC, " ");
576 int stt_get_engine(stt_h stt, char** engine_id)
578 stt_client_s* client = NULL;
579 if (0 != __stt_get_feature_enabled()) {
580 return STT_ERROR_NOT_SUPPORTED;
582 if (0 != __stt_check_privilege()) {
583 return STT_ERROR_PERMISSION_DENIED;
585 if (0 != __stt_check_handle(stt, &client)) {
586 return STT_ERROR_INVALID_PARAMETER;
589 SLOG(LOG_INFO, TAG_STTC, "===== Get current engine");
591 if (NULL == engine_id) {
592 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
593 return STT_ERROR_INVALID_PARAMETER;
596 if (client->current_state != STT_STATE_CREATED) {
597 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
598 return STT_ERROR_INVALID_STATE;
603 if (NULL != client->current_engine_id) {
604 *engine_id = strdup(client->current_engine_id);
605 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", *engine_id);
608 ret = stt_config_mgr_get_engine(engine_id);
609 ret = __stt_convert_config_error_code(ret);
611 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request get current engine : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
613 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", *engine_id);
617 SLOG(LOG_INFO, TAG_STTC, "=====");
618 SLOG(LOG_DEBUG, TAG_STTC, " ");
623 int __stt_set_buxtonkey(const char* engine_id)
626 struct buxton_client * bux_cli;
627 struct buxton_layer * bux_layer;
628 struct buxton_value * bux_val;
630 int ret = buxton_open(&bux_cli, NULL, NULL);
632 SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] Fail to open buxton client"); //LCOV_EXCL_LINE
633 return STT_ERROR_OPERATION_FAILED;
635 SLOG(LOG_DEBUG, stt_tag(), "[DBUS-BUXTON2] buxton_open: %d", ret);
636 bux_layer = buxton_create_layer("system");
637 if (NULL == bux_layer) {
639 SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] buxton_create_layer FAIL");
640 buxton_close(bux_cli);
642 return STT_ERROR_OPERATION_FAILED;
645 bux_val = buxton_value_create_string(engine_id);
646 if (NULL == bux_val) {
648 SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] buxton_value_create_string FAIL");
649 buxton_free_layer(bux_layer);
650 buxton_close(bux_cli);
653 return STT_ERROR_OPERATION_FAILED;
657 ret = buxton_set_value_sync(bux_cli, bux_layer, STT_ENGINE_DB_CUSTOM, bux_val);
660 SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] Fail to set value sync");
661 buxton_value_free(bux_val);
662 buxton_free_layer(bux_layer);
663 buxton_close(bux_cli);
668 return STT_ERROR_OPERATION_FAILED;
671 SLOG(LOG_DEBUG, stt_tag(), "[DBUS-BUXTON2] buxton_set_value_sync: %d, %s", ret, STT_ENGINE_DB_CUSTOM);
673 buxton_value_free(bux_val);
674 buxton_free_layer(bux_layer);
675 buxton_close(bux_cli);
681 return STT_ERROR_NONE;
684 int stt_set_engine(stt_h stt, const char* engine_id)
686 stt_client_s* client = NULL;
687 if (0 != __stt_get_feature_enabled()) {
688 return STT_ERROR_NOT_SUPPORTED;
690 if (0 != __stt_check_privilege()) {
691 return STT_ERROR_PERMISSION_DENIED;
693 if (0 != __stt_check_privilege_for_applaunch()) {
694 return STT_ERROR_PERMISSION_DENIED;
696 if (0 != __stt_check_handle(stt, &client)) {
697 return STT_ERROR_INVALID_PARAMETER;
700 SLOG(LOG_INFO, TAG_STTC, "===== Set current engine");
702 if (NULL == engine_id) {
703 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
704 return STT_ERROR_INVALID_PARAMETER;
708 if (client->current_state != STT_STATE_CREATED) {
709 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
710 return STT_ERROR_INVALID_STATE;
713 if (NULL != client->current_engine_id) {
714 free(client->current_engine_id);
715 client->current_engine_id = NULL;
718 SLOG(LOG_INFO, TAG_STTC, "===== engined_id(%s)", engine_id);
720 client->current_engine_id = strdup(engine_id);
723 int ret = __stt_set_buxtonkey(engine_id);
725 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] set buxtonkey Failed!!!"); //LCOV_EXCL_LINE
729 SLOG(LOG_INFO, TAG_STTC, "=====");
730 SLOG(LOG_DEBUG, TAG_STTC, " ");
735 int stt_set_credential(stt_h stt, const char* credential)
737 stt_client_s* client = NULL;
738 if (0 != __stt_get_feature_enabled()) {
739 return STT_ERROR_NOT_SUPPORTED;
741 if (0 != __stt_check_privilege()) {
742 return STT_ERROR_PERMISSION_DENIED;
744 if (0 != __stt_check_handle(stt, &client)) {
745 return STT_ERROR_INVALID_PARAMETER;
748 SLOG(LOG_INFO, TAG_STTC, "===== Set credential");
750 if (NULL == credential) {
751 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
752 return STT_ERROR_INVALID_PARAMETER;
756 if (client->current_state != STT_STATE_CREATED && client->current_state != STT_STATE_READY) {
757 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not CREATED or READY", client->current_state); //LCOV_EXCL_LINE
758 return STT_ERROR_INVALID_STATE;
761 if (NULL != client->credential) {
762 free(client->credential);
763 client->credential = NULL;
765 client->credential = strdup(credential);
767 SLOG(LOG_INFO, TAG_STTC, "=====");
768 SLOG(LOG_DEBUG, TAG_STTC, " ");
770 return STT_ERROR_NONE;
773 int stt_set_private_data(stt_h stt, const char* key, const char* data)
775 stt_client_s* client = NULL;
776 if (0 != __stt_get_feature_enabled()) {
777 return STT_ERROR_NOT_SUPPORTED;
779 if (0 != __stt_check_privilege()) {
780 return STT_ERROR_PERMISSION_DENIED;
782 if (0 != __stt_check_handle(stt, &client)) {
783 return STT_ERROR_INVALID_PARAMETER;
786 SLOG(LOG_INFO, TAG_STTC, "===== Set private data");
788 if (NULL == key || NULL == data) {
789 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid parameter");
790 return STT_ERROR_INVALID_PARAMETER;
794 if (STT_STATE_READY != client->current_state) {
795 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
796 return STT_ERROR_INVALID_STATE;
799 if (true != client->internal && (0 == strcmp(key, "server") || 0 == strcmp(key, "rampcode") || 0 == strcmp(key, "epd"))) {
800 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] This is not an internal app"); //LCOV_EXCL_LINE
801 return STT_ERROR_INVALID_PARAMETER;
807 ret = stt_dbus_request_set_private_data(client->uid, key, data);
810 if (STT_ERROR_TIMED_OUT != ret) {
811 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set private data : %s", __stt_get_error_code(ret));
814 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry : %s", __stt_get_error_code(ret));
817 if (STT_RETRY_COUNT == count) {
818 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
826 SLOG(LOG_INFO, TAG_STTC, "=====");
827 SLOG(LOG_DEBUG, TAG_STTC, "");
829 return STT_ERROR_NONE;
832 int stt_get_private_data(stt_h stt, const char* key, char** data)
834 stt_client_s* client = NULL;
835 if (0 != __stt_get_feature_enabled()) {
836 return STT_ERROR_NOT_SUPPORTED;
838 if (0 != __stt_check_privilege()) {
839 return STT_ERROR_PERMISSION_DENIED;
841 if (0 != __stt_check_handle(stt, &client)) {
842 return STT_ERROR_INVALID_PARAMETER;
845 SLOG(LOG_INFO, TAG_STTC, "===== Get private data");
847 if (NULL == key || NULL == data) {
848 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid parameter");
849 return STT_ERROR_INVALID_PARAMETER;
853 if (STT_STATE_READY != client->current_state) {
854 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
855 return STT_ERROR_INVALID_STATE;
861 ret = stt_dbus_request_get_private_data(client->uid, key, data);
864 if (STT_ERROR_TIMED_OUT != ret) {
865 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get private data : %s", __stt_get_error_code(ret));
868 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry : %s", __stt_get_error_code(ret));
871 if (STT_RETRY_COUNT == count) {
872 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
880 if (0 == strncmp(*data, "NULL", strlen(*data))) {
885 SLOG(LOG_INFO, TAG_STTC, "=====");
886 SLOG(LOG_DEBUG, TAG_STTC, "");
888 return STT_ERROR_NONE;
892 int stt_set_server_stt(stt_h stt, const char* key, char* user_data)
895 stt_client_s* client = NULL;
897 if (0 != __stt_get_feature_enabled()) {
898 return STT_ERROR_NOT_SUPPORTED;
900 if (0 != __stt_check_privilege()) {
901 return STT_ERROR_PERMISSION_DENIED;
903 if (0 != __stt_check_handle(stt, &client)) {
904 return STT_ERROR_INVALID_PARAMETER;
907 SLOG(LOG_INFO, TAG_STTC, "===== Set STT server");
909 if (NULL == key || NULL == user_data) {
910 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid parameter");
911 return STT_ERROR_INVALID_PARAMETER;
914 if (STT_STATE_CREATED != client->current_state && STT_STATE_READY != client->current_state) {
915 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] The current state is invalid (%d).", client->current_state);
916 return STT_ERROR_INVALID_STATE;
920 client->internal = true;
922 char* private_key = NULL;
923 private_key = strdup(key);
924 if (NULL == private_key) {
925 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory(private_key)");
926 return STT_ERROR_OUT_OF_MEMORY;
930 data = strdup(user_data);
932 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory(data)");
935 return STT_ERROR_OUT_OF_MEMORY;
938 ret = stt_set_private_data(stt, private_key, data);
940 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set private data, ret(%d), key(%s)", ret, private_key);
948 SLOG(LOG_INFO, TAG_STTC, "======");
949 SLOG(LOG_DEBUG, TAG_STTC, " ");
955 static Eina_Bool __stt_connect_daemon(void *data)
957 stt_client_s* client = (stt_client_s*)data;
960 if (NULL == client) {
961 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] A handle is not available"); //LCOV_EXCL_LINE
962 g_connect_timer = NULL;
966 if (0 == stt_client_get_size() || NULL == stt_client_get_by_uid(client->uid)) {
967 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Client has been already destroyed"); //LCOV_EXCL_LINE
971 /* Check and Set vconfkey of custom engine before sending hello */
972 if (NULL != client->current_engine_id && 0 == __stt_check_privilege_for_applaunch()) {
974 ret = __stt_set_buxtonkey(client->current_engine_id);
977 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] set buxtonkey Failed!!! (inside __stt_connect_daemon)");
984 ret = stt_dbus_request_hello(client->uid);
987 if (STT_ERROR_INVALID_STATE == ret) {
988 g_connect_timer = NULL;
994 g_connect_timer = NULL;
995 SLOG(LOG_INFO, TAG_STTC, "===== Connect stt-service");
997 /* request initialization */
998 bool silence_supported = false;
999 bool credential_needed = false;
1001 ret = stt_dbus_request_initialize(client->uid, &silence_supported, &credential_needed);
1003 if (STT_ERROR_ENGINE_NOT_FOUND == ret) {
1004 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to initialize : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1006 client->reason = STT_ERROR_ENGINE_NOT_FOUND;
1007 ecore_main_loop_thread_safe_call_async(__stt_notify_error, (void*)client);
1011 } else if (STT_ERROR_NONE != ret) {
1012 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] Fail to connection. Retry to connect"); //LCOV_EXCL_LINE
1015 /* success to connect stt-service */
1016 client->silence_supported = silence_supported;
1017 client->credential_needed = credential_needed;
1018 SLOG(LOG_DEBUG, TAG_STTC, "Supported options : silence(%s), credential(%s)", silence_supported ? "support" : "no support", credential_needed ? "need" : "no need");
1022 #ifdef __UNUSED_CODES__
1023 if (NULL != client->current_engine_id) {
1026 silence_supported = false;
1027 credential_needed = false;
1028 SLOG(LOG_DEBUG, TAG_STTC, "[WARNING] current_engine_id(%s)", client->current_engine_id);
1031 ret = stt_dbus_request_set_current_engine(client->uid, client->current_engine_id, &silence_supported, &credential_needed);
1033 if (STT_ERROR_TIMED_OUT != ret) {
1034 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set current engine : %s", __stt_get_error_code(ret));
1037 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1040 if (STT_RETRY_COUNT == count) {
1041 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1046 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", client->current_engine_id);
1048 /* success to change engine */
1049 client->silence_supported = silence_supported;
1050 SLOG(LOG_DEBUG, TAG_STTC, "Supported options : silence(%s), credential(%s)", silence_supported ? "support" : "no support", credential_needed ? "need" : "no need");
1056 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] uid(%d)", client->uid);
1058 client->before_state = client->current_state;
1059 client->current_state = STT_STATE_READY;
1061 if (NULL != client->state_changed_cb) {
1062 stt_client_use_callback(client);
1063 client->state_changed_cb(client->stt, client->before_state,
1064 client->current_state, client->state_changed_user_data);
1065 stt_client_not_use_callback(client);
1066 SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called");
1068 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
1071 SLOG(LOG_INFO, TAG_STTC, "=====");
1072 SLOG(LOG_DEBUG, TAG_STTC, " ");
1077 int stt_prepare(stt_h stt)
1079 stt_client_s* client = NULL;
1080 if (0 != __stt_get_feature_enabled()) {
1081 return STT_ERROR_NOT_SUPPORTED;
1083 if (0 != __stt_check_privilege()) {
1084 return STT_ERROR_PERMISSION_DENIED;
1086 if (0 != __stt_check_handle(stt, &client)) {
1087 return STT_ERROR_INVALID_PARAMETER;
1090 SLOG(LOG_INFO, TAG_STTC, "===== Prepare STT");
1093 if (client->current_state != STT_STATE_CREATED) {
1094 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not 'CREATED'", client->current_state);
1095 return STT_ERROR_INVALID_STATE;
1098 ecore_thread_main_loop_begin();
1099 g_connect_timer = ecore_timer_add(0.02, __stt_connect_daemon, (void*)client);
1100 ecore_thread_main_loop_end();
1102 SLOG(LOG_INFO, TAG_STTC, "=====");
1103 SLOG(LOG_DEBUG, TAG_STTC, " ");
1105 return STT_ERROR_NONE;
1108 int stt_unprepare(stt_h stt)
1110 stt_client_s* client = NULL;
1111 if (0 != __stt_get_feature_enabled()) {
1112 return STT_ERROR_NOT_SUPPORTED;
1114 if (0 != __stt_check_privilege()) {
1115 return STT_ERROR_PERMISSION_DENIED;
1117 if (0 != __stt_check_handle(stt, &client)) {
1118 return STT_ERROR_INVALID_PARAMETER;
1121 SLOG(LOG_INFO, TAG_STTC, "===== Unprepare STT");
1124 if (client->current_state != STT_STATE_READY) {
1125 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not 'READY'", client->current_state);
1126 return STT_ERROR_INVALID_STATE;
1132 ret = stt_dbus_request_finalize(client->uid);
1135 if (STT_ERROR_TIMED_OUT != ret) {
1136 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request finalize : %s", __stt_get_error_code(ret));
1139 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1142 if (STT_RETRY_COUNT == count) {
1143 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1151 client->internal_state = STT_INTERNAL_STATE_NONE;
1153 client->before_state = client->current_state;
1154 client->current_state = STT_STATE_CREATED;
1156 if (NULL != client->state_changed_cb) {
1157 stt_client_use_callback(client);
1158 client->state_changed_cb(client->stt, client->before_state,
1159 client->current_state, client->state_changed_user_data);
1160 stt_client_not_use_callback(client);
1162 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
1165 if (g_connect_timer) {
1166 ecore_timer_del(g_connect_timer);
1167 g_connect_timer = NULL;
1170 SLOG(LOG_INFO, TAG_STTC, "=====");
1171 SLOG(LOG_DEBUG, TAG_STTC, " ");
1173 return STT_ERROR_NONE;
1176 bool __stt_config_supported_language_cb(const char* engine_id, const char* language, void* user_data)
1178 stt_h stt = (stt_h)user_data;
1180 stt_client_s* client = stt_client_get(stt);
1181 if (NULL == client) {
1182 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid"); //LCOV_EXCL_LINE
1186 SLOG(LOG_INFO, TAG_STTC, "===== supported language callback");
1188 /* call callback function */
1189 if (NULL != client->supported_lang_cb) {
1190 return client->supported_lang_cb(stt, language, client->supported_lang_user_data);
1192 SLOG(LOG_WARN, TAG_STTC, "No registered callback function of supported languages"); //LCOV_EXCL_LINE
1198 int stt_foreach_supported_languages(stt_h stt, stt_supported_language_cb callback, void* user_data)
1200 stt_client_s* client = NULL;
1201 if (0 != __stt_get_feature_enabled()) {
1202 return STT_ERROR_NOT_SUPPORTED;
1204 if (0 != __stt_check_privilege()) {
1205 return STT_ERROR_PERMISSION_DENIED;
1207 if (0 != __stt_check_handle(stt, &client)) {
1208 return STT_ERROR_INVALID_PARAMETER;
1211 SLOG(LOG_INFO, TAG_STTC, "===== Foreach Supported Language");
1213 if (NULL == callback) {
1214 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1215 return STT_ERROR_INVALID_PARAMETER;
1219 char* current_engine_id = NULL;
1221 if (NULL == client->current_engine_id) {
1222 ret = stt_config_mgr_get_engine(¤t_engine_id);
1223 ret = __stt_convert_config_error_code(ret);
1225 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default engine id : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1229 current_engine_id = strdup(client->current_engine_id);
1231 if (NULL == current_engine_id) {
1232 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
1233 return STT_ERROR_OUT_OF_MEMORY;
1238 client->supported_lang_cb = callback;
1239 client->supported_lang_user_data = user_data;
1241 ret = stt_config_mgr_get_language_list(current_engine_id, __stt_config_supported_language_cb, client->stt);
1242 ret = __stt_convert_config_error_code(ret);
1244 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get languages : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1247 if (NULL != current_engine_id) {
1248 free(current_engine_id);
1249 current_engine_id = NULL;
1252 client->supported_lang_cb = NULL;
1253 client->supported_lang_user_data = NULL;
1255 SLOG(LOG_INFO, TAG_STTC, "=====");
1256 SLOG(LOG_DEBUG, TAG_STTC, " ");
1261 int stt_get_default_language(stt_h stt, char** language)
1263 stt_client_s* client = NULL;
1264 if (0 != __stt_get_feature_enabled()) {
1265 return STT_ERROR_NOT_SUPPORTED;
1267 if (0 != __stt_check_privilege()) {
1268 return STT_ERROR_PERMISSION_DENIED;
1270 if (0 != __stt_check_handle(stt, &client)) {
1271 return STT_ERROR_INVALID_PARAMETER;
1274 SLOG(LOG_INFO, TAG_STTC, "===== Get Default Language");
1276 if (NULL == language) {
1277 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1278 return STT_ERROR_INVALID_PARAMETER;
1282 ret = stt_config_mgr_get_default_language(language);
1283 ret = __stt_convert_config_error_code(ret);
1285 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default language : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1287 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current language = %s", *language);
1290 SLOG(LOG_INFO, TAG_STTC, "=====");
1291 SLOG(LOG_DEBUG, TAG_STTC, " ");
1296 int stt_get_state(stt_h stt, stt_state_e* state)
1298 stt_client_s* client = NULL;
1299 if (0 != __stt_get_feature_enabled()) {
1300 return STT_ERROR_NOT_SUPPORTED;
1302 if (0 != __stt_check_privilege()) {
1303 return STT_ERROR_PERMISSION_DENIED;
1305 if (0 != __stt_check_handle(stt, &client)) {
1306 return STT_ERROR_INVALID_PARAMETER;
1309 if (NULL == state) {
1310 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1311 return STT_ERROR_INVALID_PARAMETER;
1314 *state = client->current_state;
1316 SLOG(LOG_INFO, TAG_STTC, "===== Get state(%d)", *state);
1319 case STT_STATE_CREATED: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'CREATED'"); break;
1320 case STT_STATE_READY: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Ready'"); break;
1321 case STT_STATE_RECORDING: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Recording'"); break;
1322 case STT_STATE_PROCESSING: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Processing'"); break;
1323 default: SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid value"); break;
1326 return STT_ERROR_NONE;
1329 int stt_get_error_message(stt_h stt, char** err_msg)
1331 stt_client_s* client = NULL;
1332 if (0 != __stt_get_feature_enabled()) {
1333 return STT_ERROR_NOT_SUPPORTED;
1335 if (0 != __stt_check_privilege()) {
1336 return STT_ERROR_PERMISSION_DENIED;
1338 if (0 != __stt_check_handle(stt, &client)) {
1339 return STT_ERROR_INVALID_PARAMETER;
1342 if (NULL == err_msg) {
1343 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1344 return STT_ERROR_INVALID_PARAMETER;
1347 if (false == g_err_callback_status) {
1348 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] This callback should be called during an err_callback");
1349 return STT_ERROR_OPERATION_FAILED;
1352 if (NULL != client->err_msg) {
1353 *err_msg = strdup(client->err_msg);
1354 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Error msg (%s)", *err_msg); //LCOV_EXCL_LINE
1356 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Error msg (NULL)"); //LCOV_EXCL_LINE
1359 SLOG(LOG_INFO, TAG_STTC, "====="); //LCOV_EXCL_LINE
1360 SLOG(LOG_DEBUG, TAG_STTC, " "); //LCOV_EXCL_LINE
1362 return STT_ERROR_NONE;
1365 int stt_is_recognition_type_supported(stt_h stt, const char* type, bool* support)
1367 stt_client_s* client = NULL;
1368 if (0 != __stt_get_feature_enabled()) {
1369 return STT_ERROR_NOT_SUPPORTED;
1371 if (0 != __stt_check_privilege()) {
1372 return STT_ERROR_PERMISSION_DENIED;
1374 if (0 != __stt_check_handle(stt, &client)) {
1375 return STT_ERROR_INVALID_PARAMETER;
1378 if (NULL == type || NULL == support) {
1379 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1380 return STT_ERROR_INVALID_PARAMETER;
1384 if (client->current_state != STT_STATE_READY) {
1385 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1386 return STT_ERROR_INVALID_STATE;
1392 ret = stt_dbus_request_is_recognition_type_supported(client->uid, type, support);
1395 if (STT_ERROR_TIMED_OUT != ret) {
1396 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get recognition type supported : %s", __stt_get_error_code(ret));
1399 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1402 if (STT_RETRY_COUNT == count) {
1403 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1409 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] recognition type is %s", *support ? "true " : "false");
1414 return STT_ERROR_NONE;
1417 int stt_set_silence_detection(stt_h stt, stt_option_silence_detection_e type)
1419 stt_client_s* client = NULL;
1420 if (0 != __stt_get_feature_enabled()) {
1421 return STT_ERROR_NOT_SUPPORTED;
1423 if (0 != __stt_check_privilege()) {
1424 return STT_ERROR_PERMISSION_DENIED;
1426 if (0 != __stt_check_handle(stt, &client)) {
1427 return STT_ERROR_INVALID_PARAMETER;
1431 if (client->current_state != STT_STATE_READY) {
1432 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1433 return STT_ERROR_INVALID_STATE;
1436 SLOG(LOG_INFO, TAG_STTC, "===== Set silence detection, supported(%d), type(%d)", client->silence_supported, type);
1438 if (true == client->silence_supported) {
1439 if (type >= STT_OPTION_SILENCE_DETECTION_FALSE && type <= STT_OPTION_SILENCE_DETECTION_AUTO) {
1440 client->silence = type;
1442 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Type is invalid");
1443 return STT_ERROR_INVALID_PARAMETER;
1446 return STT_ERROR_NOT_SUPPORTED_FEATURE;
1449 return STT_ERROR_NONE;
1452 int stt_set_start_sound(stt_h stt, const char* filename)
1454 stt_client_s* client = NULL;
1455 if (0 != __stt_get_feature_enabled()) {
1456 return STT_ERROR_NOT_SUPPORTED;
1458 if (0 != __stt_check_privilege()) {
1459 return STT_ERROR_PERMISSION_DENIED;
1461 if (0 != __stt_check_handle(stt, &client)) {
1462 return STT_ERROR_INVALID_PARAMETER;
1465 SLOG(LOG_INFO, TAG_STTC, "===== STT SET START SOUND");
1467 if (NULL == filename) {
1468 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1469 return STT_ERROR_INVALID_PARAMETER;
1472 if (0 != access(filename, F_OK)) {
1473 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] File does not exist"); //LCOV_EXCL_LINE
1474 return STT_ERROR_INVALID_PARAMETER;
1478 if (client->current_state != STT_STATE_READY) {
1479 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state); //LCOV_EXCL_LINE
1480 return STT_ERROR_INVALID_STATE;
1486 ret = stt_dbus_request_set_start_sound(client->uid, filename);
1489 if (STT_ERROR_TIMED_OUT != ret) {
1490 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set start sound : %s", __stt_get_error_code(ret));
1493 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1496 if (STT_RETRY_COUNT == count) {
1497 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1503 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Set start sound : %s", filename);
1508 return STT_ERROR_NONE;
1511 int stt_unset_start_sound(stt_h stt)
1513 stt_client_s* client = NULL;
1514 if (0 != __stt_get_feature_enabled()) {
1515 return STT_ERROR_NOT_SUPPORTED;
1517 if (0 != __stt_check_privilege()) {
1518 return STT_ERROR_PERMISSION_DENIED;
1520 if (0 != __stt_check_handle(stt, &client)) {
1521 return STT_ERROR_INVALID_PARAMETER;
1524 SLOG(LOG_INFO, TAG_STTC, "===== STT UNSET START SOUND");
1527 if (client->current_state != STT_STATE_READY) {
1528 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1529 return STT_ERROR_INVALID_STATE;
1535 ret = stt_dbus_request_unset_start_sound(client->uid);
1538 if (STT_ERROR_TIMED_OUT != ret) {
1539 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset start sound : %s", __stt_get_error_code(ret));
1542 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1545 if (STT_RETRY_COUNT == count) {
1546 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1552 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Unset start sound");
1557 return STT_ERROR_NONE;
1560 int stt_set_stop_sound(stt_h stt, const char* filename)
1562 stt_client_s* client = NULL;
1563 if (0 != __stt_get_feature_enabled()) {
1564 return STT_ERROR_NOT_SUPPORTED;
1566 if (0 != __stt_check_privilege()) {
1567 return STT_ERROR_PERMISSION_DENIED;
1569 if (0 != __stt_check_handle(stt, &client)) {
1570 return STT_ERROR_INVALID_PARAMETER;
1573 SLOG(LOG_INFO, TAG_STTC, "===== STT SET STOP SOUND");
1575 if (NULL == filename) {
1576 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1577 return STT_ERROR_INVALID_PARAMETER;
1580 if (0 != access(filename, F_OK)) {
1581 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] File does not exist"); //LCOV_EXCL_LINE
1582 return STT_ERROR_INVALID_PARAMETER;
1586 if (client->current_state != STT_STATE_READY) {
1587 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state); //LCOV_EXCL_LINE
1588 return STT_ERROR_INVALID_STATE;
1594 ret = stt_dbus_request_set_stop_sound(client->uid, filename);
1597 if (STT_ERROR_TIMED_OUT != ret) {
1598 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set stop sound : %s", __stt_get_error_code(ret));
1601 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1604 if (STT_RETRY_COUNT == count) {
1605 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1611 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Set stop sound : %s", filename);
1616 return STT_ERROR_NONE;
1619 int stt_unset_stop_sound(stt_h stt)
1621 stt_client_s* client = NULL;
1622 if (0 != __stt_get_feature_enabled()) {
1623 return STT_ERROR_NOT_SUPPORTED;
1625 if (0 != __stt_check_privilege()) {
1626 return STT_ERROR_PERMISSION_DENIED;
1628 if (0 != __stt_check_handle(stt, &client)) {
1629 return STT_ERROR_INVALID_PARAMETER;
1632 SLOG(LOG_INFO, TAG_STTC, "===== STT UNSET STOP SOUND");
1635 if (client->current_state != STT_STATE_READY) {
1636 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1637 return STT_ERROR_INVALID_STATE;
1643 ret = stt_dbus_request_unset_stop_sound(client->uid);
1646 if (STT_ERROR_TIMED_OUT != ret) {
1647 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset stop sound : %s", __stt_get_error_code(ret));
1650 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1653 if (STT_RETRY_COUNT == count) {
1654 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1660 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Unset stop sound");
1665 return STT_ERROR_NONE;
1668 int stt_start(stt_h stt, const char* language, const char* type)
1670 stt_client_s* client = NULL;
1671 if (0 != __stt_get_feature_enabled()) {
1672 return STT_ERROR_NOT_SUPPORTED;
1674 if (0 != __stt_check_privilege()) {
1675 return STT_ERROR_PERMISSION_DENIED;
1677 if (0 != __stt_check_handle(stt, &client)) {
1678 return STT_ERROR_INVALID_PARAMETER;
1681 SLOG(LOG_INFO, TAG_STTC, "===== STT START");
1684 if (client->current_state != STT_STATE_READY) {
1685 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1686 return STT_ERROR_INVALID_STATE;
1689 if (STT_INTERNAL_STATE_NONE != client->internal_state) {
1690 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is NOT none : %d", client->internal_state); //LCOV_EXCL_LINE
1691 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1695 char appid[1024] = {0, };
1696 ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
1698 if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1699 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID"); //LCOV_EXCL_LINE
1701 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
1705 if (NULL == language) {
1706 temp = strdup("default");
1708 temp = strdup(language);
1712 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
1713 return STT_ERROR_OUT_OF_MEMORY;
1716 if (true == client->credential_needed && NULL == client->credential) {
1717 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id); //LCOV_EXCL_LINE
1720 return STT_ERROR_PERMISSION_DENIED;
1723 client->internal_state = STT_INTERNAL_STATE_STARTING;
1724 ret = stt_dbus_request_start(client->uid, temp, type, client->silence, appid, client->credential);
1726 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1727 client->internal_state = STT_INTERNAL_STATE_NONE;
1729 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start is successful but not done");
1735 SLOG(LOG_INFO, TAG_STTC, "=====");
1736 SLOG(LOG_DEBUG, TAG_STTC, " ");
1741 int stt_stop(stt_h stt)
1743 stt_client_s* client = NULL;
1744 if (0 != __stt_get_feature_enabled()) {
1745 return STT_ERROR_NOT_SUPPORTED;
1747 if (0 != __stt_check_privilege()) {
1748 return STT_ERROR_PERMISSION_DENIED;
1750 if (0 != __stt_check_handle(stt, &client)) {
1751 return STT_ERROR_INVALID_PARAMETER;
1754 SLOG(LOG_INFO, TAG_STTC, "===== STT STOP");
1757 if (client->current_state != STT_STATE_RECORDING) {
1758 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Current state(%d) is NOT RECORDING", client->current_state); //LCOV_EXCL_LINE
1759 return STT_ERROR_INVALID_STATE;
1762 if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1763 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state); //LCOV_EXCL_LINE
1764 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1765 } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1766 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state); //LCOV_EXCL_LINE
1767 return STT_ERROR_IN_PROGRESS_TO_READY;
1768 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1769 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state); //LCOV_EXCL_LINE
1770 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1773 client->internal_state = STT_INTERNAL_STATE_STOPPING;
1774 int ret = stt_dbus_request_stop(client->uid);
1776 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to stop : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1777 client->internal_state = STT_INTERNAL_STATE_NONE;
1779 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Stop is successful but not done");
1782 SLOG(LOG_DEBUG, TAG_STTC, "=====");
1783 SLOG(LOG_DEBUG, TAG_STTC, " ");
1789 int stt_cancel(stt_h stt)
1791 stt_client_s* client = NULL;
1792 if (0 != __stt_get_feature_enabled()) {
1793 return STT_ERROR_NOT_SUPPORTED;
1795 if (0 != __stt_check_privilege()) {
1796 return STT_ERROR_PERMISSION_DENIED;
1798 if (0 != __stt_check_handle(stt, &client)) {
1799 return STT_ERROR_INVALID_PARAMETER;
1802 SLOG(LOG_INFO, TAG_STTC, "===== STT CANCEL");
1805 if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
1806 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state); //LCOV_EXCL_LINE
1807 return STT_ERROR_INVALID_STATE;
1810 if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1811 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state); //LCOV_EXCL_LINE
1812 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1813 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1814 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state); //LCOV_EXCL_LINE
1815 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1816 } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1817 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state); //LCOV_EXCL_LINE
1818 return STT_ERROR_IN_PROGRESS_TO_READY;
1821 client->internal_state = STT_INTERNAL_STATE_CANCELING;
1822 int ret = stt_dbus_request_cancel(client->uid);
1824 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1825 client->internal_state = STT_INTERNAL_STATE_NONE;
1827 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Cancel is successful but not done");
1830 SLOG(LOG_DEBUG, TAG_STTC, "=====");
1831 SLOG(LOG_DEBUG, TAG_STTC, " ");
1836 int __stt_cb_set_volume(int uid, float volume)
1838 stt_client_s* client = NULL;
1840 client = stt_client_get_by_uid(uid);
1841 if (NULL == client) {
1843 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
1844 return STT_ERROR_INVALID_PARAMETER;
1848 if (STT_STATE_RECORDING != client->current_state) {
1850 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1851 return STT_ERROR_INVALID_STATE;
1855 g_volume_db = volume;
1856 SLOG(LOG_INFO, TAG_STTC, "Set volume (%f)", g_volume_db);
1861 int stt_get_recording_volume(stt_h stt, float* volume)
1863 stt_client_s* client = NULL;
1864 if (0 != __stt_get_feature_enabled()) {
1865 return STT_ERROR_NOT_SUPPORTED;
1867 if (0 != __stt_check_privilege()) {
1868 return STT_ERROR_PERMISSION_DENIED;
1870 if (0 != __stt_check_handle(stt, &client)) {
1871 return STT_ERROR_INVALID_PARAMETER;
1874 if (NULL == volume) {
1875 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1876 return STT_ERROR_INVALID_PARAMETER;
1879 if (STT_STATE_RECORDING != client->current_state) {
1880 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state); //LCOV_EXCL_LINE
1881 return STT_ERROR_INVALID_STATE;
1884 *volume = g_volume_db;
1886 SLOG(LOG_INFO, TAG_STTC, "Get recording volume (%f)", *volume);
1888 return STT_ERROR_NONE;
1892 bool __stt_result_time_cb(int index, int event, const char* text, long start_time, long end_time, void* user_data)
1894 stt_client_s* client = (stt_client_s*)user_data;
1897 if (NULL == client) {
1898 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1902 if (NULL != client->result_time_cb) {
1903 SLOG(LOG_INFO, TAG_STTC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
1904 index, event, text, start_time, end_time);
1905 client->result_time_cb(client->stt, index, (stt_result_time_event_e)event,
1906 text, start_time, end_time, client->result_time_user_data);
1908 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Callback is NULL");
1916 int stt_foreach_detailed_result(stt_h stt, stt_result_time_cb callback, void* user_data)
1918 stt_client_s* client = NULL;
1919 if (0 != __stt_get_feature_enabled()) {
1920 return STT_ERROR_NOT_SUPPORTED;
1922 if (0 != __stt_check_privilege()) {
1923 return STT_ERROR_PERMISSION_DENIED;
1925 if (0 != __stt_check_handle(stt, &client)) {
1926 return STT_ERROR_INVALID_PARAMETER;
1929 SLOG(LOG_INFO, TAG_STTC, "===== STT FOREACH DETAILED RESULT");
1931 if (NULL == callback) {
1932 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1933 return STT_ERROR_INVALID_PARAMETER;
1936 client->result_time_cb = callback;
1937 client->result_time_user_data = user_data;
1940 ret = stt_config_mgr_foreach_time_info(__stt_result_time_cb, client);
1941 ret = __stt_convert_config_error_code(ret);
1943 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to foreach time info : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1946 client->result_time_cb = NULL;
1947 client->result_time_user_data = NULL;
1949 SLOG(LOG_INFO, TAG_STTC, "=====");
1950 SLOG(LOG_DEBUG, TAG_STTC, " ");
1955 static void __stt_notify_error(void *data)
1957 stt_client_s* client = (stt_client_s*)data;
1959 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error from sttd");
1962 if (NULL == client) {
1963 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid"); //LCOV_EXCL_LINE
1967 if (NULL == stt_client_get_by_uid(client->uid))
1970 if (NULL != client->error_cb) {
1971 stt_client_use_callback(client);
1972 g_err_callback_status = true;
1973 client->error_cb(client->stt, client->reason, client->error_user_data);
1974 g_err_callback_status = false;
1975 stt_client_not_use_callback(client);
1976 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is called : reason [%d]", client->reason);
1978 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null"); //LCOV_EXCL_LINE
1984 int __stt_cb_error(int uid, int reason, char* err_msg)
1988 GList* client_list = NULL;
1989 client_list = stt_client_get_client_list();
1992 stt_client_s *data = NULL;
1994 if (g_list_length(client_list) > 0) {
1995 /* Get a first item */
1996 iter = g_list_first(client_list);
1998 while (NULL != iter) {
2001 data->reason = reason;
2002 data->internal_state = STT_INTERNAL_STATE_NONE;
2003 if (NULL != data->err_msg) {
2004 free(data->err_msg);
2005 data->err_msg = NULL;
2007 if (NULL != err_msg)
2008 data->err_msg = strdup(err_msg);
2010 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
2012 if (NULL != data->error_cb) {
2013 ecore_main_loop_thread_safe_call_async(__stt_notify_error, data);
2015 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
2018 if (STT_ERROR_SERVICE_RESET == reason) {
2019 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
2021 data->current_state = STT_STATE_CREATED;
2022 if (0 != stt_prepare(data->stt)) {
2023 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
2028 iter = g_list_next(iter);
2033 stt_client_s* client = stt_client_get_by_uid(uid);
2034 if (NULL == client) {
2035 SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
2039 client->reason = reason;
2040 client->internal_state = STT_INTERNAL_STATE_NONE;
2041 if (NULL != client->err_msg) {
2042 free(client->err_msg);
2043 client->err_msg = NULL;
2045 if (NULL != err_msg)
2046 client->err_msg = strdup(err_msg);
2048 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
2050 if (NULL != client->error_cb) {
2051 ecore_main_loop_thread_safe_call_async(__stt_notify_error, client);
2053 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
2056 if (STT_ERROR_SERVICE_RESET == reason) {
2057 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset"); //LCOV_EXCL_LINE
2059 client->current_state = STT_STATE_CREATED;
2060 if (0 != stt_prepare(client->stt)) {
2061 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare"); //LCOV_EXCL_LINE
2069 static void __stt_notify_state_changed(void *data)
2071 stt_client_s* client = (stt_client_s*)data;
2074 if (NULL == client) {
2075 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid"); //LCOV_EXCL_LINE
2079 if (NULL == stt_client_get_by_uid(client->uid)) {
2083 if (STT_INTERNAL_STATE_STARTING == client->internal_state && STT_STATE_RECORDING == client->current_state) {
2084 client->internal_state = STT_INTERNAL_STATE_NONE;
2085 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
2086 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state && STT_STATE_PROCESSING == client->current_state) {
2087 client->internal_state = STT_INTERNAL_STATE_NONE;
2088 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
2089 } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state && STT_STATE_READY == client->current_state) {
2090 client->internal_state = STT_INTERNAL_STATE_NONE;
2091 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
2094 if (NULL != client->state_changed_cb) {
2095 stt_client_use_callback(client);
2096 client->state_changed_cb(client->stt, client->before_state,
2097 client->current_state, client->state_changed_user_data);
2098 stt_client_not_use_callback(client);
2099 SLOG(LOG_INFO, TAG_STTC, "State changed callback is called, State(%d)", client->current_state);
2101 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null, State(%d)", client->current_state); //LCOV_EXCL_LINE
2107 static Eina_Bool __stt_notify_result(void *data)
2109 stt_client_s* client = (stt_client_s*)data;
2112 if (NULL == client) {
2113 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid"); //LCOV_EXCL_LINE
2117 if (NULL == stt_client_get_by_uid(client->uid)) {
2121 if (NULL != client->recognition_result_cb) {
2122 stt_client_use_callback(client);
2123 client->recognition_result_cb(client->stt, client->event, (const char**)client->data_list, client->data_count,
2124 client->msg, client->recognition_result_user_data);
2125 stt_client_not_use_callback(client);
2126 SLOG(LOG_INFO, TAG_STTC, "client recognition result callback called");
2128 SLOG(LOG_WARN, TAG_STTC, "[WARNING] User recognition result callback is NULL");
2131 if (NULL != client->msg) {
2136 if (NULL != client->data_list) {
2138 temp = client->data_list;
2141 for (i = 0; i < client->data_count; i++) {
2142 if (NULL != temp[i]) {
2146 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error"); //LCOV_EXCL_LINE
2149 free(client->data_list);
2150 client->data_list = NULL;
2153 client->data_count = 0;
2155 stt_config_mgr_remove_time_info_file();
2157 if (STT_RESULT_EVENT_FINAL_RESULT == client->event || STT_RESULT_EVENT_ERROR == client->event) {
2158 client->before_state = client->current_state;
2159 client->current_state = STT_STATE_READY;
2161 if (NULL != client->state_changed_cb) {
2162 ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
2164 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null"); //LCOV_EXCL_LINE
2171 int __stt_cb_result(int uid, int event, char** data, int data_count, const char* msg)
2173 stt_client_s* client = NULL;
2175 client = stt_client_get_by_uid(uid);
2176 if (NULL == client) {
2177 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid"); //LCOV_EXCL_LINE
2178 return STT_ERROR_INVALID_PARAMETER;
2182 SECURE_SLOG(LOG_INFO, TAG_STTC, "Recognition Result Message = %s", msg);
2185 for (i = 0; i < data_count; i++) {
2186 if (NULL != data[i])
2187 SECURE_SLOG(LOG_INFO, TAG_STTC, "Recognition Result[%d] = %s", i, data[i]);
2190 if (NULL != client->recognition_result_cb) {
2191 client->event = event;
2193 client->msg = strdup(msg);
2196 client->data_count = data_count;
2198 if (data_count > 0) {
2200 temp = (char**)calloc(data_count, sizeof(char*));
2202 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
2203 return STT_ERROR_OUT_OF_MEMORY;
2206 for (i = 0; i < data_count; i++) {
2207 if (NULL != data[i])
2208 temp[i] = strdup(data[i]);
2210 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error"); //LCOV_EXCL_LINE
2213 client->data_list = temp;
2216 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] User result callback is null");
2219 __stt_notify_result(client);
2221 return STT_ERROR_NONE;
2224 int __stt_cb_set_state(int uid, int state)
2226 stt_client_s* client = stt_client_get_by_uid(uid);
2227 if (NULL == client) {
2228 SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
2232 stt_state_e state_from_daemon = (stt_state_e)state;
2234 if (client->current_state == state_from_daemon) {
2235 SLOG(LOG_DEBUG, TAG_STTC, "Current state has already been %d", client->current_state);
2239 client->before_state = client->current_state;
2240 client->current_state = state_from_daemon;
2242 ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
2246 static void __stt_notify_speech_status(void *data)
2248 stt_client_s* client = (stt_client_s*)data;
2251 if (NULL == client) {
2252 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify speech status : A handle is not valid"); //LCOV_EXCL_LINE
2256 if (NULL == stt_client_get_by_uid(client->uid)) {
2260 if (NULL != client->speech_status_cb) {
2261 stt_client_use_callback(client);
2262 client->speech_status_cb(client->stt, client->speech_status, client->speech_status_user_data);
2263 stt_client_not_use_callback(client);
2264 SLOG(LOG_INFO, TAG_STTC, "Speech status callback is called"); //LCOV_EXCL_LINE
2266 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Speech status callback is null"); //LCOV_EXCL_LINE
2272 int __stt_cb_speech_status(int uid, int status)
2274 stt_client_s* client = stt_client_get_by_uid(uid);
2275 if (NULL == client) {
2276 SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
2280 client->speech_status = status;
2282 ecore_main_loop_thread_safe_call_async(__stt_notify_speech_status, client);
2286 int stt_set_recognition_result_cb(stt_h stt, stt_recognition_result_cb callback, void* user_data)
2288 stt_client_s* client = NULL;
2289 if (0 != __stt_get_feature_enabled()) {
2290 return STT_ERROR_NOT_SUPPORTED;
2292 if (0 != __stt_check_privilege()) {
2293 return STT_ERROR_PERMISSION_DENIED;
2295 if (0 != __stt_check_handle(stt, &client)) {
2296 return STT_ERROR_INVALID_PARAMETER;
2299 if (callback == NULL)
2300 return STT_ERROR_INVALID_PARAMETER;
2302 if (STT_STATE_CREATED != client->current_state) {
2303 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2304 return STT_ERROR_INVALID_STATE;
2307 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set recognition result cb");
2309 client->recognition_result_cb = callback;
2310 client->recognition_result_user_data = user_data;
2315 int stt_unset_recognition_result_cb(stt_h stt)
2317 stt_client_s* client = NULL;
2318 if (0 != __stt_get_feature_enabled()) {
2319 return STT_ERROR_NOT_SUPPORTED;
2321 if (0 != __stt_check_privilege()) {
2322 return STT_ERROR_PERMISSION_DENIED;
2324 if (0 != __stt_check_handle(stt, &client)) {
2325 return STT_ERROR_INVALID_PARAMETER;
2328 if (STT_STATE_CREATED != client->current_state) {
2329 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2330 return STT_ERROR_INVALID_STATE;
2333 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset recognition result cb");
2335 client->recognition_result_cb = NULL;
2336 client->recognition_result_user_data = NULL;
2341 int stt_set_state_changed_cb(stt_h stt, stt_state_changed_cb callback, void* user_data)
2343 stt_client_s* client = NULL;
2344 if (0 != __stt_get_feature_enabled()) {
2345 return STT_ERROR_NOT_SUPPORTED;
2347 if (0 != __stt_check_privilege()) {
2348 return STT_ERROR_PERMISSION_DENIED;
2350 if (0 != __stt_check_handle(stt, &client)) {
2351 return STT_ERROR_INVALID_PARAMETER;
2354 if (NULL == callback)
2355 return STT_ERROR_INVALID_PARAMETER;
2357 if (STT_STATE_CREATED != client->current_state) {
2358 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2359 return STT_ERROR_INVALID_STATE;
2362 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set state changed cb");
2364 client->state_changed_cb = callback;
2365 client->state_changed_user_data = user_data;
2370 int stt_unset_state_changed_cb(stt_h stt)
2372 stt_client_s* client = NULL;
2373 if (0 != __stt_get_feature_enabled()) {
2374 return STT_ERROR_NOT_SUPPORTED;
2376 if (0 != __stt_check_privilege()) {
2377 return STT_ERROR_PERMISSION_DENIED;
2379 if (0 != __stt_check_handle(stt, &client)) {
2380 return STT_ERROR_INVALID_PARAMETER;
2383 if (STT_STATE_CREATED != client->current_state) {
2384 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2385 return STT_ERROR_INVALID_STATE;
2388 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset state changed cb");
2390 client->state_changed_cb = NULL;
2391 client->state_changed_user_data = NULL;
2396 int stt_set_error_cb(stt_h stt, stt_error_cb callback, void* user_data)
2398 stt_client_s* client = NULL;
2399 if (0 != __stt_get_feature_enabled()) {
2400 return STT_ERROR_NOT_SUPPORTED;
2402 if (0 != __stt_check_privilege()) {
2403 return STT_ERROR_PERMISSION_DENIED;
2405 if (0 != __stt_check_handle(stt, &client)) {
2406 return STT_ERROR_INVALID_PARAMETER;
2409 if (NULL == callback)
2410 return STT_ERROR_INVALID_PARAMETER;
2412 if (STT_STATE_CREATED != client->current_state) {
2413 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2414 return STT_ERROR_INVALID_STATE;
2417 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set error cb");
2419 client->error_cb = callback;
2420 client->error_user_data = user_data;
2425 int stt_unset_error_cb(stt_h stt)
2427 stt_client_s* client = NULL;
2428 if (0 != __stt_get_feature_enabled()) {
2429 return STT_ERROR_NOT_SUPPORTED;
2431 if (0 != __stt_check_privilege()) {
2432 return STT_ERROR_PERMISSION_DENIED;
2434 if (0 != __stt_check_handle(stt, &client)) {
2435 return STT_ERROR_INVALID_PARAMETER;
2438 if (STT_STATE_CREATED != client->current_state) {
2439 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2440 return STT_ERROR_INVALID_STATE;
2443 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset error cb");
2445 client->error_cb = NULL;
2446 client->error_user_data = NULL;
2451 int stt_set_default_language_changed_cb(stt_h stt, stt_default_language_changed_cb callback, void* user_data)
2453 stt_client_s* client = NULL;
2454 if (0 != __stt_get_feature_enabled()) {
2455 return STT_ERROR_NOT_SUPPORTED;
2457 if (0 != __stt_check_privilege()) {
2458 return STT_ERROR_PERMISSION_DENIED;
2460 if (0 != __stt_check_handle(stt, &client)) {
2461 return STT_ERROR_INVALID_PARAMETER;
2464 if (NULL == callback)
2465 return STT_ERROR_INVALID_PARAMETER;
2467 if (STT_STATE_CREATED != client->current_state) {
2468 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2469 return STT_ERROR_INVALID_STATE;
2472 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set default language changed cb");
2474 client->default_lang_changed_cb = callback;
2475 client->default_lang_changed_user_data = user_data;
2480 int stt_unset_default_language_changed_cb(stt_h stt)
2482 stt_client_s* client = NULL;
2483 if (0 != __stt_get_feature_enabled()) {
2484 return STT_ERROR_NOT_SUPPORTED;
2486 if (0 != __stt_check_privilege()) {
2487 return STT_ERROR_PERMISSION_DENIED;
2489 if (0 != __stt_check_handle(stt, &client)) {
2490 return STT_ERROR_INVALID_PARAMETER;
2493 if (STT_STATE_CREATED != client->current_state) {
2494 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2495 return STT_ERROR_INVALID_STATE;
2498 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset default language changed cb");
2500 client->default_lang_changed_cb = NULL;
2501 client->default_lang_changed_user_data = NULL;
2506 int stt_set_engine_changed_cb(stt_h stt, stt_engine_changed_cb callback, void* user_data)
2508 stt_client_s* client = NULL;
2509 if (0 != __stt_get_feature_enabled()) {
2510 return STT_ERROR_NOT_SUPPORTED;
2512 if (0 != __stt_check_privilege()) {
2513 return STT_ERROR_PERMISSION_DENIED;
2515 if (0 != __stt_check_handle(stt, &client)) {
2516 return STT_ERROR_INVALID_PARAMETER;
2519 if (NULL == callback)
2520 return STT_ERROR_INVALID_PARAMETER;
2522 if (STT_STATE_CREATED != client->current_state) {
2523 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2524 return STT_ERROR_INVALID_STATE;
2527 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set engine changed cb");
2529 client->engine_changed_cb = callback;
2530 client->engine_changed_user_data = user_data;
2535 int stt_unset_engine_changed_cb(stt_h stt)
2537 stt_client_s* client = NULL;
2538 if (0 != __stt_get_feature_enabled()) {
2539 return STT_ERROR_NOT_SUPPORTED;
2541 if (0 != __stt_check_privilege()) {
2542 return STT_ERROR_PERMISSION_DENIED;
2544 if (0 != __stt_check_handle(stt, &client)) {
2545 return STT_ERROR_INVALID_PARAMETER;
2548 if (STT_STATE_CREATED != client->current_state) {
2549 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2550 return STT_ERROR_INVALID_STATE;
2553 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset engine changed cb");
2555 client->engine_changed_cb = NULL;
2556 client->engine_changed_user_data = NULL;
2562 int stt_set_speech_status_cb(stt_h stt, stt_speech_status_cb callback, void* user_data)
2564 stt_client_s* client = NULL;
2565 if (0 != __stt_get_feature_enabled()) {
2566 return STT_ERROR_NOT_SUPPORTED;
2568 if (0 != __stt_check_privilege()) {
2569 return STT_ERROR_PERMISSION_DENIED;
2571 if (0 != __stt_check_handle(stt, &client)) {
2572 return STT_ERROR_INVALID_PARAMETER;
2575 if (NULL == callback)
2576 return STT_ERROR_INVALID_PARAMETER;
2578 if (STT_STATE_CREATED != client->current_state) {
2579 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2580 return STT_ERROR_INVALID_STATE;
2583 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set speech status cb");
2585 client->speech_status_cb = callback;
2586 client->speech_status_user_data = user_data;
2591 int stt_unset_speech_status_cb(stt_h stt)
2593 stt_client_s* client = NULL;
2594 if (0 != __stt_get_feature_enabled()) {
2595 return STT_ERROR_NOT_SUPPORTED;
2597 if (0 != __stt_check_privilege()) {
2598 return STT_ERROR_PERMISSION_DENIED;
2600 if (0 != __stt_check_handle(stt, &client)) {
2601 return STT_ERROR_INVALID_PARAMETER;
2604 if (STT_STATE_CREATED != client->current_state) {
2605 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2606 return STT_ERROR_INVALID_STATE;
2609 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset speech status cb");
2611 client->speech_status_cb = NULL;
2612 client->speech_status_user_data = NULL;
2617 int stt_start_file(stt_h stt, const char* language, const char* type, const char* filepath, stt_audio_type_e audio_type, int sample_rate)
2619 stt_client_s* client = NULL;
2620 if (0 != __stt_get_feature_enabled()) {
2621 return STT_ERROR_NOT_SUPPORTED;
2623 if (0 != __stt_check_privilege()) {
2624 return STT_ERROR_PERMISSION_DENIED;
2626 if (0 != __stt_check_handle(stt, &client)) {
2627 return STT_ERROR_INVALID_PARAMETER;
2629 if (NULL == filepath) {
2630 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
2631 return STT_ERROR_INVALID_PARAMETER;
2634 SLOG(LOG_INFO, TAG_STTC, "===== STT START FILE");
2637 if (client->current_state != STT_STATE_READY) {
2638 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
2639 return STT_ERROR_INVALID_STATE;
2642 if (STT_INTERNAL_STATE_NONE != client->internal_state) {
2643 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is NOT none : %d", client->internal_state);
2644 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2648 char appid[1024] = {0, };
2649 ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
2651 if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
2652 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID");
2654 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Current app id is %s", appid);
2658 if (NULL == language) {
2659 temp = strdup("default");
2661 temp = strdup(language);
2665 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
2666 return STT_ERROR_OUT_OF_MEMORY;
2669 if (true == client->credential_needed && NULL == client->credential) {
2670 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
2673 return STT_ERROR_PERMISSION_DENIED;
2676 client->internal_state = STT_INTERNAL_STATE_STARTING;
2677 ret = stt_dbus_request_start_file(client->uid, temp, type, client->silence, appid, client->credential, filepath, audio_type, sample_rate);
2679 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start file : %s", __stt_get_error_code(ret));
2680 client->internal_state = STT_INTERNAL_STATE_NONE;
2682 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start is successful but not done");
2688 SLOG(LOG_DEBUG, TAG_STTC, "=====");
2689 SLOG(LOG_DEBUG, TAG_STTC, " ");
2694 int stt_cancel_file(stt_h stt)
2696 stt_client_s* client = NULL;
2697 if (0 != __stt_get_feature_enabled()) {
2698 return STT_ERROR_NOT_SUPPORTED;
2700 if (0 != __stt_check_privilege()) {
2701 return STT_ERROR_PERMISSION_DENIED;
2703 if (0 != __stt_check_handle(stt, &client)) {
2704 return STT_ERROR_INVALID_PARAMETER;
2707 SLOG(LOG_INFO, TAG_STTC, "===== STT CANCEL FILE");
2710 if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
2711 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state);
2712 return STT_ERROR_INVALID_STATE;
2715 if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
2716 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
2717 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2718 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
2719 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
2720 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
2721 } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
2722 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
2723 return STT_ERROR_IN_PROGRESS_TO_READY;
2726 client->internal_state = STT_INTERNAL_STATE_CANCELING;
2727 int ret = stt_dbus_request_cancel_file(client->uid);
2729 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel file : %s", __stt_get_error_code(ret));
2730 client->internal_state = STT_INTERNAL_STATE_NONE;
2732 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Cancel file is successful but not done");
2735 SLOG(LOG_DEBUG, TAG_STTC, "=====");
2736 SLOG(LOG_DEBUG, TAG_STTC, " ");
2741 void __sound_stream_ducking_state_changed_cb(sound_stream_ducking_h stream_ducking, bool is_ducked, void *user_data)
2743 SLOG(LOG_DEBUG, TAG_STTC, "@@@ ducking state changed cb");
2744 SLOG(LOG_DEBUG, TAG_STTC, "[Volume] is ducked : %d", is_ducked);
2745 // ducking_flag = true;
2749 static char* __get_ducking_stream(sound_stream_type_e stream_type)
2751 if (SOUND_STREAM_TYPE_MEDIA == stream_type)
2752 return "Media stream";
2753 else if (SOUND_STREAM_TYPE_SYSTEM == stream_type)
2754 return "System stream";
2755 else if (SOUND_STREAM_TYPE_NOTIFICATION == stream_type)
2756 return "Notification stream";
2757 else if (SOUND_STREAM_TYPE_ALARM == stream_type)
2758 return "Alarm stream";
2760 return "Non matched stream";
2763 static int __activate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h, double bg_volume_ratio)
2765 bool is_ducked = false;
2766 int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
2768 SLOG(LOG_DEBUG, TAG_STTC, "[Volume] The %s is already ducked", __get_ducking_stream(stream_type));
2770 ret = sound_manager_activate_ducking(stream_ducking_h, SND_MGR_DUCKING_DURATION, bg_volume_ratio);
2771 if (SOUND_MANAGER_ERROR_NONE != ret) {
2772 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to activate ducking for %s", __get_ducking_stream(stream_type));
2774 SLOG(LOG_INFO, TAG_STTC, "[Volume SUCCESS] Activate ducking for %s", __get_ducking_stream(stream_type));
2780 static void __change_background_volume(stt_system_volume_event_e volume_event)
2782 double bg_volume_ratio = 0.0;
2784 if (STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_FARFIELD == volume_event) {
2785 bg_volume_ratio = STT_BG_VOLUME_RATIO_FARFIELD;
2786 } else if (STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_NEARFIELD == volume_event) {
2787 bg_volume_ratio = STT_BG_VOLUME_RATIO_NEARFIELD;
2790 SLOG(LOG_INFO, TAG_STTC, "[Volume] volume ratio(%lf)", bg_volume_ratio);
2792 if (1.0 > bg_volume_ratio) {
2793 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking, bg_volume_ratio);
2794 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking, bg_volume_ratio);
2795 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking, bg_volume_ratio);
2796 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking, bg_volume_ratio);
2800 static int __deactivate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h)
2802 bool is_ducked = false;
2803 int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
2805 SLOG(LOG_DEBUG, TAG_STTC, "[Volume] The %s is already recovered from ducking", __get_ducking_stream(stream_type));
2807 ret = sound_manager_deactivate_ducking(stream_ducking_h);
2808 if (SOUND_MANAGER_ERROR_NONE != ret) {
2809 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to deactivate ducking for %s", __get_ducking_stream(stream_type));
2811 SLOG(LOG_INFO, TAG_STTC, "[Volume SUCCESS] Deactivate ducking for %s", __get_ducking_stream(stream_type));
2817 static void __recover_background_volume()
2819 SLOG(LOG_INFO, TAG_STTC, "[Volume] background volume recover");
2821 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking);
2822 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking);
2823 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking);
2824 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking);
2827 int __create_ducking_handle(void)
2830 if (NULL == g_media_stream_ducking) {
2831 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_MEDIA, __sound_stream_ducking_state_changed_cb, NULL, &g_media_stream_ducking);
2832 if (SOUND_MANAGER_ERROR_NONE != ret)
2833 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for type media, ret(%d)", ret);
2835 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for media stream is already created");
2838 if (NULL == g_system_stream_ducking) {
2839 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_SYSTEM, __sound_stream_ducking_state_changed_cb, NULL, &g_system_stream_ducking);
2840 if (SOUND_MANAGER_ERROR_NONE != ret)
2841 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for system type, ret(%d)", ret);
2843 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for system stream is already created");
2846 if (NULL == g_notification_stream_ducking) {
2847 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_NOTIFICATION, __sound_stream_ducking_state_changed_cb, NULL, &g_notification_stream_ducking);
2848 if (SOUND_MANAGER_ERROR_NONE != ret)
2849 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for notification type, ret(%d)", ret);
2851 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for notification stream is already created");
2854 if (NULL == g_alarm_stream_ducking) {
2855 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_ALARM, __sound_stream_ducking_state_changed_cb, NULL, &g_alarm_stream_ducking);
2856 if (SOUND_MANAGER_ERROR_NONE != ret)
2857 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for alarm type, ret(%d)", ret);
2859 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for alarm stream is already created");
2865 int stt_change_system_volume(stt_h stt, stt_system_volume_event_e volume_event)
2867 SLOG(LOG_DEBUG, TAG_STTC, "[STT] Change system volume, volume_event(%d)", volume_event);
2869 stt_client_s* client = NULL;
2870 if (0 != __stt_get_feature_enabled()) {
2871 return STT_ERROR_NOT_SUPPORTED;
2873 if (0 != __stt_check_privilege()) {
2874 return STT_ERROR_PERMISSION_DENIED;
2876 if (0 != __stt_check_handle(stt, &client)) {
2877 return STT_ERROR_INVALID_PARAMETER;
2881 if (client->current_state != STT_STATE_READY && client->current_state != STT_STATE_CREATED) {
2882 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY nor CREATED", client->current_state);
2883 return STT_ERROR_INVALID_STATE;
2886 /* change system volume */
2887 int ret = __create_ducking_handle();
2889 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create volume handle");
2891 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Success to create volume handle");
2894 __change_background_volume(volume_event);
2895 return STT_ERROR_NONE;
2898 int __destroy_ducking_handle(void)
2901 if (g_media_stream_ducking) {
2902 ret = sound_manager_destroy_stream_ducking(g_media_stream_ducking);
2903 if (SOUND_MANAGER_ERROR_NONE != ret)
2904 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy media stream ducking, ret(%d)", ret);
2905 g_media_stream_ducking = NULL;
2907 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for media stream is already created");
2910 if (g_system_stream_ducking) {
2911 ret = sound_manager_destroy_stream_ducking(g_system_stream_ducking);
2912 if (SOUND_MANAGER_ERROR_NONE != ret)
2913 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy system stream ducking, ret(%d)", ret);
2914 g_system_stream_ducking = NULL;
2916 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for system stream is already created");
2919 if (g_notification_stream_ducking) {
2920 ret = sound_manager_destroy_stream_ducking(g_notification_stream_ducking);
2921 if (SOUND_MANAGER_ERROR_NONE != ret)
2922 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy notification stream ducking, ret(%d)", ret);
2923 g_notification_stream_ducking = NULL;
2925 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for notification stream is already created");
2928 if (g_alarm_stream_ducking) {
2929 ret = sound_manager_destroy_stream_ducking(g_alarm_stream_ducking);
2930 if (SOUND_MANAGER_ERROR_NONE != ret)
2931 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy alarm stream ducking, ret(%d)", ret);
2932 g_alarm_stream_ducking = NULL;
2934 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for alarm stream is already created");
2939 int stt_recover_system_volume(stt_h stt)
2941 SLOG(LOG_DEBUG, TAG_STTC, "[STT] recover system volume");
2943 stt_client_s* client = NULL;
2944 if (0 != __stt_get_feature_enabled()) {
2945 return STT_ERROR_NOT_SUPPORTED;
2947 if (0 != __stt_check_privilege()) {
2948 return STT_ERROR_PERMISSION_DENIED;
2950 if (0 != __stt_check_handle(stt, &client)) {
2951 return STT_ERROR_INVALID_PARAMETER;
2955 if (client->current_state != STT_STATE_READY && client->current_state != STT_STATE_CREATED) {
2956 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY nor CREATED", client->current_state);
2957 return STT_ERROR_INVALID_STATE;
2960 /* recover volume */
2961 __recover_background_volume();
2962 int ret = __destroy_ducking_handle();
2964 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to destroy volume handle");
2966 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Success to destroy volume handle");
2969 return STT_ERROR_NONE;