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 Affero 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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 struct GNUNET_SCHEDULER_Task *switch_task;
82 static struct GNUNET_SCHEDULER_Task *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;
101 do_shutdown (void *cls)
103 struct Recording *rec;
106 if (NULL != switch_task)
107 GNUNET_SCHEDULER_cancel (switch_task);
108 if (NULL != microphone)
109 GNUNET_MICROPHONE_destroy (microphone);
111 GNUNET_SPEAKER_destroy (speaker);
112 while (NULL != (rec = rec_head))
114 GNUNET_CONTAINER_DLL_remove (rec_head,
120 _ ("\nEnd of transmission. Have a GNU day.\n"));
125 * Terminate recording process and switch to playback.
130 switch_to_speaker (void *cls)
134 microphone->disable_microphone (microphone->cls);
136 speaker->enable_speaker (speaker->cls))
139 "Failed to enable microphone\n");
141 GNUNET_SCHEDULER_shutdown ();
146 "\nWe are now playing your recording back. If you can hear it, your audio settings are working..."));
147 for (struct Recording *rec = rec_head; NULL != rec; rec = rec->next)
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "Replaying %u bytes\n",
151 (unsigned int) rec->size);
152 speaker->play (speaker->cls,
156 GNUNET_SCHEDULER_cancel (st);
157 st = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
164 * Process recorded audio data.
167 * @param data_size number of bytes in @a data
168 * @param data audio data to play
175 struct Recording *rec;
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Recorded %u bytes\n",
180 (unsigned int) data_size);
181 rec = GNUNET_malloc (sizeof(struct Recording) + data_size);
182 rec->size = data_size;
183 GNUNET_memcpy (&rec[1], data, data_size);
184 GNUNET_CONTAINER_DLL_insert_tail (rec_head,
191 * Main function that will be run by the scheduler.
194 * @param args remaining command-line arguments
195 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
196 * @param cfg configuration
202 const struct GNUNET_CONFIGURATION_Handle *cfg)
207 microphone = GNUNET_MICROPHONE_create_from_hardware (cfg);
208 GNUNET_assert (NULL != microphone);
209 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
210 GNUNET_assert (NULL != speaker);
211 switch_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
214 st = GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
218 "We will now be recording you for %s. After that time, the recording will be played back to you..."),
219 GNUNET_STRINGS_relative_time_to_string (TIMEOUT, GNUNET_YES));
221 microphone->enable_microphone (microphone->cls,
225 "Failed to enable microphone\n");
227 GNUNET_SCHEDULER_shutdown ();
234 * The main function of our code to test microphone and speaker.
236 * @param argc number of arguments from the command line
237 * @param argv command line arguments
238 * @return 0 ok, 1 on error
244 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
245 GNUNET_GETOPT_OPTION_END
249 GNUNET_STRINGS_get_utf8_args (argc, argv,
254 GNUNET_PROGRAM_run (argc, argv,
255 "gnunet-conversation-test",
256 gettext_noop ("help text"),
260 GNUNET_free ((void*) argv);
264 /* end of gnunet-conversation-test.c */