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(%u)", 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(%u)", client->uid);
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 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 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(%u)", 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 static 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 if (NULL == current_engine_id) {
1224 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory or Engine id is NULL");
1225 return STT_ERROR_OPERATION_FAILED;
1227 ret = __stt_convert_config_error_code(ret);
1229 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default engine id : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1230 free(current_engine_id);
1231 current_engine_id = NULL;
1235 current_engine_id = strdup(client->current_engine_id);
1237 if (NULL == current_engine_id) {
1238 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
1239 return STT_ERROR_OUT_OF_MEMORY;
1244 client->supported_lang_cb = callback;
1245 client->supported_lang_user_data = user_data;
1247 ret = stt_config_mgr_get_language_list(current_engine_id, __stt_config_supported_language_cb, client->stt);
1248 ret = __stt_convert_config_error_code(ret);
1250 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get languages : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1253 if (NULL != current_engine_id) {
1254 free(current_engine_id);
1255 current_engine_id = NULL;
1258 client->supported_lang_cb = NULL;
1259 client->supported_lang_user_data = NULL;
1261 SLOG(LOG_INFO, TAG_STTC, "=====");
1262 SLOG(LOG_DEBUG, TAG_STTC, " ");
1267 int stt_get_default_language(stt_h stt, char** language)
1269 stt_client_s* client = NULL;
1270 if (0 != __stt_get_feature_enabled()) {
1271 return STT_ERROR_NOT_SUPPORTED;
1273 if (0 != __stt_check_privilege()) {
1274 return STT_ERROR_PERMISSION_DENIED;
1276 if (0 != __stt_check_handle(stt, &client)) {
1277 return STT_ERROR_INVALID_PARAMETER;
1280 SLOG(LOG_INFO, TAG_STTC, "===== Get Default Language");
1282 if (NULL == language) {
1283 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1284 return STT_ERROR_INVALID_PARAMETER;
1288 ret = stt_config_mgr_get_default_language(language);
1289 ret = __stt_convert_config_error_code(ret);
1291 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default language : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1293 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current language = %s", *language);
1296 SLOG(LOG_INFO, TAG_STTC, "=====");
1297 SLOG(LOG_DEBUG, TAG_STTC, " ");
1302 int stt_get_state(stt_h stt, stt_state_e* state)
1304 stt_client_s* client = NULL;
1305 if (0 != __stt_get_feature_enabled()) {
1306 return STT_ERROR_NOT_SUPPORTED;
1308 if (0 != __stt_check_privilege()) {
1309 return STT_ERROR_PERMISSION_DENIED;
1311 if (0 != __stt_check_handle(stt, &client)) {
1312 return STT_ERROR_INVALID_PARAMETER;
1315 if (NULL == state) {
1316 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1317 return STT_ERROR_INVALID_PARAMETER;
1320 *state = client->current_state;
1322 SLOG(LOG_INFO, TAG_STTC, "===== Get state(%d)", *state);
1325 case STT_STATE_CREATED: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'CREATED'"); break;
1326 case STT_STATE_READY: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Ready'"); break;
1327 case STT_STATE_RECORDING: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Recording'"); break;
1328 case STT_STATE_PROCESSING: SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Processing'"); break;
1329 default: SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid value"); break;
1332 return STT_ERROR_NONE;
1335 int stt_get_error_message(stt_h stt, char** err_msg)
1337 stt_client_s* client = NULL;
1338 if (0 != __stt_get_feature_enabled()) {
1339 return STT_ERROR_NOT_SUPPORTED;
1341 if (0 != __stt_check_privilege()) {
1342 return STT_ERROR_PERMISSION_DENIED;
1344 if (0 != __stt_check_handle(stt, &client)) {
1345 return STT_ERROR_INVALID_PARAMETER;
1348 if (NULL == err_msg) {
1349 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1350 return STT_ERROR_INVALID_PARAMETER;
1353 if (false == g_err_callback_status) {
1354 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] This callback should be called during an err_callback");
1355 return STT_ERROR_OPERATION_FAILED;
1358 if (NULL != client->err_msg) {
1359 *err_msg = strdup(client->err_msg);
1360 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Error msg (%s)", *err_msg); //LCOV_EXCL_LINE
1362 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Error msg (NULL)"); //LCOV_EXCL_LINE
1365 SLOG(LOG_INFO, TAG_STTC, "====="); //LCOV_EXCL_LINE
1366 SLOG(LOG_DEBUG, TAG_STTC, " "); //LCOV_EXCL_LINE
1368 return STT_ERROR_NONE;
1371 int stt_is_recognition_type_supported(stt_h stt, const char* type, bool* support)
1373 stt_client_s* client = NULL;
1374 if (0 != __stt_get_feature_enabled()) {
1375 return STT_ERROR_NOT_SUPPORTED;
1377 if (0 != __stt_check_privilege()) {
1378 return STT_ERROR_PERMISSION_DENIED;
1380 if (0 != __stt_check_handle(stt, &client)) {
1381 return STT_ERROR_INVALID_PARAMETER;
1384 if (NULL == type || NULL == support) {
1385 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1386 return STT_ERROR_INVALID_PARAMETER;
1390 if (client->current_state != STT_STATE_READY) {
1391 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1392 return STT_ERROR_INVALID_STATE;
1398 ret = stt_dbus_request_is_recognition_type_supported(client->uid, type, support);
1401 if (STT_ERROR_TIMED_OUT != ret) {
1402 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get recognition type supported : %s", __stt_get_error_code(ret));
1405 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1408 if (STT_RETRY_COUNT == count) {
1409 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1415 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] recognition type is %s", *support ? "true " : "false");
1420 return STT_ERROR_NONE;
1423 int stt_set_silence_detection(stt_h stt, stt_option_silence_detection_e type)
1425 stt_client_s* client = NULL;
1426 if (0 != __stt_get_feature_enabled()) {
1427 return STT_ERROR_NOT_SUPPORTED;
1429 if (0 != __stt_check_privilege()) {
1430 return STT_ERROR_PERMISSION_DENIED;
1432 if (0 != __stt_check_handle(stt, &client)) {
1433 return STT_ERROR_INVALID_PARAMETER;
1437 if (client->current_state != STT_STATE_READY) {
1438 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1439 return STT_ERROR_INVALID_STATE;
1442 SLOG(LOG_INFO, TAG_STTC, "===== Set silence detection, supported(%d), type(%d)", client->silence_supported, type);
1444 if (true == client->silence_supported) {
1445 if (type >= STT_OPTION_SILENCE_DETECTION_FALSE && type <= STT_OPTION_SILENCE_DETECTION_AUTO) {
1446 client->silence = type;
1448 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Type is invalid");
1449 return STT_ERROR_INVALID_PARAMETER;
1452 return STT_ERROR_NOT_SUPPORTED_FEATURE;
1455 return STT_ERROR_NONE;
1458 int stt_set_start_sound(stt_h stt, const char* filename)
1460 stt_client_s* client = NULL;
1461 if (0 != __stt_get_feature_enabled()) {
1462 return STT_ERROR_NOT_SUPPORTED;
1464 if (0 != __stt_check_privilege()) {
1465 return STT_ERROR_PERMISSION_DENIED;
1467 if (0 != __stt_check_handle(stt, &client)) {
1468 return STT_ERROR_INVALID_PARAMETER;
1471 SLOG(LOG_INFO, TAG_STTC, "===== STT SET START SOUND");
1473 if (NULL == filename) {
1474 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1475 return STT_ERROR_INVALID_PARAMETER;
1478 if (0 != access(filename, F_OK)) {
1479 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] File does not exist"); //LCOV_EXCL_LINE
1480 return STT_ERROR_INVALID_PARAMETER;
1484 if (client->current_state != STT_STATE_READY) {
1485 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state); //LCOV_EXCL_LINE
1486 return STT_ERROR_INVALID_STATE;
1492 ret = stt_dbus_request_set_start_sound(client->uid, filename);
1495 if (STT_ERROR_TIMED_OUT != ret) {
1496 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set start sound : %s", __stt_get_error_code(ret));
1499 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1502 if (STT_RETRY_COUNT == count) {
1503 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1509 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Set start sound : %s", filename);
1514 return STT_ERROR_NONE;
1517 int stt_unset_start_sound(stt_h stt)
1519 stt_client_s* client = NULL;
1520 if (0 != __stt_get_feature_enabled()) {
1521 return STT_ERROR_NOT_SUPPORTED;
1523 if (0 != __stt_check_privilege()) {
1524 return STT_ERROR_PERMISSION_DENIED;
1526 if (0 != __stt_check_handle(stt, &client)) {
1527 return STT_ERROR_INVALID_PARAMETER;
1530 SLOG(LOG_INFO, TAG_STTC, "===== STT UNSET START SOUND");
1533 if (client->current_state != STT_STATE_READY) {
1534 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1535 return STT_ERROR_INVALID_STATE;
1541 ret = stt_dbus_request_unset_start_sound(client->uid);
1544 if (STT_ERROR_TIMED_OUT != ret) {
1545 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset start sound : %s", __stt_get_error_code(ret));
1548 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1551 if (STT_RETRY_COUNT == count) {
1552 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1558 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Unset start sound");
1563 return STT_ERROR_NONE;
1566 int stt_set_stop_sound(stt_h stt, const char* filename)
1568 stt_client_s* client = NULL;
1569 if (0 != __stt_get_feature_enabled()) {
1570 return STT_ERROR_NOT_SUPPORTED;
1572 if (0 != __stt_check_privilege()) {
1573 return STT_ERROR_PERMISSION_DENIED;
1575 if (0 != __stt_check_handle(stt, &client)) {
1576 return STT_ERROR_INVALID_PARAMETER;
1579 SLOG(LOG_INFO, TAG_STTC, "===== STT SET STOP SOUND");
1581 if (NULL == filename) {
1582 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1583 return STT_ERROR_INVALID_PARAMETER;
1586 if (0 != access(filename, F_OK)) {
1587 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] File does not exist"); //LCOV_EXCL_LINE
1588 return STT_ERROR_INVALID_PARAMETER;
1592 if (client->current_state != STT_STATE_READY) {
1593 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state); //LCOV_EXCL_LINE
1594 return STT_ERROR_INVALID_STATE;
1600 ret = stt_dbus_request_set_stop_sound(client->uid, filename);
1603 if (STT_ERROR_TIMED_OUT != ret) {
1604 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set stop sound : %s", __stt_get_error_code(ret));
1607 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1610 if (STT_RETRY_COUNT == count) {
1611 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1617 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Set stop sound : %s", filename);
1622 return STT_ERROR_NONE;
1625 int stt_unset_stop_sound(stt_h stt)
1627 stt_client_s* client = NULL;
1628 if (0 != __stt_get_feature_enabled()) {
1629 return STT_ERROR_NOT_SUPPORTED;
1631 if (0 != __stt_check_privilege()) {
1632 return STT_ERROR_PERMISSION_DENIED;
1634 if (0 != __stt_check_handle(stt, &client)) {
1635 return STT_ERROR_INVALID_PARAMETER;
1638 SLOG(LOG_INFO, TAG_STTC, "===== STT UNSET STOP SOUND");
1641 if (client->current_state != STT_STATE_READY) {
1642 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1643 return STT_ERROR_INVALID_STATE;
1649 ret = stt_dbus_request_unset_stop_sound(client->uid);
1652 if (STT_ERROR_TIMED_OUT != ret) {
1653 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset stop sound : %s", __stt_get_error_code(ret));
1656 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1659 if (STT_RETRY_COUNT == count) {
1660 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1666 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Unset stop sound");
1671 return STT_ERROR_NONE;
1674 int stt_start(stt_h stt, const char* language, const char* type)
1676 stt_client_s* client = NULL;
1677 if (0 != __stt_get_feature_enabled()) {
1678 return STT_ERROR_NOT_SUPPORTED;
1680 if (0 != __stt_check_privilege()) {
1681 return STT_ERROR_PERMISSION_DENIED;
1683 if (0 != __stt_check_handle(stt, &client)) {
1684 return STT_ERROR_INVALID_PARAMETER;
1687 SLOG(LOG_INFO, TAG_STTC, "===== STT START");
1690 if (client->current_state != STT_STATE_READY) {
1691 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1692 return STT_ERROR_INVALID_STATE;
1695 if (STT_INTERNAL_STATE_NONE != client->internal_state) {
1696 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is NOT none : %d", client->internal_state); //LCOV_EXCL_LINE
1697 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1701 char appid[1024] = {0, };
1702 ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
1704 if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1705 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID"); //LCOV_EXCL_LINE
1707 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
1711 if (NULL == language) {
1712 temp = strdup("default");
1714 temp = strdup(language);
1718 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
1719 return STT_ERROR_OUT_OF_MEMORY;
1722 if (true == client->credential_needed && NULL == client->credential) {
1723 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id); //LCOV_EXCL_LINE
1726 return STT_ERROR_PERMISSION_DENIED;
1729 client->internal_state = STT_INTERNAL_STATE_STARTING;
1730 ret = stt_dbus_request_start(client->uid, temp, type, client->silence, appid, client->credential);
1732 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1733 client->internal_state = STT_INTERNAL_STATE_NONE;
1735 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start is successful but not done");
1741 SLOG(LOG_INFO, TAG_STTC, "=====");
1742 SLOG(LOG_DEBUG, TAG_STTC, " ");
1747 int stt_stop(stt_h stt)
1749 stt_client_s* client = NULL;
1750 if (0 != __stt_get_feature_enabled()) {
1751 return STT_ERROR_NOT_SUPPORTED;
1753 if (0 != __stt_check_privilege()) {
1754 return STT_ERROR_PERMISSION_DENIED;
1756 if (0 != __stt_check_handle(stt, &client)) {
1757 return STT_ERROR_INVALID_PARAMETER;
1760 SLOG(LOG_INFO, TAG_STTC, "===== STT STOP");
1763 if (client->current_state != STT_STATE_RECORDING) {
1764 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Current state(%d) is NOT RECORDING", client->current_state); //LCOV_EXCL_LINE
1765 return STT_ERROR_INVALID_STATE;
1768 if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1769 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state); //LCOV_EXCL_LINE
1770 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1771 } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1772 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state); //LCOV_EXCL_LINE
1773 return STT_ERROR_IN_PROGRESS_TO_READY;
1774 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1775 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state); //LCOV_EXCL_LINE
1776 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1779 client->internal_state = STT_INTERNAL_STATE_STOPPING;
1780 int ret = stt_dbus_request_stop(client->uid);
1782 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to stop : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1783 client->internal_state = STT_INTERNAL_STATE_NONE;
1785 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Stop is successful but not done");
1788 SLOG(LOG_DEBUG, TAG_STTC, "=====");
1789 SLOG(LOG_DEBUG, TAG_STTC, " ");
1795 int stt_cancel(stt_h stt)
1797 stt_client_s* client = NULL;
1798 if (0 != __stt_get_feature_enabled()) {
1799 return STT_ERROR_NOT_SUPPORTED;
1801 if (0 != __stt_check_privilege()) {
1802 return STT_ERROR_PERMISSION_DENIED;
1804 if (0 != __stt_check_handle(stt, &client)) {
1805 return STT_ERROR_INVALID_PARAMETER;
1808 SLOG(LOG_INFO, TAG_STTC, "===== STT CANCEL");
1811 if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
1812 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state); //LCOV_EXCL_LINE
1813 return STT_ERROR_INVALID_STATE;
1816 if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1817 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state); //LCOV_EXCL_LINE
1818 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1819 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1820 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state); //LCOV_EXCL_LINE
1821 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1822 } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1823 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state); //LCOV_EXCL_LINE
1824 return STT_ERROR_IN_PROGRESS_TO_READY;
1827 client->internal_state = STT_INTERNAL_STATE_CANCELING;
1828 int ret = stt_dbus_request_cancel(client->uid);
1830 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1831 client->internal_state = STT_INTERNAL_STATE_NONE;
1833 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Cancel is successful but not done");
1836 SLOG(LOG_DEBUG, TAG_STTC, "=====");
1837 SLOG(LOG_DEBUG, TAG_STTC, " ");
1842 int __stt_cb_set_volume(unsigned int uid, float volume)
1844 stt_client_s* client = NULL;
1846 client = stt_client_get_by_uid(uid);
1847 if (NULL == client) {
1849 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
1850 return STT_ERROR_INVALID_PARAMETER;
1854 if (STT_STATE_RECORDING != client->current_state) {
1856 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1857 return STT_ERROR_INVALID_STATE;
1861 g_volume_db = volume;
1862 SLOG(LOG_INFO, TAG_STTC, "Set volume (%f)", g_volume_db);
1867 int stt_get_recording_volume(stt_h stt, float* volume)
1869 stt_client_s* client = NULL;
1870 if (0 != __stt_get_feature_enabled()) {
1871 return STT_ERROR_NOT_SUPPORTED;
1873 if (0 != __stt_check_privilege()) {
1874 return STT_ERROR_PERMISSION_DENIED;
1876 if (0 != __stt_check_handle(stt, &client)) {
1877 return STT_ERROR_INVALID_PARAMETER;
1880 if (NULL == volume) {
1881 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1882 return STT_ERROR_INVALID_PARAMETER;
1885 if (STT_STATE_RECORDING != client->current_state) {
1886 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state); //LCOV_EXCL_LINE
1887 return STT_ERROR_INVALID_STATE;
1890 *volume = g_volume_db;
1892 SLOG(LOG_INFO, TAG_STTC, "Get recording volume (%f)", *volume);
1894 return STT_ERROR_NONE;
1898 bool __stt_result_time_cb(int index, int event, const char* text, long start_time, long end_time, void* user_data)
1900 stt_client_s* client = (stt_client_s*)user_data;
1903 if (NULL == client) {
1904 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1908 if (NULL != client->result_time_cb) {
1909 SLOG(LOG_INFO, TAG_STTC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
1910 index, event, text, start_time, end_time);
1911 client->result_time_cb(client->stt, index, (stt_result_time_event_e)event,
1912 text, start_time, end_time, client->result_time_user_data);
1914 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Callback is NULL");
1922 int stt_foreach_detailed_result(stt_h stt, stt_result_time_cb callback, void* user_data)
1924 stt_client_s* client = NULL;
1925 if (0 != __stt_get_feature_enabled()) {
1926 return STT_ERROR_NOT_SUPPORTED;
1928 if (0 != __stt_check_privilege()) {
1929 return STT_ERROR_PERMISSION_DENIED;
1931 if (0 != __stt_check_handle(stt, &client)) {
1932 return STT_ERROR_INVALID_PARAMETER;
1935 SLOG(LOG_INFO, TAG_STTC, "===== STT FOREACH DETAILED RESULT");
1937 if (NULL == callback) {
1938 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
1939 return STT_ERROR_INVALID_PARAMETER;
1942 client->result_time_cb = callback;
1943 client->result_time_user_data = user_data;
1946 ret = stt_config_mgr_foreach_time_info(__stt_result_time_cb, client);
1947 ret = __stt_convert_config_error_code(ret);
1949 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to foreach time info : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1952 client->result_time_cb = NULL;
1953 client->result_time_user_data = NULL;
1955 SLOG(LOG_INFO, TAG_STTC, "=====");
1956 SLOG(LOG_DEBUG, TAG_STTC, " ");
1961 static void __stt_notify_error(void *data)
1963 stt_client_s* client = (stt_client_s*)data;
1965 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error from sttd");
1968 if (NULL == client) {
1969 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid"); //LCOV_EXCL_LINE
1973 if (NULL == stt_client_get_by_uid(client->uid))
1976 if (NULL != client->error_cb) {
1977 stt_client_use_callback(client);
1978 g_err_callback_status = true;
1979 client->error_cb(client->stt, client->reason, client->error_user_data);
1980 g_err_callback_status = false;
1981 stt_client_not_use_callback(client);
1982 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is called : reason [%d]", client->reason);
1984 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null"); //LCOV_EXCL_LINE
1990 int __stt_cb_error(unsigned int uid, int reason, char* err_msg)
1994 GList* client_list = NULL;
1995 client_list = stt_client_get_client_list();
1998 stt_client_s *data = NULL;
2000 if (g_list_length(client_list) > 0) {
2001 /* Get a first item */
2002 iter = g_list_first(client_list);
2004 while (NULL != iter) {
2007 data->reason = reason;
2008 data->internal_state = STT_INTERNAL_STATE_NONE;
2009 if (NULL != data->err_msg) {
2010 free(data->err_msg);
2011 data->err_msg = NULL;
2013 if (NULL != err_msg)
2014 data->err_msg = strdup(err_msg);
2016 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
2018 if (NULL != data->error_cb) {
2019 ecore_main_loop_thread_safe_call_async(__stt_notify_error, data);
2021 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
2024 if (STT_ERROR_SERVICE_RESET == reason) {
2025 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
2027 data->current_state = STT_STATE_CREATED;
2028 if (0 != stt_prepare(data->stt)) {
2029 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
2034 iter = g_list_next(iter);
2039 stt_client_s* client = stt_client_get_by_uid(uid);
2040 if (NULL == client) {
2041 SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
2045 client->reason = reason;
2046 client->internal_state = STT_INTERNAL_STATE_NONE;
2047 if (NULL != client->err_msg) {
2048 free(client->err_msg);
2049 client->err_msg = NULL;
2051 if (NULL != err_msg)
2052 client->err_msg = strdup(err_msg);
2054 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
2056 if (NULL != client->error_cb) {
2057 ecore_main_loop_thread_safe_call_async(__stt_notify_error, client);
2059 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
2062 if (STT_ERROR_SERVICE_RESET == reason) {
2063 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset"); //LCOV_EXCL_LINE
2065 client->current_state = STT_STATE_CREATED;
2066 if (0 != stt_prepare(client->stt)) {
2067 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare"); //LCOV_EXCL_LINE
2075 static void __stt_notify_state_changed(void *data)
2077 stt_client_s* client = (stt_client_s*)data;
2080 if (NULL == client) {
2081 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid"); //LCOV_EXCL_LINE
2085 if (NULL == stt_client_get_by_uid(client->uid)) {
2089 if (STT_INTERNAL_STATE_STARTING == client->internal_state && STT_STATE_RECORDING == client->current_state) {
2090 client->internal_state = STT_INTERNAL_STATE_NONE;
2091 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
2092 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state && STT_STATE_PROCESSING == client->current_state) {
2093 client->internal_state = STT_INTERNAL_STATE_NONE;
2094 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
2095 } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state && STT_STATE_READY == client->current_state) {
2096 client->internal_state = STT_INTERNAL_STATE_NONE;
2097 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
2100 if (NULL != client->state_changed_cb) {
2101 stt_client_use_callback(client);
2102 client->state_changed_cb(client->stt, client->before_state,
2103 client->current_state, client->state_changed_user_data);
2104 stt_client_not_use_callback(client);
2105 SLOG(LOG_INFO, TAG_STTC, "State changed callback is called, State(%d)", client->current_state);
2107 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null, State(%d)", client->current_state); //LCOV_EXCL_LINE
2113 static Eina_Bool __stt_notify_result(void *data)
2115 stt_client_s* client = (stt_client_s*)data;
2118 if (NULL == client) {
2119 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid"); //LCOV_EXCL_LINE
2123 if (NULL == stt_client_get_by_uid(client->uid)) {
2127 if (NULL != client->recognition_result_cb) {
2128 stt_client_use_callback(client);
2129 client->recognition_result_cb(client->stt, client->event, (const char**)client->data_list, client->data_count,
2130 client->msg, client->recognition_result_user_data);
2131 stt_client_not_use_callback(client);
2132 SLOG(LOG_INFO, TAG_STTC, "client recognition result callback called");
2134 SLOG(LOG_WARN, TAG_STTC, "[WARNING] User recognition result callback is NULL");
2137 if (NULL != client->msg) {
2142 if (NULL != client->data_list) {
2144 temp = client->data_list;
2147 for (i = 0; i < client->data_count; i++) {
2148 if (NULL != temp[i]) {
2152 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error"); //LCOV_EXCL_LINE
2155 free(client->data_list);
2156 client->data_list = NULL;
2159 client->data_count = 0;
2161 stt_config_mgr_remove_time_info_file();
2163 if (STT_RESULT_EVENT_FINAL_RESULT == client->event || STT_RESULT_EVENT_ERROR == client->event) {
2164 client->before_state = client->current_state;
2165 client->current_state = STT_STATE_READY;
2167 if (NULL != client->state_changed_cb) {
2168 ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
2170 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null"); //LCOV_EXCL_LINE
2177 int __stt_cb_result(unsigned int uid, int event, char** data, int data_count, const char* msg)
2179 stt_client_s* client = NULL;
2181 client = stt_client_get_by_uid(uid);
2182 if (NULL == client) {
2183 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid"); //LCOV_EXCL_LINE
2184 return STT_ERROR_INVALID_PARAMETER;
2188 SECURE_SLOG(LOG_INFO, TAG_STTC, "Recognition Result Message = %s", msg);
2191 for (i = 0; i < data_count; i++) {
2192 if (NULL != data[i])
2193 SECURE_SLOG(LOG_INFO, TAG_STTC, "Recognition Result[%d] = %s", i, data[i]);
2196 if (NULL != client->recognition_result_cb) {
2197 client->event = event;
2199 client->msg = strdup(msg);
2202 client->data_count = data_count;
2204 if (data_count > 0) {
2206 temp = (char**)calloc(data_count, sizeof(char*));
2208 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
2209 return STT_ERROR_OUT_OF_MEMORY;
2212 for (i = 0; i < data_count; i++) {
2213 if (NULL != data[i])
2214 temp[i] = strdup(data[i]);
2216 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error"); //LCOV_EXCL_LINE
2219 client->data_list = temp;
2222 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] User result callback is null");
2225 __stt_notify_result(client);
2227 return STT_ERROR_NONE;
2230 int __stt_cb_set_state(unsigned int uid, int state)
2232 stt_client_s* client = stt_client_get_by_uid(uid);
2233 if (NULL == client) {
2234 SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
2238 stt_state_e state_from_daemon = (stt_state_e)state;
2240 if (client->current_state == state_from_daemon) {
2241 SLOG(LOG_DEBUG, TAG_STTC, "Current state has already been %d", client->current_state);
2245 client->before_state = client->current_state;
2246 client->current_state = state_from_daemon;
2248 ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
2252 static void __stt_notify_speech_status(void *data)
2254 stt_client_s* client = (stt_client_s*)data;
2257 if (NULL == client) {
2258 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify speech status : A handle is not valid"); //LCOV_EXCL_LINE
2262 if (NULL == stt_client_get_by_uid(client->uid)) {
2266 if (NULL != client->speech_status_cb) {
2267 stt_client_use_callback(client);
2268 client->speech_status_cb(client->stt, client->speech_status, client->speech_status_user_data);
2269 stt_client_not_use_callback(client);
2270 SLOG(LOG_INFO, TAG_STTC, "Speech status callback is called"); //LCOV_EXCL_LINE
2272 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Speech status callback is null"); //LCOV_EXCL_LINE
2278 int __stt_cb_speech_status(unsigned int uid, int status)
2280 stt_client_s* client = stt_client_get_by_uid(uid);
2281 if (NULL == client) {
2282 SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
2286 client->speech_status = status;
2288 ecore_main_loop_thread_safe_call_async(__stt_notify_speech_status, client);
2292 int stt_set_recognition_result_cb(stt_h stt, stt_recognition_result_cb callback, void* user_data)
2294 stt_client_s* client = NULL;
2295 if (0 != __stt_get_feature_enabled()) {
2296 return STT_ERROR_NOT_SUPPORTED;
2298 if (0 != __stt_check_privilege()) {
2299 return STT_ERROR_PERMISSION_DENIED;
2301 if (0 != __stt_check_handle(stt, &client)) {
2302 return STT_ERROR_INVALID_PARAMETER;
2305 if (callback == NULL)
2306 return STT_ERROR_INVALID_PARAMETER;
2308 if (STT_STATE_CREATED != client->current_state) {
2309 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2310 return STT_ERROR_INVALID_STATE;
2313 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set recognition result cb");
2315 client->recognition_result_cb = callback;
2316 client->recognition_result_user_data = user_data;
2321 int stt_unset_recognition_result_cb(stt_h stt)
2323 stt_client_s* client = NULL;
2324 if (0 != __stt_get_feature_enabled()) {
2325 return STT_ERROR_NOT_SUPPORTED;
2327 if (0 != __stt_check_privilege()) {
2328 return STT_ERROR_PERMISSION_DENIED;
2330 if (0 != __stt_check_handle(stt, &client)) {
2331 return STT_ERROR_INVALID_PARAMETER;
2334 if (STT_STATE_CREATED != client->current_state) {
2335 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2336 return STT_ERROR_INVALID_STATE;
2339 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset recognition result cb");
2341 client->recognition_result_cb = NULL;
2342 client->recognition_result_user_data = NULL;
2347 int stt_set_state_changed_cb(stt_h stt, stt_state_changed_cb callback, void* user_data)
2349 stt_client_s* client = NULL;
2350 if (0 != __stt_get_feature_enabled()) {
2351 return STT_ERROR_NOT_SUPPORTED;
2353 if (0 != __stt_check_privilege()) {
2354 return STT_ERROR_PERMISSION_DENIED;
2356 if (0 != __stt_check_handle(stt, &client)) {
2357 return STT_ERROR_INVALID_PARAMETER;
2360 if (NULL == callback)
2361 return STT_ERROR_INVALID_PARAMETER;
2363 if (STT_STATE_CREATED != client->current_state) {
2364 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2365 return STT_ERROR_INVALID_STATE;
2368 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set state changed cb");
2370 client->state_changed_cb = callback;
2371 client->state_changed_user_data = user_data;
2376 int stt_unset_state_changed_cb(stt_h stt)
2378 stt_client_s* client = NULL;
2379 if (0 != __stt_get_feature_enabled()) {
2380 return STT_ERROR_NOT_SUPPORTED;
2382 if (0 != __stt_check_privilege()) {
2383 return STT_ERROR_PERMISSION_DENIED;
2385 if (0 != __stt_check_handle(stt, &client)) {
2386 return STT_ERROR_INVALID_PARAMETER;
2389 if (STT_STATE_CREATED != client->current_state) {
2390 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2391 return STT_ERROR_INVALID_STATE;
2394 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset state changed cb");
2396 client->state_changed_cb = NULL;
2397 client->state_changed_user_data = NULL;
2402 int stt_set_error_cb(stt_h stt, stt_error_cb callback, void* user_data)
2404 stt_client_s* client = NULL;
2405 if (0 != __stt_get_feature_enabled()) {
2406 return STT_ERROR_NOT_SUPPORTED;
2408 if (0 != __stt_check_privilege()) {
2409 return STT_ERROR_PERMISSION_DENIED;
2411 if (0 != __stt_check_handle(stt, &client)) {
2412 return STT_ERROR_INVALID_PARAMETER;
2415 if (NULL == callback)
2416 return STT_ERROR_INVALID_PARAMETER;
2418 if (STT_STATE_CREATED != client->current_state) {
2419 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2420 return STT_ERROR_INVALID_STATE;
2423 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set error cb");
2425 client->error_cb = callback;
2426 client->error_user_data = user_data;
2431 int stt_unset_error_cb(stt_h stt)
2433 stt_client_s* client = NULL;
2434 if (0 != __stt_get_feature_enabled()) {
2435 return STT_ERROR_NOT_SUPPORTED;
2437 if (0 != __stt_check_privilege()) {
2438 return STT_ERROR_PERMISSION_DENIED;
2440 if (0 != __stt_check_handle(stt, &client)) {
2441 return STT_ERROR_INVALID_PARAMETER;
2444 if (STT_STATE_CREATED != client->current_state) {
2445 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2446 return STT_ERROR_INVALID_STATE;
2449 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset error cb");
2451 client->error_cb = NULL;
2452 client->error_user_data = NULL;
2457 int stt_set_default_language_changed_cb(stt_h stt, stt_default_language_changed_cb callback, void* user_data)
2459 stt_client_s* client = NULL;
2460 if (0 != __stt_get_feature_enabled()) {
2461 return STT_ERROR_NOT_SUPPORTED;
2463 if (0 != __stt_check_privilege()) {
2464 return STT_ERROR_PERMISSION_DENIED;
2466 if (0 != __stt_check_handle(stt, &client)) {
2467 return STT_ERROR_INVALID_PARAMETER;
2470 if (NULL == callback)
2471 return STT_ERROR_INVALID_PARAMETER;
2473 if (STT_STATE_CREATED != client->current_state) {
2474 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2475 return STT_ERROR_INVALID_STATE;
2478 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set default language changed cb");
2480 client->default_lang_changed_cb = callback;
2481 client->default_lang_changed_user_data = user_data;
2486 int stt_unset_default_language_changed_cb(stt_h stt)
2488 stt_client_s* client = NULL;
2489 if (0 != __stt_get_feature_enabled()) {
2490 return STT_ERROR_NOT_SUPPORTED;
2492 if (0 != __stt_check_privilege()) {
2493 return STT_ERROR_PERMISSION_DENIED;
2495 if (0 != __stt_check_handle(stt, &client)) {
2496 return STT_ERROR_INVALID_PARAMETER;
2499 if (STT_STATE_CREATED != client->current_state) {
2500 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2501 return STT_ERROR_INVALID_STATE;
2504 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset default language changed cb");
2506 client->default_lang_changed_cb = NULL;
2507 client->default_lang_changed_user_data = NULL;
2512 int stt_set_engine_changed_cb(stt_h stt, stt_engine_changed_cb callback, void* user_data)
2514 stt_client_s* client = NULL;
2515 if (0 != __stt_get_feature_enabled()) {
2516 return STT_ERROR_NOT_SUPPORTED;
2518 if (0 != __stt_check_privilege()) {
2519 return STT_ERROR_PERMISSION_DENIED;
2521 if (0 != __stt_check_handle(stt, &client)) {
2522 return STT_ERROR_INVALID_PARAMETER;
2525 if (NULL == callback)
2526 return STT_ERROR_INVALID_PARAMETER;
2528 if (STT_STATE_CREATED != client->current_state) {
2529 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2530 return STT_ERROR_INVALID_STATE;
2533 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set engine changed cb");
2535 client->engine_changed_cb = callback;
2536 client->engine_changed_user_data = user_data;
2541 int stt_unset_engine_changed_cb(stt_h stt)
2543 stt_client_s* client = NULL;
2544 if (0 != __stt_get_feature_enabled()) {
2545 return STT_ERROR_NOT_SUPPORTED;
2547 if (0 != __stt_check_privilege()) {
2548 return STT_ERROR_PERMISSION_DENIED;
2550 if (0 != __stt_check_handle(stt, &client)) {
2551 return STT_ERROR_INVALID_PARAMETER;
2554 if (STT_STATE_CREATED != client->current_state) {
2555 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2556 return STT_ERROR_INVALID_STATE;
2559 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset engine changed cb");
2561 client->engine_changed_cb = NULL;
2562 client->engine_changed_user_data = NULL;
2568 int stt_set_speech_status_cb(stt_h stt, stt_speech_status_cb callback, void* user_data)
2570 stt_client_s* client = NULL;
2571 if (0 != __stt_get_feature_enabled()) {
2572 return STT_ERROR_NOT_SUPPORTED;
2574 if (0 != __stt_check_privilege()) {
2575 return STT_ERROR_PERMISSION_DENIED;
2577 if (0 != __stt_check_handle(stt, &client)) {
2578 return STT_ERROR_INVALID_PARAMETER;
2581 if (NULL == callback)
2582 return STT_ERROR_INVALID_PARAMETER;
2584 if (STT_STATE_CREATED != client->current_state) {
2585 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2586 return STT_ERROR_INVALID_STATE;
2589 SLOG(LOG_INFO, TAG_STTC, "[INFO] Set speech status cb");
2591 client->speech_status_cb = callback;
2592 client->speech_status_user_data = user_data;
2597 int stt_unset_speech_status_cb(stt_h stt)
2599 stt_client_s* client = NULL;
2600 if (0 != __stt_get_feature_enabled()) {
2601 return STT_ERROR_NOT_SUPPORTED;
2603 if (0 != __stt_check_privilege()) {
2604 return STT_ERROR_PERMISSION_DENIED;
2606 if (0 != __stt_check_handle(stt, &client)) {
2607 return STT_ERROR_INVALID_PARAMETER;
2610 if (STT_STATE_CREATED != client->current_state) {
2611 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2612 return STT_ERROR_INVALID_STATE;
2615 SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset speech status cb");
2617 client->speech_status_cb = NULL;
2618 client->speech_status_user_data = NULL;
2623 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)
2625 stt_client_s* client = NULL;
2626 if (0 != __stt_get_feature_enabled()) {
2627 return STT_ERROR_NOT_SUPPORTED;
2629 if (0 != __stt_check_privilege()) {
2630 return STT_ERROR_PERMISSION_DENIED;
2632 if (0 != __stt_check_handle(stt, &client)) {
2633 return STT_ERROR_INVALID_PARAMETER;
2635 if (NULL == filepath) {
2636 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
2637 return STT_ERROR_INVALID_PARAMETER;
2640 SLOG(LOG_INFO, TAG_STTC, "===== STT START FILE");
2643 if (client->current_state != STT_STATE_READY) {
2644 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
2645 return STT_ERROR_INVALID_STATE;
2648 if (STT_INTERNAL_STATE_NONE != client->internal_state) {
2649 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is NOT none : %d", client->internal_state);
2650 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2654 char appid[1024] = {0, };
2655 ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
2657 if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
2658 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID");
2660 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Current app id is %s", appid);
2664 if (NULL == language) {
2665 temp = strdup("default");
2667 temp = strdup(language);
2671 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
2672 return STT_ERROR_OUT_OF_MEMORY;
2675 if (true == client->credential_needed && NULL == client->credential) {
2676 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
2679 return STT_ERROR_PERMISSION_DENIED;
2682 client->internal_state = STT_INTERNAL_STATE_STARTING;
2683 ret = stt_dbus_request_start_file(client->uid, temp, type, client->silence, appid, client->credential, filepath, audio_type, sample_rate);
2685 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start file : %s", __stt_get_error_code(ret));
2686 client->internal_state = STT_INTERNAL_STATE_NONE;
2688 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start is successful but not done");
2694 SLOG(LOG_DEBUG, TAG_STTC, "=====");
2695 SLOG(LOG_DEBUG, TAG_STTC, " ");
2700 int stt_cancel_file(stt_h stt)
2702 stt_client_s* client = NULL;
2703 if (0 != __stt_get_feature_enabled()) {
2704 return STT_ERROR_NOT_SUPPORTED;
2706 if (0 != __stt_check_privilege()) {
2707 return STT_ERROR_PERMISSION_DENIED;
2709 if (0 != __stt_check_handle(stt, &client)) {
2710 return STT_ERROR_INVALID_PARAMETER;
2713 SLOG(LOG_INFO, TAG_STTC, "===== STT CANCEL FILE");
2716 if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
2717 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state);
2718 return STT_ERROR_INVALID_STATE;
2721 if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
2722 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
2723 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2724 } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
2725 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
2726 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
2727 } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
2728 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
2729 return STT_ERROR_IN_PROGRESS_TO_READY;
2732 client->internal_state = STT_INTERNAL_STATE_CANCELING;
2733 int ret = stt_dbus_request_cancel_file(client->uid);
2735 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel file : %s", __stt_get_error_code(ret));
2736 client->internal_state = STT_INTERNAL_STATE_NONE;
2738 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Cancel file is successful but not done");
2741 SLOG(LOG_DEBUG, TAG_STTC, "=====");
2742 SLOG(LOG_DEBUG, TAG_STTC, " ");
2747 void __sound_stream_ducking_state_changed_cb(sound_stream_ducking_h stream_ducking, bool is_ducked, void *user_data)
2749 SLOG(LOG_DEBUG, TAG_STTC, "@@@ ducking state changed cb");
2750 SLOG(LOG_DEBUG, TAG_STTC, "[Volume] is ducked : %d", is_ducked);
2751 // ducking_flag = true;
2755 static char* __get_ducking_stream(sound_stream_type_e stream_type)
2757 if (SOUND_STREAM_TYPE_MEDIA == stream_type)
2758 return "Media stream";
2759 else if (SOUND_STREAM_TYPE_SYSTEM == stream_type)
2760 return "System stream";
2761 else if (SOUND_STREAM_TYPE_NOTIFICATION == stream_type)
2762 return "Notification stream";
2763 else if (SOUND_STREAM_TYPE_ALARM == stream_type)
2764 return "Alarm stream";
2766 return "Non matched stream";
2769 static int __activate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h, double bg_volume_ratio)
2771 bool is_ducked = false;
2772 int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
2774 SLOG(LOG_DEBUG, TAG_STTC, "[Volume] The %s is already ducked", __get_ducking_stream(stream_type));
2776 ret = sound_manager_activate_ducking(stream_ducking_h, SND_MGR_DUCKING_DURATION, bg_volume_ratio);
2777 if (SOUND_MANAGER_ERROR_NONE != ret) {
2778 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to activate ducking for %s", __get_ducking_stream(stream_type));
2780 SLOG(LOG_INFO, TAG_STTC, "[Volume SUCCESS] Activate ducking for %s", __get_ducking_stream(stream_type));
2786 static void __change_background_volume(stt_system_volume_event_e volume_event)
2788 double bg_volume_ratio = 0.0;
2790 if (STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_FARFIELD == volume_event) {
2791 bg_volume_ratio = STT_BG_VOLUME_RATIO_FARFIELD;
2792 } else if (STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_NEARFIELD == volume_event) {
2793 bg_volume_ratio = STT_BG_VOLUME_RATIO_NEARFIELD;
2796 SLOG(LOG_INFO, TAG_STTC, "[Volume] volume ratio(%lf)", bg_volume_ratio);
2798 if (1.0 > bg_volume_ratio) {
2799 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking, bg_volume_ratio);
2800 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking, bg_volume_ratio);
2801 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking, bg_volume_ratio);
2802 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking, bg_volume_ratio);
2806 static int __deactivate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h)
2808 bool is_ducked = false;
2809 int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
2811 SLOG(LOG_DEBUG, TAG_STTC, "[Volume] The %s is already recovered from ducking", __get_ducking_stream(stream_type));
2813 ret = sound_manager_deactivate_ducking(stream_ducking_h);
2814 if (SOUND_MANAGER_ERROR_NONE != ret) {
2815 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to deactivate ducking for %s", __get_ducking_stream(stream_type));
2817 SLOG(LOG_INFO, TAG_STTC, "[Volume SUCCESS] Deactivate ducking for %s", __get_ducking_stream(stream_type));
2823 static void __recover_background_volume()
2825 SLOG(LOG_INFO, TAG_STTC, "[Volume] background volume recover");
2827 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking);
2828 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking);
2829 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking);
2830 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking);
2833 int __create_ducking_handle(void)
2836 if (NULL == g_media_stream_ducking) {
2837 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_MEDIA, __sound_stream_ducking_state_changed_cb, NULL, &g_media_stream_ducking);
2838 if (SOUND_MANAGER_ERROR_NONE != ret)
2839 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for type media, ret(%d)", ret);
2841 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for media stream is already created");
2844 if (NULL == g_system_stream_ducking) {
2845 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_SYSTEM, __sound_stream_ducking_state_changed_cb, NULL, &g_system_stream_ducking);
2846 if (SOUND_MANAGER_ERROR_NONE != ret)
2847 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for system type, ret(%d)", ret);
2849 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for system stream is already created");
2852 if (NULL == g_notification_stream_ducking) {
2853 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_NOTIFICATION, __sound_stream_ducking_state_changed_cb, NULL, &g_notification_stream_ducking);
2854 if (SOUND_MANAGER_ERROR_NONE != ret)
2855 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for notification type, ret(%d)", ret);
2857 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for notification stream is already created");
2860 if (NULL == g_alarm_stream_ducking) {
2861 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_ALARM, __sound_stream_ducking_state_changed_cb, NULL, &g_alarm_stream_ducking);
2862 if (SOUND_MANAGER_ERROR_NONE != ret)
2863 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for alarm type, ret(%d)", ret);
2865 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for alarm stream is already created");
2871 int stt_change_system_volume(stt_h stt, stt_system_volume_event_e volume_event)
2873 SLOG(LOG_DEBUG, TAG_STTC, "[STT] Change system volume, volume_event(%d)", volume_event);
2875 stt_client_s* client = NULL;
2876 if (0 != __stt_get_feature_enabled()) {
2877 return STT_ERROR_NOT_SUPPORTED;
2879 if (0 != __stt_check_privilege()) {
2880 return STT_ERROR_PERMISSION_DENIED;
2882 if (0 != __stt_check_handle(stt, &client)) {
2883 return STT_ERROR_INVALID_PARAMETER;
2887 if (client->current_state != STT_STATE_READY && client->current_state != STT_STATE_CREATED) {
2888 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY nor CREATED", client->current_state);
2889 return STT_ERROR_INVALID_STATE;
2892 /* change system volume */
2893 int ret = __create_ducking_handle();
2895 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create volume handle");
2897 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Success to create volume handle");
2900 __change_background_volume(volume_event);
2901 return STT_ERROR_NONE;
2904 int __destroy_ducking_handle(void)
2907 if (g_media_stream_ducking) {
2908 ret = sound_manager_destroy_stream_ducking(g_media_stream_ducking);
2909 if (SOUND_MANAGER_ERROR_NONE != ret)
2910 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy media stream ducking, ret(%d)", ret);
2911 g_media_stream_ducking = NULL;
2913 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for media stream is already created");
2916 if (g_system_stream_ducking) {
2917 ret = sound_manager_destroy_stream_ducking(g_system_stream_ducking);
2918 if (SOUND_MANAGER_ERROR_NONE != ret)
2919 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy system stream ducking, ret(%d)", ret);
2920 g_system_stream_ducking = NULL;
2922 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for system stream is already created");
2925 if (g_notification_stream_ducking) {
2926 ret = sound_manager_destroy_stream_ducking(g_notification_stream_ducking);
2927 if (SOUND_MANAGER_ERROR_NONE != ret)
2928 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy notification stream ducking, ret(%d)", ret);
2929 g_notification_stream_ducking = NULL;
2931 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for notification stream is already created");
2934 if (g_alarm_stream_ducking) {
2935 ret = sound_manager_destroy_stream_ducking(g_alarm_stream_ducking);
2936 if (SOUND_MANAGER_ERROR_NONE != ret)
2937 SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy alarm stream ducking, ret(%d)", ret);
2938 g_alarm_stream_ducking = NULL;
2940 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for alarm stream is already created");
2945 int stt_recover_system_volume(stt_h stt)
2947 SLOG(LOG_DEBUG, TAG_STTC, "[STT] recover system volume");
2949 stt_client_s* client = NULL;
2950 if (0 != __stt_get_feature_enabled()) {
2951 return STT_ERROR_NOT_SUPPORTED;
2953 if (0 != __stt_check_privilege()) {
2954 return STT_ERROR_PERMISSION_DENIED;
2956 if (0 != __stt_check_handle(stt, &client)) {
2957 return STT_ERROR_INVALID_PARAMETER;
2961 if (client->current_state != STT_STATE_READY && client->current_state != STT_STATE_CREATED) {
2962 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY nor CREATED", client->current_state);
2963 return STT_ERROR_INVALID_STATE;
2966 /* recover volume */
2967 __recover_background_volume();
2968 int ret = __destroy_ducking_handle();
2970 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to destroy volume handle");
2972 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Success to destroy volume handle");
2975 return STT_ERROR_NONE;