2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
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.
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.
16 * @file conversation/gnunet-helper-audio-record.c
17 * @brief program to record audio data from the microphone
18 * @author Siomon Dieterle
19 * @author Andreas Fuchs
20 * @author Christian Grothoff
23 #include "gnunet_util_lib.h"
24 #include "gnunet_protocols.h"
25 #include "conversation.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_core_service.h"
29 #include <pulse/simple.h>
30 #include <pulse/error.h>
31 #include <pulse/rtclock.h>
33 #include <pulse/pulseaudio.h>
34 #include <opus/opus.h>
35 #include <opus/opus_types.h>
38 #define DEBUG_RECORD_PURE_OGG 1
43 #define SAMPLING_RATE 48000
46 * How many ms of audio to buffer before encoding them.
48 * 60, 40, 20, 10, 5, 2.5
50 #define FRAME_SIZE_MS 40
53 * How many samples to buffer before encoding them.
55 #define FRAME_SIZE (SAMPLING_RATE / 1000 * FRAME_SIZE_MS)
58 * Pages are commited when their size goes over this value.
59 * Note that in practice we flush pages VERY often (every frame),
60 * which means that pages NEVER really get to be this big.
61 * With one-packet-per-page, pages are roughly 100-300 bytes each.
63 * This value is chosen to make MAX_PAYLOAD_BYTES=1024 fit
66 #define PAGE_WATERLINE 800
69 * Maximum length of opus payload
71 #define MAX_PAYLOAD_BYTES 1024
79 * Configures the encoder's expected packet loss percentage.
81 * Higher values will trigger progressively more loss resistant behavior
82 * in the encoder at the expense of quality at a given bitrate
83 * in the lossless case, but greater quality under loss.
85 #define CONV_OPUS_PACKET_LOSS_PERCENTAGE 1
88 * Configures the encoder's computational complexity.
90 * The supported range is 0-10 inclusive with 10 representing
91 * the highest complexity.
93 #define CONV_OPUS_ENCODING_COMPLEXITY 10
96 * Configures the encoder's use of inband forward error correction (FEC).
98 * Note: This is only applicable to the LPC layer.
100 #define CONV_OPUS_INBAND_FEC 1
103 * Configures the type of signal being encoded.
105 * This is a hint which helps the encoder's mode selection.
108 * OPUS_AUTO - (default) Encoder detects the type automatically.
109 * OPUS_SIGNAL_VOICE - Bias thresholds towards choosing LPC or Hybrid modes.
110 * OPUS_SIGNAL_MUSIC - Bias thresholds towards choosing MDCT modes.
112 #define CONV_OPUS_SIGNAL OPUS_SIGNAL_VOICE
118 * OPUS_APPLICATION_VOIP - gives best quality at a given bitrate for voice
119 * signals. It enhances the input signal by high-pass filtering and
120 * emphasizing formants and harmonics. Optionally it includes in-band forward
121 * error correction to protect against packet loss. Use this mode for typical
122 * VoIP applications. Because of the enhancement, even at high bitrates
123 * the output may sound different from the input.
124 * OPUS_APPLICATION_AUDIO - gives best quality at a given bitrate for most
125 * non-voice signals like music. Use this mode for music and mixed
126 * (music/voice) content, broadcast, and applications requiring less than
127 * 15 ms of coding delay.
128 * OPUS_APPLICATION_RESTRICTED_LOWDELAY - configures low-delay mode that
129 * disables the speech-optimized mode in exchange for slightly reduced delay.
130 * This mode can only be set on an newly initialized or freshly reset encoder
131 * because it changes the codec delay.
133 #define CONV_OPUS_APP_TYPE OPUS_APPLICATION_VOIP
136 * Specification for recording. May change in the future to spec negotiation.
138 static pa_sample_spec sample_spec = {
139 .format = PA_SAMPLE_FLOAT32LE,
140 .rate = SAMPLING_RATE,
144 GNUNET_NETWORK_STRUCT_BEGIN
146 /* OggOpus spec says the numbers must be in little-endian order */
147 struct OpusHeadPacket
152 uint16_t preskip GNUNET_PACKED;
153 uint32_t sampling_rate GNUNET_PACKED;
154 uint16_t gain GNUNET_PACKED;
155 uint8_t channel_mapping;
158 struct OpusCommentsPacket
161 uint32_t vendor_length;
163 char vendor[vendor_length];
164 uint32_t string_count;
165 followed by @a string_count pairs of:
166 uint32_t string_length;
167 char string[string_length];
171 GNUNET_NETWORK_STRUCT_END
174 * Pulseaudio mainloop api
176 static pa_mainloop_api *mainloop_api;
179 * Pulseaudio mainloop
181 static pa_mainloop *m;
186 static pa_context *context;
189 * Pulseaudio recording stream
191 static pa_stream *stream_in;
194 * Pulseaudio io events
196 static pa_io_event *stdio_event;
201 static OpusEncoder *enc;
204 * Buffer for encoded data
206 static unsigned char *opus_data;
209 * PCM data buffer for one OPUS frame
211 static float *pcm_buffer;
214 * Length of the pcm data needed for one OPUS frame
216 static int pcm_length;
221 static char *transmit_buffer;
224 * Length of audio buffer
226 static size_t transmit_buffer_length;
229 * Read index for transmit buffer
231 static size_t transmit_buffer_index;
234 * Audio message skeleton
236 static struct AudioMessage *audio_message;
241 static ogg_stream_state os;
246 static int32_t packet_id;
249 * Ogg granule for current packet
251 static int64_t enc_granulepos;
253 #ifdef DEBUG_RECORD_PURE_OGG
255 * 1 to not to write GNUnet message headers,
256 * producing pure playable ogg output
258 static int dump_pure_ogg;
262 * Pulseaudio shutdown task
267 mainloop_api->quit (mainloop_api,
274 write_data (const char *ptr,
280 while (off < msg_size)
282 ret = write (STDOUT_FILENO,
288 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
298 write_page (ogg_page *og)
300 static unsigned long long toff;
302 msg_size = sizeof (struct AudioMessage) + og->header_len + og->body_len;
303 audio_message->header.size = htons ((uint16_t) msg_size);
304 GNUNET_memcpy (&audio_message[1], og->header, og->header_len);
305 GNUNET_memcpy (((char *) &audio_message[1]) + og->header_len, og->body, og->body_len);
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309 "Sending %u bytes of audio data (total: %llu)\n",
310 (unsigned int) msg_size,
312 #ifdef DEBUG_RECORD_PURE_OGG
314 write_data ((const char *) &audio_message[1],
315 og->header_len + og->body_len);
318 write_data ((const char *) audio_message,
324 * Creates OPUS packets from PCM data
335 while (transmit_buffer_length >= transmit_buffer_index + pcm_length)
337 GNUNET_memcpy (pcm_buffer,
338 &transmit_buffer[transmit_buffer_index],
340 transmit_buffer_index += pcm_length;
342 opus_encode_float (enc, pcm_buffer, FRAME_SIZE, opus_data,
347 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
348 _("opus_encode_float() failed: %s. Aborting\n"),
349 opus_strerror (len));
352 if (((uint32_t)len) > UINT16_MAX - sizeof (struct AudioMessage))
358 /* As per OggOpus spec, granule is calculated as if the audio
359 had 48kHz sampling rate. */
360 enc_granulepos += FRAME_SIZE * 48000 / SAMPLING_RATE;
362 op.packet = (unsigned char *) opus_data;
366 op.granulepos = enc_granulepos;
367 op.packetno = packet_id++;
368 ogg_stream_packetin (&os, &op);
370 while (ogg_stream_flush_fill (&os, &og, PAGE_WATERLINE))
372 if ( ((unsigned long long) og.header_len) +
373 ((unsigned long long) og.body_len) >
374 UINT16_MAX - sizeof (struct AudioMessage))
383 new_size = transmit_buffer_length - transmit_buffer_index;
386 nbuf = pa_xmalloc (new_size);
388 &transmit_buffer[transmit_buffer_index],
390 pa_xfree (transmit_buffer);
391 transmit_buffer = nbuf;
395 pa_xfree (transmit_buffer);
396 transmit_buffer = NULL;
398 transmit_buffer_index = 0;
399 transmit_buffer_length = new_size;
404 * Pulseaudio callback when new data is available.
407 stream_read_callback (pa_stream * s,
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Got %u/%d bytes of PCM data\n",
416 (unsigned int) length,
419 GNUNET_assert (NULL != s);
420 GNUNET_assert (length > 0);
422 mainloop_api->io_enable (stdio_event, PA_IO_EVENT_OUTPUT);
424 if (pa_stream_peek (s, (const void **) &data, &length) < 0)
426 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
427 _("pa_stream_peek() failed: %s\n"),
428 pa_strerror (pa_context_errno (context)));
432 GNUNET_assert (NULL != data);
433 GNUNET_assert (length > 0);
434 if (NULL != transmit_buffer)
436 transmit_buffer = pa_xrealloc (transmit_buffer,
437 transmit_buffer_length + length);
438 GNUNET_memcpy (&transmit_buffer[transmit_buffer_length],
441 transmit_buffer_length += length;
445 transmit_buffer = pa_xmalloc (length);
446 GNUNET_memcpy (transmit_buffer, data, length);
447 transmit_buffer_length = length;
448 transmit_buffer_index = 0;
456 * Exit callback for SIGTERM and SIGINT
459 exit_signal_callback (pa_mainloop_api * m,
468 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
469 _("Got signal, exiting.\n"));
475 * Pulseaudio stream state callback
478 stream_state_callback (pa_stream * s,
482 GNUNET_assert (NULL != s);
483 switch (pa_stream_get_state (s))
485 case PA_STREAM_CREATING:
486 case PA_STREAM_TERMINATED:
488 case PA_STREAM_READY:
490 const pa_buffer_attr *a;
491 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX];
492 char sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
494 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
495 _("Stream successfully created.\n"));
497 if (!(a = pa_stream_get_buffer_attr (s)))
499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500 _("pa_stream_get_buffer_attr() failed: %s\n"),
501 pa_strerror (pa_context_errno
502 (pa_stream_get_context (s))));
507 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
508 _("Buffer metrics: maxlength=%u, fragsize=%u\n"),
509 a->maxlength, a->fragsize);
511 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
512 _("Using sample spec '%s', channel map '%s'.\n"),
513 pa_sample_spec_snprint (sst, sizeof (sst),
514 pa_stream_get_sample_spec (s)),
515 pa_channel_map_snprint (cmt, sizeof (cmt),
516 pa_stream_get_channel_map (s)));
518 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
519 _("Connected to device %s (%u, %ssuspended).\n"),
520 pa_stream_get_device_name (s),
521 pa_stream_get_device_index (s),
522 pa_stream_is_suspended (s) ? "" : "not ");
525 case PA_STREAM_FAILED:
527 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
528 _("Stream error: %s\n"),
529 pa_strerror (pa_context_errno (pa_stream_get_context (s))));
536 * Pulseaudio context state callback
539 context_state_callback (pa_context * c,
545 switch (pa_context_get_state (c))
547 case PA_CONTEXT_CONNECTING:
548 case PA_CONTEXT_AUTHORIZING:
549 case PA_CONTEXT_SETTING_NAME:
551 case PA_CONTEXT_READY:
556 GNUNET_assert (!stream_in);
557 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
558 _("Connection established.\n"));
560 pa_stream_new (c, "GNUNET_VoIP recorder", &sample_spec, NULL)))
562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
563 _("pa_stream_new() failed: %s\n"),
564 pa_strerror (pa_context_errno (c)));
567 pa_stream_set_state_callback (stream_in, &stream_state_callback, NULL);
568 pa_stream_set_read_callback (stream_in, &stream_read_callback, NULL);
569 memset (&na, 0, sizeof (na));
570 na.maxlength = UINT32_MAX;
571 na.fragsize = pcm_length;
572 if ((r = pa_stream_connect_record (stream_in, NULL, &na,
573 PA_STREAM_ADJUST_LATENCY)) < 0)
575 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
576 _("pa_stream_connect_record() failed: %s\n"),
577 pa_strerror (pa_context_errno (c)));
583 case PA_CONTEXT_TERMINATED:
586 case PA_CONTEXT_FAILED:
588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
589 _("Connection failure: %s\n"),
590 pa_strerror (pa_context_errno (c)));
609 if (!pa_sample_spec_valid (&sample_spec))
611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
614 /* set up main record loop */
615 if (!(m = pa_mainloop_new ()))
617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
618 _("pa_mainloop_new() failed.\n"));
620 mainloop_api = pa_mainloop_get_api (m);
622 /* listen to signals */
623 r = pa_signal_init (mainloop_api);
624 GNUNET_assert (r == 0);
625 pa_signal_new (SIGINT, &exit_signal_callback, NULL);
626 pa_signal_new (SIGTERM, &exit_signal_callback, NULL);
628 /* connect to the main pulseaudio context */
630 if (!(context = pa_context_new (mainloop_api, "GNUNET VoIP")))
632 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
633 _("pa_context_new() failed.\n"));
635 pa_context_set_state_callback (context, &context_state_callback, NULL);
636 if (pa_context_connect (context, NULL, 0, NULL) < 0)
638 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
639 _("pa_context_connect() failed: %s\n"),
640 pa_strerror (pa_context_errno (context)));
642 if (pa_mainloop_run (m, &i) < 0)
644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
645 _("pa_mainloop_run() failed.\n"));
658 pcm_length = FRAME_SIZE * CHANNELS * sizeof (float);
659 pcm_buffer = pa_xmalloc (pcm_length);
660 opus_data = GNUNET_malloc (MAX_PAYLOAD_BYTES);
661 enc = opus_encoder_create (SAMPLING_RATE,
665 opus_encoder_ctl (enc,
666 OPUS_SET_PACKET_LOSS_PERC (CONV_OPUS_PACKET_LOSS_PERCENTAGE));
667 opus_encoder_ctl (enc,
668 OPUS_SET_COMPLEXITY (CONV_OPUS_ENCODING_COMPLEXITY));
669 opus_encoder_ctl (enc,
670 OPUS_SET_INBAND_FEC (CONV_OPUS_INBAND_FEC));
671 opus_encoder_ctl (enc,
672 OPUS_SET_SIGNAL (CONV_OPUS_SIGNAL));
680 struct OpusHeadPacket headpacket;
681 struct OpusCommentsPacket *commentspacket;
682 size_t commentspacket_len;
684 serialno = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
686 /*Initialize Ogg stream struct*/
687 if (-1 == ogg_stream_init (&os, serialno))
689 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
690 _("ogg_stream_init() failed.\n"));
703 GNUNET_memcpy (headpacket.magic, "OpusHead", 8);
704 headpacket.version = 1;
705 headpacket.channels = CHANNELS;
706 headpacket.preskip = GNUNET_htole16 (0);
707 headpacket.sampling_rate = GNUNET_htole32 (SAMPLING_RATE);
708 headpacket.gain = GNUNET_htole16 (0);
709 headpacket.channel_mapping = 0; /* Mono or stereo */
711 op.packet = (unsigned char *) &headpacket;
712 op.bytes = sizeof (headpacket);
716 op.packetno = packet_id++;
717 ogg_stream_packetin (&os, &op);
719 /* Head packet must be alone on its page */
720 while (ogg_stream_flush (&os, &og))
725 commentspacket_len = sizeof (*commentspacket);
726 opusver = opus_get_version_string ();
727 vendor_length = strlen (opusver);
728 commentspacket_len += vendor_length;
729 commentspacket_len += sizeof (uint32_t);
731 commentspacket = (struct OpusCommentsPacket *) malloc (commentspacket_len);
732 if (NULL == commentspacket)
734 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
735 _("Failed to allocate %u bytes for second packet\n"),
736 (unsigned int) commentspacket_len);
740 GNUNET_memcpy (commentspacket->magic, "OpusTags", 8);
741 commentspacket->vendor_length = GNUNET_htole32 (vendor_length);
742 GNUNET_memcpy (&commentspacket[1], opusver, vendor_length);
743 *(uint32_t *) &((char *) &commentspacket[1])[vendor_length] = \
744 GNUNET_htole32 (0); /* no tags */
746 op.packet = (unsigned char *) commentspacket;
747 op.bytes = commentspacket_len;
751 op.packetno = packet_id++;
752 ogg_stream_packetin (&os, &op);
754 /* Comment packets must not be mixed with audio packets on their pages */
755 while (ogg_stream_flush (&os, &og))
760 free (commentspacket);
765 * The main function for the record helper.
767 * @param argc number of arguments from the command line
768 * @param argv command line arguments
769 * @return 0 ok, 1 on error
777 GNUNET_assert (GNUNET_OK ==
778 GNUNET_log_setup ("gnunet-helper-audio-record",
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Audio source starts\n");
783 audio_message = GNUNET_malloc (UINT16_MAX);
784 audio_message->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
786 #ifdef DEBUG_RECORD_PURE_OGG
787 dump_pure_ogg = getenv ("GNUNET_RECORD_PURE_OGG") ? 1 : 0;