gnunet-conversation-test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file conversation/gnunet-conversation-test.c
  18. * @brief tool to test speaker and microphone (for end users!)
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_speaker_lib.h"
  24. #include "gnunet_microphone_lib.h"
  25. /**
  26. * How long do we record before we replay?
  27. */
  28. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  29. /**
  30. * A recording we made.
  31. */
  32. struct Recording
  33. {
  34. /**
  35. * Kept in a DLL.
  36. */
  37. struct Recording *next;
  38. /**
  39. * Kept in a DLL.
  40. */
  41. struct Recording *prev;
  42. /**
  43. * Number of bytes that follow.
  44. */
  45. size_t size;
  46. };
  47. /**
  48. * Final status code.
  49. */
  50. static int ret;
  51. /**
  52. * Handle to the microphone.
  53. */
  54. static struct GNUNET_MICROPHONE_Handle *microphone;
  55. /**
  56. * Handle to the speaker.
  57. */
  58. static struct GNUNET_SPEAKER_Handle *speaker;
  59. /**
  60. * Task scheduled to switch from recording to playback.
  61. */
  62. static struct GNUNET_SCHEDULER_Task * switch_task;
  63. /**
  64. * The shutdown task.
  65. */
  66. static struct GNUNET_SCHEDULER_Task * st;
  67. /**
  68. * Head of DLL with recorded frames.
  69. */
  70. static struct Recording *rec_head;
  71. /**
  72. * Tail of DLL with recorded frames.
  73. */
  74. static struct Recording *rec_tail;
  75. /**
  76. * Terminate test.
  77. *
  78. * @param cls NULL
  79. */
  80. static void
  81. do_shutdown (void *cls)
  82. {
  83. struct Recording *rec;
  84. (void) cls;
  85. if (NULL != switch_task)
  86. GNUNET_SCHEDULER_cancel (switch_task);
  87. if (NULL != microphone)
  88. GNUNET_MICROPHONE_destroy (microphone);
  89. if (NULL != speaker)
  90. GNUNET_SPEAKER_destroy (speaker);
  91. while (NULL != (rec = rec_head))
  92. {
  93. GNUNET_CONTAINER_DLL_remove (rec_head,
  94. rec_tail,
  95. rec);
  96. GNUNET_free (rec);
  97. }
  98. fprintf (stderr,
  99. _("\nEnd of transmission. Have a GNU day.\n"));
  100. }
  101. /**
  102. * Terminate recording process and switch to playback.
  103. *
  104. * @param cls NULL
  105. */
  106. static void
  107. switch_to_speaker (void *cls)
  108. {
  109. (void) cls;
  110. switch_task = NULL;
  111. microphone->disable_microphone (microphone->cls);
  112. if (GNUNET_OK !=
  113. speaker->enable_speaker (speaker->cls))
  114. {
  115. fprintf (stderr,
  116. "Failed to enable microphone\n");
  117. ret = 1;
  118. GNUNET_SCHEDULER_shutdown ();
  119. return;
  120. }
  121. fprintf (stderr,
  122. _("\nWe are now playing your recording back. If you can hear it, your audio settings are working..."));
  123. for (struct Recording *rec=rec_head; NULL != rec; rec = rec->next)
  124. {
  125. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  126. "Replaying %u bytes\n",
  127. (unsigned int) rec->size);
  128. speaker->play (speaker->cls,
  129. rec->size,
  130. &rec[1]);
  131. }
  132. GNUNET_SCHEDULER_cancel (st);
  133. st = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  134. &do_shutdown,
  135. NULL);
  136. }
  137. /**
  138. * Process recorded audio data.
  139. *
  140. * @param cls clsoure
  141. * @param data_size number of bytes in @a data
  142. * @param data audio data to play
  143. */
  144. static void
  145. record (void *cls,
  146. size_t data_size,
  147. const void *data)
  148. {
  149. struct Recording *rec;
  150. (void) cls;
  151. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  152. "Recorded %u bytes\n",
  153. (unsigned int) data_size);
  154. rec = GNUNET_malloc (sizeof (struct Recording) + data_size);
  155. rec->size = data_size;
  156. GNUNET_memcpy (&rec[1], data, data_size);
  157. GNUNET_CONTAINER_DLL_insert_tail (rec_head,
  158. rec_tail,
  159. rec);
  160. }
  161. /**
  162. * Main function that will be run by the scheduler.
  163. *
  164. * @param cls closure
  165. * @param args remaining command-line arguments
  166. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  167. * @param cfg configuration
  168. */
  169. static void
  170. run (void *cls,
  171. char *const *args,
  172. const char *cfgfile,
  173. const struct GNUNET_CONFIGURATION_Handle *cfg)
  174. {
  175. (void) cls;
  176. (void) args;
  177. (void) cfgfile;
  178. microphone = GNUNET_MICROPHONE_create_from_hardware (cfg);
  179. GNUNET_assert (NULL != microphone);
  180. speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
  181. GNUNET_assert (NULL != speaker);
  182. switch_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  183. &switch_to_speaker,
  184. NULL);
  185. st = GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  186. NULL);
  187. fprintf (stderr,
  188. _("We will now be recording you for %s. After that time, the recording will be played back to you..."),
  189. GNUNET_STRINGS_relative_time_to_string (TIMEOUT, GNUNET_YES));
  190. if (GNUNET_OK !=
  191. microphone->enable_microphone (microphone->cls,
  192. &record, NULL))
  193. {
  194. fprintf (stderr,
  195. "Failed to enable microphone\n");
  196. ret = 1;
  197. GNUNET_SCHEDULER_shutdown ();
  198. return;
  199. }
  200. }
  201. /**
  202. * The main function of our code to test microphone and speaker.
  203. *
  204. * @param argc number of arguments from the command line
  205. * @param argv command line arguments
  206. * @return 0 ok, 1 on error
  207. */
  208. int
  209. main (int argc,
  210. char *const *argv)
  211. {
  212. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  213. GNUNET_GETOPT_OPTION_END
  214. };
  215. if (GNUNET_OK !=
  216. GNUNET_STRINGS_get_utf8_args (argc, argv,
  217. &argc, &argv))
  218. return 2;
  219. ret = (GNUNET_OK ==
  220. GNUNET_PROGRAM_run (argc, argv,
  221. "gnunet-conversation-test",
  222. gettext_noop ("help text"),
  223. options,
  224. &run,
  225. NULL)) ? ret : 1;
  226. GNUNET_free ((void*) argv);
  227. return ret;
  228. }
  229. /* end of gnunet-conversation-test.c */