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 int __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");
273 return STT_FILE_ERROR_OPERATION_FAILED;
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");
307 return STT_FILE_ERROR_NONE;
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 */
362 struct dirent *dirp = NULL;
363 dp = opendir(STT_DEFAULT_ENGINE);
369 if (!strcmp(".", dirp->d_name) || !strcmp("..", dirp->d_name))
372 sttengine_info_s* info = NULL;
376 filesize = strlen(STT_DEFAULT_ENGINE) + strlen(dirp->d_name) + 5;
377 filepath = (char*)calloc(filesize, sizeof(char));
380 if (NULL != filepath) {
381 snprintf(filepath, filesize, "%s/%s", STT_DEFAULT_ENGINE, dirp->d_name);
383 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Memory not enough!!");
387 if (false == __stt_file_is_engine(filepath)) {
388 /* get its and update engine list */
389 if (0 == __stt_file_get_engine_info(filepath, &info)) {
390 /* add engine info to g_engine_list */
391 g_engine_list = g_slist_append(g_engine_list, info);
395 if (NULL != filepath)
398 } while (NULL != dirp);
402 SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent WARNING] Fail to open default directory");
405 if (0 >= g_slist_length(g_engine_list)) {
406 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Not found engine");
407 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
410 ret = stt_file_client_new();
412 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to create client : %s", __stt_file_get_error_code(ret));
413 return STT_FILE_ERROR_OPERATION_FAILED;
416 client = stt_file_client_get();
417 if (NULL == client) {
418 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Not initialized");
419 return STT_FILE_ERROR_OPERATION_FAILED;
422 ret = stt_config_mgr_initialize(getpid() + STT_FILE_CONFIG_HANDLE);
424 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to init config manager : %s", __stt_file_get_error_code(ret));
425 stt_file_client_destroy();
426 return STT_FILE_ERROR_OPERATION_FAILED;
430 char* engine_id = NULL;
431 ret = stt_config_mgr_get_engine(&engine_id);
433 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to get engine id : %s", __stt_file_get_error_code(ret));
434 stt_file_client_destroy();
435 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
436 __stt_file_relseae_engine_info();
437 return STT_FILE_ERROR_OPERATION_FAILED;
440 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Get engine id : %s", engine_id);
442 bool is_found = false;
444 sttengine_info_s* engine = NULL;
447 if (0 < g_slist_length(g_engine_list)) {
448 /* Get a first item */
449 iter = g_slist_nth(g_engine_list, 0);
451 while (NULL != iter) {
452 /* Get handle data from list */
455 if (0 == strcmp(engine->engine_uuid, engine_id)) {
460 iter = g_slist_next(iter);
464 if (NULL != engine_id) {
469 if (false == is_found) {
471 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Fail to find default engine");
472 iter = g_slist_nth(g_engine_list, 0);
476 if (NULL == engine) {
477 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
478 stt_file_client_destroy();
479 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
480 __stt_file_relseae_engine_info();
481 return STT_FILE_ERROR_OPERATION_FAILED;
483 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Set default engine (%s)", engine->engine_name);
486 ret = __stt_file_load_engine(engine);
488 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
489 stt_file_client_destroy();
490 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
491 __stt_file_relseae_engine_info();
492 return STT_FILE_ERROR_OPERATION_FAILED;
495 client->current_engine_id = engine->engine_id;
497 stt_network_initialize();
499 SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE Success] Initialize : pid(%d)", getpid());
501 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
502 SLOG(LOG_DEBUG, TAG_STTFC, " ");
504 return STT_FILE_ERROR_NONE;
507 int stt_file_deinitialize(void)
509 SLOG(LOG_DEBUG, TAG_STTFC, "===== Deinitialize STT FILE");
511 stt_file_client_s* client = stt_file_client_get();
512 if (NULL == client) {
513 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Not initialized");
514 return STT_FILE_ERROR_INVALID_STATE;
517 /* check used callback */
518 if (0 != stt_file_client_get_use_callback(client)) {
519 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Client should NOT deinitialize STT FILE in callback function");
520 return STT_FILE_ERROR_OPERATION_FAILED;
523 stt_network_finalize();
525 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
528 switch (client->current_state) {
529 case STT_FILE_STATE_PROCESSING:
530 /* Cancel file recognition */
531 stt_engine_recognize_cancel_file();
533 case STT_FILE_STATE_READY:
535 __stt_file_relseae_engine_info();
538 stt_file_client_destroy();
541 case STT_FILE_STATE_NONE:
542 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] NOT initialized");
548 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
549 SLOG(LOG_DEBUG, TAG_STTFC, " ");
551 return STT_FILE_ERROR_NONE;
554 int stt_file_get_state(stt_file_state_e* state)
557 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
558 return STT_FILE_ERROR_INVALID_PARAMETER;
561 stt_file_client_s* client = stt_file_client_get();
563 if (NULL == client) {
564 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Not initialized");
565 *state = STT_FILE_STATE_NONE;
566 return STT_FILE_ERROR_NONE;
569 *state = client->current_state;
572 case STT_FILE_STATE_NONE: SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'NONE'"); break;
573 case STT_FILE_STATE_READY: SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'Ready'"); break;
574 case STT_FILE_STATE_PROCESSING: SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'Processing'"); break;
575 default: SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid value"); break;
578 return STT_FILE_ERROR_NONE;
581 int stt_file_foreach_supported_engines(stt_file_supported_engine_cb callback, void* user_data)
583 SLOG(LOG_DEBUG, TAG_STTFC, "===== Foreach Supported engine");
585 if (NULL == callback) {
586 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
587 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
588 SLOG(LOG_DEBUG, TAG_STTFC, " ");
589 return STT_FILE_ERROR_INVALID_PARAMETER;
592 stt_file_client_s* client = stt_file_client_get();
595 if (NULL == client) {
596 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
597 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
598 SLOG(LOG_DEBUG, TAG_STTFC, " ");
599 return STT_FILE_ERROR_INVALID_PARAMETER;
602 if (client->current_state != STT_FILE_STATE_READY) {
603 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
604 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
605 SLOG(LOG_DEBUG, TAG_STTFC, " ");
606 return STT_FILE_ERROR_INVALID_STATE;
610 sttengine_info_s *engine = NULL;
612 /* Get a first item */
613 iter = g_slist_nth(g_engine_list, 0);
615 while (NULL != iter) {
616 /* Get handle data from list */
619 if (NULL != engine) {
620 if (false == callback(engine->engine_uuid, engine->engine_name, user_data)) {
624 iter = g_slist_next(iter);
627 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
628 SLOG(LOG_DEBUG, TAG_STTFC, " ");
630 return STT_FILE_ERROR_NONE;
633 int stt_file_get_engine(char** engine_id)
635 SLOG(LOG_DEBUG, TAG_STTFC, "===== Get current engine");
637 if (NULL == engine_id) {
638 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
639 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
640 SLOG(LOG_DEBUG, TAG_STTFC, " ");
641 return STT_FILE_ERROR_INVALID_PARAMETER;
644 stt_file_client_s* client = stt_file_client_get();
647 if (NULL == client) {
648 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
649 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
650 SLOG(LOG_DEBUG, TAG_STTFC, " ");
651 return STT_FILE_ERROR_INVALID_PARAMETER;
654 if (client->current_state != STT_FILE_STATE_READY) {
655 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
656 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
657 SLOG(LOG_DEBUG, TAG_STTFC, " ");
658 return STT_FILE_ERROR_INVALID_STATE;
661 sttengine_info_s* engine = NULL;
662 engine = __stt_file_get_engine_by_id(client->current_engine_id);
663 if (NULL == engine) {
664 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
665 return STT_FILE_ERROR_OPERATION_FAILED;
668 *engine_id = strdup(engine->engine_uuid);
670 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
671 SLOG(LOG_DEBUG, TAG_STTFC, " ");
673 return STT_FILE_ERROR_NONE;
676 int stt_file_set_engine(const char* engine_id)
678 SLOG(LOG_DEBUG, TAG_STTFC, "===== Set current engine");
680 if (NULL == engine_id) {
681 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
682 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
683 SLOG(LOG_DEBUG, TAG_STTFC, " ");
684 return STT_FILE_ERROR_INVALID_PARAMETER;
687 stt_file_client_s* client = stt_file_client_get();
690 if (NULL == client) {
691 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
692 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
693 SLOG(LOG_DEBUG, TAG_STTFC, " ");
694 return STT_FILE_ERROR_INVALID_PARAMETER;
698 if (client->current_state != STT_FILE_STATE_READY) {
699 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
700 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
701 SLOG(LOG_DEBUG, TAG_STTFC, " ");
702 return STT_FILE_ERROR_INVALID_STATE;
706 sttengine_info_s *engine = NULL;
708 int temp_old_engine = -1;
710 /* Get a first item */
711 iter = g_slist_nth(g_engine_list, 0);
713 while (NULL != iter) {
714 /* Get handle data from list */
717 if (0 == strcmp(engine->engine_uuid, engine_id)) {
718 if (client->current_engine_id != engine->engine_id) {
719 temp_old_engine = client->current_engine_id;
721 SLOG(LOG_DEBUG, TAG_STTFC, "Already loaded engine : %s", engine_id);
728 iter = g_slist_next(iter);
732 if (NULL == engine) {
733 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Engine id is NOT valid");
734 return STT_FILE_ERROR_INVALID_PARAMETER;
737 int ret = __stt_file_load_engine(engine);
739 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
740 return STT_FILE_ERROR_OPERATION_FAILED;
743 client->current_engine_id = engine->engine_id;
745 if (-1 != temp_old_engine) {
746 stt_engine_deinitialize();
750 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
751 SLOG(LOG_DEBUG, TAG_STTFC, " ");
756 bool __stt_config_supported_language_cb(const char* engine_id, const char* language, void* user_data)
758 stt_file_client_s* client = stt_file_client_get();
759 if (NULL == client) {
760 SLOG(LOG_ERROR, TAG_STTFC, "[WARNING] A handle is not valid");
764 /* call callback function */
765 if (NULL != client->supported_lang_cb) {
766 return client->supported_lang_cb(language, client->supported_lang_user_data);
768 SLOG(LOG_WARN, TAG_STTFC, "No registered callback function of supported languages");
774 int stt_file_foreach_supported_languages(stt_file_supported_language_cb callback, void* user_data)
776 SLOG(LOG_DEBUG, TAG_STTFC, "===== Foreach Supported Language");
778 if (NULL == callback) {
779 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
780 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
781 SLOG(LOG_DEBUG, TAG_STTFC, " ");
782 return STT_FILE_ERROR_INVALID_PARAMETER;
785 stt_file_client_s* client = stt_file_client_get();
788 if (NULL == client) {
789 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
790 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
791 SLOG(LOG_DEBUG, TAG_STTFC, " ");
792 return STT_FILE_ERROR_INVALID_PARAMETER;
795 sttengine_info_s* engine = NULL;
796 engine = __stt_file_get_engine_by_id(client->current_engine_id);
797 if (NULL == engine) {
798 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
799 return STT_FILE_ERROR_OPERATION_FAILED;
802 client->supported_lang_cb = callback;
803 client->supported_lang_user_data = user_data;
806 ret = stt_config_mgr_get_language_list(engine->engine_uuid, __stt_config_supported_language_cb, NULL);
808 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get languages");
811 client->supported_lang_cb = NULL;
812 client->supported_lang_user_data = NULL;
814 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
815 SLOG(LOG_DEBUG, TAG_STTFC, " ");
817 return STT_FILE_ERROR_NONE;
820 int stt_file_start(const char* language, const char* type, const char* filepath,
821 stt_file_audio_type_e audio_type, int sample_rate)
823 SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE START");
825 stt_file_client_s* client = stt_file_client_get();
828 if (NULL == client) {
829 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
830 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
831 SLOG(LOG_DEBUG, TAG_STTFC, " ");
832 return STT_FILE_ERROR_INVALID_STATE;
836 if (client->current_state != STT_FILE_STATE_READY) {
837 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
838 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
839 SLOG(LOG_DEBUG, TAG_STTFC, " ");
840 return STT_FILE_ERROR_INVALID_STATE;
843 if (NULL == language || NULL == filepath) {
844 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
845 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
846 SLOG(LOG_DEBUG, TAG_STTFC, " ");
847 return STT_FILE_ERROR_INVALID_PARAMETER;
850 /* check if engine use network */
851 sttengine_info_s* engine = NULL;
852 engine = __stt_file_get_engine_by_id(client->current_engine_id);
853 if (NULL == engine) {
854 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
855 return STT_FILE_ERROR_OPERATION_FAILED;
858 if (true == engine->use_network) {
859 if (false == stt_network_is_connected()) {
860 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Disconnect network. Current engine needs to network connection.");
861 return STT_FILE_ERROR_OUT_OF_NETWORK;
865 SLOG(LOG_DEBUG, TAG_STTFC, "[START Info] Engine(%d) Lang(%s) Type(%s) Filepath(%s) Audio(%d) Sample rate(%d)",
866 client->current_engine_id, language, type, filepath, audio_type, sample_rate);
869 ret = stt_engine_recognize_start_file(language, type, filepath, audio_type, sample_rate, NULL);
871 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to start file recognition");
872 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
873 SLOG(LOG_DEBUG, TAG_STTFC, " ");
874 return STT_FILE_ERROR_OPERATION_FAILED;
877 client->before_state = client->current_state;
878 client->current_state = STT_FILE_STATE_PROCESSING;
880 if (NULL != client->state_changed_cb) {
881 stt_file_client_use_callback(client);
882 client->state_changed_cb(client->before_state, client->current_state, client->state_changed_user_data);
883 stt_file_client_not_use_callback(client);
884 SLOG(LOG_DEBUG, TAG_STTFC, "State changed callback is called");
886 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] State changed callback is null");
889 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
890 SLOG(LOG_DEBUG, TAG_STTFC, " ");
892 return STT_FILE_ERROR_NONE;
895 int stt_file_cancel(void)
897 SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE CANCEL");
899 stt_file_client_s* client = stt_file_client_get();
902 if (NULL == client) {
903 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
904 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
905 SLOG(LOG_DEBUG, TAG_STTFC, " ");
906 return STT_FILE_ERROR_INVALID_PARAMETER;
910 if (STT_FILE_STATE_PROCESSING != client->current_state) {
911 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid state : Current state(%d) is NOT 'Processing'", client->current_state);
912 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
913 SLOG(LOG_DEBUG, TAG_STTFC, " ");
914 return STT_FILE_ERROR_INVALID_STATE;
918 ret = stt_engine_recognize_cancel_file();
920 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to cancel file recognition");
921 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
922 SLOG(LOG_DEBUG, TAG_STTFC, " ");
923 return STT_FILE_ERROR_OPERATION_FAILED;
926 client->before_state = client->current_state;
927 client->current_state = STT_FILE_STATE_READY;
929 if (NULL != client->state_changed_cb) {
930 stt_file_client_use_callback(client);
931 client->state_changed_cb(client->before_state, client->current_state, client->state_changed_user_data);
932 stt_file_client_not_use_callback(client);
933 SLOG(LOG_DEBUG, TAG_STTFC, "State changed callback is called");
935 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] State changed callback is null");
938 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
939 SLOG(LOG_DEBUG, TAG_STTFC, " ");
941 return STT_FILE_ERROR_NONE;
944 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)
946 SLOG(LOG_DEBUG, TAG_STTFC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
947 index, event, text, start_time, end_time);
949 stt_file_client_s* client = stt_file_client_get();
952 if (NULL == client) {
953 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to notify error : A handle is not valid");
957 if (NULL != client->result_time_cb) {
958 client->result_time_cb(index, (stt_file_result_time_event_e)event,
959 text, start_time, end_time, client->result_time_user_data);
961 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Callback is NULL");
968 int stt_file_foreach_detailed_result(stt_file_result_time_cb callback, void* user_data)
970 SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE FOREACH DETAILED RESULT");
972 if (NULL == callback) {
973 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
974 return STT_FILE_ERROR_INVALID_PARAMETER;
977 stt_file_client_s* client = stt_file_client_get();
980 if (NULL == client) {
981 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail : A handle is not valid");
982 return STT_FILE_ERROR_INVALID_PARAMETER;
985 client->result_time_cb = callback;
986 client->result_time_user_data = user_data;
988 stt_engine_foreach_result_time(client->time_info, __stt_file_result_time_cb, NULL);
990 client->result_time_cb = NULL;
991 client->result_time_user_data = NULL;
993 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
994 SLOG(LOG_DEBUG, TAG_STTFC, " ");
996 return STT_FILE_ERROR_NONE;
999 int stt_file_set_recognition_result_cb(stt_file_recognition_result_cb callback, void* user_data)
1001 if (NULL == callback)
1002 return STT_FILE_ERROR_INVALID_PARAMETER;
1004 stt_file_client_s* client = stt_file_client_get();
1007 if (NULL == client) {
1008 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1009 return STT_FILE_ERROR_INVALID_PARAMETER;
1012 if (STT_FILE_STATE_READY != client->current_state) {
1013 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1014 return STT_FILE_ERROR_INVALID_STATE;
1017 int ret = stt_engine_set_recognition_result_cb(__stt_file_result_cb, NULL);
1019 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to set recognition result cb");
1022 client->recognition_result_cb = callback;
1023 client->recognition_result_user_data = user_data;
1028 int stt_file_unset_recognition_result_cb(void)
1030 stt_file_client_s* client = stt_file_client_get();
1033 if (NULL == client) {
1034 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1035 return STT_FILE_ERROR_INVALID_PARAMETER;
1038 if (STT_FILE_STATE_READY != client->current_state) {
1039 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1040 return STT_FILE_ERROR_INVALID_STATE;
1043 int ret = stt_engine_set_recognition_result_cb(NULL, NULL);
1045 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to set recognition result cb");
1048 client->recognition_result_cb = NULL;
1049 client->recognition_result_user_data = NULL;
1054 int stt_file_set_state_changed_cb(stt_file_state_changed_cb callback, void* user_data)
1056 if (NULL == callback)
1057 return STT_FILE_ERROR_INVALID_PARAMETER;
1059 stt_file_client_s* client = stt_file_client_get();
1062 if (NULL == client) {
1063 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1064 return STT_FILE_ERROR_INVALID_PARAMETER;
1067 if (STT_FILE_STATE_READY != client->current_state) {
1068 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1069 return STT_FILE_ERROR_INVALID_STATE;
1072 client->state_changed_cb = callback;
1073 client->state_changed_user_data = user_data;
1078 int stt_file_unset_state_changed_cb(void)
1080 stt_file_client_s* client = stt_file_client_get();
1083 if (NULL == client) {
1084 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1085 return STT_FILE_ERROR_INVALID_PARAMETER;
1088 if (STT_FILE_STATE_READY != client->current_state) {
1089 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1090 return STT_FILE_ERROR_INVALID_STATE;
1093 client->state_changed_cb = NULL;
1094 client->state_changed_user_data = NULL;