diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-10-02 20:06:07 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-10-02 20:06:07 +0000 |
commit | d08455c093922745b614102c67ab08364e47b587 (patch) | |
tree | df4858c8fd3803ef80ee4a6942927aab5a63c3fe /src/conversation/gnunet-conversation-test.c | |
parent | e4d1ce975f6bbb5c89904ad9c174f1e29b49a76b (diff) | |
download | gnunet-d08455c093922745b614102c67ab08364e47b587.tar.gz gnunet-d08455c093922745b614102c67ab08364e47b587.zip |
-tool to record and replay, to test microphone and speaker logic
Diffstat (limited to 'src/conversation/gnunet-conversation-test.c')
-rw-r--r-- | src/conversation/gnunet-conversation-test.c | 243 |
1 files changed, 243 insertions, 0 deletions
diff --git a/src/conversation/gnunet-conversation-test.c b/src/conversation/gnunet-conversation-test.c new file mode 100644 index 000000000..13a55ad0c --- /dev/null +++ b/src/conversation/gnunet-conversation-test.c | |||
@@ -0,0 +1,243 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file conversation/gnunet-conversation-test.c | ||
23 | * @brief tool to test speaker and microphone (for end users!) | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_speaker_lib.h" | ||
29 | #include "gnunet_microphone_lib.h" | ||
30 | |||
31 | /** | ||
32 | * How long do we record before we replay? | ||
33 | */ | ||
34 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) | ||
35 | |||
36 | |||
37 | /** | ||
38 | * A recording we made. | ||
39 | */ | ||
40 | struct Recording | ||
41 | { | ||
42 | /** | ||
43 | * Kept in a DLL. | ||
44 | */ | ||
45 | struct Recording *next; | ||
46 | |||
47 | /** | ||
48 | * Kept in a DLL. | ||
49 | */ | ||
50 | struct Recording *prev; | ||
51 | |||
52 | /** | ||
53 | * Number of bytes that follow. | ||
54 | */ | ||
55 | size_t size; | ||
56 | }; | ||
57 | |||
58 | |||
59 | /** | ||
60 | * Final status code. | ||
61 | */ | ||
62 | static int ret; | ||
63 | |||
64 | /** | ||
65 | * Handle to the microphone. | ||
66 | */ | ||
67 | static struct GNUNET_MICROPHONE_Handle *microphone; | ||
68 | |||
69 | /** | ||
70 | * Handle to the speaker. | ||
71 | */ | ||
72 | static struct GNUNET_SPEAKER_Handle *speaker; | ||
73 | |||
74 | /** | ||
75 | * Task scheduled to switch from recording to playback. | ||
76 | */ | ||
77 | static GNUNET_SCHEDULER_TaskIdentifier switch_task; | ||
78 | |||
79 | /** | ||
80 | * The shutdown task. | ||
81 | */ | ||
82 | static GNUNET_SCHEDULER_TaskIdentifier st; | ||
83 | |||
84 | /** | ||
85 | * Head of DLL with recorded frames. | ||
86 | */ | ||
87 | static struct Recording *rec_head; | ||
88 | |||
89 | /** | ||
90 | * Tail of DLL with recorded frames. | ||
91 | */ | ||
92 | static struct Recording *rec_tail; | ||
93 | |||
94 | |||
95 | /** | ||
96 | * Terminate test. | ||
97 | * | ||
98 | * @param cls NULL | ||
99 | * @param tc unused | ||
100 | */ | ||
101 | static void | ||
102 | do_shutdown (void *cls, | ||
103 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
104 | { | ||
105 | struct Recording *rec; | ||
106 | |||
107 | if (GNUNET_SCHEDULER_NO_TASK != switch_task) | ||
108 | GNUNET_SCHEDULER_cancel (switch_task); | ||
109 | if (NULL != microphone) | ||
110 | GNUNET_MICROPHONE_destroy (microphone); | ||
111 | if (NULL != speaker) | ||
112 | GNUNET_SPEAKER_destroy (speaker); | ||
113 | while (NULL != (rec = rec_head)) | ||
114 | { | ||
115 | GNUNET_CONTAINER_DLL_remove (rec_head, | ||
116 | rec_tail, | ||
117 | rec); | ||
118 | GNUNET_free (rec); | ||
119 | } | ||
120 | fprintf (stderr, "\n"); | ||
121 | } | ||
122 | |||
123 | |||
124 | /** | ||
125 | * Terminate recording process and switch to playback. | ||
126 | * | ||
127 | * @param cls NULL | ||
128 | * @param tc unused | ||
129 | */ | ||
130 | static void | ||
131 | switch_to_speaker (void *cls, | ||
132 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
133 | { | ||
134 | struct Recording *rec; | ||
135 | |||
136 | switch_task = GNUNET_SCHEDULER_NO_TASK; | ||
137 | microphone->disable_microphone (microphone->cls); | ||
138 | if (GNUNET_OK != | ||
139 | speaker->enable_speaker (speaker->cls)) | ||
140 | { | ||
141 | fprintf (stderr, | ||
142 | "Failed to enable microphone\n"); | ||
143 | ret = 1; | ||
144 | GNUNET_SCHEDULER_shutdown (); | ||
145 | return; | ||
146 | } | ||
147 | fprintf (stderr, "\nPlaying..."); | ||
148 | for (rec=rec_head;NULL != rec; rec = rec->next) | ||
149 | speaker->play (speaker->cls, | ||
150 | rec->size, | ||
151 | &rec[1]); | ||
152 | GNUNET_SCHEDULER_cancel (st); | ||
153 | st = GNUNET_SCHEDULER_add_delayed (TIMEOUT, | ||
154 | &do_shutdown, | ||
155 | NULL); | ||
156 | } | ||
157 | |||
158 | |||
159 | /** | ||
160 | * Process recorded audio data. | ||
161 | * | ||
162 | * @param cls clsoure | ||
163 | * @param data_size number of bytes in @a data | ||
164 | * @param data audio data to play | ||
165 | */ | ||
166 | static void | ||
167 | record (void *cls, | ||
168 | size_t data_size, | ||
169 | const void *data) | ||
170 | { | ||
171 | struct Recording *rec; | ||
172 | |||
173 | fprintf (stderr, "."); | ||
174 | rec = GNUNET_malloc (sizeof (struct Recording) + data_size); | ||
175 | rec->size = data_size; | ||
176 | memcpy (&rec[1], data, data_size); | ||
177 | GNUNET_CONTAINER_DLL_insert_tail (rec_head, | ||
178 | rec_tail, | ||
179 | rec); | ||
180 | } | ||
181 | |||
182 | |||
183 | /** | ||
184 | * Main function that will be run by the scheduler. | ||
185 | * | ||
186 | * @param cls closure | ||
187 | * @param args remaining command-line arguments | ||
188 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
189 | * @param cfg configuration | ||
190 | */ | ||
191 | static void | ||
192 | run (void *cls, char *const *args, const char *cfgfile, | ||
193 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
194 | { | ||
195 | microphone = GNUNET_MICROPHONE_create_from_hardware (cfg); | ||
196 | GNUNET_assert (NULL != microphone); | ||
197 | speaker = GNUNET_SPEAKER_create_from_hardware (cfg); | ||
198 | GNUNET_assert (NULL != speaker); | ||
199 | switch_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, | ||
200 | &switch_to_speaker, | ||
201 | NULL); | ||
202 | st = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | ||
203 | &do_shutdown, | ||
204 | NULL); | ||
205 | fprintf (stderr, "Recording..."); | ||
206 | if (GNUNET_OK != | ||
207 | microphone->enable_microphone (microphone->cls, | ||
208 | &record, NULL)) | ||
209 | { | ||
210 | fprintf (stderr, | ||
211 | "Failed to enable microphone\n"); | ||
212 | ret = 1; | ||
213 | GNUNET_SCHEDULER_shutdown (); | ||
214 | return; | ||
215 | } | ||
216 | } | ||
217 | |||
218 | |||
219 | /** | ||
220 | * The main function of our code to test microphone and speaker. | ||
221 | * | ||
222 | * @param argc number of arguments from the command line | ||
223 | * @param argv command line arguments | ||
224 | * @return 0 ok, 1 on error | ||
225 | */ | ||
226 | int | ||
227 | main (int argc, char *const *argv) | ||
228 | { | ||
229 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
230 | GNUNET_GETOPT_OPTION_END | ||
231 | }; | ||
232 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | ||
233 | return 2; | ||
234 | |||
235 | ret = (GNUNET_OK == | ||
236 | GNUNET_PROGRAM_run (argc, argv, "gnunet-conversation-test", | ||
237 | gettext_noop ("help text"), options, &run, | ||
238 | NULL)) ? ret : 1; | ||
239 | GNUNET_free ((void*) argv); | ||
240 | return ret; | ||
241 | } | ||
242 | |||
243 | /* end of gnunet-conversation-test.c */ | ||