fprintf (stderr, "\nPlaying...");
for (rec=rec_head; NULL != rec; rec = rec->next)
{
- fprintf (stderr, "<-%u\n", (unsigned int) rec->size);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Replaying %u bytes\n",
+ (unsigned int) rec->size);
speaker->play (speaker->cls,
rec->size,
&rec[1]);
{
struct Recording *rec;
- fprintf (stderr, "->%u\n", (unsigned int) data_size);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Recorded %u bytes\n",
+ (unsigned int) data_size);
rec = GNUNET_malloc (sizeof (struct Recording) + data_size);
rec->size = data_size;
memcpy (&rec[1], data, data_size);
*/
static pa_stream *stream_out;
-/**
- * Pulseaudio io events
- */
-static pa_io_event *stdio_event;
-
/**
* OPUS decoder
*/
static int frame_size;
/**
- * Audio buffer
- */
-static void *buffer;
-
-/**
- * Length of audio buffer
+ * Pipe we use to signal the main loop that we are ready to receive.
*/
-static size_t buffer_length;
-
-/**
- * Read index for transmit buffer
- */
-static size_t buffer_index;
-
+static int ready_pipe[2];
/**
* Message callback
return GNUNET_OK;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Decoded frame\n");
+ "Decoded frame with %u bytes\n",
+ ntohs (audio->header.size));
if (pa_stream_write
- (stream_out, (uint8_t *) pcm_buffer, pcm_length, NULL, 0,
+ (stream_out, pcm_buffer, pcm_length, NULL, 0,
PA_SEEK_RELATIVE) < 0)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
}
-/**
- * Write some data to the stream
- */
-static void
-do_stream_write (size_t length)
-{
- size_t l;
-
- GNUNET_assert (0 != length);
- if ( (! buffer) || (! buffer_length) )
- return;
-
- l = length;
- if (l > buffer_length)
- l = buffer_length;
- if (0 > pa_stream_write (stream_out,
- (uint8_t *) buffer + buffer_index,
- l,
- NULL, 0,
- PA_SEEK_RELATIVE))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _("pa_stream_write() failed: %s\n"),
- pa_strerror (pa_context_errno (context)));
- quit (1);
- return;
- }
- buffer_length -= l;
- buffer_index += l;
- if (! buffer_length)
- {
- pa_xfree (buffer);
- buffer = NULL;
- buffer_index = buffer_length = 0;
- }
-}
-
-
/**
* Callback when data is there for playback
*/
size_t length,
void *userdata)
{
- if (stdio_event)
- mainloop_api->io_enable (stdio_event, PA_IO_EVENT_INPUT);
- if (!buffer)
- return;
- do_stream_write (length);
+ /* unblock 'main' */
+ if (-1 != ready_pipe[1])
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Unblocking main loop!\n");
+ write (ready_pipe[1], "r", 1);
+ }
}
goto fail;
}
pa_stream_set_write_callback (stream_out,
- stream_write_callback,
+ &stream_write_callback,
NULL);
if ((p =
pa_stream_connect_playback (stream_out, NULL, NULL, 0, NULL,
_("pa_stream_connect_playback() failed: %s\n"),
pa_strerror (pa_context_errno (c)));
goto fail;
- }
+ }
break;
}
case PA_CONTEXT_TERMINATED:
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)
{
- ssize_t ret = read (0, readbuf, sizeof (readbuf));
+ ret = read (0, readbuf, sizeof (readbuf));
toff += ret;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Received %d bytes of audio data (total: %llu)\n",
{
const void *data;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Got %u/%u bytes of PCM data\n",
+ length,
+ pcm_length);
+
GNUNET_assert (NULL != s);
GNUNET_assert (length > 0);
if (stdio_event)
case PA_STREAM_READY:
{
const pa_buffer_attr *a;
+
char cmt[PA_CHANNEL_MAP_SNPRINT_MAX],
sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Buffer metrics: maxlength=%u, fragsize=%u\n"),
a->maxlength, a->fragsize);
- }
+ }
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Using sample spec '%s', channel map '%s'.\n"),
pa_sample_spec_snprint (sst, sizeof (sst),
pa_stream_get_device_index (s),
pa_stream_is_suspended (s) ? "" : "not ");
}
- break;
+ break;
case PA_STREAM_FAILED:
default:
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
case PA_CONTEXT_READY:
{
int r;
+ pa_buffer_attr na;
GNUNET_assert (!stream_in);
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
pa_strerror (pa_context_errno (c)));
goto fail;
}
- pa_stream_set_state_callback (stream_in, stream_state_callback, NULL);
- pa_stream_set_read_callback (stream_in, stream_read_callback, NULL);
- if ((r = pa_stream_connect_record (stream_in, NULL, NULL, 0)) < 0)
+ pa_stream_set_state_callback (stream_in, &stream_state_callback, NULL);
+ pa_stream_set_read_callback (stream_in, &stream_read_callback, NULL);
+ memset (&na, 0, sizeof (na));
+ na.maxlength = UINT32_MAX;
+ na.fragsize = pcm_length;
+ if ((r = pa_stream_connect_record (stream_in, NULL, &na,
+ PA_STREAM_EARLY_REQUESTS)) < 0)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("pa_stream_connect_record() failed: %s\n"),
/* 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);
+ pa_signal_new (SIGINT, &exit_signal_callback, NULL);
+ pa_signal_new (SIGTERM, &exit_signal_callback, NULL);
/* connect to the main pulseaudio context */
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("pa_context_new() failed.\n"));
}
- pa_context_set_state_callback (context, context_state_callback, NULL);
+ 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,
GNUNET_log_setup ("gnunet-helper-audio-record",
"DEBUG",
"/tmp/helper-audio-record"));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Audio source starts\n");
audio_message = GNUNET_malloc (UINT16_MAX);
audio_message->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
opus_init ();