2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file conversation/gnunet-conversation-test.c
23 * @brief tool to test speaker and microphone (for end users!)
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_speaker_lib.h"
29 #include "gnunet_microphone_lib.h"
32 * How long do we record before we replay?
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
38 * A recording we made.
45 struct Recording *next;
50 struct Recording *prev;
53 * Number of bytes that follow.
65 * Handle to the microphone.
67 static struct GNUNET_MICROPHONE_Handle *microphone;
70 * Handle to the speaker.
72 static struct GNUNET_SPEAKER_Handle *speaker;
75 * Task scheduled to switch from recording to playback.
77 static GNUNET_SCHEDULER_TaskIdentifier switch_task;
82 static GNUNET_SCHEDULER_TaskIdentifier st;
85 * Head of DLL with recorded frames.
87 static struct Recording *rec_head;
90 * Tail of DLL with recorded frames.
92 static struct Recording *rec_tail;
102 do_shutdown (void *cls,
103 const struct GNUNET_SCHEDULER_TaskContext *tc)
105 struct Recording *rec;
107 if (GNUNET_SCHEDULER_NO_TASK != switch_task)
108 GNUNET_SCHEDULER_cancel (switch_task);
109 if (NULL != microphone)
110 GNUNET_MICROPHONE_destroy (microphone);
112 GNUNET_SPEAKER_destroy (speaker);
113 while (NULL != (rec = rec_head))
115 GNUNET_CONTAINER_DLL_remove (rec_head,
121 _("\nEnd of transmission. Have a GNU day.\n"));
126 * Terminate recording process and switch to playback.
132 switch_to_speaker (void *cls,
133 const struct GNUNET_SCHEDULER_TaskContext *tc)
135 struct Recording *rec;
137 switch_task = GNUNET_SCHEDULER_NO_TASK;
138 microphone->disable_microphone (microphone->cls);
140 speaker->enable_speaker (speaker->cls))
143 "Failed to enable microphone\n");
145 GNUNET_SCHEDULER_shutdown ();
149 _("\nWe are now playing your recording back. If you can hear it, your audio settings are working..."));
150 for (rec=rec_head; NULL != rec; rec = rec->next)
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Replaying %u bytes\n",
154 (unsigned int) rec->size);
155 speaker->play (speaker->cls,
159 GNUNET_SCHEDULER_cancel (st);
160 st = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
167 * Process recorded audio data.
170 * @param data_size number of bytes in @a data
171 * @param data audio data to play
178 struct Recording *rec;
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Recorded %u bytes\n",
182 (unsigned int) data_size);
183 rec = GNUNET_malloc (sizeof (struct Recording) + data_size);
184 rec->size = data_size;
185 memcpy (&rec[1], data, data_size);
186 GNUNET_CONTAINER_DLL_insert_tail (rec_head,
193 * Main function that will be run by the scheduler.
196 * @param args remaining command-line arguments
197 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
198 * @param cfg configuration
201 run (void *cls, char *const *args, const char *cfgfile,
202 const struct GNUNET_CONFIGURATION_Handle *cfg)
204 microphone = GNUNET_MICROPHONE_create_from_hardware (cfg);
205 GNUNET_assert (NULL != microphone);
206 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
207 GNUNET_assert (NULL != speaker);
208 switch_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
211 st = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
215 _("We will now be recording you for %s. After that time, the recording will be played back to you..."),
216 GNUNET_STRINGS_relative_time_to_string (TIMEOUT, GNUNET_YES));
218 microphone->enable_microphone (microphone->cls,
222 "Failed to enable microphone\n");
224 GNUNET_SCHEDULER_shutdown ();
231 * The main function of our code to test microphone and speaker.
233 * @param argc number of arguments from the command line
234 * @param argv command line arguments
235 * @return 0 ok, 1 on error
238 main (int argc, char *const *argv)
240 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
241 GNUNET_GETOPT_OPTION_END
243 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
247 GNUNET_PROGRAM_run (argc, argv, "gnunet-conversation-test",
248 gettext_noop ("help text"), options, &run,
250 GNUNET_free ((void*) argv);
254 /* end of gnunet-conversation-test.c */