src: for every AGPL3.0 file, add SPDX identifier.
[oweals/gnunet.git] / src / conversation / gnunet-conversation-test.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 GNUnet e.V.
4
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.
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      Affero General Public License for more details.
14     
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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, 5)
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 struct GNUNET_SCHEDULER_Task * switch_task;
78
79 /**
80  * The shutdown task.
81  */
82 static struct GNUNET_SCHEDULER_Task * 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  */
100 static void
101 do_shutdown (void *cls)
102 {
103   struct Recording *rec;
104
105   (void) cls;
106   if (NULL != switch_task)
107     GNUNET_SCHEDULER_cancel (switch_task);
108   if (NULL != microphone)
109     GNUNET_MICROPHONE_destroy (microphone);
110   if (NULL != speaker)
111     GNUNET_SPEAKER_destroy (speaker);
112   while (NULL != (rec = rec_head))
113   {
114     GNUNET_CONTAINER_DLL_remove (rec_head,
115                                  rec_tail,
116                                  rec);
117     GNUNET_free (rec);
118   }
119   fprintf (stderr,
120            _("\nEnd of transmission.  Have a GNU day.\n"));
121 }
122
123
124 /**
125  * Terminate recording process and switch to playback.
126  *
127  * @param cls NULL
128  */
129 static void
130 switch_to_speaker (void *cls)
131 {
132   (void) cls;
133   switch_task = NULL;
134   microphone->disable_microphone (microphone->cls);
135   if (GNUNET_OK !=
136       speaker->enable_speaker (speaker->cls))
137   {
138     fprintf (stderr,
139              "Failed to enable microphone\n");
140     ret = 1;
141     GNUNET_SCHEDULER_shutdown ();
142     return;
143   }
144   fprintf (stderr,
145            _("\nWe are now playing your recording back.  If you can hear it, your audio settings are working..."));
146   for (struct Recording *rec=rec_head; NULL != rec; rec = rec->next)
147   {
148     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149                 "Replaying %u bytes\n",
150                 (unsigned int) rec->size);
151     speaker->play (speaker->cls,
152                    rec->size,
153                    &rec[1]);
154   }
155   GNUNET_SCHEDULER_cancel (st);
156   st = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
157                                      &do_shutdown,
158                                      NULL);
159 }
160
161
162 /**
163  * Process recorded audio data.
164  *
165  * @param cls clsoure
166  * @param data_size number of bytes in @a data
167  * @param data audio data to play
168  */
169 static void
170 record (void *cls,
171         size_t data_size,
172         const void *data)
173 {
174   struct Recording *rec;
175
176   (void) cls;
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178               "Recorded %u bytes\n",
179               (unsigned int) data_size);
180   rec = GNUNET_malloc (sizeof (struct Recording) + data_size);
181   rec->size = data_size;
182   GNUNET_memcpy (&rec[1], data, data_size);
183   GNUNET_CONTAINER_DLL_insert_tail (rec_head,
184                                     rec_tail,
185                                     rec);
186 }
187
188
189 /**
190  * Main function that will be run by the scheduler.
191  *
192  * @param cls closure
193  * @param args remaining command-line arguments
194  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
195  * @param cfg configuration
196  */
197 static void
198 run (void *cls,
199      char *const *args,
200      const char *cfgfile,
201      const struct GNUNET_CONFIGURATION_Handle *cfg)
202 {
203   (void) cls;
204   (void) args;
205   (void) cfgfile;
206   microphone = GNUNET_MICROPHONE_create_from_hardware (cfg);
207   GNUNET_assert (NULL != microphone);
208   speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
209   GNUNET_assert (NULL != speaker);
210   switch_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
211                                               &switch_to_speaker,
212                                               NULL);
213   st = GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
214                                       NULL);
215   fprintf (stderr,
216            _("We will now be recording you for %s. After that time, the recording will be played back to you..."),
217            GNUNET_STRINGS_relative_time_to_string (TIMEOUT, GNUNET_YES));
218   if (GNUNET_OK !=
219       microphone->enable_microphone (microphone->cls,
220                                      &record, NULL))
221   {
222     fprintf (stderr,
223              "Failed to enable microphone\n");
224     ret = 1;
225     GNUNET_SCHEDULER_shutdown ();
226     return;
227   }
228 }
229
230
231 /**
232  * The main function of our code to test microphone and speaker.
233  *
234  * @param argc number of arguments from the command line
235  * @param argv command line arguments
236  * @return 0 ok, 1 on error
237  */
238 int
239 main (int argc,
240       char *const *argv)
241 {
242   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
243     GNUNET_GETOPT_OPTION_END
244   };
245   
246   if (GNUNET_OK !=
247       GNUNET_STRINGS_get_utf8_args (argc, argv,
248                                     &argc, &argv))
249     return 2;
250
251   ret = (GNUNET_OK ==
252          GNUNET_PROGRAM_run (argc, argv,
253                              "gnunet-conversation-test",
254                              gettext_noop ("help text"),
255                              options,
256                              &run,
257                              NULL)) ? ret : 1;
258   GNUNET_free ((void*) argv);
259   return ret;
260 }
261
262 /* end of gnunet-conversation-test.c */