Fix bugs
[platform/core/uifw/stt.git] / common / stt_engine.c
1 /*
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.
12 */
13
14 #include <dlog.h>
15 #include <dlfcn.h>
16 #include <dirent.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdbool.h>
20
21 #include "stt_engine.h"
22
23 /*
24 * Internal data structure
25 */
26
27 typedef struct {
28         int     engine_id;
29         char*   engine_path;
30         void    *handle;
31
32         sttpe_funcs_s*  pefuncs;
33         sttpd_funcs_s*  pdfuncs;
34
35         int (*sttp_load_engine)(sttpd_funcs_s* pdfuncs, sttpe_funcs_s* pefuncs);
36         int (*sttp_unload_engine)();
37 } sttengine_s;
38
39 extern const char* stt_tag();
40
41 /** stt engine list */
42 static GSList *g_engine_list;
43
44 static const char* __stt_get_engine_error_code(sttp_error_e err)
45 {
46         switch (err) {
47         case STTP_ERROR_NONE:                   return "STTP_ERROR_NONE";
48         case STTP_ERROR_OUT_OF_MEMORY:          return "STTP_ERROR_OUT_OF_MEMORY";
49         case STTP_ERROR_IO_ERROR:               return "STTP_ERROR_IO_ERROR";
50         case STTP_ERROR_INVALID_PARAMETER:      return "STTP_ERROR_INVALID_PARAMETER";
51         case STTP_ERROR_TIMED_OUT:              return "STTP_ERROR_TIMED_OUT";
52         case STTP_ERROR_RECORDER_BUSY:          return "STTP_ERROR_RECORDER_BUSY";
53         case STTP_ERROR_OUT_OF_NETWORK:         return "STTP_ERROR_OUT_OF_NETWORK";
54         case STTP_ERROR_PERMISSION_DENIED:      return "STTP_ERROR_PERMISSION_DENIED";
55         case STTP_ERROR_NOT_SUPPORTED:          return "STTP_ERROR_NOT_SUPPORTED";
56         case STTP_ERROR_INVALID_STATE:          return "STTP_ERROR_INVALID_STATE";
57         case STTP_ERROR_INVALID_LANGUAGE:       return "STTP_ERROR_INVALID_LANGUAGE";
58         case STTP_ERROR_ENGINE_NOT_FOUND:       return "STTP_ERROR_ENGINE_NOT_FOUND";
59         case STTP_ERROR_OPERATION_FAILED:       return "STTP_ERROR_OPERATION_FAILED";
60         case STTP_ERROR_NOT_SUPPORTED_FEATURE:  return "STTP_ERROR_NOT_SUPPORTED_FEATURE";
61         default:
62                 return "Invalid error code";
63         }
64 }
65
66 static sttengine_s* __get_engine(int engine_id)
67 {
68         /* check whether engine id is valid or not.*/
69         GSList *iter = NULL;
70         sttengine_s *engine = NULL;
71
72         if (g_slist_length(g_engine_list) > 0) {
73                 /*Get a first item*/
74                 iter = g_slist_nth(g_engine_list, 0);
75
76                 while (NULL != iter) {
77                         /*Get handle data from list*/
78                         engine = iter->data;
79
80                         if (engine_id == engine->engine_id) {
81                                 return engine;
82                         }
83
84                         /*Get next item*/
85                         iter = g_slist_next(iter);
86                 }
87         }
88
89         return NULL;
90 }
91
92 /* Register engine id */
93 int stt_engine_load(int engine_id, const char* filepath)
94 {
95         if (NULL == filepath || engine_id < 0) {
96                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
97                 return STTP_ERROR_INVALID_PARAMETER;
98         }
99
100         sttengine_s* engine = NULL;
101         engine = __get_engine(engine_id);
102         if (NULL != engine) {
103                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is already loaded", engine_id);
104                 return 0;
105         }
106
107         SECURE_SLOG(LOG_DEBUG, stt_tag(), "[Engine] Load engine id(%d), path(%s)", engine_id, filepath);
108
109         /* allocation memory */
110         engine = (sttengine_s*)calloc(1, sizeof(sttengine_s));
111         if (NULL == engine) {
112                 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory");
113                 return STTP_ERROR_OUT_OF_MEMORY;
114         }
115
116         /* load engine */
117         char *error;
118
119         engine->handle = dlopen(filepath, RTLD_LAZY);
120         if (!engine->handle) {
121                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine] Invalid engine (Fail dlopen) : %s", filepath);
122                 free(engine);
123                 return STTP_ERROR_OPERATION_FAILED;
124         }
125
126         engine->pefuncs = (sttpe_funcs_s*)calloc(1, sizeof(sttpe_funcs_s));
127         if (NULL == engine->pefuncs) {
128                 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory");
129                 dlclose(engine->handle);
130                 free(engine);
131                 return STTP_ERROR_OUT_OF_MEMORY;
132         }
133         engine->pdfuncs = (sttpd_funcs_s*)calloc(1, sizeof(sttpd_funcs_s));
134         if (NULL == engine->pdfuncs) {
135                 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory");
136                 dlclose(engine->handle);
137                 free(engine->pefuncs);
138                 free(engine);
139                 return STTP_ERROR_OUT_OF_MEMORY;
140         }
141
142         engine->sttp_unload_engine = NULL;
143         engine->sttp_load_engine = NULL;
144
145         engine->sttp_unload_engine = (int (*)())dlsym(engine->handle, "sttp_unload_engine");
146         if (NULL != (error = dlerror()) || NULL == engine->sttp_unload_engine) {
147                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to link daemon to sttp_unload_engine() : %s", error);
148                 dlclose(engine->handle);
149                 free(engine->pefuncs);
150                 free(engine->pdfuncs);
151                 free(engine);
152                 return STTP_ERROR_OPERATION_FAILED;
153         }
154
155         engine->sttp_load_engine = (int (*)(sttpd_funcs_s*, sttpe_funcs_s*))dlsym(engine->handle, "sttp_load_engine");
156         if (NULL != (error = dlerror()) || NULL == engine->sttp_load_engine) {
157                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to link daemon to sttp_load_engine() : %s", error);
158                 dlclose(engine->handle);
159                 free(engine->pefuncs);
160                 free(engine->pdfuncs);
161                 free(engine);
162                 return STTP_ERROR_OPERATION_FAILED;
163         }
164
165         engine->engine_id = engine_id;
166         engine->engine_path = strdup(filepath);
167
168         engine->pdfuncs->version = 1;
169         engine->pdfuncs->size = sizeof(sttpd_funcs_s);
170
171         int ret = engine->sttp_load_engine(engine->pdfuncs, engine->pefuncs);
172         if (0 != ret) {
173                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail sttp_load_engine() : %s", __stt_get_engine_error_code(ret));
174                 dlclose(engine->handle);
175                 free(engine->pefuncs);
176                 free(engine->pdfuncs);
177                 free(engine->engine_path);
178                 free(engine);
179                 return ret;
180         }
181
182         /* engine error check */
183         if (engine->pefuncs->size != sizeof(sttpe_funcs_s)) {
184                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine is not valid : function size is not matched");
185         }
186
187         if (NULL == engine->pefuncs->initialize ||
188                 NULL == engine->pefuncs->deinitialize ||
189                 NULL == engine->pefuncs->foreach_langs ||
190                 NULL == engine->pefuncs->is_valid_lang ||
191                 NULL == engine->pefuncs->support_silence ||
192                 NULL == engine->pefuncs->support_recognition_type ||
193                 NULL == engine->pefuncs->get_audio_format ||
194                 NULL == engine->pefuncs->set_silence_detection ||
195                 NULL == engine->pefuncs->start ||
196                 NULL == engine->pefuncs->set_recording ||
197                 NULL == engine->pefuncs->stop ||
198                 NULL == engine->pefuncs->cancel ||
199                 NULL == engine->pefuncs->foreach_result_time)
200                 /* Current unused functions
201                 NULL == engine->pefuncs->start_file_recognition ||
202                 */
203         {
204                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] The engine functions are NOT valid");
205                 dlclose(engine->handle);
206                 free(engine->pefuncs);
207                 free(engine->pdfuncs);
208                 free(engine->engine_path);
209                 free(engine);
210
211                 return STTP_ERROR_OPERATION_FAILED;
212         }
213
214         SLOG(LOG_DEBUG, stt_tag(), "[Engine Success] Load engine : version(%d), size(%d)", engine->pefuncs->version, engine->pefuncs->size);
215
216         g_engine_list = g_slist_append(g_engine_list, engine);
217
218         return 0;
219 }
220
221 /* Unregister engine id */
222 int stt_engine_unload(int engine_id)
223 {
224         sttengine_s* engine = NULL;
225         engine = __get_engine(engine_id);
226         if (NULL == engine) {
227                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
228                 return STTP_ERROR_INVALID_PARAMETER;
229         }
230
231         /* unload engine */
232         engine->sttp_unload_engine();
233         dlclose(engine->handle);
234
235         if (NULL != engine->engine_path)        free(engine->engine_path);
236         if (NULL != engine->pefuncs)            free(engine->pefuncs);
237         if (NULL != engine->pdfuncs)            free(engine->pdfuncs);
238
239         g_engine_list = g_slist_remove(g_engine_list, engine);
240
241         free(engine);
242
243         return 0;
244 }
245
246
247 /* Initialize / Deinitialize */
248 int stt_engine_initialize(int engine_id, sttpe_result_cb result_cb, sttpe_silence_detected_cb silence_cb)
249 {
250         if (NULL == result_cb || NULL == silence_cb) {
251                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
252                 return STTP_ERROR_INVALID_PARAMETER;
253         }
254
255         sttengine_s* engine = NULL;
256         engine = __get_engine(engine_id);
257         if (NULL == engine) {
258                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
259                 return STTP_ERROR_INVALID_PARAMETER;
260         }
261
262         int ret;
263         ret = engine->pefuncs->initialize(result_cb, silence_cb);
264         if (0 != ret) {
265                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to initialize : %s", __stt_get_engine_error_code(ret));
266         }
267
268         return ret;
269 }
270
271 int stt_engine_deinitialize(int engine_id)
272 {
273         sttengine_s* engine = NULL;
274         engine = __get_engine(engine_id);
275         if (NULL == engine) {
276                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
277                 return STTP_ERROR_INVALID_PARAMETER;
278         }
279
280         int ret;
281         ret = engine->pefuncs->deinitialize();
282         if (0 != ret) {
283                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Fail to deinitialize : %s", __stt_get_engine_error_code(ret));
284         }
285
286         return ret;
287 }
288
289 static bool __supported_language_cb(const char* language, void* user_data)
290 {
291         GSList** lang_list = (GSList**)user_data;
292
293         if (NULL == language || NULL == lang_list) {
294                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Input parameter is NULL in callback!!!!");
295                 return false;
296         }
297
298         char* temp_lang = g_strdup(language);
299
300         *lang_list = g_slist_append(*lang_list, temp_lang);
301
302         return true;
303 }
304
305 /* Get option */
306 int stt_engine_get_supported_langs(int engine_id, GSList** lang_list)
307 {
308         if (NULL == lang_list) {
309                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
310                 return STTP_ERROR_INVALID_PARAMETER;
311         }
312
313         sttengine_s* engine = NULL;
314         engine = __get_engine(engine_id);
315         if (NULL == engine) {
316                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
317                 return STTP_ERROR_INVALID_PARAMETER;
318         }
319
320         int ret;
321         ret = engine->pefuncs->foreach_langs(__supported_language_cb, (void*)lang_list);
322         if (0 != ret) {
323                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] get language list error : %s", __stt_get_engine_error_code(ret));
324         }
325
326         return ret;
327 }
328
329 int stt_engine_is_valid_language(int engine_id, const char* language, bool *is_valid)
330 {
331         if (NULL == language || NULL == is_valid) {
332                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
333                 return STTP_ERROR_INVALID_PARAMETER;
334         }
335
336         sttengine_s* engine = NULL;
337         engine = __get_engine(engine_id);
338         if (NULL == engine) {
339                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
340                 return STTP_ERROR_INVALID_PARAMETER;
341         }
342
343         bool result;
344         result = engine->pefuncs->is_valid_lang(language);
345
346         *is_valid = result;
347
348         return 0;
349 }
350
351 int stt_engine_set_private_data(int engine_id, const char* key, const char* data)
352 {
353         if (NULL == key || NULL == data) {
354                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
355                 return STTP_ERROR_INVALID_PARAMETER;
356         }
357
358         sttengine_s* engine = NULL;
359         engine = __get_engine(engine_id);
360         if (NULL == engine) {
361                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
362                 return STTP_ERROR_INVALID_PARAMETER;
363         }
364
365         int ret = engine->pefuncs->set_private_data(key, data);
366         if (0 != ret) {
367                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set private data(%d)", ret);
368         }
369         return ret;
370 }
371
372 int stt_engine_get_private_data(int engine_id, const char* key, char** data)
373 {
374         if (NULL == key || NULL == data) {
375                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
376                 return STTP_ERROR_INVALID_PARAMETER;
377         }
378
379         sttengine_s* engine = NULL;
380         engine = __get_engine(engine_id);
381         if (NULL == engine) {
382                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
383                 return STTP_ERROR_INVALID_PARAMETER;
384         }
385
386         char* temp = NULL;
387         int ret = engine->pefuncs->get_private_data(key, &temp);
388         if (0 != ret) {
389                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get private data(%d)", ret);
390                 return ret;
391         }
392
393         *data = strdup(temp);
394
395         return STTP_ERROR_NONE;
396 }
397
398 int stt_engine_get_first_language(int engine_id, char** language)
399 {
400         if (NULL == language) {
401                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
402                 return STTP_ERROR_INVALID_PARAMETER;
403         }
404
405         sttengine_s* engine = NULL;
406         engine = __get_engine(engine_id);
407         if (NULL == engine) {
408                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
409                 return STTP_ERROR_INVALID_PARAMETER;
410         }
411
412         GSList* lang_list = NULL;
413         int ret;
414         ret = engine->pefuncs->foreach_langs(__supported_language_cb, &lang_list);
415         if (0 != ret) {
416                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] get language list error : %s", __stt_get_engine_error_code(ret));
417                 return ret;
418         }
419
420         GSList *iter = NULL;
421         char* data = NULL;
422
423         iter = g_slist_nth(lang_list, 0);
424         if (NULL != iter) {
425                 data = iter->data;
426
427                 if (true == engine->pefuncs->is_valid_lang(data)) {
428                         *language = strdup(data);
429                 } else {
430                         ret = STTP_ERROR_OPERATION_FAILED;
431                 }
432         }
433
434         /* if list have item */
435         if (g_slist_length(lang_list) > 0) {
436                 /* Get a first item */
437                 iter = g_slist_nth(lang_list, 0);
438
439                 while (NULL != iter) {
440                         data = iter->data;
441
442                         if (NULL != data)
443                                 free(data);
444
445                         lang_list = g_slist_remove_link(lang_list, iter);
446
447                         iter = g_slist_nth(lang_list, 0);
448                 }
449         }
450
451         return ret;
452 }
453
454 int stt_engine_support_silence(int engine_id, bool* support)
455 {
456         if (NULL == support) {
457                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
458                 return STTP_ERROR_INVALID_PARAMETER;
459         }
460
461         sttengine_s* engine = NULL;
462         engine = __get_engine(engine_id);
463         if (NULL == engine) {
464                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
465                 return STTP_ERROR_INVALID_PARAMETER;
466         }
467
468         bool result;
469         result = engine->pefuncs->support_silence();
470         *support = result;
471
472         return 0;
473 }
474
475
476 int stt_engine_need_app_credential(int engine_id, bool* need)
477 {
478         if (NULL == need) {
479                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
480                 return STTP_ERROR_INVALID_PARAMETER;
481         }
482
483         sttengine_s* engine = NULL;
484         engine = __get_engine(engine_id);
485         if (NULL == engine) {
486                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
487                 return STTP_ERROR_INVALID_PARAMETER;
488         }
489
490         bool result;
491         if (NULL != engine->pefuncs->need_app_credential) {
492                 result = engine->pefuncs->need_app_credential();
493                 *need = result;
494                 return STTP_ERROR_NONE;
495         }
496
497         return STTP_ERROR_OPERATION_FAILED;
498 }
499
500 int stt_engine_support_recognition_type(int engine_id, const char* type, bool* support)
501 {
502         if (NULL == type || NULL == support) {
503                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
504                 return STTP_ERROR_INVALID_PARAMETER;
505         }
506
507         sttengine_s* engine = NULL;
508         engine = __get_engine(engine_id);
509         if (NULL == engine) {
510                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
511                 return STTP_ERROR_INVALID_PARAMETER;
512         }
513
514         bool result;
515         if (NULL != engine->pefuncs->support_recognition_type) {
516                 result = engine->pefuncs->support_recognition_type(type);
517                 *support = result;
518                 return 0;
519         }
520
521         return STTP_ERROR_OPERATION_FAILED;
522 }
523
524 int stt_engine_get_audio_type(int engine_id, sttp_audio_type_e* types, int* rate, int* channels)
525 {
526         if (NULL == types || NULL == rate || NULL == channels) {
527                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
528                 return STTP_ERROR_INVALID_PARAMETER;
529         }
530
531         sttengine_s* engine = NULL;
532         engine = __get_engine(engine_id);
533         if (NULL == engine) {
534                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
535                 return STTP_ERROR_INVALID_PARAMETER;
536         }
537
538         int ret;
539         ret = engine->pefuncs->get_audio_format(types, rate, channels);
540         if (0 != ret) {
541                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get audio format : %s", __stt_get_engine_error_code(ret));
542         }
543
544         return ret;
545 }
546
547 /* Set option */
548 int stt_engine_set_silence_detection(int engine_id, bool value)
549 {
550         sttengine_s* engine = NULL;
551         engine = __get_engine(engine_id);
552         if (NULL == engine) {
553                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
554                 return STTP_ERROR_INVALID_PARAMETER;
555         }
556
557         int ret = engine->pefuncs->set_silence_detection(value);
558         if (STTP_ERROR_NOT_SUPPORTED_FEATURE == ret) {
559                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Not support silence detection");
560                 return STTP_ERROR_NOT_SUPPORTED_FEATURE;
561         } else if (0 != ret) {
562                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set silence detection : %d", ret);
563         }
564
565         return ret;
566 }
567
568 int stt_engine_check_app_agreed(int engine_id, const char* appid, bool* value)
569 {
570         sttengine_s* engine = NULL;
571         engine = __get_engine(engine_id);
572         if (NULL == engine) {
573                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
574                 return STTP_ERROR_INVALID_PARAMETER;
575         }
576
577         if (NULL == engine->pefuncs->check_app_agreed) {
578                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Not support app agreement. All app is available");
579                 *value = true;
580                 return 0;
581         }
582
583         int ret = engine->pefuncs->check_app_agreed(appid, value);
584         if (0 != ret) {
585                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get app agreement : %s", __stt_get_engine_error_code(ret));
586                 *value = false;
587         }
588
589         return ret;
590 }
591
592 /* Recognition */
593 int stt_engine_recognize_start(int engine_id, const char* lang, const char* recognition_type, const char* credential, void* user_param)
594 {
595         if (NULL == lang || NULL == recognition_type) {
596                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
597                 return STTP_ERROR_INVALID_PARAMETER;
598         }
599
600         sttengine_s* engine = NULL;
601         engine = __get_engine(engine_id);
602         if (NULL == engine) {
603                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
604                 return STTP_ERROR_INVALID_PARAMETER;
605         }
606
607         int ret = engine->pefuncs->start(lang, recognition_type, credential, user_param);
608         if (0 != ret) {
609                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start recognition : %s", __stt_get_engine_error_code(ret));
610                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start recognition : lang(%s), recognition_type(%s), credential(%s)", lang, recognition_type, credential);
611         }
612
613         return ret;
614 }
615
616 int stt_engine_set_recording_data(int engine_id, const void* data, unsigned int length)
617 {
618         if (NULL == data) {
619                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
620                 return STTP_ERROR_INVALID_PARAMETER;
621         }
622
623         sttengine_s* engine = NULL;
624         engine = __get_engine(engine_id);
625         if (NULL == engine) {
626                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
627                 return STTP_ERROR_INVALID_PARAMETER;
628         }
629
630         int ret = engine->pefuncs->set_recording(data, length);
631         if (0 != ret) {
632                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Fail to set recording : %s", __stt_get_engine_error_code(ret));
633         }
634
635         return ret;
636 }
637
638 int stt_engine_recognize_stop(int engine_id)
639 {
640         sttengine_s* engine = NULL;
641         engine = __get_engine(engine_id);
642         if (NULL == engine) {
643                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
644                 return STTP_ERROR_INVALID_PARAMETER;
645         }
646
647         int ret = engine->pefuncs->stop();
648         if (0 != ret) {
649                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to stop : %s", __stt_get_engine_error_code(ret));
650         }
651
652         return ret;
653 }
654
655 int stt_engine_recognize_cancel(int engine_id)
656 {
657         sttengine_s* engine = NULL;
658         engine = __get_engine(engine_id);
659         if (NULL == engine) {
660                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
661                 return STTP_ERROR_INVALID_PARAMETER;
662         }
663
664         int ret = engine->pefuncs->cancel();
665         if (0 != ret) {
666                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to cancel : %s", __stt_get_engine_error_code(ret));
667         }
668
669         return ret;
670 }
671
672 int stt_engine_foreach_result_time(int engine_id, void* time_info, sttpe_result_time_cb callback, void* user_data)
673 {
674         sttengine_s* engine = NULL;
675         engine = __get_engine(engine_id);
676         if (NULL == engine) {
677                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
678                 return STTP_ERROR_INVALID_PARAMETER;
679         }
680
681         int ret = engine->pefuncs->foreach_result_time(time_info, callback, user_data);
682         if (0 != ret) {
683                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to foreach result time : %s", __stt_get_engine_error_code(ret));
684         }
685
686         return ret;
687 }
688
689 int stt_engine_recognize_start_file(int engine_id, const char* lang, const char* recognition_type, 
690                                      const char* filepath, sttp_audio_type_e audio_type, int sample_rate, void* user_param)
691 {
692         if (NULL == filepath || NULL == lang || NULL == recognition_type) {
693                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
694                 return STTP_ERROR_INVALID_PARAMETER;
695         }
696
697         sttengine_s* engine = NULL;
698         engine = __get_engine(engine_id);
699         if (NULL == engine) {
700                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
701                 return STTP_ERROR_INVALID_PARAMETER;
702         }
703
704         if (NULL == engine->pefuncs->start_file) {
705                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine API is invalid");
706                 return STTP_ERROR_NOT_SUPPORTED_FEATURE;
707         }
708
709         int ret = engine->pefuncs->start_file(lang, recognition_type, filepath, audio_type, sample_rate, user_param);
710         if (0 != ret) {
711                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start file recognition : %s", __stt_get_engine_error_code(ret));
712         }
713
714         return ret;
715 }
716
717 int stt_engine_recognize_cancel_file(int engine_id)
718 {
719         sttengine_s* engine = NULL;
720         engine = __get_engine(engine_id);
721         if (NULL == engine) {
722                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
723                 return STTP_ERROR_INVALID_PARAMETER;
724         }
725
726         if (NULL == engine->pefuncs->cancel_file) {
727                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine API is invalid");
728                 return STTP_ERROR_NOT_SUPPORTED_FEATURE;
729         }
730
731         int ret = engine->pefuncs->cancel_file();
732         if (0 != ret) {
733                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start file recognition : %s", __stt_get_engine_error_code(ret));
734         }
735
736         return ret;
737 }