2 This file is part of GNUnet
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file conversation/microphone.c
23 * @brief API to access an audio microphone; provides access to hardware microphones;
24 * actually just wraps the gnunet-helper-audio-record
25 * @author Simon Dieterle
26 * @author Andreas Fuchs
27 * @author Christian Grothoff
30 #include "gnunet_microphone_lib.h"
31 #include "conversation.h"
35 * Internal data structures for the microphone.
43 const struct GNUNET_CONFIGURATION_Handle *cfg;
46 * Handle for the record helper
48 struct GNUNET_HELPER_Handle *record_helper;
51 * Function to call with audio data (if we are enabled).
53 GNUNET_MICROPHONE_RecordedDataCallback rdc;
64 * Function to process the audio from the record helper
66 * @param cls clsoure with our `struct Microphone`
68 * @param msg the message from the helper
69 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
72 process_record_messages (void *cls,
74 const struct GNUNET_MessageHeader *msg)
76 struct Microphone *mic = cls;
78 if ( (ntohs (msg->size) != sizeof (struct AudioMessage)) ||
79 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO) )
84 mic->rdc (mic->rdc_cls,
85 sizeof (struct AudioMessage),
92 * Enable a microphone.
94 * @param cls clsoure with our `struct Microphone`
95 * @param rdc function to call with recorded data
96 * @param rdc_cls closure for @a dc
100 GNUNET_MICROPHONE_RecordedDataCallback rdc,
103 struct Microphone *mic = cls;
104 static char * const record_helper_argv[] =
106 "gnunet-helper-audio-record",
111 mic->rdc_cls = rdc_cls;
112 mic->record_helper = GNUNET_HELPER_start (GNUNET_NO,
113 "gnunet-helper-audio-record",
115 &process_record_messages,
117 if (NULL == mic->record_helper)
119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
120 _("Could not start record audio helper\n"));
121 return GNUNET_SYSERR;
128 * Function that disables a microphone.
135 struct Microphone *mic = cls;
137 if (NULL == mic->record_helper)
142 GNUNET_break (GNUNET_OK ==
143 GNUNET_HELPER_kill (mic->record_helper, GNUNET_NO));
144 GNUNET_HELPER_destroy (mic->record_helper);
145 mic->record_helper = NULL;
150 * Function to destroy a microphone.
157 struct Microphone *mic = cls;
159 if (NULL != mic->record_helper)
165 * Create a microphone that corresponds to the microphone hardware
168 * @param cfg configuration to use
169 * @return NULL on error
171 struct GNUNET_MICROPHONE_Handle *
172 GNUNET_MICROPHONE_create_from_hardware (const struct GNUNET_CONFIGURATION_Handle *cfg)
174 struct GNUNET_MICROPHONE_Handle *microphone;
175 struct Microphone *mic;
177 mic = GNUNET_new (struct Microphone);
179 microphone = GNUNET_new (struct GNUNET_MICROPHONE_Handle);
180 microphone->cls = mic;
181 microphone->enable_microphone = &enable;
182 microphone->disable_microphone = &disable;
183 microphone->destroy_microphone = &destroy;
189 * Destroy a microphone.
191 * @param microphone microphone to destroy
194 GNUNET_MICROPHONE_destroy (struct GNUNET_MICROPHONE_Handle *microphone)
196 microphone->destroy_microphone (microphone->cls);
197 GNUNET_free (microphone);
200 /* end of microphone.c */