aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/microphone.c
blob: f42f571c8671c75c2d6a5c98aed4079fe65fc106 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
  This file is part of GNUnet
  (C) 2013 Christian Grothoff (and other contributing authors)
  
  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 3, or (at your
  option) any later version.
  
  GNUnet is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with GNUnet; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  Boston, MA 02111-1307, USA.
 */

/**
 * @file conversation/microphone.c
 * @brief API to access an audio microphone; provides access to hardware microphones;
 *        actually just wraps the gnunet-helper-audio-record
 * @author Simon Dieterle
 * @author Andreas Fuchs
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_microphone_lib.h"
#include "conversation.h"


/**
 * Internal data structures for the microphone.
 */
struct Microphone
{

  /**
   * Our configuration.
   */
  const struct GNUNET_CONFIGURATION_Handle *cfg;

  /**
   * Handle for the record helper
   */
  struct GNUNET_HELPER_Handle *record_helper;

  /**
   * Function to call with audio data (if we are enabled).
   */
  GNUNET_MICROPHONE_RecordedDataCallback rdc;

  /**
   * Closure for @e rdc.
   */
  void *rdc_cls;

};


/**
 * Function to process the audio from the record helper
 *
 * @param cls clsoure with our `struct Microphone`
 * @param client NULL
 * @param msg the message from the helper
 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
 */
static int
process_record_messages (void *cls,
			 void *client,
			 const struct GNUNET_MessageHeader *msg)
{
  struct Microphone *mic = cls;

  if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO)
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  mic->rdc (mic->rdc_cls,
	    ntohs (msg->size),
	    (const char *) msg);
  return GNUNET_OK;
}


/**
 * Enable a microphone.
 *
 * @param cls clsoure with our `struct Microphone`
 * @param rdc function to call with recorded data
 * @param rdc_cls closure for @a dc
 */
static int
enable (void *cls,
	GNUNET_MICROPHONE_RecordedDataCallback rdc,
	void *rdc_cls)
{
  struct Microphone *mic = cls;  
  static char * const record_helper_argv[] = 
  {
    "gnunet-helper-audio-record",
    NULL
  };

  mic->rdc = rdc;
  mic->rdc_cls = rdc_cls;
  mic->record_helper = GNUNET_HELPER_start (GNUNET_NO,
					    "gnunet-helper-audio-record",
					    record_helper_argv,
					    &process_record_messages, 
					    NULL, mic);  
  if (NULL == mic->record_helper)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("Could not start record audio helper\n"));
    return GNUNET_SYSERR;
  }  
  return GNUNET_OK;
}


/**
 * Function that disables a microphone.
 *
 * @param cls clsoure
 */
static void
disable (void *cls)
{
  struct Microphone *mic = cls;

  if (NULL == mic->record_helper)
  {
    GNUNET_break (0);
    return;
  }
  GNUNET_break (GNUNET_OK ==
		GNUNET_HELPER_kill (mic->record_helper, GNUNET_NO));
  GNUNET_HELPER_destroy (mic->record_helper);
  mic->record_helper = NULL;
}


/**
 * Function to destroy a microphone.
 *
 * @param cls clsoure
 */
static void
destroy (void *cls)
{
  struct Microphone *mic = cls;

  if (NULL != mic->record_helper)
    disable (mic);
}


/**
 * Create a microphone that corresponds to the microphone hardware
 * of our system.
 *
 * @param cfg configuration to use
 * @return NULL on error
 */
struct GNUNET_MICROPHONE_Handle *
GNUNET_MICROPHONE_create_from_hardware (const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_MICROPHONE_Handle *microphone;
  struct Microphone *mic;

  mic = GNUNET_new (struct Microphone);
  mic->cfg = cfg;
  microphone = GNUNET_new (struct GNUNET_MICROPHONE_Handle);
  microphone->cls = mic;
  microphone->enable_microphone = &enable;
  microphone->disable_microphone = &disable;
  microphone->destroy_microphone = &destroy;
  return microphone;
}


/**
 * Destroy a microphone.
 *
 * @param microphone microphone to destroy
 */
void
GNUNET_MICROPHONE_destroy (struct GNUNET_MICROPHONE_Handle *microphone)
{
  microphone->destroy_microphone (microphone->cls);
  GNUNET_free (microphone);
}

/* end of microphone.c */