2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
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 Affero General Public License for more details.
17 * @file conversation/gnunet-conversation-test.c
18 * @brief tool to test speaker and microphone (for end users!)
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_speaker_lib.h"
24 #include "gnunet_microphone_lib.h"
27 * How long do we record before we replay?
29 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
33 * A recording we made.
40 struct Recording *next;
45 struct Recording *prev;
48 * Number of bytes that follow.
60 * Handle to the microphone.
62 static struct GNUNET_MICROPHONE_Handle *microphone;
65 * Handle to the speaker.
67 static struct GNUNET_SPEAKER_Handle *speaker;
70 * Task scheduled to switch from recording to playback.
72 static struct GNUNET_SCHEDULER_Task * switch_task;
77 static struct GNUNET_SCHEDULER_Task * st;
80 * Head of DLL with recorded frames.
82 static struct Recording *rec_head;
85 * Tail of DLL with recorded frames.
87 static struct Recording *rec_tail;
96 do_shutdown (void *cls)
98 struct Recording *rec;
100 if (NULL != switch_task)
101 GNUNET_SCHEDULER_cancel (switch_task);
102 if (NULL != microphone)
103 GNUNET_MICROPHONE_destroy (microphone);
105 GNUNET_SPEAKER_destroy (speaker);
106 while (NULL != (rec = rec_head))
108 GNUNET_CONTAINER_DLL_remove (rec_head,
114 _("\nEnd of transmission. Have a GNU day.\n"));
119 * Terminate recording process and switch to playback.
124 switch_to_speaker (void *cls)
126 struct Recording *rec;
129 microphone->disable_microphone (microphone->cls);
131 speaker->enable_speaker (speaker->cls))
134 "Failed to enable microphone\n");
136 GNUNET_SCHEDULER_shutdown ();
140 _("\nWe are now playing your recording back. If you can hear it, your audio settings are working..."));
141 for (rec=rec_head; NULL != rec; rec = rec->next)
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144 "Replaying %u bytes\n",
145 (unsigned int) rec->size);
146 speaker->play (speaker->cls,
150 GNUNET_SCHEDULER_cancel (st);
151 st = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
158 * Process recorded audio data.
161 * @param data_size number of bytes in @a data
162 * @param data audio data to play
169 struct Recording *rec;
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172 "Recorded %u bytes\n",
173 (unsigned int) data_size);
174 rec = GNUNET_malloc (sizeof (struct Recording) + data_size);
175 rec->size = data_size;
176 GNUNET_memcpy (&rec[1], data, data_size);
177 GNUNET_CONTAINER_DLL_insert_tail (rec_head,
184 * Main function that will be run by the scheduler.
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
192 run (void *cls, char *const *args, const char *cfgfile,
193 const struct GNUNET_CONFIGURATION_Handle *cfg)
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,
202 st = GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
205 _("We will now be recording you for %s. After that time, the recording will be played back to you..."),
206 GNUNET_STRINGS_relative_time_to_string (TIMEOUT, GNUNET_YES));
208 microphone->enable_microphone (microphone->cls,
212 "Failed to enable microphone\n");
214 GNUNET_SCHEDULER_shutdown ();
221 * The main function of our code to test microphone and speaker.
223 * @param argc number of arguments from the command line
224 * @param argv command line arguments
225 * @return 0 ok, 1 on error
228 main (int argc, char *const *argv)
230 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
231 GNUNET_GETOPT_OPTION_END
233 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
237 GNUNET_PROGRAM_run (argc, argv, "gnunet-conversation-test",
238 gettext_noop ("help text"), options, &run,
240 GNUNET_free ((void*) argv);
244 /* end of gnunet-conversation-test.c */