[ACR-1449] Add new api to set audio type
[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 #include "sttd_engine_agent.h"
23
24 /*
25 * Internal data structure
26 */
27
28 typedef struct {
29         char*   engine_path;
30         stte_request_callback_s *callback;
31 } sttengine_s;
32
33 extern const char* stt_tag();
34
35 /** stt engine */
36 static sttengine_s *g_engine = NULL;
37
38 static bool g_is_from_lib = false;
39
40 /** callback functions */
41 static stt_engine_result_cb g_result_cb = NULL;
42 static stte_private_data_set_cb g_set_private_data_cb = NULL;
43 static stte_private_data_requested_cb g_get_private_data_cb = NULL;
44 static stte_audio_type_cb g_set_audio_type_cb = NULL;
45 static void* g_set_audio_type_user_data = NULL;
46
47
48 static int __stt_set_engine_from(bool is_from_lib)
49 {
50         g_is_from_lib = is_from_lib;
51         return 0;
52 }
53
54 static bool __stt_get_engine_from(void)
55 {
56         return g_is_from_lib;
57 }
58
59 static const char* __stt_get_engine_error_code(stte_error_e err)
60 {
61         switch (err) {
62         case STTE_ERROR_NONE:                   return "STTE_ERROR_NONE";
63         case STTE_ERROR_OUT_OF_MEMORY:          return "STTE_ERROR_OUT_OF_MEMORY";
64         case STTE_ERROR_IO_ERROR:               return "STTE_ERROR_IO_ERROR";
65         case STTE_ERROR_INVALID_PARAMETER:      return "STTE_ERROR_INVALID_PARAMETER";
66         case STTE_ERROR_NETWORK_DOWN:           return "STTE_ERROR_NETWORK_DOWN";
67         case STTE_ERROR_PERMISSION_DENIED:      return "STTE_ERROR_PERMISSION_DENIED";
68         case STTE_ERROR_NOT_SUPPORTED:          return "STTE_ERROR_NOT_SUPPORTED";
69         case STTE_ERROR_INVALID_STATE:          return "STTE_ERROR_INVALID_STATE";
70         case STTE_ERROR_INVALID_LANGUAGE:       return "STTE_ERROR_INVALID_LANGUAGE";
71         case STTE_ERROR_OPERATION_FAILED:       return "STTE_ERROR_OPERATION_FAILED";
72         case STTE_ERROR_NOT_SUPPORTED_FEATURE:  return "STTE_ERROR_NOT_SUPPORTED_FEATURE";
73         case STTE_ERROR_RECORDING_TIMED_OUT:    return "STTE_ERROR_RECORDING_TIMED_OUT";
74         default:
75                 return "Invalid error code";
76         }
77 }
78
79 /* Register engine id */
80 int stt_engine_load(const char* filepath, stte_request_callback_s *callback)
81 {
82         if (NULL == callback || NULL == filepath) {
83                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
84                 return STTE_ERROR_INVALID_PARAMETER;
85         }
86
87         /* allocation memory */
88         if (NULL != g_engine) {
89                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine is already loaded");
90         } else {
91                 g_engine = (sttengine_s*)calloc(1, sizeof(sttengine_s));
92                 if (NULL == g_engine) {
93                         SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory");
94                         return STTE_ERROR_OUT_OF_MEMORY;
95                 }
96
97                 /* load engine */
98                 g_engine->callback = callback;
99                 g_engine->engine_path = strdup(filepath);
100         }
101
102         SLOG(LOG_DEBUG, stt_tag(), "[Engine Success] Load engine : version(%d)", g_engine->callback->version);
103
104         return 0;
105 }
106
107 /* Unregister engine id */
108 int stt_engine_unload()
109 {
110         if (NULL == g_engine) {
111                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
112                 return STTE_ERROR_OPERATION_FAILED;
113         }
114
115         g_engine->callback = NULL;
116
117         if (NULL != g_engine->engine_path) {
118                 free(g_engine->engine_path);
119                 g_engine->engine_path = NULL;
120         }
121
122         free(g_engine);
123         g_engine = NULL;
124
125         return 0;
126 }
127
128
129 /* Initialize / Deinitialize */
130 int stt_engine_initialize(bool is_from_lib)
131 {
132         if (NULL == g_engine) {
133                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
134                 return STTE_ERROR_OPERATION_FAILED;
135         }
136
137         if (NULL == g_engine->callback) {
138                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
139                 return STTE_ERROR_OPERATION_FAILED;
140         }
141
142         if (NULL == g_engine->callback->initialize) {
143                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
144                 return STTE_ERROR_OPERATION_FAILED;
145         }
146
147         int ret;
148         ret = __stt_set_engine_from(is_from_lib);
149         if (0 != ret) {
150                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set engine : %s", __stt_get_engine_error_code(ret));
151                 return STTE_ERROR_OPERATION_FAILED;
152         }
153
154         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to initialize");
155
156         ret = g_engine->callback->initialize();
157         if (0 != ret) {
158                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to initialize : %s", __stt_get_engine_error_code(ret));
159         }
160
161         return ret;
162 }
163
164 int stt_engine_deinitialize()
165 {
166         if (NULL == g_engine) {
167                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
168                 return STTE_ERROR_OPERATION_FAILED;
169         }
170
171         if (NULL == g_engine->callback) {
172                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
173                 return STTE_ERROR_OPERATION_FAILED;
174         }
175
176         if (NULL == g_engine->callback->deinitialize) {
177                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
178                 return STTE_ERROR_OPERATION_FAILED;
179         }
180
181         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to deinitialize");
182
183         int ret;
184         ret = g_engine->callback->deinitialize();
185         if (0 != ret) {
186                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Fail to deinitialize : %s", __stt_get_engine_error_code(ret));
187         }
188
189         return ret;
190 }
191
192 static bool __supported_language_cb(const char* language, void* user_data)
193 {
194         GSList** lang_list = (GSList**)user_data;
195
196         if (NULL == language || NULL == lang_list) {
197                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Input parameter is NULL in callback!!!!");
198                 return false;
199         }
200
201         char* temp_lang = g_strdup(language);
202
203         *lang_list = g_slist_append(*lang_list, temp_lang);
204
205         return true;
206 }
207
208 /* Get option */
209 int stt_engine_get_supported_langs(GSList** lang_list)
210 {
211         if (NULL == lang_list) {
212                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
213                 return STTE_ERROR_INVALID_PARAMETER;
214         }
215
216         if (NULL == g_engine) {
217                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
218                 return STTE_ERROR_OPERATION_FAILED;
219         }
220
221         if (NULL == g_engine->callback) {
222                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
223                 return STTE_ERROR_OPERATION_FAILED;
224         }
225
226         if (NULL == g_engine->callback->foreach_langs) {
227                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
228                 return STTE_ERROR_OPERATION_FAILED;
229         }
230
231         int ret;
232         ret = g_engine->callback->foreach_langs(__supported_language_cb, (void*)lang_list);
233         if (0 != ret) {
234                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] get language list error : %s", __stt_get_engine_error_code(ret));
235         }
236
237         return ret;
238 }
239
240 int stt_engine_is_valid_language(const char* language, bool *is_valid)
241 {
242         if (NULL == language || NULL == is_valid) {
243                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
244                 return STTE_ERROR_INVALID_PARAMETER;
245         }
246
247         if (NULL == g_engine) {
248                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
249                 return STTE_ERROR_OPERATION_FAILED;
250         }
251
252         if (NULL == g_engine->callback) {
253                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
254                 return STTE_ERROR_OPERATION_FAILED;
255         }
256
257         if (NULL == g_engine->callback->is_valid_lang) {
258                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
259                 return STTE_ERROR_OPERATION_FAILED;
260         }
261
262         int ret = STTE_ERROR_NONE;
263         ret = g_engine->callback->is_valid_lang(language, is_valid);
264         if (0 != ret) {
265                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to check valid language(%d)", ret);
266         }
267         return ret;
268 }
269
270 int stt_engine_set_private_data(const char* key, const char* data)
271 {
272         if (NULL == key || NULL == data) {
273                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
274                 return STTE_ERROR_INVALID_PARAMETER;
275         }
276
277         int ret = STTE_ERROR_NONE;
278         if (NULL != g_set_private_data_cb) {
279                 ret = g_set_private_data_cb(key, data);
280                 if (0 != ret) {
281                         SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set private data(%d)", ret);
282                 }
283         }
284
285         return ret;
286 }
287
288 int stt_engine_get_private_data(const char* key, char** data)
289 {
290         if (NULL == key || NULL == data) {
291                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
292                 return STTE_ERROR_INVALID_PARAMETER;
293         }
294
295         int ret = STTE_ERROR_NONE;
296         char* temp = NULL;
297         if (NULL != g_get_private_data_cb) {
298                 ret = g_get_private_data_cb(key, &temp);
299                 if (0 != ret) {
300                         SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get private data(%d)", ret);
301                         return ret;
302                 }
303         } else {
304                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] There's no private data function)");
305         }
306
307         if (NULL == temp)
308                 *data = strdup("NULL");
309         else
310                 *data = strdup(temp);
311
312         return STTE_ERROR_NONE;
313 }
314
315 int stt_engine_get_first_language(char** language)
316 {
317         if (NULL == language) {
318                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
319                 return STTE_ERROR_INVALID_PARAMETER;
320         }
321
322         if (NULL == g_engine) {
323                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
324                 return STTE_ERROR_OPERATION_FAILED;
325         }
326
327         if (NULL == g_engine->callback) {
328                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
329                 return STTE_ERROR_OPERATION_FAILED;
330         }
331
332         if (NULL == g_engine->callback->foreach_langs || NULL == g_engine->callback->is_valid_lang) {
333                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
334                 return STTE_ERROR_OPERATION_FAILED;
335         }
336
337         GSList* lang_list = NULL;
338         int ret;
339         ret = g_engine->callback->foreach_langs(__supported_language_cb, &lang_list);
340         if (0 != ret) {
341                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] get language list error : %s", __stt_get_engine_error_code(ret));
342                 return ret;
343         }
344
345         GSList *iter = NULL;
346         char* data = NULL;
347
348         iter = g_slist_nth(lang_list, 0);
349         if (NULL != iter) {
350                 data = iter->data;
351
352                 bool is_valid = false;
353                 ret = g_engine->callback->is_valid_lang(data, &is_valid);
354                 if (0 == ret && true == is_valid) {
355                         *language = strdup(data);
356                 } else {
357                         ret = STTE_ERROR_OPERATION_FAILED;
358                 }
359         }
360
361         /* if list have item */
362         if (g_slist_length(lang_list) > 0) {
363                 /* Get a first item */
364                 iter = g_slist_nth(lang_list, 0);
365
366                 while (NULL != iter) {
367                         data = iter->data;
368
369                         if (NULL != data) {
370                                 free(data);
371                                 data = NULL;
372                         }
373
374                         lang_list = g_slist_remove_link(lang_list, iter);
375
376                         iter = g_slist_nth(lang_list, 0);
377                 }
378         }
379
380         return ret;
381 }
382
383 int stt_engine_support_silence(bool* support)
384 {
385         if (NULL == support) {
386                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
387                 return STTE_ERROR_INVALID_PARAMETER;
388         }
389
390         if (NULL == g_engine) {
391                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
392                 return STTE_ERROR_OPERATION_FAILED;
393         }
394
395         if (NULL == g_engine->callback) {
396                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
397                 return STTE_ERROR_OPERATION_FAILED;
398         }
399
400         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to support silence");
401
402         if (NULL == g_engine->callback->support_silence) {
403                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
404                 return STTE_ERROR_OPERATION_FAILED;
405         }
406
407         bool result;
408         result = g_engine->callback->support_silence();
409         *support = result;
410
411         return STTE_ERROR_NONE;
412 }
413
414 int stt_engine_need_app_credential(bool* need)
415 {
416         if (NULL == need) {
417                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
418                 return STTE_ERROR_INVALID_PARAMETER;
419         }
420
421         if (NULL == g_engine) {
422                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
423                 return STTE_ERROR_OPERATION_FAILED;
424         }
425
426         if (NULL == g_engine->callback) {
427                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
428                 return STTE_ERROR_OPERATION_FAILED;
429         }
430
431         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to need app credential");
432
433         if (NULL == g_engine->callback->need_app_credential) {
434                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
435                 return STTE_ERROR_OPERATION_FAILED;
436         }
437
438         bool result;
439         result = g_engine->callback->need_app_credential();
440         *need = result;
441
442         return STTE_ERROR_NONE;
443 }
444
445 int stt_engine_support_recognition_type(const char* type, bool* support)
446 {
447         if (NULL == type || NULL == support) {
448                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
449                 return STTE_ERROR_INVALID_PARAMETER;
450         }
451
452         if (NULL == g_engine) {
453                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
454                 return STTE_ERROR_OPERATION_FAILED;
455         }
456
457         if (NULL == g_engine->callback) {
458                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
459                 return STTE_ERROR_OPERATION_FAILED;
460         }
461
462         if (NULL == g_engine->callback->support_recognition_type) {
463                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR} Invalid engine");
464                 return STTE_ERROR_OPERATION_FAILED;
465         }
466
467         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to support recognition type, type(%s)", type);
468
469         int ret = STTE_ERROR_NONE;
470         ret = g_engine->callback->support_recognition_type(type, support);
471         if (0 != ret) {
472                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get supporting recognition type(%d)", ret);
473         }
474         return ret;
475 }
476
477 int stt_engine_get_audio_format(stte_audio_type_e* types, int* rate, int* channels)
478 {
479         if (NULL == types || NULL == rate || NULL == channels) {
480                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
481                 return STTE_ERROR_INVALID_PARAMETER;
482         }
483
484         if (NULL == g_engine) {
485                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
486                 return STTE_ERROR_OPERATION_FAILED;
487         }
488
489         if (NULL == g_engine->callback) {
490                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
491                 return STTE_ERROR_OPERATION_FAILED;
492         }
493
494         if (NULL == g_engine->callback->get_audio_format) {
495                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
496                 return STTE_ERROR_OPERATION_FAILED;
497         }
498
499         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to get audio format");
500
501         int ret;
502         ret = g_engine->callback->get_audio_format(types, rate, channels);
503         if (0 != ret) {
504                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get audio format : %s", __stt_get_engine_error_code(ret));
505         }
506
507         return ret;
508 }
509
510 /* Set option */
511 int stt_engine_set_silence_detection(bool value)
512 {
513         if (NULL == g_engine) {
514                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
515                 return STTE_ERROR_OPERATION_FAILED;
516         }
517
518         if (NULL == g_engine->callback) {
519                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
520                 return STTE_ERROR_OPERATION_FAILED;
521         }
522
523         if (NULL == g_engine->callback->set_silence_detection) {
524                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
525                 return STTE_ERROR_OPERATION_FAILED;
526         }
527
528         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to set silence detection(%d)", value);
529
530         int ret = g_engine->callback->set_silence_detection(value);
531         if (STTE_ERROR_NOT_SUPPORTED_FEATURE == ret) {
532                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Not support silence detection");
533         } else if (0 != ret) {
534                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set silence detection : %d", ret);
535         }
536         return ret;
537 }
538
539 int stt_engine_check_app_agreed(const char* appid, bool* is_agreed)
540 {
541         if (NULL == is_agreed) {
542                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
543                 return STTE_ERROR_INVALID_PARAMETER;
544         }
545
546         if (NULL == g_engine) {
547                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
548                 return STTE_ERROR_OPERATION_FAILED;
549         }
550
551         if (NULL == g_engine->callback) {
552                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
553                 return STTE_ERROR_OPERATION_FAILED;
554         }
555
556         if (NULL == g_engine->callback->check_app_agreed) {
557                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Not support app agreement. All app is available");
558                 *is_agreed = true;
559                 return 0;
560         }
561
562         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to app agreed, appid(%s), is_agreed(%d)", appid, *is_agreed);
563
564         int ret = g_engine->callback->check_app_agreed(appid, is_agreed);
565         if (0 != ret) {
566                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get app agreement : %s", __stt_get_engine_error_code(ret));
567                 *is_agreed = false;
568         }
569
570         return ret;
571 }
572
573 /* Recognition */
574 int stt_engine_recognize_start(const char* lang, const char* recognition_type, const char* appid, const char* credential, void* user_param)
575 {
576         if (NULL == lang || NULL == recognition_type) {
577                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
578                 return STTE_ERROR_INVALID_PARAMETER;
579         }
580
581         if (NULL == g_engine) {
582                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
583                 return STTE_ERROR_OPERATION_FAILED;
584         }
585
586         if (NULL == g_engine->callback) {
587                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
588                 return STTE_ERROR_OPERATION_FAILED;
589         }
590
591         if (NULL == g_engine->callback->start) {
592                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
593                 return STTE_ERROR_OPERATION_FAILED;
594         }
595
596         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to start, lang(%s), recognition_type(%s), credential(%s)", lang, recognition_type, credential);
597
598         int ret = g_engine->callback->start(lang, recognition_type, appid, credential, user_param);
599
600         if (0 != ret) {
601                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start recognition : %s", __stt_get_engine_error_code(ret));
602                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start recognition : lang(%s), recognition_type(%s), credential(%s)", lang, recognition_type, credential);
603         }
604
605         return ret;
606 }
607
608 int stt_engine_set_recording_data(const void* data, unsigned int length)
609 {
610         if (NULL == data) {
611                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
612                 return STTE_ERROR_INVALID_PARAMETER;
613         }
614
615         if (NULL == g_engine) {
616                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
617                 return STTE_ERROR_OPERATION_FAILED;
618         }
619
620         if (NULL == g_engine->callback) {
621                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
622                 return STTE_ERROR_OPERATION_FAILED;
623         }
624
625         if (NULL == g_engine->callback->set_recording) {
626                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
627                 return STTE_ERROR_OPERATION_FAILED;
628         }
629
630         int ret = g_engine->callback->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()
639 {
640         if (NULL == g_engine) {
641                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
642                 return STTE_ERROR_OPERATION_FAILED;
643         }
644
645         if (NULL == g_engine->callback) {
646                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
647                 return STTE_ERROR_OPERATION_FAILED;
648         }
649
650         if (NULL == g_engine->callback->stop) {
651                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
652                 return STTE_ERROR_OPERATION_FAILED;
653         }
654
655         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to stop");
656
657         int ret = g_engine->callback->stop();
658         if (0 != ret) {
659                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to stop : %s", __stt_get_engine_error_code(ret));
660         }
661
662         return ret;
663 }
664
665 int stt_engine_recognize_cancel()
666 {
667         if (NULL == g_engine) {
668                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
669                 return STTE_ERROR_OPERATION_FAILED;
670         }
671
672         if (NULL == g_engine->callback) {
673                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
674                 return STTE_ERROR_OPERATION_FAILED;
675         }
676
677         if (NULL == g_engine->callback->cancel) {
678                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
679                 return STTE_ERROR_OPERATION_FAILED;
680         }
681
682         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to cancel");
683
684         int ret = g_engine->callback->cancel();
685         if (0 != ret) {
686                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to cancel : %s", __stt_get_engine_error_code(ret));
687         }
688
689         return ret;
690 }
691
692 int stt_engine_foreach_result_time(void* time_info, stte_result_time_cb callback, void* user_data)
693 {
694         if (NULL == g_engine) {
695                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
696                 return STTE_ERROR_OPERATION_FAILED;
697         }
698
699         if (NULL == g_engine->callback) {
700                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
701                 return STTE_ERROR_OPERATION_FAILED;
702         }
703
704         if (NULL == g_engine->callback->foreach_result_time) {
705                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
706                 return STTE_ERROR_OPERATION_FAILED;
707         }
708
709         int ret = g_engine->callback->foreach_result_time(time_info, callback, user_data);
710         if (0 != ret) {
711                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to foreach result time : %s", __stt_get_engine_error_code(ret));
712         }
713
714         return ret;
715 }
716
717 int stt_engine_recognize_start_file(const char* lang, const char* recognition_type, 
718                                      const char* filepath, stte_audio_type_e audio_type, int sample_rate, void* user_param)
719 {
720         if (NULL == filepath || NULL == lang || NULL == recognition_type) {
721                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
722                 return STTE_ERROR_INVALID_PARAMETER;
723         }
724
725         if (NULL == g_engine) {
726                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
727                 return STTE_ERROR_OPERATION_FAILED;
728         }
729
730         if (NULL == g_engine->callback) {
731                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
732                 return STTE_ERROR_OPERATION_FAILED;
733         }
734
735 #ifdef __UNUSED_CODES__
736         if (NULL == g_engine->callback->start_file) {
737                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine API is invalid");
738                 return STTE_ERROR_NOT_SUPPORTED_FEATURE;
739         }
740
741         int ret = g_engine->callback->start_file(lang, recognition_type, filepath, audio_type, sample_rate, user_param);
742         if (0 != ret) {
743                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start file recognition : %s", __stt_get_engine_error_code(ret));
744         }
745 #endif
746         return 0;
747 }
748
749 int stt_engine_recognize_cancel_file()
750 {
751         if (NULL == g_engine) {
752                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
753                 return STTE_ERROR_OPERATION_FAILED;
754         }
755
756         if (NULL == g_engine->callback) {
757                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
758                 return STTE_ERROR_OPERATION_FAILED;
759         }
760
761 #ifdef __UNUSED_CODES__
762         if (NULL == g_engine->callback->cancel_file) {
763                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine API is invalid");
764                 return STTE_ERROR_NOT_SUPPORTED_FEATURE;
765         }
766
767         int ret = g_engine->callback->cancel_file();
768         if (0 != ret) {
769                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start file recognition : %s", __stt_get_engine_error_code(ret));
770         }
771 #endif
772         return 0;
773 }
774
775 int stt_engine_set_recognition_result_cb(stt_engine_result_cb result_cb, void* user_data)
776 {
777         if (NULL == result_cb) {
778                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
779                 return STTE_ERROR_INVALID_PARAMETER;
780         }
781
782         g_result_cb = result_cb;
783
784         return 0;
785 }
786
787 int stt_engine_send_result(stte_result_event_e event, const char* type, const char** result, int result_count,
788                                 const char* msg, void* time_info, void* user_data)
789 {
790         if (NULL == type || NULL == result) {
791                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
792         }
793
794         int ret = STTE_ERROR_NONE;
795         if (false == __stt_get_engine_from()) {
796                 ret = sttd_engine_agent_send_result(event, type, result, result_count, msg, time_info, user_data);
797                 if (0 != ret) {
798                         SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to send result");
799                 }
800         } else {
801                 ret = g_result_cb(event, type, result, result_count, msg, time_info, user_data);
802         }
803         return ret;
804 }
805
806 int stt_engine_send_error(stte_error_e error, const char* msg)
807 {
808         int ret = STTE_ERROR_NONE;
809         ret = sttd_engine_agent_send_error(error, msg);
810         if (0 != ret) {
811                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to send error info");
812         }
813         return ret;
814 }
815
816 int stt_engine_send_speech_status(stte_speech_status_e status, void* user_data)
817 {
818         int ret = STTE_ERROR_NONE;
819         ret = sttd_engine_agent_send_speech_status(status, user_data);
820         if (0 != ret) {
821                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to send speech status");
822         }
823         return ret;
824 }
825
826 int stt_engine_set_private_data_set_cb(stte_private_data_set_cb private_data_set_cb, void* user_data)
827 {
828         if (NULL == private_data_set_cb) {
829                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
830                 return STTE_ERROR_INVALID_PARAMETER;
831         }
832
833         g_set_private_data_cb = private_data_set_cb;
834
835         return 0;
836 }
837
838 int stt_engine_set_private_data_requested_cb(stte_private_data_requested_cb private_data_requested_cb, void* user_data)
839 {
840         if (NULL == private_data_requested_cb) {
841                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
842                 return STTE_ERROR_INVALID_PARAMETER;
843         }
844
845         g_get_private_data_cb = private_data_requested_cb;
846
847         return 0;
848 }
849
850 int stt_engine_set_audio_type(const char* audio_type)
851 {
852         if (NULL == audio_type) {
853                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
854                 return STTE_ERROR_INVALID_PARAMETER;
855         }
856
857         SLOG(LOG_INFO, stt_tag(), "[Engine Info] set audio type (%s)", audio_type);
858
859         int ret = STTE_ERROR_NONE;
860         if (NULL != g_set_audio_type_cb) {
861                 ret = g_set_audio_type_cb(audio_type, g_set_audio_type_user_data);
862                 if (0 != ret) {
863                         SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set audio type, ret(%d)", ret);
864                 }
865         } else {
866                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] There's no set audio function)");
867         }
868
869         return ret;
870 }
871
872 int stt_engine_set_audio_type_set_cb(stte_audio_type_cb audio_type_set_cb, void* user_data)
873 {
874         if (NULL == audio_type_set_cb) {
875                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
876                 return STTE_ERROR_INVALID_PARAMETER;
877         }
878
879         g_set_audio_type_cb = audio_type_set_cb;
880         g_set_audio_type_user_data = user_data;
881
882         return 0;
883 }