summaryrefslogtreecommitdiff
path: root/src/conversation/microphone.c
blob: a4a40796f2eb5bb986591c60d0d7713c446d9998 (plain)
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
200
/*
   This file is part of GNUnet
   Copyright (C) 2013 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   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
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @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 msg the message from the helper
 * @return #GNUNET_OK on success,
 *    #GNUNET_NO to stop further processing (no error)
 *    #GNUNET_SYSERR to stop further processing with error
 */
static int
process_record_messages (void *cls,
                         const struct GNUNET_MessageHeader *msg)
{
  struct Microphone *mic = cls;
  const struct AudioMessage *am;

  if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO)
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  am = (const struct AudioMessage *) msg;
  mic->rdc (mic->rdc_cls,
            ntohs (msg->size) - sizeof(struct AudioMessage),
            &am[1]);
  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 */