microphone.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. This file is part of GNUnet
  3. (C) 2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file conversation/microphone.c
  19. * @brief API to access an audio microphone; provides access to hardware microphones;
  20. * actually just wraps the gnunet-helper-audio-record
  21. * @author Simon Dieterle
  22. * @author Andreas Fuchs
  23. * @author Christian Grothoff
  24. */
  25. #include "platform.h"
  26. #include "gnunet_microphone_lib.h"
  27. #include "conversation.h"
  28. /**
  29. * Internal data structures for the microphone.
  30. */
  31. struct Microphone
  32. {
  33. /**
  34. * Our configuration.
  35. */
  36. const struct GNUNET_CONFIGURATION_Handle *cfg;
  37. /**
  38. * Handle for the record helper
  39. */
  40. struct GNUNET_HELPER_Handle *record_helper;
  41. /**
  42. * Function to call with audio data (if we are enabled).
  43. */
  44. GNUNET_MICROPHONE_RecordedDataCallback rdc;
  45. /**
  46. * Closure for @e rdc.
  47. */
  48. void *rdc_cls;
  49. };
  50. /**
  51. * Function to process the audio from the record helper
  52. *
  53. * @param cls clsoure with our `struct Microphone`
  54. * @param client NULL
  55. * @param msg the message from the helper
  56. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  57. */
  58. static int
  59. process_record_messages (void *cls,
  60. void *client,
  61. const struct GNUNET_MessageHeader *msg)
  62. {
  63. struct Microphone *mic = cls;
  64. const struct AudioMessage *am;
  65. if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO)
  66. {
  67. GNUNET_break (0);
  68. return GNUNET_SYSERR;
  69. }
  70. am = (const struct AudioMessage *) msg;
  71. mic->rdc (mic->rdc_cls,
  72. ntohs (msg->size) - sizeof (struct AudioMessage),
  73. &am[1]);
  74. return GNUNET_OK;
  75. }
  76. /**
  77. * Enable a microphone.
  78. *
  79. * @param cls clsoure with our `struct Microphone`
  80. * @param rdc function to call with recorded data
  81. * @param rdc_cls closure for @a dc
  82. */
  83. static int
  84. enable (void *cls,
  85. GNUNET_MICROPHONE_RecordedDataCallback rdc,
  86. void *rdc_cls)
  87. {
  88. struct Microphone *mic = cls;
  89. static char * const record_helper_argv[] =
  90. {
  91. "gnunet-helper-audio-record",
  92. NULL
  93. };
  94. mic->rdc = rdc;
  95. mic->rdc_cls = rdc_cls;
  96. mic->record_helper = GNUNET_HELPER_start (GNUNET_NO,
  97. "gnunet-helper-audio-record",
  98. record_helper_argv,
  99. &process_record_messages,
  100. NULL, mic);
  101. if (NULL == mic->record_helper)
  102. {
  103. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  104. _("Could not start record audio helper\n"));
  105. return GNUNET_SYSERR;
  106. }
  107. return GNUNET_OK;
  108. }
  109. /**
  110. * Function that disables a microphone.
  111. *
  112. * @param cls clsoure
  113. */
  114. static void
  115. disable (void *cls)
  116. {
  117. struct Microphone *mic = cls;
  118. if (NULL == mic->record_helper)
  119. {
  120. GNUNET_break (0);
  121. return;
  122. }
  123. GNUNET_break (GNUNET_OK ==
  124. GNUNET_HELPER_kill (mic->record_helper, GNUNET_NO));
  125. GNUNET_HELPER_destroy (mic->record_helper);
  126. mic->record_helper = NULL;
  127. }
  128. /**
  129. * Function to destroy a microphone.
  130. *
  131. * @param cls clsoure
  132. */
  133. static void
  134. destroy (void *cls)
  135. {
  136. struct Microphone *mic = cls;
  137. if (NULL != mic->record_helper)
  138. disable (mic);
  139. }
  140. /**
  141. * Create a microphone that corresponds to the microphone hardware
  142. * of our system.
  143. *
  144. * @param cfg configuration to use
  145. * @return NULL on error
  146. */
  147. struct GNUNET_MICROPHONE_Handle *
  148. GNUNET_MICROPHONE_create_from_hardware (const struct GNUNET_CONFIGURATION_Handle *cfg)
  149. {
  150. struct GNUNET_MICROPHONE_Handle *microphone;
  151. struct Microphone *mic;
  152. mic = GNUNET_new (struct Microphone);
  153. mic->cfg = cfg;
  154. microphone = GNUNET_new (struct GNUNET_MICROPHONE_Handle);
  155. microphone->cls = mic;
  156. microphone->enable_microphone = &enable;
  157. microphone->disable_microphone = &disable;
  158. microphone->destroy_microphone = &destroy;
  159. return microphone;
  160. }
  161. /**
  162. * Destroy a microphone.
  163. *
  164. * @param microphone microphone to destroy
  165. */
  166. void
  167. GNUNET_MICROPHONE_destroy (struct GNUNET_MICROPHONE_Handle *microphone)
  168. {
  169. microphone->destroy_microphone (microphone->cls);
  170. GNUNET_free (microphone);
  171. }
  172. /* end of microphone.c */