modify files according to coding rule
[platform/core/uifw/stt.git] / test / test_main.c
1 /*
2 *  Copyright (c) 2011-2014 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 <stdio.h>
15 #include <Ecore.h>
16 #include <dlog/dlog.h>
17
18 #include <stt.h>
19 #include <stt_file.h>
20 #include <unistd.h>
21
22 #define TAG_STT_TEST "stt_test"
23
24 static stt_h g_stt = NULL;
25
26 static Eina_Bool __stt_test_finalize(void *data)
27 {
28         int ret;
29         SLOG(LOG_DEBUG, TAG_STT_TEST, "Unset callbacks");
30         ret = stt_file_unset_recognition_result_cb();
31         if (STT_FILE_ERROR_NONE != ret) {
32                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to unset recognition cb");
33         }
34
35         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT file deinitialize");
36         ret = stt_file_deinitialize();
37         if (STT_FILE_ERROR_NONE != ret) {
38                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to deinitialize");
39         }
40
41         ecore_main_loop_quit();
42
43         return EINA_FALSE;
44 }
45
46 static void __stt_file_state_changed_cb(stt_file_state_e previous, stt_file_state_e current, void* user_data)
47 {
48 }
49
50 static bool __stt_file_supported_language_cb(const char* language, void* user_data)
51 {
52         if (NULL == language)
53                 return false;
54
55         SLOG(LOG_DEBUG, TAG_STT_TEST, "=== %s ===", language);
56         return true;
57 }
58
59 static bool __stt_file_result_time_cb(int index, stt_file_result_time_event_e event, const char* text,
60                                       long start_time, long end_time, void* user_data)
61 {
62         if (NULL != text) {
63                 SLOG(LOG_DEBUG, TAG_STT_TEST, "[%d] %s - %ld to %ld", index, text, start_time, end_time);
64                 return true;
65         }
66         return false;
67 }
68
69 static void __stt_file_recognition_result_cb(stt_file_result_event_e event, const char** data, int data_count, const char* msg, void *user_data)
70 {
71         SLOG(LOG_DEBUG, TAG_STT_TEST, "==== Result Callback ====");
72         if (STT_FILE_RESULT_EVENT_ERROR == event) {
73                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Error Result");
74                 ecore_timer_add(0, __stt_test_finalize, NULL);
75                 return;
76         }
77
78         if (NULL != data) {
79                 SLOG(LOG_DEBUG, TAG_STT_TEST, "( %s )", data[0]);
80         }
81
82
83         int ret;
84         ret = stt_file_foreach_detailed_result(__stt_file_result_time_cb, NULL);
85         if (STT_FILE_ERROR_NONE != ret) {
86                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to get result time");
87         }
88
89         if (STT_FILE_RESULT_EVENT_FINAL_RESULT == event) {
90                 ecore_timer_add(0, __stt_test_finalize, NULL);
91         }
92
93         return;
94 }
95
96 static bool __supported_engine_cb(const char* engine_id, const char* engine_name, void* user_data)
97 {
98         SLOG(LOG_DEBUG, TAG_STT_TEST, "Engine id(%s) name(%s)", engine_id, engine_name);
99
100         return true;
101 }
102
103 static Eina_Bool __get_engine_list(void *data)
104 {
105         int ret;
106         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT get engine list");
107         ret = stt_file_foreach_supported_engines(__supported_engine_cb, NULL);
108         if (STT_FILE_ERROR_NONE != ret) {
109                 SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to get engine list");
110         }
111
112         char *cur_engine;
113         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT get engine");
114         ret = stt_file_get_engine(&cur_engine);
115         if (STT_FILE_ERROR_NONE != ret) {
116                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to get engine");
117                 return EINA_FALSE;
118         }
119
120         if (NULL != cur_engine) {
121                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Current engine - %s", cur_engine);
122                 free(cur_engine);
123         }
124
125         return EINA_FALSE;
126 }
127
128 static Eina_Bool __file_start(void *data)
129 {
130         char *tmp = (char *)data;
131         int ret;
132         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT start file recognition");
133         ret = stt_file_start("en_US", "stt.recognition.type.FREE", tmp, STT_FILE_AUDIO_TYPE_RAW_S16, 16000);
134         if (STT_FILE_ERROR_NONE != ret) {
135                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to start");
136         }
137
138         return EINA_FALSE;
139 }
140
141 static Eina_Bool __stt_start(void *data)
142 {
143         int ret;
144         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT set silence detection");
145         ret = stt_set_silence_detection(g_stt, STT_OPTION_SILENCE_DETECTION_TRUE);
146         if (STT_ERROR_NONE != ret) {
147                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to set silence detection");
148         }
149
150         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT start");
151         ret = stt_start(g_stt, "en_US", "stt.recognition.type.FREE");
152         if (STT_ERROR_NONE != ret) {
153                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to start");
154         }
155         return EINA_FALSE;
156 }
157
158 static void __stt_state_changed_cb(stt_h stt, stt_state_e previous, stt_state_e current, void* user_data)
159 {
160         if (STT_STATE_CREATED == previous && STT_STATE_READY == current) {
161                 SLOG(LOG_DEBUG, TAG_STT_TEST, "State is Created -> Ready");
162                 ecore_timer_add(0, __stt_start, NULL);
163         }
164 }
165
166 static Eina_Bool __stt_finalize(void *data)
167 {
168         int ret;
169         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT destroy");
170         ret = stt_destroy(g_stt);
171         if (STT_ERROR_NONE != ret) {
172                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to stt destroy");
173         }
174
175         ecore_main_loop_quit();
176         return EINA_FALSE;
177 }
178
179 static void __stt_recognition_result_cb(stt_h stt, stt_result_event_e event, const char** data, int data_count, const char* msg, void *user_data)
180 {
181         SLOG(LOG_DEBUG, TAG_STT_TEST, "==== STT result cb ====");
182
183         if (NULL != data) {
184                 SLOG(LOG_DEBUG, TAG_STT_TEST, "( %s )", data[0]);
185         }
186
187         ecore_timer_add(0, __stt_finalize, NULL);
188 }
189
190 int main(int argc, char *argv[])
191 {
192         if (2 < argc) {
193                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check parameter");
194                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -f <file path>");
195                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -m");
196                 return 0;
197         }
198
199         if (strcmp("-f", argv[1]) && strcmp("-m", argv[1])) {
200                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check parameter");
201                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -f <file path>");
202                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -m");
203                 return 0;
204         }
205
206         if (!strcmp("-f", argv[1])) {
207                 if (0 != access(argv[2], F_OK)) {
208                         SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check filepath");
209                         return 0;
210                 }
211
212                 SLOG(LOG_DEBUG, TAG_STT_TEST, "===== STT File (%s) Sample start =====", argv[2]);
213
214                 if (!ecore_init()) {
215                         SLOG(LOG_ERROR, TAG_STT_TEST, "[Main ERROR] Fail ecore_init()");
216                         return 0;
217                 }
218
219                 int ret;
220                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT File initialize");
221                 ret = stt_file_initialize();
222                 if (STT_FILE_ERROR_NONE != ret) {
223                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to initialize");
224                         return 0;
225                 }
226
227                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT Set Callbacks");
228                 ret = stt_file_set_recognition_result_cb(__stt_file_recognition_result_cb, NULL);
229                 if (STT_FILE_ERROR_NONE != ret) {
230                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set recognition result cb");
231                         return 0;
232                 }
233
234                 ret = stt_file_set_state_changed_cb(__stt_file_state_changed_cb, NULL);
235                 if (STT_FILE_ERROR_NONE != ret) {
236                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set state changed cb");
237                         return 0;
238                 }
239
240                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Get supported langauge");
241                 ret = stt_file_foreach_supported_languages(__stt_file_supported_language_cb, NULL);
242                 if (STT_FILE_ERROR_NONE != ret) {
243                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to get supported language");
244                         return 0;
245                 }
246
247                 ecore_timer_add(1, __get_engine_list, NULL);
248
249                 char *tmp = strdup(argv[2]);
250                 ecore_timer_add(1, __file_start, tmp);
251         } else if (!strcmp("-m", argv[1])) {
252
253                 ecore_init();
254
255                 int ret;
256
257                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT Create");
258                 ret = stt_create(&g_stt);
259                 if (STT_ERROR_NONE != ret) {
260                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to create");
261                         return 0;
262                 }
263
264                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Set callback");
265                 ret = stt_set_state_changed_cb(g_stt, __stt_state_changed_cb, NULL);
266                 if (STT_ERROR_NONE != ret) {
267                         stt_destroy(g_stt);
268                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set state changed cb");
269                         return 0;
270                 }
271                 ret = stt_set_recognition_result_cb(g_stt, __stt_recognition_result_cb, NULL);
272                 if (STT_ERROR_NONE != ret) {
273                         stt_destroy(g_stt);
274                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set recognition result cb");
275                         return 0;
276                 }
277
278                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT prepare");
279                 ret = stt_prepare(g_stt);
280                 if (STT_ERROR_NONE != ret) {
281                         stt_destroy(g_stt);
282                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to prepare");
283                         return 0;
284                 }
285         }
286
287         ecore_main_loop_begin();
288
289         ecore_shutdown();
290
291         SLOG(LOG_DEBUG, TAG_STT_TEST, "===== STT END =====");
292
293         return 0;
294 }