program to record audio data from the microphone More...
#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>
#include <ogg/ogg.h>
Go to the source code of this file.
Data Structures | |
struct | OpusHeadPacket |
struct | OpusCommentsPacket |
Macros | |
#define | DEBUG_RECORD_PURE_OGG 1 |
#define | SAMPLING_RATE 48000 |
Sampling rate. More... | |
#define | FRAME_SIZE_MS 40 |
How many ms of audio to buffer before encoding them. More... | |
#define | FRAME_SIZE (SAMPLING_RATE / 1000 * FRAME_SIZE_MS) |
How many samples to buffer before encoding them. More... | |
#define | PAGE_WATERLINE 800 |
Pages are committed when their size goes over this value. More... | |
#define | MAX_PAYLOAD_BYTES 1024 |
Maximum length of opus payload. More... | |
#define | CHANNELS 1 |
Number of channels. More... | |
#define | CONV_OPUS_PACKET_LOSS_PERCENTAGE 1 |
Configures the encoder's expected packet loss percentage. More... | |
#define | CONV_OPUS_ENCODING_COMPLEXITY 10 |
Configures the encoder's computational complexity. More... | |
#define | CONV_OPUS_INBAND_FEC 1 |
Configures the encoder's use of inband forward error correction (FEC). More... | |
#define | CONV_OPUS_SIGNAL OPUS_SIGNAL_VOICE |
Configures the type of signal being encoded. More... | |
#define | CONV_OPUS_APP_TYPE OPUS_APPLICATION_VOIP |
Coding mode. More... | |
Functions | |
static void | quit (int ret) |
Pulseaudio shutdown task. More... | |
static void | write_data (const char *ptr, size_t msg_size) |
static void | write_page (ogg_page *og) |
static void | packetizer () |
Creates OPUS packets from PCM data. More... | |
static void | stream_read_callback (pa_stream *s, size_t length, void *userdata) |
Pulseaudio callback when new data is available. More... | |
static void | exit_signal_callback (pa_mainloop_api *m_, pa_signal_event *e, int sig, void *userdata) |
Exit callback for SIGTERM and SIGINT. More... | |
static void | stream_state_callback (pa_stream *s, void *userdata) |
Pulseaudio stream state callback. More... | |
static void | context_state_callback (pa_context *c, void *userdata) |
Pulseaudio context state callback. More... | |
static void | pa_init () |
Pulsaudio init. More... | |
static void | opus_init () |
OPUS init. More... | |
static void | ogg_init () |
int | main (int argc, char *argv[]) |
The main function for the record helper. More... | |
Variables | |
static pa_sample_spec | sample_spec |
Specification for recording. More... | |
static GNUNET_NETWORK_STRUCT_END pa_mainloop_api * | mainloop_api |
Pulseaudio mainloop api. More... | |
static pa_mainloop * | m |
Pulseaudio mainloop. More... | |
static pa_context * | context |
Pulseaudio context. More... | |
static pa_stream * | stream_in |
Pulseaudio recording stream. More... | |
static pa_io_event * | stdio_event |
Pulseaudio io events. More... | |
static OpusEncoder * | enc |
OPUS encoder. More... | |
static unsigned char * | opus_data |
Buffer for encoded data. More... | |
static float * | pcm_buffer |
PCM data buffer for one OPUS frame. More... | |
static int | pcm_length |
Length of the pcm data needed for one OPUS frame. More... | |
static char * | transmit_buffer |
Audio buffer. More... | |
static size_t | transmit_buffer_length |
Length of audio buffer. More... | |
static size_t | transmit_buffer_index |
Read index for transmit buffer. More... | |
static struct AudioMessage * | audio_message |
Audio message skeleton. More... | |
static ogg_stream_state | os |
Ogg muxer state. More... | |
static int32_t | packet_id |
Ogg packet id. More... | |
static int64_t | enc_granulepos |
Ogg granule for current packet. More... | |
static int | dump_pure_ogg |
1 to not to write GNUnet message headers, producing pure playable ogg output More... | |
program to record audio data from the microphone
Definition in file gnunet-helper-audio-record.c.
#define DEBUG_RECORD_PURE_OGG 1 |
Definition at line 43 of file gnunet-helper-audio-record.c.
#define SAMPLING_RATE 48000 |
Sampling rate.
Definition at line 48 of file gnunet-helper-audio-record.c.
#define FRAME_SIZE_MS 40 |
How many ms of audio to buffer before encoding them.
Possible values: 60, 40, 20, 10, 5, 2.5
Definition at line 55 of file gnunet-helper-audio-record.c.
#define FRAME_SIZE (SAMPLING_RATE / 1000 * FRAME_SIZE_MS) |
How many samples to buffer before encoding them.
Definition at line 60 of file gnunet-helper-audio-record.c.
#define PAGE_WATERLINE 800 |
Pages are committed when their size goes over this value.
Note that in practice we flush pages VERY often (every frame), which means that pages NEVER really get to be this big. With one-packet-per-page, pages are roughly 100-300 bytes each.
This value is chosen to make MAX_PAYLOAD_BYTES=1024 fit into a single page.
Definition at line 71 of file gnunet-helper-audio-record.c.
#define MAX_PAYLOAD_BYTES 1024 |
Maximum length of opus payload.
Definition at line 76 of file gnunet-helper-audio-record.c.
#define CHANNELS 1 |
Number of channels.
Definition at line 81 of file gnunet-helper-audio-record.c.
#define CONV_OPUS_PACKET_LOSS_PERCENTAGE 1 |
Configures the encoder's expected packet loss percentage.
Higher values will trigger progressively more loss resistant behavior in the encoder at the expense of quality at a given bitrate in the lossless case, but greater quality under loss.
Definition at line 90 of file gnunet-helper-audio-record.c.
#define CONV_OPUS_ENCODING_COMPLEXITY 10 |
Configures the encoder's computational complexity.
The supported range is 0-10 inclusive with 10 representing the highest complexity.
Definition at line 98 of file gnunet-helper-audio-record.c.
#define CONV_OPUS_INBAND_FEC 1 |
Configures the encoder's use of inband forward error correction (FEC).
Note: This is only applicable to the LPC layer.
Definition at line 105 of file gnunet-helper-audio-record.c.
#define CONV_OPUS_SIGNAL OPUS_SIGNAL_VOICE |
Configures the type of signal being encoded.
This is a hint which helps the encoder's mode selection.
Possible values: OPUS_AUTO - (default) Encoder detects the type automatically. OPUS_SIGNAL_VOICE - Bias thresholds towards choosing LPC or Hybrid modes. OPUS_SIGNAL_MUSIC - Bias thresholds towards choosing MDCT modes.
Definition at line 117 of file gnunet-helper-audio-record.c.
#define CONV_OPUS_APP_TYPE OPUS_APPLICATION_VOIP |
Coding mode.
Possible values: OPUS_APPLICATION_VOIP - gives best quality at a given bitrate for voice signals. It enhances the input signal by high-pass filtering and emphasizing formants and harmonics. Optionally it includes in-band forward error correction to protect against packet loss. Use this mode for typical VoIP applications. Because of the enhancement, even at high bitrates the output may sound different from the input. OPUS_APPLICATION_AUDIO - gives best quality at a given bitrate for most non-voice signals like music. Use this mode for music and mixed (music/voice) content, broadcast, and applications requiring less than 15 ms of coding delay. OPUS_APPLICATION_RESTRICTED_LOWDELAY - configures low-delay mode that disables the speech-optimized mode in exchange for slightly reduced delay. This mode can only be set on an newly initialized or freshly reset encoder because it changes the codec delay.
Definition at line 138 of file gnunet-helper-audio-record.c.
|
static |
Pulseaudio shutdown task.
Definition at line 270 of file gnunet-helper-audio-record.c.
References mainloop_api, and ret.
Referenced by context_state_callback(), exit_signal_callback(), packetizer(), stream_read_callback(), stream_state_callback(), and write_data().
|
static |
Definition at line 279 of file gnunet-helper-audio-record.c.
References GNUNET_ERROR_TYPE_ERROR, GNUNET_log_strerror, quit(), and ret.
Referenced by write_page().
|
static |
Definition at line 304 of file gnunet-helper-audio-record.c.
References audio_message, dump_pure_ogg, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_memcpy, AudioMessage::header, GNUNET_MessageHeader::size, and write_data().
Referenced by ogg_init(), and packetizer().
|
static |
Creates OPUS packets from PCM data.
Definition at line 335 of file gnunet-helper-audio-record.c.
References _, enc, enc_granulepos, FRAME_SIZE, GNUNET_assert, GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_memcpy, MAX_PAYLOAD_BYTES, op, opus_data, os, packet_id, PAGE_WATERLINE, pcm_buffer, pcm_length, quit(), SAMPLING_RATE, transmit_buffer, transmit_buffer_index, transmit_buffer_length, and write_page().
Referenced by stream_read_callback().
|
static |
Pulseaudio callback when new data is available.
Definition at line 415 of file gnunet-helper-audio-record.c.
References _, context, data, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_memcpy, mainloop_api, packetizer(), pcm_length, quit(), stdio_event, transmit_buffer, transmit_buffer_index, and transmit_buffer_length.
Referenced by context_state_callback().
|
static |
Exit callback for SIGTERM and SIGINT.
Definition at line 467 of file gnunet-helper-audio-record.c.
References _, GNUNET_ERROR_TYPE_INFO, GNUNET_log, and quit().
Referenced by pa_init().
|
static |
Pulseaudio stream state callback.
Definition at line 486 of file gnunet-helper-audio-record.c.
References _, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log, and quit().
Referenced by context_state_callback().
|
static |
Pulseaudio context state callback.
Definition at line 548 of file gnunet-helper-audio-record.c.
References _, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log, pcm_length, quit(), sample_spec, stream_in, stream_read_callback(), and stream_state_callback().
Referenced by pa_init().
|
static |
Pulsaudio init.
Definition at line 616 of file gnunet-helper-audio-record.c.
References _, context, context_state_callback(), exit_signal_callback(), GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, m, mainloop_api, and sample_spec.
Referenced by main().
|
static |
OPUS init.
Definition at line 666 of file gnunet-helper-audio-record.c.
References CHANNELS, CONV_OPUS_APP_TYPE, CONV_OPUS_ENCODING_COMPLEXITY, CONV_OPUS_INBAND_FEC, CONV_OPUS_PACKET_LOSS_PERCENTAGE, CONV_OPUS_SIGNAL, enc, FRAME_SIZE, GNUNET_malloc, MAX_PAYLOAD_BYTES, opus_data, pcm_buffer, pcm_length, and SAMPLING_RATE.
Referenced by main().
|
static |
Definition at line 690 of file gnunet-helper-audio-record.c.
References _, OpusHeadPacket::channel_mapping, OpusHeadPacket::channels, CHANNELS, OpusHeadPacket::gain, GNUNET_CRYPTO_QUALITY_STRONG, GNUNET_CRYPTO_random_u32(), GNUNET_ERROR_TYPE_ERROR, GNUNET_htole16, GNUNET_htole32, GNUNET_log, GNUNET_memcpy, OpusHeadPacket::magic, OpusCommentsPacket::magic, op, os, packet_id, OpusHeadPacket::preskip, OpusHeadPacket::sampling_rate, SAMPLING_RATE, OpusCommentsPacket::vendor_length, OpusHeadPacket::version, and write_page().
Referenced by main().
int main | ( | int | argc, |
char * | argv[] | ||
) |
The main function for the record helper.
argc | number of arguments from the command line |
argv | command line arguments |
Definition at line 786 of file gnunet-helper-audio-record.c.
References audio_message, dump_pure_ogg, getenv(), GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_log_setup(), GNUNET_malloc, GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO, GNUNET_OK, AudioMessage::header, ogg_init(), opus_init(), pa_init(), and GNUNET_MessageHeader::type.
|
static |
Specification for recording.
May change in the future to spec negotiation.
Definition at line 143 of file gnunet-helper-audio-record.c.
Referenced by context_state_callback(), and pa_init().
|
static |
Pulseaudio mainloop api.
Definition at line 181 of file gnunet-helper-audio-record.c.
Referenced by pa_init(), quit(), and stream_read_callback().
|
static |
Pulseaudio mainloop.
Definition at line 186 of file gnunet-helper-audio-record.c.
Referenced by pa_init().
|
static |
Pulseaudio context.
Definition at line 191 of file gnunet-helper-audio-record.c.
Referenced by pa_init(), and stream_read_callback().
|
static |
Pulseaudio recording stream.
Definition at line 196 of file gnunet-helper-audio-record.c.
Referenced by context_state_callback().
|
static |
Pulseaudio io events.
Definition at line 201 of file gnunet-helper-audio-record.c.
Referenced by stream_read_callback().
|
static |
OPUS encoder.
Definition at line 206 of file gnunet-helper-audio-record.c.
Referenced by encapsulate_for_dv(), encrypt_existing_match(), get_update_information_directory(), GNUNET_CONFIGURATION_get_data(), GNUNET_CRYPTO_ecdsa_public_key_from_string(), GNUNET_CRYPTO_eddsa_private_key_from_string(), GNUNET_CRYPTO_eddsa_public_key_from_string(), GNUNET_CRYPTO_hash_from_string2(), GNUNET_CRYPTO_hpke_kem_encaps_norand(), GNUNET_CRYPTO_hpke_open_oneshot(), GNUNET_CRYPTO_hpke_receiver_setup(), GNUNET_CRYPTO_hpke_receiver_setup2(), GNUNET_CRYPTO_hpke_seal_oneshot(), GNUNET_CRYPTO_hpke_sender_setup(), GNUNET_CRYPTO_hpke_sender_setup2(), GNUNET_FS_tree_encoder_next(), GNUNET_STRINGS_base64url_encode(), GNUNET_STRINGS_string_to_data(), GNUNET_STRINGS_string_to_data_alloc(), main(), opus_init(), packetizer(), parse_fixed64_data(), parse_fixed_data(), parse_rsa_public_key(), try_match_block(), and verify_confirmation().
|
static |
Buffer for encoded data.
Definition at line 211 of file gnunet-helper-audio-record.c.
Referenced by opus_init(), and packetizer().
|
static |
PCM data buffer for one OPUS frame.
Definition at line 216 of file gnunet-helper-audio-record.c.
Referenced by opus_init(), and packetizer().
|
static |
Length of the pcm data needed for one OPUS frame.
Definition at line 221 of file gnunet-helper-audio-record.c.
Referenced by context_state_callback(), opus_init(), packetizer(), and stream_read_callback().
|
static |
Audio buffer.
Definition at line 226 of file gnunet-helper-audio-record.c.
Referenced by packetizer(), and stream_read_callback().
|
static |
Length of audio buffer.
Definition at line 231 of file gnunet-helper-audio-record.c.
Referenced by packetizer(), and stream_read_callback().
|
static |
Read index for transmit buffer.
Definition at line 236 of file gnunet-helper-audio-record.c.
Referenced by packetizer(), and stream_read_callback().
|
static |
Audio message skeleton.
Definition at line 241 of file gnunet-helper-audio-record.c.
Referenced by main(), and write_page().
|
static |
Ogg muxer state.
Definition at line 246 of file gnunet-helper-audio-record.c.
Referenced by ogg_init(), and packetizer().
|
static |
Ogg packet id.
Definition at line 251 of file gnunet-helper-audio-record.c.
Referenced by ogg_init(), and packetizer().
|
static |
Ogg granule for current packet.
Definition at line 256 of file gnunet-helper-audio-record.c.
Referenced by packetizer().
|
static |
1 to not to write GNUnet message headers, producing pure playable ogg output
Definition at line 263 of file gnunet-helper-audio-record.c.
Referenced by main(), and write_page().