-much better latency
[oweals/gnunet.git] / src / conversation / gnunet-helper-audio-record.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file conversation/gnunet-helper-audio-playback.c
22  * @brief constants for network protocols
23  * @author Siomon Dieterle
24  * @author Andreas Fuchs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_protocols.h"
30 #include "conversation.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_core_service.h"
33
34 #include <pulse/simple.h>
35 #include <pulse/error.h>
36 #include <pulse/rtclock.h>
37
38 #include <pulse/pulseaudio.h>
39 #include <opus/opus.h>
40 #include <opus/opus_types.h>
41
42 #define SAMPLING_RATE 48000
43
44
45 /**
46  * Specification for recording. May change in the future to spec negotiation.
47  */
48 static pa_sample_spec sample_spec = {
49   .format = PA_SAMPLE_FLOAT32LE,
50   .rate = SAMPLING_RATE,
51   .channels = 1
52 };
53
54 /**
55  * Pulseaudio mainloop api
56  */
57 static pa_mainloop_api *mainloop_api;
58
59 /**
60  * Pulseaudio mainloop
61  */
62 static pa_mainloop *m;
63
64 /**
65  * Pulseaudio context
66  */
67 static pa_context *context;
68
69 /**
70  * Pulseaudio recording stream
71  */
72 static pa_stream *stream_in;
73
74 /**
75  * Pulseaudio io events
76  */
77 static pa_io_event *stdio_event;
78
79 /**
80  * OPUS encoder
81  */
82 static OpusEncoder *enc;
83
84 /**
85  *
86  */
87 static unsigned char *opus_data;
88
89 /**
90  * PCM data buffer for one OPUS frame
91  */
92 static float *pcm_buffer;
93
94 /**
95  * Length of the pcm data needed for one OPUS frame 
96  */
97 static int pcm_length;
98
99 /**
100  * Number of samples for one frame
101  */
102 static int frame_size;
103
104 /**
105 * Maximum length of opus payload
106 */
107 static int max_payload_bytes = 1500;
108
109 /**
110  * Audio buffer
111  */
112 static char *transmit_buffer;
113
114 /**
115  * Length of audio buffer
116  */
117 static size_t transmit_buffer_length;
118
119 /**
120  * Read index for transmit buffer
121  */
122 static size_t transmit_buffer_index;
123
124 /**
125  * Audio message skeleton
126  */
127 static struct AudioMessage *audio_message;
128
129
130 /**
131  * Pulseaudio shutdown task
132  */
133 static void
134 quit (int ret)
135 {
136   mainloop_api->quit (mainloop_api, ret);
137   exit (ret);
138 }
139
140
141 /**
142  * Creates OPUS packets from PCM data
143  */
144 static void
145 packetizer ()
146 {
147   static unsigned long long toff;
148   char *nbuf;
149   size_t new_size;
150   const char *ptr;
151   size_t off;
152   ssize_t ret;
153   int len; // FIXME: int?
154   size_t msg_size;  
155
156   while (transmit_buffer_length >= transmit_buffer_index + pcm_length)
157   {
158     memcpy (pcm_buffer,
159             &transmit_buffer[transmit_buffer_index],
160             pcm_length);
161     transmit_buffer_index += pcm_length;
162     len =
163       opus_encode_float (enc, pcm_buffer, frame_size, opus_data,
164                          max_payload_bytes);
165     if (len > UINT16_MAX - sizeof (struct AudioMessage))
166     {
167       GNUNET_break (0);
168       len = UINT16_MAX - sizeof (struct AudioMessage);
169     }
170     msg_size = sizeof (struct AudioMessage) + len;
171     audio_message->header.size = htons ((uint16_t) msg_size);
172     memcpy (&audio_message[1], opus_data, len);
173
174     toff += msg_size;
175     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176                 "Sending %u bytes of audio data (total: %llu)\n",
177                 (unsigned int) msg_size,
178                 toff);
179     ptr = (const char *) audio_message;
180     off = 0;
181     while (off < msg_size)
182     {
183       ret = write (1, &ptr[off], msg_size - off);
184       if (0 >= ret)
185       {
186         if (-1 == ret)
187           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "write");
188         quit (2);
189       }
190       off += ret;
191     }
192   }
193
194   new_size = transmit_buffer_length - transmit_buffer_index;
195   if (0 != new_size)
196   {
197     nbuf = pa_xmalloc (new_size);
198     memmove (nbuf, 
199              &transmit_buffer[transmit_buffer_index],
200              new_size);    
201     pa_xfree (transmit_buffer);
202     transmit_buffer = nbuf;
203   }
204   else
205   {
206     pa_xfree (transmit_buffer);
207     transmit_buffer = NULL;
208   }
209   transmit_buffer_index = 0;
210   transmit_buffer_length = new_size;  
211 }
212
213
214 /**
215  * Pulseaudio callback when new data is available.
216  */
217 static void
218 stream_read_callback (pa_stream * s,
219                       size_t length, 
220                       void *userdata)
221 {
222   const void *data;
223
224   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225               "Got %u/%u bytes of PCM data\n",
226               length,
227               pcm_length);
228
229   GNUNET_assert (NULL != s);
230   GNUNET_assert (length > 0);
231   if (stdio_event)
232     mainloop_api->io_enable (stdio_event, PA_IO_EVENT_OUTPUT);
233
234   if (pa_stream_peek (s, (const void **) &data, &length) < 0)
235   {
236     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
237                 _("pa_stream_peek() failed: %s\n"),
238                 pa_strerror (pa_context_errno (context)));
239     quit (1);
240     return;
241   }
242   GNUNET_assert (NULL != data);
243   GNUNET_assert (length > 0);
244   if (NULL != transmit_buffer)
245   {
246     transmit_buffer = pa_xrealloc (transmit_buffer, 
247                                    transmit_buffer_length + length);
248     memcpy (&transmit_buffer[transmit_buffer_length], 
249             data,
250             length);
251     transmit_buffer_length += length;
252   }
253   else
254   {
255     transmit_buffer = pa_xmalloc (length);
256     memcpy (transmit_buffer, data, length);
257     transmit_buffer_length = length;
258     transmit_buffer_index = 0;
259   }
260   pa_stream_drop (s);
261   packetizer ();
262 }
263
264
265 /**
266  * Exit callback for SIGTERM and SIGINT
267  */
268 static void
269 exit_signal_callback (pa_mainloop_api * m, 
270                       pa_signal_event * e, 
271                       int sig,
272                       void *userdata)
273 {
274   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
275               _("Got signal, exiting.\n"));
276   quit (1);
277 }
278
279
280 /**
281  * Pulseaudio stream state callback
282  */
283 static void
284 stream_state_callback (pa_stream * s, void *userdata)
285 {
286   GNUNET_assert (NULL != s);
287
288   switch (pa_stream_get_state (s))
289   {
290   case PA_STREAM_CREATING:
291   case PA_STREAM_TERMINATED:
292     break;    
293   case PA_STREAM_READY:
294     {
295       const pa_buffer_attr *a;
296
297       char cmt[PA_CHANNEL_MAP_SNPRINT_MAX],
298         sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
299       
300       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
301                   _("Stream successfully created.\n"));
302       
303       if (!(a = pa_stream_get_buffer_attr (s)))
304       {
305         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
306                     _("pa_stream_get_buffer_attr() failed: %s\n"),
307                     pa_strerror (pa_context_errno
308                                  (pa_stream_get_context (s))));
309         
310       }
311       else
312       {
313         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
314                     _("Buffer metrics: maxlength=%u, fragsize=%u\n"),
315                     a->maxlength, a->fragsize);
316       }       
317       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
318                   _("Using sample spec '%s', channel map '%s'.\n"),
319                   pa_sample_spec_snprint (sst, sizeof (sst),
320                                           pa_stream_get_sample_spec (s)),
321                   pa_channel_map_snprint (cmt, sizeof (cmt),
322                                           pa_stream_get_channel_map (s)));
323       
324       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
325                   _("Connected to device %s (%u, %ssuspended).\n"),
326                   pa_stream_get_device_name (s),
327                   pa_stream_get_device_index (s),
328                   pa_stream_is_suspended (s) ? "" : "not ");
329     }    
330     break;
331   case PA_STREAM_FAILED:
332   default:
333     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
334                 _("Stream error: %s\n"),
335                 pa_strerror (pa_context_errno (pa_stream_get_context (s))));
336     quit (1);
337   }
338 }
339
340
341 /**
342  * Pulseaudio context state callback
343  */
344 static void
345 context_state_callback (pa_context * c,
346                         void *userdata)
347 {
348   GNUNET_assert (c);
349
350   switch (pa_context_get_state (c))
351   {
352   case PA_CONTEXT_CONNECTING:
353   case PA_CONTEXT_AUTHORIZING:
354   case PA_CONTEXT_SETTING_NAME:
355     break;
356   case PA_CONTEXT_READY:
357   {
358     int r;
359     pa_buffer_attr na;
360     
361     GNUNET_assert (!stream_in);    
362     GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
363                 _("Connection established.\n"));
364     if (! (stream_in =
365            pa_stream_new (c, "GNUNET_VoIP recorder", &sample_spec, NULL)))
366     {
367       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
368                   _("pa_stream_new() failed: %s\n"),
369                   pa_strerror (pa_context_errno (c)));
370       goto fail;
371     }
372     pa_stream_set_state_callback (stream_in, &stream_state_callback, NULL);
373     pa_stream_set_read_callback (stream_in, &stream_read_callback, NULL);
374     memset (&na, 0, sizeof (na));
375     na.maxlength = UINT32_MAX;
376     na.fragsize = pcm_length;
377     if ((r = pa_stream_connect_record (stream_in, NULL, &na, 
378                                        PA_STREAM_EARLY_REQUESTS)) < 0)
379     {
380       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
381                   _("pa_stream_connect_record() failed: %s\n"),
382                   pa_strerror (pa_context_errno (c)));
383       goto fail;
384     }
385     
386     break;
387   }  
388   case PA_CONTEXT_TERMINATED:
389     quit (0);
390     break;    
391   case PA_CONTEXT_FAILED:
392   default:
393     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
394                 _("Connection failure: %s\n"),
395                 pa_strerror (pa_context_errno (c)));
396     goto fail;
397   }
398   return;
399
400 fail:
401   quit (1);
402 }
403
404
405 /**
406  * Pulsaudio init
407  */
408 static void
409 pa_init ()
410 {
411   int r;
412   int i;
413
414   if (!pa_sample_spec_valid (&sample_spec))
415   {
416     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
417                 _("Wrong Spec\n"));
418   }
419   /* set up main record loop */
420   if (!(m = pa_mainloop_new ()))
421   {
422     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
423                 _("pa_mainloop_new() failed.\n"));
424   }
425   mainloop_api = pa_mainloop_get_api (m);
426
427   /* listen to signals */
428   r = pa_signal_init (mainloop_api);
429   GNUNET_assert (r == 0);
430   pa_signal_new (SIGINT, &exit_signal_callback, NULL);
431   pa_signal_new (SIGTERM, &exit_signal_callback, NULL);
432
433   /* connect to the main pulseaudio context */
434
435   if (!(context = pa_context_new (mainloop_api, "GNUNET VoIP")))
436   {
437     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
438                 _("pa_context_new() failed.\n"));
439   }  
440   pa_context_set_state_callback (context, &context_state_callback, NULL);
441   if (pa_context_connect (context, NULL, 0, NULL) < 0)
442   {
443     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
444                 _("pa_context_connect() failed: %s\n"),
445                 pa_strerror (pa_context_errno (context)));
446   }
447   if (pa_mainloop_run (m, &i) < 0)
448   {
449     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
450                 _("pa_mainloop_run() failed.\n"));
451   }
452 }
453
454
455 /**
456  * OPUS init
457  */
458 static void
459 opus_init ()
460 {
461   int channels = 1;
462   int err;
463
464   frame_size = SAMPLING_RATE / 50;
465   pcm_length = frame_size * channels * sizeof (float);
466   pcm_buffer = pa_xmalloc (pcm_length);
467   opus_data = GNUNET_malloc (max_payload_bytes);
468   enc = opus_encoder_create (SAMPLING_RATE,
469                              channels, 
470                              OPUS_APPLICATION_VOIP,
471                              &err);
472 }
473
474
475 /**
476  * The main function for the record helper.
477  *
478  * @param argc number of arguments from the command line
479  * @param argv command line arguments
480  * @return 0 ok, 1 on error
481  */
482 int
483 main (int argc, char *argv[])
484 {
485   GNUNET_assert (GNUNET_OK ==
486                  GNUNET_log_setup ("gnunet-helper-audio-record",
487                                    "DEBUG",
488                                    "/tmp/helper-audio-record"));
489   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490               "Audio source starts\n");
491   audio_message = GNUNET_malloc (UINT16_MAX);
492   audio_message->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
493   opus_init ();
494   pa_init ();
495   return 0;
496 }