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.
26 #include "stt_config_mgr.h"
28 #include "stt_engine.h"
30 #include "stt_file_client.h"
31 #include "stt_network.h"
34 typedef struct _sttengine_info {
40 char* engine_setting_path;
46 static GSList* g_engine_list;
49 #define STT_FILE_CONFIG_HANDLE 100000
52 static const char* __stt_file_get_error_code(stt_file_error_e err)
55 case STT_FILE_ERROR_NONE: return "STT_FILE_ERROR_NONE";
56 case STT_FILE_ERROR_OUT_OF_MEMORY: return "STT_FILE_ERROR_OUT_OF_MEMORY";
57 case STT_FILE_ERROR_IO_ERROR: return "STT_FILE_ERROR_IO_ERROR";
58 case STT_FILE_ERROR_INVALID_PARAMETER: return "STT_FILE_ERROR_INVALID_PARAMETER";
59 case STT_FILE_ERROR_OUT_OF_NETWORK: return "STT_FILE_ERROR_OUT_OF_NETWORK";
60 case STT_FILE_ERROR_INVALID_STATE: return "STT_FILE_ERROR_INVALID_STATE";
61 case STT_FILE_ERROR_INVALID_LANGUAGE: return "STT_FILE_ERROR_INVALID_LANGUAGE";
62 case STT_FILE_ERROR_ENGINE_NOT_FOUND: return "STT_FILE_ERROR_ENGINE_NOT_FOUND";
63 case STT_FILE_ERROR_OPERATION_FAILED: return "STT_FILE_ERROR_OPERATION_FAILED";
64 case STT_FILE_ERROR_NOT_SUPPORTED_FEATURE: return "STT_FILE_ERROR_NOT_SUPPORTED_FEATURE";
65 case STT_FILE_ERROR_NOT_AGREE_SERVICE: return "STT_FILE_ERROR_NOT_AGREE_SERVICE";
67 return "Invalid error code";
71 void __stt_file_engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name,
72 bool use_network, void* user_data)
74 sttengine_info_s* temp = (sttengine_info_s*)user_data;
76 temp->engine_uuid = g_strdup(engine_uuid);
77 temp->engine_name = g_strdup(engine_name);
78 temp->engine_setting_path = g_strdup(setting_ug_name);
79 temp->use_network = use_network;
82 static int __stt_file_get_engine_info(const char* filepath, sttengine_info_s** info)
84 #ifdef __UNUSED_CODES__
85 if (NULL == filepath || NULL == info) {
86 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Invalid Parameter");
87 return STT_FILE_ERROR_INVALID_PARAMETER;
94 handle = dlopen(filepath, RTLD_LAZY);
97 SECURE_SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent] Invalid engine : %s", filepath);
98 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
101 /* link engine to stt-service */
102 dlsym(handle, "sttp_load_engine");
103 if ((error = dlerror()) != NULL) {
104 SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent] Invalid engine. Fail to open sttp_load_engine : %s", error);
106 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
109 dlsym(handle, "sttp_unload_engine");
110 if ((error = dlerror()) != NULL) {
111 SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent] Invalid engine. Fail to open sttp_unload_engine : %s", error);
113 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
116 int (*get_engine_info)(sttpe_engine_info_cb callback, void* user_data);
118 get_engine_info = (int (*)(sttpe_engine_info_cb, void*))dlsym(handle, "sttp_get_engine_info");
119 if ((error = dlerror()) != NULL || NULL == get_engine_info) {
120 SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent WARNING] Invalid engine. Fail to open sttp_get_engine_info : %s", error);
122 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
125 sttengine_info_s* temp;
126 temp = (sttengine_info_s*)calloc(1, sizeof(sttengine_info_s));
128 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to allocate memory");
130 return STT_FILE_ERROR_OUT_OF_MEMORY;
133 /* get engine info */
134 if (0 != get_engine_info(__stt_file_engine_info_cb, (void*)temp)) {
135 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to get engine info from engine");
138 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
144 temp->engine_id = g_engine_id_count;
147 temp->engine_path = g_strdup(filepath);
148 temp->is_loaded = false;
150 SLOG(LOG_DEBUG, TAG_STTFC, "----- Valid Engine");
151 SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Engine id : %d", temp->engine_id);
152 SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Engine uuid : %s", temp->engine_uuid);
153 SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Engine name : %s", temp->engine_name);
154 SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Engine path : %s", temp->engine_path);
155 SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Engine setting path : %s", temp->engine_setting_path);
156 SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Use network : %s", temp->use_network ? "true" : "false");
157 SLOG(LOG_DEBUG, TAG_STTFC, "-----");
158 SLOG(LOG_DEBUG, TAG_STTFC, " ");
163 return STT_FILE_ERROR_NONE;
166 static bool __stt_file_is_engine(const char* filepath)
169 sttengine_info_s *engine = NULL;
171 if (0 < g_slist_length(g_engine_list)) {
172 /* Get a first item */
173 iter = g_slist_nth(g_engine_list, 0);
175 while (NULL != iter) {
176 /* Get handle data from list */
179 if (0 == strcmp(engine->engine_path, filepath)) {
183 iter = g_slist_next(iter);
190 void __stt_file_relseae_engine_info(void)
194 /* Release engine list */
195 sttengine_info_s *engine = NULL;
197 if (0 < g_slist_length(g_engine_list)) {
198 /* Get a first item */
199 iter = g_slist_nth(g_engine_list, 0);
201 while (NULL != iter) {
202 /* Get handle data from list */
204 g_engine_list = g_slist_remove_link(g_engine_list, iter);
206 /* Check engine unload */
207 if (NULL != engine) {
208 if (engine->is_loaded) {
209 SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "[Engine Agent] Unload engine id(%d)", engine->engine_id);
211 if (0 != stt_engine_deinitialize())
212 SECURE_SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent] Fail to deinitialize engine id(%d)", engine->engine_id);
214 if (0 != stt_engine_unload())
215 SECURE_SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent] Fail to unload engine id(%d)", engine->engine_id);
217 engine->is_loaded = false;
220 if (NULL != engine->engine_uuid) free(engine->engine_uuid);
221 if (NULL != engine->engine_path) free(engine->engine_path);
222 if (NULL != engine->engine_name) free(engine->engine_name);
223 if (NULL != engine->engine_setting_path)free(engine->engine_setting_path);
228 iter = g_slist_nth(g_engine_list, 0);
233 static sttengine_info_s* __stt_file_get_engine_by_id(int engine_id)
236 sttengine_info_s *data = NULL;
238 iter = g_slist_nth(g_engine_list, 0);
240 while (NULL != iter) {
244 if (data->engine_id == engine_id)
247 iter = g_slist_next(iter);
253 void __stt_file_result_cb(stte_result_event_e event, const char* type, const char** data, int data_count,
254 const char* msg, void* time_info, void *user_data)
257 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Result event(%d) type(%s) msg(%s)", event, type, msg);
261 for (i = 0; i < data_count; i++) {
262 if (NULL != data[i]) {
263 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] [%d] %s", i, data[i]);
268 stt_file_client_s* client = stt_file_client_get();
271 if (NULL == client) {
272 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to notify error : A handle is not valid");
276 if (NULL != time_info) {
277 client->time_info = time_info;
280 if (NULL != client->recognition_result_cb) {
281 stt_file_client_use_callback(client);
282 client->recognition_result_cb(event, data, data_count, msg, client->recognition_result_user_data);
283 stt_file_client_not_use_callback(client);
284 SLOG(LOG_DEBUG, TAG_STTFC, "client recognition result callback called");
286 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] User recognition result callback is NULL");
289 client->time_info = NULL;
291 if (STTE_RESULT_EVENT_FINAL_RESULT == event || STTE_RESULT_EVENT_ERROR == event) {
292 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] State change : 'Ready'");
294 client->before_state = client->current_state;
295 client->current_state = STT_FILE_STATE_READY;
297 if (NULL != client->state_changed_cb) {
298 stt_file_client_use_callback(client);
299 client->state_changed_cb(client->before_state, client->current_state, client->state_changed_user_data);
300 stt_file_client_not_use_callback(client);
301 SLOG(LOG_DEBUG, TAG_STTFC, "State changed callback is called");
303 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] State changed callback is null");
310 void __stt_file_speech_status_cb(stte_speech_status_e status, void *user_data)
312 /* it seems to be no necessity yet */
313 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] This callback should NOT be called.");
317 void __stt_file_error_cb(stte_error_e error, const char* msg)
319 /* it seems to be no necessity yet */
320 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Error callback is called");
324 static int __stt_file_load_engine(sttengine_info_s* engine)
326 if (NULL == engine) {
327 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Input engine is NULL");
328 return STT_FILE_ERROR_INVALID_PARAMETER;
331 if (0 != stt_engine_load(engine->engine_path, NULL)) {
332 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to load engine(%s)", engine->engine_path);
333 return STT_FILE_ERROR_OPERATION_FAILED;
336 int ret = stt_engine_initialize(true);
338 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine : id(%d) path(%s)", engine->engine_id, engine->engine_path);
339 return STT_FILE_ERROR_OPERATION_FAILED;
342 engine->is_loaded = true;
344 SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE Success] Load engine id(%d) path(%s)", engine->engine_id, engine->engine_path);
346 return STT_FILE_ERROR_NONE;
349 int stt_file_initialize(void)
351 SLOG(LOG_DEBUG, TAG_STTFC, "===== Initialize STT FILE");
353 stt_file_client_s* client = stt_file_client_get();
354 if (NULL != client) {
355 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Already initialized");
356 return STT_FILE_ERROR_INVALID_STATE;
359 /* Get file name from default engine directory */
363 struct dirent *dirp = NULL;
364 dp = opendir(STT_DEFAULT_ENGINE);
367 ret = readdir_r(dp, &entry, &dirp);
369 SLOG(LOG_ERROR, TAG_STTFC, "[File ERROR] Fail to read directory");
374 sttengine_info_s* info = NULL;
378 filesize = strlen(STT_DEFAULT_ENGINE) + strlen(dirp->d_name) + 5;
379 filepath = (char*)calloc(filesize, sizeof(char));
382 if (NULL != filepath) {
383 snprintf(filepath, filesize, "%s/%s", STT_DEFAULT_ENGINE, dirp->d_name);
385 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Memory not enough!!");
389 if (false == __stt_file_is_engine(filepath)) {
390 /* get its and update engine list */
391 if (0 == __stt_file_get_engine_info(filepath, &info)) {
392 /* add engine info to g_engine_list */
393 g_engine_list = g_slist_append(g_engine_list, info);
397 if (NULL != filepath)
400 } while (NULL != dirp);
404 SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent WARNING] Fail to open default directory");
407 if (0 >= g_slist_length(g_engine_list)) {
408 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Not found engine");
409 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
412 ret = stt_file_client_new();
414 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to create client : %s", __stt_file_get_error_code(ret));
415 return STT_FILE_ERROR_OPERATION_FAILED;
418 client = stt_file_client_get();
419 if (NULL == client) {
420 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Not initialized");
421 return STT_FILE_ERROR_OPERATION_FAILED;
424 ret = stt_config_mgr_initialize(getpid() + STT_FILE_CONFIG_HANDLE);
426 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to init config manager : %s", __stt_file_get_error_code(ret));
427 stt_file_client_destroy();
428 return STT_FILE_ERROR_OPERATION_FAILED;
432 char* engine_id = NULL;
433 ret = stt_config_mgr_get_engine(&engine_id);
435 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to get engine id : %s", __stt_file_get_error_code(ret));
436 stt_file_client_destroy();
437 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
438 __stt_file_relseae_engine_info();
439 return STT_FILE_ERROR_OPERATION_FAILED;
442 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Get engine id : %s", engine_id);
444 bool is_found = false;
446 sttengine_info_s* engine = NULL;
449 if (0 < g_slist_length(g_engine_list)) {
450 /* Get a first item */
451 iter = g_slist_nth(g_engine_list, 0);
453 while (NULL != iter) {
454 /* Get handle data from list */
457 if (0 == strcmp(engine->engine_uuid, engine_id)) {
462 iter = g_slist_next(iter);
466 if (NULL != engine_id) {
471 if (false == is_found) {
473 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Fail to find default engine");
474 iter = g_slist_nth(g_engine_list, 0);
478 if (NULL == engine) {
479 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
480 stt_file_client_destroy();
481 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
482 __stt_file_relseae_engine_info();
483 return STT_FILE_ERROR_OPERATION_FAILED;
485 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Set default engine (%s)", engine->engine_name);
488 ret = __stt_file_load_engine(engine);
490 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
491 stt_file_client_destroy();
492 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
493 __stt_file_relseae_engine_info();
494 return STT_FILE_ERROR_OPERATION_FAILED;
497 client->current_engine_id = engine->engine_id;
499 stt_network_initialize();
501 SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE Success] Initialize : pid(%d)", getpid());
503 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
504 SLOG(LOG_DEBUG, TAG_STTFC, " ");
506 return STT_FILE_ERROR_NONE;
509 int stt_file_deinitialize(void)
511 SLOG(LOG_DEBUG, TAG_STTFC, "===== Deinitialize STT FILE");
513 stt_file_client_s* client = stt_file_client_get();
514 if (NULL == client) {
515 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Not initialized");
516 return STT_FILE_ERROR_INVALID_STATE;
519 /* check used callback */
520 if (0 != stt_file_client_get_use_callback(client)) {
521 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Client should NOT deinitialize STT FILE in callback function");
522 return STT_FILE_ERROR_OPERATION_FAILED;
525 stt_network_finalize();
527 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
530 switch (client->current_state) {
531 case STT_FILE_STATE_PROCESSING:
532 /* Cancel file recognition */
533 stt_engine_recognize_cancel_file();
535 case STT_FILE_STATE_READY:
537 __stt_file_relseae_engine_info();
540 stt_file_client_destroy();
543 case STT_FILE_STATE_NONE:
544 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] NOT initialized");
550 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
551 SLOG(LOG_DEBUG, TAG_STTFC, " ");
553 return STT_FILE_ERROR_NONE;
556 int stt_file_get_state(stt_file_state_e* state)
559 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
560 return STT_FILE_ERROR_INVALID_PARAMETER;
563 stt_file_client_s* client = stt_file_client_get();
565 if (NULL == client) {
566 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Not initialized");
567 *state = STT_FILE_STATE_NONE;
568 return STT_FILE_ERROR_NONE;
571 *state = client->current_state;
574 case STT_FILE_STATE_NONE: SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'NONE'"); break;
575 case STT_FILE_STATE_READY: SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'Ready'"); break;
576 case STT_FILE_STATE_PROCESSING: SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'Processing'"); break;
577 default: SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid value"); break;
580 return STT_FILE_ERROR_NONE;
583 int stt_file_foreach_supported_engines(stt_file_supported_engine_cb callback, void* user_data)
585 SLOG(LOG_DEBUG, TAG_STTFC, "===== Foreach Supported engine");
587 if (NULL == callback) {
588 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
589 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
590 SLOG(LOG_DEBUG, TAG_STTFC, " ");
591 return STT_FILE_ERROR_INVALID_PARAMETER;
594 stt_file_client_s* client = stt_file_client_get();
597 if (NULL == client) {
598 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
599 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
600 SLOG(LOG_DEBUG, TAG_STTFC, " ");
601 return STT_FILE_ERROR_INVALID_PARAMETER;
604 if (client->current_state != STT_FILE_STATE_READY) {
605 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
606 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
607 SLOG(LOG_DEBUG, TAG_STTFC, " ");
608 return STT_FILE_ERROR_INVALID_STATE;
612 sttengine_info_s *engine = NULL;
614 /* Get a first item */
615 iter = g_slist_nth(g_engine_list, 0);
617 while (NULL != iter) {
618 /* Get handle data from list */
621 if (NULL != engine) {
622 if (false == callback(engine->engine_uuid, engine->engine_name, user_data)) {
626 iter = g_slist_next(iter);
629 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
630 SLOG(LOG_DEBUG, TAG_STTFC, " ");
632 return STT_FILE_ERROR_NONE;
635 int stt_file_get_engine(char** engine_id)
637 SLOG(LOG_DEBUG, TAG_STTFC, "===== Get current engine");
639 if (NULL == engine_id) {
640 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
641 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
642 SLOG(LOG_DEBUG, TAG_STTFC, " ");
643 return STT_FILE_ERROR_INVALID_PARAMETER;
646 stt_file_client_s* client = stt_file_client_get();
649 if (NULL == client) {
650 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
651 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
652 SLOG(LOG_DEBUG, TAG_STTFC, " ");
653 return STT_FILE_ERROR_INVALID_PARAMETER;
656 if (client->current_state != STT_FILE_STATE_READY) {
657 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
658 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
659 SLOG(LOG_DEBUG, TAG_STTFC, " ");
660 return STT_FILE_ERROR_INVALID_STATE;
663 sttengine_info_s* engine = NULL;
664 engine = __stt_file_get_engine_by_id(client->current_engine_id);
665 if (NULL == engine) {
666 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
667 return STT_FILE_ERROR_OPERATION_FAILED;
670 *engine_id = strdup(engine->engine_uuid);
672 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
673 SLOG(LOG_DEBUG, TAG_STTFC, " ");
675 return STT_FILE_ERROR_NONE;
678 int stt_file_set_engine(const char* engine_id)
680 SLOG(LOG_DEBUG, TAG_STTFC, "===== Set current engine");
682 if (NULL == engine_id) {
683 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
684 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
685 SLOG(LOG_DEBUG, TAG_STTFC, " ");
686 return STT_FILE_ERROR_INVALID_PARAMETER;
689 stt_file_client_s* client = stt_file_client_get();
692 if (NULL == client) {
693 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
694 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
695 SLOG(LOG_DEBUG, TAG_STTFC, " ");
696 return STT_FILE_ERROR_INVALID_PARAMETER;
700 if (client->current_state != STT_FILE_STATE_READY) {
701 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
702 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
703 SLOG(LOG_DEBUG, TAG_STTFC, " ");
704 return STT_FILE_ERROR_INVALID_STATE;
708 sttengine_info_s *engine = NULL;
710 int temp_old_engine = -1;
712 /* Get a first item */
713 iter = g_slist_nth(g_engine_list, 0);
715 while (NULL != iter) {
716 /* Get handle data from list */
719 if (0 == strcmp(engine->engine_uuid, engine_id)) {
720 if (client->current_engine_id != engine->engine_id) {
721 temp_old_engine = client->current_engine_id;
723 SLOG(LOG_DEBUG, TAG_STTFC, "Already loaded engine : %s", engine_id);
730 iter = g_slist_next(iter);
734 if (NULL == engine) {
735 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Engine id is NOT valid");
736 return STT_FILE_ERROR_INVALID_PARAMETER;
739 int ret = __stt_file_load_engine(engine);
741 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
742 return STT_FILE_ERROR_OPERATION_FAILED;
745 client->current_engine_id = engine->engine_id;
747 if (-1 != temp_old_engine) {
748 stt_engine_deinitialize();
752 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
753 SLOG(LOG_DEBUG, TAG_STTFC, " ");
758 bool __stt_config_supported_language_cb(const char* engine_id, const char* language, void* user_data)
760 stt_file_client_s* client = stt_file_client_get();
761 if (NULL == client) {
762 SLOG(LOG_ERROR, TAG_STTFC, "[WARNING] A handle is not valid");
766 /* call callback function */
767 if (NULL != client->supported_lang_cb) {
768 return client->supported_lang_cb(language, client->supported_lang_user_data);
770 SLOG(LOG_WARN, TAG_STTFC, "No registered callback function of supported languages");
776 int stt_file_foreach_supported_languages(stt_file_supported_language_cb callback, void* user_data)
778 SLOG(LOG_DEBUG, TAG_STTFC, "===== Foreach Supported Language");
780 if (NULL == callback) {
781 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
782 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
783 SLOG(LOG_DEBUG, TAG_STTFC, " ");
784 return STT_FILE_ERROR_INVALID_PARAMETER;
787 stt_file_client_s* client = stt_file_client_get();
790 if (NULL == client) {
791 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
792 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
793 SLOG(LOG_DEBUG, TAG_STTFC, " ");
794 return STT_FILE_ERROR_INVALID_PARAMETER;
797 sttengine_info_s* engine = NULL;
798 engine = __stt_file_get_engine_by_id(client->current_engine_id);
799 if (NULL == engine) {
800 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
801 return STT_FILE_ERROR_OPERATION_FAILED;
804 client->supported_lang_cb = callback;
805 client->supported_lang_user_data = user_data;
808 ret = stt_config_mgr_get_language_list(engine->engine_uuid, __stt_config_supported_language_cb, NULL);
810 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get languages");
813 client->supported_lang_cb = NULL;
814 client->supported_lang_user_data = NULL;
816 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
817 SLOG(LOG_DEBUG, TAG_STTFC, " ");
819 return STT_FILE_ERROR_NONE;
822 int stt_file_start(const char* language, const char* type, const char* filepath,
823 stt_file_audio_type_e audio_type, int sample_rate)
825 SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE START");
827 stt_file_client_s* client = stt_file_client_get();
830 if (NULL == client) {
831 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
832 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
833 SLOG(LOG_DEBUG, TAG_STTFC, " ");
834 return STT_FILE_ERROR_INVALID_STATE;
838 if (client->current_state != STT_FILE_STATE_READY) {
839 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
840 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
841 SLOG(LOG_DEBUG, TAG_STTFC, " ");
842 return STT_FILE_ERROR_INVALID_STATE;
845 if (NULL == language || NULL == filepath) {
846 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
847 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
848 SLOG(LOG_DEBUG, TAG_STTFC, " ");
849 return STT_FILE_ERROR_INVALID_PARAMETER;
852 /* check if engine use network */
853 sttengine_info_s* engine = NULL;
854 engine = __stt_file_get_engine_by_id(client->current_engine_id);
855 if (NULL == engine) {
856 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
857 return STT_FILE_ERROR_OPERATION_FAILED;
860 if (true == engine->use_network) {
861 if (false == stt_network_is_connected()) {
862 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Disconnect network. Current engine needs to network connection.");
863 return STT_FILE_ERROR_OUT_OF_NETWORK;
867 SLOG(LOG_DEBUG, TAG_STTFC, "[START Info] Engine(%d) Lang(%s) Type(%s) Filepath(%s) Audio(%d) Sample rate(%d)",
868 client->current_engine_id, language, type, filepath, audio_type, sample_rate);
871 ret = stt_engine_recognize_start_file(language, type, filepath, audio_type, sample_rate, NULL);
873 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to start file recognition");
874 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
875 SLOG(LOG_DEBUG, TAG_STTFC, " ");
876 return STT_FILE_ERROR_OPERATION_FAILED;
879 client->before_state = client->current_state;
880 client->current_state = STT_FILE_STATE_PROCESSING;
882 if (NULL != client->state_changed_cb) {
883 stt_file_client_use_callback(client);
884 client->state_changed_cb(client->before_state, client->current_state, client->state_changed_user_data);
885 stt_file_client_not_use_callback(client);
886 SLOG(LOG_DEBUG, TAG_STTFC, "State changed callback is called");
888 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] State changed callback is null");
891 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
892 SLOG(LOG_DEBUG, TAG_STTFC, " ");
894 return STT_FILE_ERROR_NONE;
897 int stt_file_cancel(void)
899 SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE CANCEL");
901 stt_file_client_s* client = stt_file_client_get();
904 if (NULL == client) {
905 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
906 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
907 SLOG(LOG_DEBUG, TAG_STTFC, " ");
908 return STT_FILE_ERROR_INVALID_PARAMETER;
912 if (STT_FILE_STATE_PROCESSING != client->current_state) {
913 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid state : Current state(%d) is NOT 'Processing'", client->current_state);
914 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
915 SLOG(LOG_DEBUG, TAG_STTFC, " ");
916 return STT_FILE_ERROR_INVALID_STATE;
920 ret = stt_engine_recognize_cancel_file();
922 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to cancel file recognition");
923 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
924 SLOG(LOG_DEBUG, TAG_STTFC, " ");
925 return STT_FILE_ERROR_OPERATION_FAILED;
928 client->before_state = client->current_state;
929 client->current_state = STT_FILE_STATE_READY;
931 if (NULL != client->state_changed_cb) {
932 stt_file_client_use_callback(client);
933 client->state_changed_cb(client->before_state, client->current_state, client->state_changed_user_data);
934 stt_file_client_not_use_callback(client);
935 SLOG(LOG_DEBUG, TAG_STTFC, "State changed callback is called");
937 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] State changed callback is null");
940 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
941 SLOG(LOG_DEBUG, TAG_STTFC, " ");
943 return STT_FILE_ERROR_NONE;
946 bool __stt_file_result_time_cb(int index, stte_result_time_event_e event, const char* text, long start_time, long end_time, void* user_data)
948 SLOG(LOG_DEBUG, TAG_STTFC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
949 index, event, text, start_time, end_time);
951 stt_file_client_s* client = stt_file_client_get();
954 if (NULL == client) {
955 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to notify error : A handle is not valid");
959 if (NULL != client->result_time_cb) {
960 client->result_time_cb(index, (stt_file_result_time_event_e)event,
961 text, start_time, end_time, client->result_time_user_data);
963 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Callback is NULL");
970 int stt_file_foreach_detailed_result(stt_file_result_time_cb callback, void* user_data)
972 SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE FOREACH DETAILED RESULT");
974 if (NULL == callback) {
975 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
976 return STT_FILE_ERROR_INVALID_PARAMETER;
979 stt_file_client_s* client = stt_file_client_get();
982 if (NULL == client) {
983 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail : A handle is not valid");
984 return STT_FILE_ERROR_INVALID_PARAMETER;
987 client->result_time_cb = callback;
988 client->result_time_user_data = user_data;
990 stt_engine_foreach_result_time(client->time_info, __stt_file_result_time_cb, NULL);
992 client->result_time_cb = NULL;
993 client->result_time_user_data = NULL;
995 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
996 SLOG(LOG_DEBUG, TAG_STTFC, " ");
998 return STT_FILE_ERROR_NONE;
1001 int stt_file_set_recognition_result_cb(stt_file_recognition_result_cb callback, void* user_data)
1003 if (NULL == callback)
1004 return STT_FILE_ERROR_INVALID_PARAMETER;
1006 stt_file_client_s* client = stt_file_client_get();
1009 if (NULL == client) {
1010 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1011 return STT_FILE_ERROR_INVALID_PARAMETER;
1014 if (STT_FILE_STATE_READY != client->current_state) {
1015 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1016 return STT_FILE_ERROR_INVALID_STATE;
1019 int ret = stt_engine_set_recognition_result_cb(__stt_file_result_cb, NULL);
1021 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to set recognition result cb");
1024 client->recognition_result_cb = callback;
1025 client->recognition_result_user_data = user_data;
1030 int stt_file_unset_recognition_result_cb(void)
1032 stt_file_client_s* client = stt_file_client_get();
1035 if (NULL == client) {
1036 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1037 return STT_FILE_ERROR_INVALID_PARAMETER;
1040 if (STT_FILE_STATE_READY != client->current_state) {
1041 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1042 return STT_FILE_ERROR_INVALID_STATE;
1045 int ret = stt_engine_set_recognition_result_cb(NULL, NULL);
1047 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to set recognition result cb");
1050 client->recognition_result_cb = NULL;
1051 client->recognition_result_user_data = NULL;
1056 int stt_file_set_state_changed_cb(stt_file_state_changed_cb callback, void* user_data)
1058 if (NULL == callback)
1059 return STT_FILE_ERROR_INVALID_PARAMETER;
1061 stt_file_client_s* client = stt_file_client_get();
1064 if (NULL == client) {
1065 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1066 return STT_FILE_ERROR_INVALID_PARAMETER;
1069 if (STT_FILE_STATE_READY != client->current_state) {
1070 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1071 return STT_FILE_ERROR_INVALID_STATE;
1074 client->state_changed_cb = callback;
1075 client->state_changed_user_data = user_data;
1080 int stt_file_unset_state_changed_cb(void)
1082 stt_file_client_s* client = stt_file_client_get();
1085 if (NULL == client) {
1086 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1087 return STT_FILE_ERROR_INVALID_PARAMETER;
1090 if (STT_FILE_STATE_READY != client->current_state) {
1091 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1092 return STT_FILE_ERROR_INVALID_STATE;
1095 client->state_changed_cb = NULL;
1096 client->state_changed_user_data = NULL;