Merge "Change uid and gid for service files" into tizen
[platform/core/uifw/tts.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
17 #include <tts.h>
18 #include <dlog.h>
19
20 #define TTS_STRDUP(src)         ((src != NULL) ? strdup(src) : NULL)
21
22 static tts_h g_tts;
23 static char* g_text = NULL;
24
25 const char* tts_tag()
26 {
27         return "ttstest";
28 }
29
30 Eina_Bool __tts_test_destroy(void *data);
31
32 static bool __tts_test_get_text_from_file(const char* path, char** text)
33 {
34         if (!path) return 0;
35         if (!text) return 0;
36
37         FILE *fp = NULL;
38
39         if ((fp = fopen(path, "rb")) == NULL) {
40                 SLOG(LOG_ERROR, tts_tag(), "Fail to open file (%s)", path);
41                 return 0;
42         }
43
44         if (0 != fseek(fp , 0 , SEEK_END)) {
45                 SLOG(LOG_ERROR, tts_tag(), "Fail to fseek()");
46                 fclose(fp);
47                 return 0;
48         }
49
50         int text_len = ftell(fp);
51         if (0 >= text_len || 0 > text_len + 1) {
52                 SLOG(LOG_ERROR, tts_tag(), "File has no contents");
53                 fclose(fp);
54                 return 0;
55         }
56         SLOG(LOG_ERROR, tts_tag(), "text_len(%d)", text_len);
57         rewind(fp);
58
59         char* temp = NULL;
60         temp = (char*)calloc(1, text_len + 1);
61
62         if (temp == NULL) {
63                 SLOG(LOG_ERROR, tts_tag(), "Fail to memory allocation");
64                 fclose(fp);
65                 return 0;
66         }
67
68         int result_len = 1;
69         while (!feof(fp)) {
70                 result_len = fread(temp, sizeof(char), text_len, fp);
71                 if (result_len != text_len) {
72                         SLOG(LOG_ERROR, tts_tag(), "Fail to read : result(%d) text_len(%d)", result_len, text_len);
73                         fclose(fp);
74                         if (NULL != temp) {
75                                 free(temp);
76                                 temp = NULL;
77                         }
78                         return 0;
79                 }
80         }
81
82         temp[result_len] = '\0';
83
84         text = &temp;
85
86         fclose(fp);
87         return 1;
88 }
89
90 Eina_Bool __tts_test_resume(void *data)
91 {
92         int ret = tts_play(g_tts);
93         if (TTS_ERROR_NONE != ret) {
94                 SLOG(LOG_ERROR, tts_tag(), "Fail to resume");
95                 ecore_timer_add(0, __tts_test_destroy, NULL);
96                 return EINA_FALSE;
97         }
98
99         return EINA_FALSE;
100 }
101
102 Eina_Bool __tts_test_pause(void *data)
103 {
104         int ret = tts_pause(g_tts);
105         if (TTS_ERROR_NONE != ret) {
106                 SLOG(LOG_ERROR, tts_tag(), "Fail to pause");
107                 ecore_timer_add(0, __tts_test_destroy, NULL);
108                 return EINA_FALSE;
109         }
110
111         ecore_timer_add(3, __tts_test_resume, data);
112
113         return EINA_FALSE;
114 }
115
116 Eina_Bool __tts_test_play(void *data)
117 {
118         int utt_id;
119         int ret;
120         char* lang = NULL;
121
122         lang = (char*)data;
123
124         ret = tts_add_text(g_tts, g_text, lang, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &utt_id);
125         if (TTS_ERROR_NONE != ret) {
126                 SLOG(LOG_ERROR, tts_tag(), "Fail to add text");
127                 ecore_timer_add(0, __tts_test_destroy, NULL);
128                 return EINA_FALSE;
129         }
130
131         SLOG(LOG_ERROR, tts_tag(), "Play : utt id(%d)", utt_id);
132         ret = tts_play(g_tts);
133         if (TTS_ERROR_NONE != ret) {
134                 SLOG(LOG_ERROR, tts_tag(), "Fail to play");
135                 ecore_timer_add(0, __tts_test_destroy, NULL);
136                 return EINA_FALSE;
137         }
138
139 //      ecore_timer_add(1, __tts_test_pause, data);
140
141         return EINA_FALSE;
142 }
143
144 Eina_Bool __tts_test_destroy(void *data)
145 {
146         int ret;
147         SLOG(LOG_ERROR, tts_tag(), "Stop");
148         ret = tts_stop(g_tts);
149         if (TTS_ERROR_NONE != ret) {
150                 SLOG(LOG_ERROR, tts_tag(), "Fail to stop");
151         }
152
153         SLOG(LOG_ERROR, tts_tag(), "Unprepare (Disconnection)");
154         ret = tts_unprepare(g_tts);
155         if (TTS_ERROR_NONE != ret) {
156                 SLOG(LOG_ERROR, tts_tag(), "Fail to unprepare");
157         }
158
159         SLOG(LOG_ERROR, tts_tag(), "Destroy tts client");
160         ret = tts_destroy(g_tts);
161         if (TTS_ERROR_NONE != ret) {
162                 SLOG(LOG_ERROR, tts_tag(), "Fail to destroy");
163         }
164
165         ecore_main_loop_quit();
166
167         return EINA_FALSE;
168 }
169
170 static void __tts_test_state_changed_cb(tts_h tts, tts_state_e previous, tts_state_e current, void* user_data)
171 {
172         if (TTS_STATE_CREATED == previous && TTS_STATE_READY == current) {
173                 SLOG(LOG_ERROR, tts_tag(), "State is ready after prepare");
174                 ecore_timer_add(0, __tts_test_play, user_data);
175         }
176
177         return;
178 }
179
180 static void __tts_test_utt_started_cb(tts_h tts, int utt_id, void* user_data)
181 {
182         SLOG(LOG_DEBUG, tts_tag(), "Utterance started : utt id(%d)", utt_id);
183
184         return;
185 }
186
187 static void __tts_test_utt_completed_cb(tts_h tts, int utt_id, void* user_data)
188 {
189         SLOG(LOG_DEBUG, tts_tag(), "Utterance completed : utt id(%d)", utt_id);
190         ecore_timer_add(0, __tts_test_destroy, NULL);
191
192         return;
193 }
194
195 int main(int argc, char *argv[])
196 {
197         if (1 == argc || 5 < argc) {
198                 SLOG(LOG_DEBUG, tts_tag(), "Please check parameter");
199                 SLOG(LOG_DEBUG, tts_tag(), "Ex> tts-test -t 'text'");
200                 return 0;
201         }
202
203         char* lang = NULL;
204         char* src_path = NULL;
205
206         int n = 0;
207
208         while (NULL != argv[n]) {
209
210                 if (!strcmp("-h", argv[n])) {
211                         SLOG(LOG_DEBUG, tts_tag(), " ==========================================");
212                         SLOG(LOG_DEBUG, tts_tag(), "  TTS test usage");
213                         SLOG(LOG_DEBUG, tts_tag(), " ==========================================");
214                         SLOG(LOG_DEBUG, tts_tag(), "  -t : Synthesize text");
215                         SLOG(LOG_DEBUG, tts_tag(), "  -l : Determine language to synthesize text, ex) en_US, ko_KR ...");
216                         SLOG(LOG_DEBUG, tts_tag(), "  -f : Determine file path which include text");
217                         SLOG(LOG_DEBUG, tts_tag(), " ***************************************************");
218                         SLOG(LOG_DEBUG, tts_tag(), "    Example : #tts-test -l en_US -t \"1 2 3 4\" ");
219                         SLOG(LOG_DEBUG, tts_tag(), " ***************************************************");
220                         return 0;
221                 }
222
223                 /* check language option */
224                 if (!strcmp("-l", argv[n])) {
225                         lang = TTS_STRDUP(argv[n+1]);
226                         SLOG(LOG_ERROR, tts_tag(), "Language : %s", lang);
227                 }
228                 /* check text to synthesize */
229                 else if (!strcmp("-t", argv[n])) {
230                         g_text = TTS_STRDUP(argv[n+1]);
231                         SLOG(LOG_ERROR, tts_tag(), "Text : %s", g_text);
232                 }
233                 /* check file path to synthesize */
234                 else if (!strcmp("-f", argv[n])) {
235                         src_path = TTS_STRDUP(argv[n+1]);
236                         SLOG(LOG_ERROR, tts_tag(), "File path : %s", src_path);
237                         if (!__tts_test_get_text_from_file(src_path, &g_text)) {
238                                 return 0;
239                         }
240                 }
241                 n++;
242         }
243
244         if (!g_text && !src_path) {
245                 SLOG(LOG_ERROR, tts_tag(), "Invalid parameter, check help with command tts-test -h");
246                 return 0;
247         }
248
249         /*=================================== */
250
251         tts_mode_e mode = TTS_MODE_DEFAULT;
252
253         SLOG(LOG_DEBUG, tts_tag(), "  ");
254         SLOG(LOG_DEBUG, tts_tag(), "  ");
255         SLOG(LOG_DEBUG, tts_tag(), "===== TTS Sample start =====");
256
257         SLOG(LOG_DEBUG, tts_tag(), "Input text : %s", g_text ? g_text : "NULL");
258         SLOG(LOG_DEBUG, tts_tag(), "Input lang : %s", lang ? lang : "NULL");
259         SLOG(LOG_DEBUG, tts_tag(), "Input file path : %s", src_path ? src_path : "NULL");
260
261         if (!ecore_init()) {
262                 SLOG(LOG_ERROR, tts_tag(), "[Main ERROR] Fail ecore_init()");
263                 return 0;
264         }
265
266         int ret;
267
268         SLOG(LOG_DEBUG, tts_tag(), "Create tts client");
269         ret = tts_create(&g_tts);
270         if (TTS_ERROR_NONE != ret) {
271                 SLOG(LOG_ERROR, tts_tag(), "Fail to create");
272                 return 0;
273         }
274
275         SLOG(LOG_DEBUG, tts_tag(), "Set tts mode - %d", mode);
276         ret = tts_set_mode(g_tts, mode);
277         if (TTS_ERROR_NONE != ret) {
278                 SLOG(LOG_ERROR, tts_tag(), "Fail to set mode");
279                 tts_destroy(g_tts);
280                 return 0;
281         }
282
283         SLOG(LOG_DEBUG, tts_tag(), "Set Callback func");
284         ret = tts_set_state_changed_cb(g_tts, __tts_test_state_changed_cb, (void*)lang);
285         if (TTS_ERROR_NONE != ret) {
286                 SLOG(LOG_ERROR, tts_tag(), "Fail to set state changed cb");
287                 tts_destroy(g_tts);
288                 return 0;
289         }
290
291         ret = tts_set_utterance_started_cb(g_tts, __tts_test_utt_started_cb, NULL);
292         if (TTS_ERROR_NONE != ret) {
293                 SLOG(LOG_ERROR, tts_tag(), "Fail to set utt started cb");
294                 tts_destroy(g_tts);
295                 return 0;
296         }
297
298         ret = tts_set_utterance_completed_cb(g_tts, __tts_test_utt_completed_cb, NULL);
299         if (TTS_ERROR_NONE != ret) {
300                 SLOG(LOG_ERROR, tts_tag(), "Fail to set utt completed cb");
301                 tts_destroy(g_tts);
302                 return 0;
303         }
304
305         SLOG(LOG_DEBUG, tts_tag(), "Prepare (Daemon Connection) asynchronously : Wait for ready state");
306         ret = tts_prepare(g_tts);
307         if (TTS_ERROR_NONE != ret) {
308                 SLOG(LOG_ERROR, tts_tag(), "Fail to prepare");
309                 tts_destroy(g_tts);
310                 return 0;
311         }
312
313         ecore_main_loop_begin();
314
315         ecore_shutdown();
316
317         if (src_path) free(src_path);
318         if (lang) free(lang);
319         if (g_text) free(g_text);
320
321         SLOG(LOG_DEBUG, tts_tag(), "===== TTS END =====");
322
323         return 0;
324 }