aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/gnunet-conversation-test.c
blob: 2e6772d92c6665b2fd6ba82d0e63726922da7c10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
     This file is part of GNUnet.
     Copyright (C) 2013 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file conversation/gnunet-conversation-test.c
 * @brief tool to test speaker and microphone (for end users!)
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_speaker_lib.h"
#include "gnunet_microphone_lib.h"

/**
 * How long do we record before we replay?
 */
#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)


/**
 * A recording we made.
 */
struct Recording
{
  /**
   * Kept in a DLL.
   */
  struct Recording *next;

  /**
   * Kept in a DLL.
   */
  struct Recording *prev;

  /**
   * Number of bytes that follow.
   */
  size_t size;
};


/**
 * Final status code.
 */
static int ret;

/**
 * Handle to the microphone.
 */
static struct GNUNET_MICROPHONE_Handle *microphone;

/**
 * Handle to the speaker.
 */
static struct GNUNET_SPEAKER_Handle *speaker;

/**
 * Task scheduled to switch from recording to playback.
 */
static struct GNUNET_SCHEDULER_Task *switch_task;

/**
 * The shutdown task.
 */
static struct GNUNET_SCHEDULER_Task *st;

/**
 * Head of DLL with recorded frames.
 */
static struct Recording *rec_head;

/**
 * Tail of DLL with recorded frames.
 */
static struct Recording *rec_tail;


/**
 * Terminate test.
 *
 * @param cls NULL
 */
static void
do_shutdown (void *cls)
{
  struct Recording *rec;

  (void) cls;
  if (NULL != switch_task)
    GNUNET_SCHEDULER_cancel (switch_task);
  if (NULL != microphone)
    GNUNET_MICROPHONE_destroy (microphone);
  if (NULL != speaker)
    GNUNET_SPEAKER_destroy (speaker);
  while (NULL != (rec = rec_head))
  {
    GNUNET_CONTAINER_DLL_remove (rec_head,
                                 rec_tail,
                                 rec);
    GNUNET_free (rec);
  }
  fprintf (stderr,
           _ ("\nEnd of transmission.  Have a GNU day.\n"));
}


/**
 * Terminate recording process and switch to playback.
 *
 * @param cls NULL
 */
static void
switch_to_speaker (void *cls)
{
  (void) cls;
  switch_task = NULL;
  microphone->disable_microphone (microphone->cls);
  if (GNUNET_OK !=
      speaker->enable_speaker (speaker->cls))
  {
    fprintf (stderr,
             "Failed to enable microphone\n");
    ret = 1;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  fprintf (stderr,
           _ (
             "\nWe are now playing your recording back.  If you can hear it, your audio settings are working..."));
  for (struct Recording *rec = rec_head; NULL != rec; rec = rec->next)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Replaying %u bytes\n",
                (unsigned int) rec->size);
    speaker->play (speaker->cls,
                   rec->size,
                   &rec[1]);
  }
  GNUNET_SCHEDULER_cancel (st);
  st = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
                                     &do_shutdown,
                                     NULL);
}


/**
 * Process recorded audio data.
 *
 * @param cls clsoure
 * @param data_size number of bytes in @a data
 * @param data audio data to play
 */
static void
record (void *cls,
        size_t data_size,
        const void *data)
{
  struct Recording *rec;

  (void) cls;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Recorded %u bytes\n",
              (unsigned int) data_size);
  rec = GNUNET_malloc (sizeof(struct Recording) + data_size);
  rec->size = data_size;
  GNUNET_memcpy (&rec[1], data, data_size);
  GNUNET_CONTAINER_DLL_insert_tail (rec_head,
                                    rec_tail,
                                    rec);
}


/**
 * Main function that will be run by the scheduler.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param cfg configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  (void) cls;
  (void) args;
  (void) cfgfile;
  microphone = GNUNET_MICROPHONE_create_from_hardware (cfg);
  GNUNET_assert (NULL != microphone);
  speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
  GNUNET_assert (NULL != speaker);
  switch_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
                                              &switch_to_speaker,
                                              NULL);
  st = GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
                                      NULL);
  fprintf (stderr,
           _ (
             "We will now be recording you for %s. After that time, the recording will be played back to you..."),
           GNUNET_STRINGS_relative_time_to_string (TIMEOUT, GNUNET_YES));
  if (GNUNET_OK !=
      microphone->enable_microphone (microphone->cls,
                                     &record, NULL))
  {
    fprintf (stderr,
             "Failed to enable microphone\n");
    ret = 1;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
}


/**
 * The main function of our code to test microphone and speaker.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc,
      char *const *argv)
{
  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };

  if (GNUNET_OK !=
      GNUNET_STRINGS_get_utf8_args (argc, argv,
                                    &argc, &argv))
    return 2;

  ret = (GNUNET_OK ==
         GNUNET_PROGRAM_run (argc, argv,
                             "gnunet-conversation-test",
                             gettext_noop ("help text"),
                             options,
                             &run,
                             NULL)) ? ret : 1;
  GNUNET_free ((void *) argv);
  return ret;
}


/* end of gnunet-conversation-test.c */