Add critical section to avoid thread safety issue
[platform/core/uifw/stt.git] / test / test_main.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 <stdio.h>
15 #include <Ecore.h>
16 #include <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_get_volume(void *data)
142 {
143         float volume = 0.0;
144         int ret = stt_get_recording_volume(g_stt, &volume);
145         if (STT_ERROR_NONE != ret) {
146                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to get volume");
147                 return EINA_FALSE;
148         }
149
150         SLOG(LOG_DEBUG, TAG_STT_TEST, "Get volume : %f", volume);
151
152         return EINA_TRUE;
153 }
154
155 static Eina_Bool __stt_start(void *data)
156 {
157         int ret;
158         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT set silence detection");
159         ret = stt_set_silence_detection(g_stt, STT_OPTION_SILENCE_DETECTION_TRUE);
160         if (STT_ERROR_NONE != ret) {
161                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to set silence detection");
162         }
163
164         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT start");
165         ret = stt_start(g_stt, "en_US", "stt.recognition.type.FREE");
166         if (STT_ERROR_NONE != ret) {
167                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to start");
168         }
169
170         ecore_timer_add(0.1, __stt_get_volume, NULL);
171
172         return EINA_FALSE;
173 }
174
175 static void __stt_state_changed_cb(stt_h stt, stt_state_e previous, stt_state_e current, void* user_data)
176 {
177         if (STT_STATE_CREATED == previous && STT_STATE_READY == current) {
178                 SLOG(LOG_DEBUG, TAG_STT_TEST, "State is Created -> Ready");
179                 ecore_timer_add(0, __stt_start, NULL);
180         }
181 }
182
183 static Eina_Bool __stt_finalize(void *data)
184 {
185         int ret;
186         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT destroy");
187         ret = stt_destroy(g_stt);
188         if (STT_ERROR_NONE != ret) {
189                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to stt destroy");
190         }
191
192         ecore_main_loop_quit();
193         return EINA_FALSE;
194 }
195
196 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)
197 {
198         SLOG(LOG_DEBUG, TAG_STT_TEST, "==== STT result cb ====");
199
200         if (NULL != data) {
201                 SLOG(LOG_DEBUG, TAG_STT_TEST, "( %s )", data[0]);
202         }
203
204         if (STT_RESULT_EVENT_FINAL_RESULT == event) {
205         ecore_timer_add(0, __stt_finalize, NULL);
206         }
207 }
208
209 int main(int argc, char *argv[])
210 {
211         if (2 > argc) {
212                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check parameter");
213                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -f <file path>");
214                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -m");
215                 return 0;
216         }
217
218         if (0 != strcmp("-f", argv[1]) && 0 != strcmp("-m", argv[1])) {
219                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check parameter");
220                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -f <file path>");
221                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -m");
222                 return 0;
223         }
224
225         if (!strcmp("-f", argv[1])) {
226                 if (0 != access(argv[2], F_OK)) {
227                         SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check filepath");
228                         return 0;
229                 }
230
231                 SLOG(LOG_DEBUG, TAG_STT_TEST, "===== STT File (%s) Sample start =====", argv[2]);
232
233                 if (!ecore_init()) {
234                         SLOG(LOG_ERROR, TAG_STT_TEST, "[Main ERROR] Fail ecore_init()");
235                         return 0;
236                 }
237
238                 int ret;
239                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT File initialize");
240                 ret = stt_file_initialize();
241                 if (STT_FILE_ERROR_NONE != ret) {
242                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to initialize");
243                         return 0;
244                 }
245
246                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT Set Callbacks");
247                 ret = stt_file_set_recognition_result_cb(__stt_file_recognition_result_cb, NULL);
248                 if (STT_FILE_ERROR_NONE != ret) {
249                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set recognition result cb");
250                         return 0;
251                 }
252
253                 ret = stt_file_set_state_changed_cb(__stt_file_state_changed_cb, NULL);
254                 if (STT_FILE_ERROR_NONE != ret) {
255                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set state changed cb");
256                         return 0;
257                 }
258
259                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Get supported langauge");
260                 ret = stt_file_foreach_supported_languages(__stt_file_supported_language_cb, NULL);
261                 if (STT_FILE_ERROR_NONE != ret) {
262                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to get supported language");
263                         return 0;
264                 }
265
266                 ecore_timer_add(1, __get_engine_list, NULL);
267
268                 char *tmp = strdup(argv[2]);
269                 ecore_timer_add(1, __file_start, tmp);
270         } else if (!strcmp("-m", argv[1])) {
271
272                 ecore_init();
273
274                 int ret;
275
276                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT Create");
277                 ret = stt_create(&g_stt);
278                 if (STT_ERROR_NONE != ret) {
279                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to create");
280                         return 0;
281                 }
282
283                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Set callback");
284                 ret = stt_set_state_changed_cb(g_stt, __stt_state_changed_cb, NULL);
285                 if (STT_ERROR_NONE != ret) {
286                         stt_destroy(g_stt);
287                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set state changed cb");
288                         return 0;
289                 }
290                 ret = stt_set_recognition_result_cb(g_stt, __stt_recognition_result_cb, NULL);
291                 if (STT_ERROR_NONE != ret) {
292                         stt_destroy(g_stt);
293                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set recognition result cb");
294                         return 0;
295                 }
296
297                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT set credential");
298                 ret = stt_set_credential(g_stt, "1Q2W3E4R5T");
299                 if (STT_ERROR_NONE != ret) {
300                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set credential");
301                         return 0;
302                 }
303
304                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT prepare");
305                 ret = stt_prepare(g_stt);
306                 if (STT_ERROR_NONE != ret) {
307                         stt_destroy(g_stt);
308                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to prepare");
309                         return 0;
310                 }
311         }
312
313         ecore_main_loop_begin();
314
315         ecore_shutdown();
316
317         SLOG(LOG_DEBUG, TAG_STT_TEST, "===== STT END =====");
318
319         return 0;
320 }