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