GNUnet  0.10.x
microphone.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2013 GNUnet e.V.
4 
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.
9 
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.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
29 #include "platform.h"
30 #include "gnunet_microphone_lib.h"
31 #include "conversation.h"
32 
33 
37 struct Microphone {
42 
47 
52 
56  void *rdc_cls;
57 };
58 
59 
69 static int
71  const struct GNUNET_MessageHeader *msg)
72 {
73  struct Microphone *mic = cls;
74  const struct AudioMessage *am;
75 
77  {
78  GNUNET_break(0);
79  return GNUNET_SYSERR;
80  }
81  am = (const struct AudioMessage *)msg;
82  mic->rdc(mic->rdc_cls,
83  ntohs(msg->size) - sizeof(struct AudioMessage),
84  &am[1]);
85  return GNUNET_OK;
86 }
87 
88 
96 static int
97 enable(void *cls,
99  void *rdc_cls)
100 {
101  struct Microphone *mic = cls;
102  static char * const record_helper_argv[] =
103  {
104  "gnunet-helper-audio-record",
105  NULL
106  };
107 
108  mic->rdc = rdc;
109  mic->rdc_cls = rdc_cls;
111  "gnunet-helper-audio-record",
112  record_helper_argv,
114  NULL, mic);
115  if (NULL == mic->record_helper)
116  {
118  _("Could not start record audio helper\n"));
119  return GNUNET_SYSERR;
120  }
121  return GNUNET_OK;
122 }
123 
124 
130 static void
131 disable(void *cls)
132 {
133  struct Microphone *mic = cls;
134 
135  if (NULL == mic->record_helper)
136  {
137  GNUNET_break(0);
138  return;
139  }
143  mic->record_helper = NULL;
144 }
145 
146 
152 static void
153 destroy(void *cls)
154 {
155  struct Microphone *mic = cls;
156 
157  if (NULL != mic->record_helper)
158  disable(mic);
159 }
160 
161 
171 {
173  struct Microphone *mic;
174 
175  mic = GNUNET_new(struct Microphone);
176  mic->cfg = cfg;
177  microphone = GNUNET_new(struct GNUNET_MICROPHONE_Handle);
178  microphone->cls = mic;
179  microphone->enable_microphone = &enable;
180  microphone->disable_microphone = &disable;
181  microphone->destroy_microphone = &destroy;
182  return microphone;
183 }
184 
185 
191 void
193 {
194  microphone->destroy_microphone(microphone->cls);
195  GNUNET_free(microphone);
196 }
197 
198 /* end of microphone.c */
int GNUNET_HELPER_kill(struct GNUNET_HELPER_Handle *h, int soft_kill)
Sends termination signal to the helper process.
Definition: helper.c:175
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static int enable(void *cls, GNUNET_MICROPHONE_RecordedDataCallback rdc, void *rdc_cls)
Enable a microphone.
Definition: microphone.c:97
The handle to a helper process.
Definition: helper.c:77
struct GNUNET_HELPER_Handle * GNUNET_HELPER_start(int with_control_pipe, const char *binary_name, char *const binary_argv[], GNUNET_MessageTokenizerCallback cb, GNUNET_HELPER_ExceptionCallback exp_cb, void *cb_cls)
Starts a helper and begins reading from it.
Definition: helper.c:487
constants for network protocols
GNUNET_MICROPHONE_DisableCallback disable_microphone
Turn the microphone off.
void(* GNUNET_MICROPHONE_RecordedDataCallback)(void *cls, size_t data_size, const void *data)
Process recorded audio data.
GNUNET_MICROPHONE_EnableCallback enable_microphone
Turn on the microphone.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
static void destroy(void *cls)
Function to destroy a microphone.
Definition: microphone.c:153
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static int process_record_messages(void *cls, const struct GNUNET_MessageHeader *msg)
Function to process the audio from the record helper.
Definition: microphone.c:70
static struct GNUNET_MICROPHONE_Handle * mic
Our microphone.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
GNUNET_MICROPHONE_DestroyCallback destroy_microphone
Destroy the microphone.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
Message to transmit the audio (between client and helpers).
Definition: conversation.h:55
void * cls
Closure for the callbacks.
struct GNUNET_HELPER_Handle * record_helper
Handle for the record helper.
Definition: microphone.c:46
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static void disable(void *cls)
Function that disables a microphone.
Definition: microphone.c:131
void GNUNET_MICROPHONE_destroy(struct GNUNET_MICROPHONE_Handle *microphone)
Destroy a microphone.
Definition: microphone.c:192
void GNUNET_HELPER_destroy(struct GNUNET_HELPER_Handle *h)
Free&#39;s the resources occupied by the helper handle.
Definition: helper.c:526
Internal data structures for the microphone.
Definition: microphone.c:37
configuration data
Definition: configuration.c:83
#define GNUNET_log(kind,...)
#define GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO
Message to transmit the audio between helper and speaker/microphone library.
Header for all communications.
void * rdc_cls
Closure for rdc.
Definition: microphone.c:56
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: microphone.c:41
A microphone is a device that can capture or otherwise produce audio data.
struct GNUNET_MICROPHONE_Handle * GNUNET_MICROPHONE_create_from_hardware(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a microphone that corresponds to the microphone hardware of our system.
Definition: microphone.c:170
GNUNET_MICROPHONE_RecordedDataCallback rdc
Function to call with audio data (if we are enabled).
Definition: microphone.c:51
static struct GNUNET_MICROPHONE_Handle * microphone
Handle to the microphone.
#define GNUNET_free(ptr)
Wrapper around free.