aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/gnunet-helper-audio-playback.c
blob: 97abd443070410cf9e4c57cccc2f0a2fda33a6de (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
     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/gnunet-helper-audio-playback.c
 * @brief constants for network protocols
 * @author Siomon Dieterle
 * @author Andreas Fuchs
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_protocols.h"
#include "conversation.h"
#include "gnunet_constants.h"
#include "gnunet_core_service.h"

#include <pulse/simple.h>
#include <pulse/error.h>
#include <pulse/rtclock.h>

#include <pulse/pulseaudio.h>
#include <opus/opus.h>
#include <opus/opus_types.h>

#define MAXLINE 4096

#define SAMPLING_RATE 48000

/**
 * Pulseaudio specification. May change in the future.
 */
static pa_sample_spec sample_spec = {
  .format = PA_SAMPLE_FLOAT32LE,
  .rate = SAMPLING_RATE,
  .channels = 1
};


/**
 * Pulseaudio mainloop api
 */
static pa_mainloop_api *mainloop_api;

/**
 * Pulseaudio threaded mainloop
 */
static pa_threaded_mainloop *m;

/**
 * Pulseaudio context
 */
static pa_context *context;

/**
 * Pulseaudio output stream
 */
static pa_stream *stream_out;

/**
 * OPUS decoder
 */
static OpusDecoder *dec;

/**
 * PCM data buffer
 */
static float *pcm_buffer;

/**
 * Length of PCM buffer
 */
static int pcm_length;

/**
 * Number of samples for one frame
 */
static int frame_size;

/**
 * Pipe we use to signal the main loop that we are ready to receive.
 */
static int ready_pipe[2];

/**
 * Message callback
 */
static int
stdin_receiver (void *cls,
		void *client,
		const struct GNUNET_MessageHeader *msg)
{
  struct AudioMessage *audio;
  int ret;

  switch (ntohs (msg->type))
  {
  case GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO:
    audio = (struct AudioMessage *) msg;

    ret = opus_decode_float (dec,
			     (const unsigned char *) &audio[1],
			     ntohs (audio->header.size) - sizeof (struct AudioMessage),
			     pcm_buffer,
			     frame_size, 0);
    if (ret < 0)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  "Opus decoding failed: %d\n",
		  ret);
      return GNUNET_OK;
    }
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		"Decoded frame with %u bytes\n",
		ntohs (audio->header.size));
    if (pa_stream_write
	(stream_out, pcm_buffer, pcm_length, NULL, 0,
	 PA_SEEK_RELATIVE) < 0)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  _("pa_stream_write() failed: %s\n"),
		  pa_strerror (pa_context_errno (context)));
      return GNUNET_OK;
    }
    break;
  default:
    break;
  }
  return GNUNET_OK;
}


/**
 * Pulseaudio shutdown task
 */
static void
quit (int ret)
{
  mainloop_api->quit (mainloop_api, ret);
  exit (ret);
}


/**
 * Callback when data is there for playback
 */
static void
stream_write_callback (pa_stream * s,
		       size_t length,
		       void *userdata)
{
  /* unblock 'main' */
  if (-1 != ready_pipe[1])
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		"Unblocking main loop!\n");
    write (ready_pipe[1], "r", 1);
  }
}


/**
 * Exit callback for SIGTERM and SIGINT
 */
static void
exit_signal_callback (pa_mainloop_api * m, pa_signal_event * e, int sig,
		      void *userdata)
{
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
	      _("gnunet-helper-audio-playback - Got signal, exiting\n"));
  quit (1);
}


/**
 * Pulseaudio stream state callback
 */
static void
context_state_callback (pa_context * c,
			void *userdata)
{
  int p;

  GNUNET_assert (NULL != c);
  switch (pa_context_get_state (c))
  {
  case PA_CONTEXT_CONNECTING:
  case PA_CONTEXT_AUTHORIZING:
  case PA_CONTEXT_SETTING_NAME:
    break;
  case PA_CONTEXT_READY:
  {
    GNUNET_assert (!stream_out);
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
		_("Connection established.\n"));
    if (!(stream_out =
	  pa_stream_new (c, "GNUNET VoIP playback", &sample_spec, NULL)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  _("pa_stream_new() failed: %s\n"),
		  pa_strerror (pa_context_errno (c)));
      goto fail;
    }
    pa_stream_set_write_callback (stream_out,
				  &stream_write_callback,
				  NULL);
    if ((p =
	 pa_stream_connect_playback (stream_out, NULL,
				     NULL,
				     PA_STREAM_ADJUST_LATENCY | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE,
				     NULL,  NULL)) < 0)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  _("pa_stream_connect_playback() failed: %s\n"),
		  pa_strerror (pa_context_errno (c)));
      goto fail;
    }
    break;
  }
  case PA_CONTEXT_TERMINATED:
    quit (0);
    break;

  case PA_CONTEXT_FAILED:
  default:
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("Connection failure: %s\n"),
		pa_strerror (pa_context_errno (c)));
    goto fail;
  }
  return;
 fail:
  quit (1);
}


/**
 * Pulseaudio initialization
 */
static void
pa_init ()
{
  int r;

  if (!pa_sample_spec_valid (&sample_spec))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
		_("Wrong Spec\n"));
  }
  /* set up threaded playback mainloop */
  if (!(m = pa_threaded_mainloop_new ()))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("pa_mainloop_new() failed.\n"));
  }
  mainloop_api = pa_threaded_mainloop_get_api (m);
  /* listen to signals */
  r = pa_signal_init (mainloop_api);
  GNUNET_assert (r == 0);
  pa_signal_new (SIGINT, exit_signal_callback, NULL);
  pa_signal_new (SIGTERM, exit_signal_callback, NULL);


  /* connect to the main pulseaudio context */
  if (!(context = pa_context_new (mainloop_api, "GNUnet VoIP")))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("pa_context_new() failed.\n"));
  }
  pa_context_set_state_callback (context, context_state_callback, NULL);

  if (pa_context_connect (context, NULL, 0, NULL) < 0)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("pa_context_connect() failed: %s\n"),
		pa_strerror (pa_context_errno (context)));
  }
  if (pa_threaded_mainloop_start (m) < 0)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("pa_mainloop_run() failed.\n"));
  }
}


/**
 * OPUS initialization
 */
static void
opus_init ()
{
  int err;
  int channels = 1;

  frame_size = SAMPLING_RATE / 50;
  pcm_length = frame_size * channels * sizeof (float);

  dec = opus_decoder_create (SAMPLING_RATE, channels, &err);
  pcm_buffer = (float *) pa_xmalloc (frame_size * channels * sizeof (float));
}


/**
 * The main function for the playback helper.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *argv[])
{
  static unsigned long long toff;

  char readbuf[MAXLINE];
  struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst;
  char c;
  ssize_t ret;

  GNUNET_assert (GNUNET_OK ==
		 GNUNET_log_setup ("gnunet-helper-audio-playback",
				   "DEBUG",
				   "/tmp/helper-audio-playback"));
  if (0 != pipe (ready_pipe))
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
    return 1;
  }
  stdin_mst = GNUNET_SERVER_mst_create (&stdin_receiver, NULL);
  opus_init ();
  pa_init ();
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
	      "Waiting for PulseAudio to be ready.\n");
  GNUNET_assert (1 == read (ready_pipe[0], &c, 1));
  close (ready_pipe[0]);
  close (ready_pipe[1]);
  ready_pipe[0] = -1;
  ready_pipe[1] = -1;
  while (1)
  {
    ret = read (0, readbuf, sizeof (readbuf));
    toff += ret;
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		"Received %d bytes of audio data (total: %llu)\n",
		(int) ret,
		toff);
    if (0 > ret)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  _("Read error from STDIN: %s\n"),
		  strerror (errno));
      break;
    }
    if (0 == ret)
      break;
    GNUNET_SERVER_mst_receive (stdin_mst, NULL,
			       readbuf, ret,
			       GNUNET_NO, GNUNET_NO);
  }
  GNUNET_SERVER_mst_destroy (stdin_mst);
  return 0;
}