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-gst.c
17 * @brief program to record audio data from the microphone (GStreamer version)
21 #include "gnunet_util_lib.h"
22 #include "gnunet_protocols.h"
23 #include "conversation.h"
24 #include "gnunet_constants.h"
25 #include "gnunet_core_service.h"
28 #include <gst/app/gstappsink.h>
29 #include <gst/audio/gstaudiobasesrc.h>
32 #define DEBUG_RECORD_PURE_OGG 1
36 * Must be one of the following (from libopusenc documentation):
39 #define OPUS_CHANNELS 1
42 * Maximal size of a single opus packet.
44 #define MAX_PAYLOAD_SIZE (1024 / OPUS_CHANNELS)
47 * Size of a single frame fed to the encoder, in ms.
48 * Must be one of the following (from libopus documentation):
49 * 2.5, 5, 10, 20, 40 or 60
51 #define OPUS_FRAME_SIZE 40
54 * Expected packet loss to prepare for, in percents.
56 #define PACKET_LOSS_PERCENTAGE 1
59 * Set to 1 to enable forward error correction.
60 * Set to 0 to disable.
62 #define INBAND_FEC_MODE 1
65 * Max number of microseconds to buffer in audiosource.
68 #define BUFFER_TIME 1000 /* 1ms */
71 * Min number of microseconds to buffer in audiosource.
74 #define LATENCY_TIME 1000 /* 1ms */
77 * Maximum delay in multiplexing streams, in ns.
78 * Setting this to 0 forces page flushing, which
79 * decreases delay, but increases overhead.
81 #define OGG_MAX_DELAY 0
84 * Maximum delay for sending out a page, in ns.
85 * Setting this to 0 forces page flushing, which
86 * decreases delay, but increases overhead.
88 #define OGG_MAX_PAGE_DELAY 0
93 static GstElement *pipeline;
95 #ifdef DEBUG_RECORD_PURE_OGG
96 static int dump_pure_ogg;
102 if (NULL != pipeline)
103 gst_element_set_state (pipeline, GST_STATE_NULL);
107 bus_call (GstBus *bus, GstMessage *msg, gpointer data)
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bus message\n");
110 switch (GST_MESSAGE_TYPE (msg))
112 case GST_MESSAGE_EOS:
113 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "End of stream\n");
117 case GST_MESSAGE_ERROR:
122 gst_message_parse_error (msg, &error, &debug);
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error: %s\n", error->message);
126 g_error_free (error);
139 source_child_added (GstChildProxy *child_proxy, GObject *object, gchar *name, gpointer user_data)
141 if (GST_IS_AUDIO_BASE_SRC (object))
142 g_object_set (object, "buffer-time", (gint64) BUFFER_TIME, "latency-time", (gint64) LATENCY_TIME, NULL);
146 signalhandler (int s)
153 main (int argc, char **argv)
155 GstElement *source, *filter, *encoder, *conv, *resampler, *sink, *oggmux;
159 struct AudioMessage audio_message;
162 typedef void (*SignalHandlerPointer) (int);
164 SignalHandlerPointer inthandler, termhandler;
165 inthandler = signal (SIGINT, signalhandler);
166 termhandler = signal (SIGTERM, signalhandler);
168 #ifdef DEBUG_RECORD_PURE_OGG
169 dump_pure_ogg = getenv ("GNUNET_RECORD_PURE_OGG") ? 1 : 0;
173 setmode (1, _O_BINARY);
177 gst_init (&argc, &argv);
179 GNUNET_assert (GNUNET_OK ==
180 GNUNET_log_setup ("gnunet-helper-audio-record",
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "Audio source starts\n");
187 audio_message.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
189 /* Create gstreamer elements */
190 pipeline = gst_pipeline_new ("audio-recorder");
191 source = gst_element_factory_make ("autoaudiosrc", "audiosource");
192 filter = gst_element_factory_make ("capsfilter", "filter");
193 conv = gst_element_factory_make ("audioconvert", "converter");
194 resampler= gst_element_factory_make ("audioresample", "resampler");
195 encoder = gst_element_factory_make ("opusenc", "opus-encoder");
196 oggmux = gst_element_factory_make ("oggmux", "ogg-muxer");
197 sink = gst_element_factory_make ("appsink", "audio-output");
199 if (!pipeline || !filter || !source || !conv || !resampler || !encoder || !oggmux || !sink)
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 "One element could not be created. Exiting.\n");
206 g_signal_connect (source, "child-added", G_CALLBACK (source_child_added), NULL);
208 /* Set up the pipeline */
210 caps = gst_caps_new_simple ("audio/x-raw",
211 "format", G_TYPE_STRING, "S16LE",
212 /* "rate", G_TYPE_INT, SAMPLING_RATE,*/
213 "channels", G_TYPE_INT, OPUS_CHANNELS,
214 /* "layout", G_TYPE_STRING, "interleaved",*/
216 g_object_set (G_OBJECT (filter),
219 gst_caps_unref (caps);
221 g_object_set (G_OBJECT (encoder),
222 /* "bitrate", 64000, */
223 /* "bandwidth", OPUS_BANDWIDTH_FULLBAND, */
224 "inband-fec", INBAND_FEC_MODE,
225 "packet-loss-percentage", PACKET_LOSS_PERCENTAGE,
226 "max-payload-size", MAX_PAYLOAD_SIZE,
227 "audio", FALSE, /* VoIP, not audio */
228 "frame-size", OPUS_FRAME_SIZE,
231 g_object_set (G_OBJECT (oggmux),
232 "max-delay", OGG_MAX_DELAY,
233 "max-page-delay", OGG_MAX_PAGE_DELAY,
236 /* we add a message handler */
237 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
238 bus_watch_id = gst_bus_add_watch (bus, bus_call, pipeline);
239 gst_object_unref (bus);
241 /* we add all elements into the pipeline */
242 /* audiosource | converter | resampler | opus-encoder | audio-output */
243 gst_bin_add_many (GST_BIN (pipeline), source, filter, conv, resampler, encoder,
246 /* we link the elements together */
247 gst_element_link_many (source, filter, conv, resampler, encoder, oggmux, sink, NULL);
249 /* Set the pipeline to "playing" state*/
250 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Now playing\n");
251 gst_element_set_state (pipeline, GST_STATE_PLAYING);
254 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running...\n");
261 size_t len, msg_size;
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pulling...\n");
266 s = gst_app_sink_pull_sample (GST_APP_SINK (sink));
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pulled NULL\n");
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "...pulled!\n");
274 const GstStructure *si;
278 si = gst_sample_get_info (s);
281 si_str = gst_structure_to_string (si);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got sample %s\n", si_str);
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got sample with no info\n");
290 s_caps = gst_sample_get_caps (s);
293 caps_str = gst_caps_to_string (s_caps);
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got sample with caps %s\n", caps_str);
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got sample with no caps\n");
303 b = gst_sample_get_buffer (s);
304 if (NULL == b || !gst_buffer_map (b, &m, GST_MAP_READ))
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got NULL buffer %p or failed to map the buffer\n", b);
307 gst_sample_unref (s);
312 if (len > UINT16_MAX - sizeof (struct AudioMessage))
315 len = UINT16_MAX - sizeof (struct AudioMessage);
317 msg_size = sizeof (struct AudioMessage) + len;
318 audio_message.header.size = htons ((uint16_t) msg_size);
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Sending %u bytes of audio data\n", (unsigned int) msg_size);
322 for (phase = 0; phase < 2; phase++)
329 #ifdef DEBUG_RECORD_PURE_OGG
333 ptr = (const char *) &audio_message;
334 to_send = sizeof (audio_message);
338 ptr = (const char *) m.data;
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Sending %u bytes on phase %d\n", (unsigned int) to_send, phase);
343 for (offset = 0; offset < to_send; offset += ret)
345 ret = write (1, &ptr[offset], to_send - offset);
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Failed to write %u bytes at offset %u (total %u) in phase %d: %s\n",
351 (unsigned int) (to_send - offset),
352 (unsigned int) offset,
353 (unsigned int) (to_send + offset),
363 gst_buffer_unmap (b, &m);
364 gst_sample_unref (s);
367 signal (SIGINT, inthandler);
368 signal (SIGINT, termhandler);
370 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Returned, stopping playback\n");
373 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Deleting pipeline\n");
374 gst_object_unref (GST_OBJECT (pipeline));
376 g_source_remove (bus_watch_id);