2 This file is part of GNUnet
3 Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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`
67 * @param msg the message from the helper
68 * @return #GNUNET_OK on success,
69 * #GNUNET_NO to stop further processing (no error)
70 * #GNUNET_SYSERR to stop further processing with error
73 process_record_messages (void *cls,
74 const struct GNUNET_MessageHeader *msg)
76 struct Microphone *mic = cls;
77 const struct AudioMessage *am;
79 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO)
84 am = (const struct AudioMessage *) msg;
85 mic->rdc (mic->rdc_cls,
86 ntohs (msg->size) - sizeof (struct AudioMessage),
93 * Enable a microphone.
95 * @param cls clsoure with our `struct Microphone`
96 * @param rdc function to call with recorded data
97 * @param rdc_cls closure for @a dc
101 GNUNET_MICROPHONE_RecordedDataCallback rdc,
104 struct Microphone *mic = cls;
105 static char * const record_helper_argv[] =
107 "gnunet-helper-audio-record",
112 mic->rdc_cls = rdc_cls;
113 mic->record_helper = GNUNET_HELPER_start (GNUNET_NO,
114 "gnunet-helper-audio-record",
116 &process_record_messages,
118 if (NULL == mic->record_helper)
120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
121 _("Could not start record audio helper\n"));
122 return GNUNET_SYSERR;
129 * Function that disables a microphone.
136 struct Microphone *mic = cls;
138 if (NULL == mic->record_helper)
143 GNUNET_break (GNUNET_OK ==
144 GNUNET_HELPER_kill (mic->record_helper, GNUNET_NO));
145 GNUNET_HELPER_destroy (mic->record_helper);
146 mic->record_helper = NULL;
151 * Function to destroy a microphone.
158 struct Microphone *mic = cls;
160 if (NULL != mic->record_helper)
166 * Create a microphone that corresponds to the microphone hardware
169 * @param cfg configuration to use
170 * @return NULL on error
172 struct GNUNET_MICROPHONE_Handle *
173 GNUNET_MICROPHONE_create_from_hardware (const struct GNUNET_CONFIGURATION_Handle *cfg)
175 struct GNUNET_MICROPHONE_Handle *microphone;
176 struct Microphone *mic;
178 mic = GNUNET_new (struct Microphone);
180 microphone = GNUNET_new (struct GNUNET_MICROPHONE_Handle);
181 microphone->cls = mic;
182 microphone->enable_microphone = &enable;
183 microphone->disable_microphone = &disable;
184 microphone->destroy_microphone = &destroy;
190 * Destroy a microphone.
192 * @param microphone microphone to destroy
195 GNUNET_MICROPHONE_destroy (struct GNUNET_MICROPHONE_Handle *microphone)
197 microphone->destroy_microphone (microphone->cls);
198 GNUNET_free (microphone);
201 /* end of microphone.c */