microphone.c 4.5 KB

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