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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
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
25 #include "gnunet_microphone_lib.h"
26 #include "conversation.h"
30 * Internal data structures for the microphone.
38 const struct GNUNET_CONFIGURATION_Handle *cfg;
41 * Handle for the record helper
43 struct GNUNET_HELPER_Handle *record_helper;
46 * Function to call with audio data (if we are enabled).
48 GNUNET_MICROPHONE_RecordedDataCallback rdc;
59 * Function to process the audio from the record helper
61 * @param cls clsoure with our `struct Microphone`
62 * @param msg the message from the helper
63 * @return #GNUNET_OK on success,
64 * #GNUNET_NO to stop further processing (no error)
65 * #GNUNET_SYSERR to stop further processing with error
68 process_record_messages (void *cls,
69 const struct GNUNET_MessageHeader *msg)
71 struct Microphone *mic = cls;
72 const struct AudioMessage *am;
74 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO)
79 am = (const struct AudioMessage *) msg;
80 mic->rdc (mic->rdc_cls,
81 ntohs (msg->size) - sizeof (struct AudioMessage),
88 * Enable a microphone.
90 * @param cls clsoure with our `struct Microphone`
91 * @param rdc function to call with recorded data
92 * @param rdc_cls closure for @a dc
96 GNUNET_MICROPHONE_RecordedDataCallback rdc,
99 struct Microphone *mic = cls;
100 static char * const record_helper_argv[] =
102 "gnunet-helper-audio-record",
107 mic->rdc_cls = rdc_cls;
108 mic->record_helper = GNUNET_HELPER_start (GNUNET_NO,
109 "gnunet-helper-audio-record",
111 &process_record_messages,
113 if (NULL == mic->record_helper)
115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 _("Could not start record audio helper\n"));
117 return GNUNET_SYSERR;
124 * Function that disables a microphone.
131 struct Microphone *mic = cls;
133 if (NULL == mic->record_helper)
138 GNUNET_break (GNUNET_OK ==
139 GNUNET_HELPER_kill (mic->record_helper, GNUNET_NO));
140 GNUNET_HELPER_destroy (mic->record_helper);
141 mic->record_helper = NULL;
146 * Function to destroy a microphone.
153 struct Microphone *mic = cls;
155 if (NULL != mic->record_helper)
161 * Create a microphone that corresponds to the microphone hardware
164 * @param cfg configuration to use
165 * @return NULL on error
167 struct GNUNET_MICROPHONE_Handle *
168 GNUNET_MICROPHONE_create_from_hardware (const struct GNUNET_CONFIGURATION_Handle *cfg)
170 struct GNUNET_MICROPHONE_Handle *microphone;
171 struct Microphone *mic;
173 mic = GNUNET_new (struct Microphone);
175 microphone = GNUNET_new (struct GNUNET_MICROPHONE_Handle);
176 microphone->cls = mic;
177 microphone->enable_microphone = &enable;
178 microphone->disable_microphone = &disable;
179 microphone->destroy_microphone = &destroy;
185 * Destroy a microphone.
187 * @param microphone microphone to destroy
190 GNUNET_MICROPHONE_destroy (struct GNUNET_MICROPHONE_Handle *microphone)
192 microphone->destroy_microphone (microphone->cls);
193 GNUNET_free (microphone);
196 /* end of microphone.c */