gnunet-conversation-test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. _ (
  123. "\new are now playing your recording back. If you can hear it, your audio settings are working..."));
  124. for (struct Recording *rec = rec_head; NULL != rec; rec = rec->next)
  125. {
  126. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  127. "Replaying %u bytes\n",
  128. (unsigned int) rec->size);
  129. speaker->play (speaker->cls,
  130. rec->size,
  131. &rec[1]);
  132. }
  133. GNUNET_SCHEDULER_cancel (st);
  134. st = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  135. &do_shutdown,
  136. NULL);
  137. }
  138. /**
  139. * Process recorded audio data.
  140. *
  141. * @param cls clsoure
  142. * @param data_size number of bytes in @a data
  143. * @param data audio data to play
  144. */
  145. static void
  146. record (void *cls,
  147. size_t data_size,
  148. const void *data)
  149. {
  150. struct Recording *rec;
  151. (void) cls;
  152. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  153. "Recorded %u bytes\n",
  154. (unsigned int) data_size);
  155. rec = GNUNET_malloc (sizeof(struct Recording) + data_size);
  156. rec->size = data_size;
  157. GNUNET_memcpy (&rec[1], data, data_size);
  158. GNUNET_CONTAINER_DLL_insert_tail (rec_head,
  159. rec_tail,
  160. rec);
  161. }
  162. /**
  163. * Main function that will be run by the scheduler.
  164. *
  165. * @param cls closure
  166. * @param args remaining command-line arguments
  167. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  168. * @param cfg configuration
  169. */
  170. static void
  171. run (void *cls,
  172. char *const *args,
  173. const char *cfgfile,
  174. const struct GNUNET_CONFIGURATION_Handle *cfg)
  175. {
  176. (void) cls;
  177. (void) args;
  178. (void) cfgfile;
  179. microphone = GNUNET_MICROPHONE_create_from_hardware (cfg);
  180. GNUNET_assert (NULL != microphone);
  181. speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
  182. GNUNET_assert (NULL != speaker);
  183. switch_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  184. &switch_to_speaker,
  185. NULL);
  186. st = GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  187. NULL);
  188. fprintf (stderr,
  189. _ (
  190. "We will now be recording you for %s. After that time, the recording will be played back to you..."),
  191. GNUNET_STRINGS_relative_time_to_string (TIMEOUT, GNUNET_YES));
  192. if (GNUNET_OK !=
  193. microphone->enable_microphone (microphone->cls,
  194. &record, NULL))
  195. {
  196. fprintf (stderr,
  197. "Failed to enable microphone\n");
  198. ret = 1;
  199. GNUNET_SCHEDULER_shutdown ();
  200. return;
  201. }
  202. }
  203. /**
  204. * The main function of our code to test microphone and speaker.
  205. *
  206. * @param argc number of arguments from the command line
  207. * @param argv command line arguments
  208. * @return 0 ok, 1 on error
  209. */
  210. int
  211. main (int argc,
  212. char *const *argv)
  213. {
  214. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  215. GNUNET_GETOPT_OPTION_END
  216. };
  217. if (GNUNET_OK !=
  218. GNUNET_STRINGS_get_utf8_args (argc, argv,
  219. &argc, &argv))
  220. return 2;
  221. ret = (GNUNET_OK ==
  222. GNUNET_PROGRAM_run (argc, argv,
  223. "gnunet-conversation-test",
  224. gettext_noop ("help text"),
  225. options,
  226. &run,
  227. NULL)) ? ret : 1;
  228. GNUNET_free_nz ((void *) argv);
  229. return ret;
  230. }
  231. /* end of gnunet-conversation-test.c */