From 517263c3f5134e5dba57a25e745cb276937602bc Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 2 Oct 2013 21:19:33 +0000 Subject: [PATCH] -use util's mst, remove broken mst.c --- src/conversation/gnunet-conversation-test.c | 13 +- .../gnunet-helper-audio-playback.c | 76 +++-- src/conversation/gnunet-helper-audio-record.c | 2 + src/conversation/mst.c | 288 ------------------ src/conversation/speaker.c | 2 +- 5 files changed, 49 insertions(+), 332 deletions(-) delete mode 100644 src/conversation/mst.c diff --git a/src/conversation/gnunet-conversation-test.c b/src/conversation/gnunet-conversation-test.c index 13a55ad0c..7f8c52228 100644 --- a/src/conversation/gnunet-conversation-test.c +++ b/src/conversation/gnunet-conversation-test.c @@ -31,7 +31,7 @@ /** * How long do we record before we replay? */ -#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) +#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) /** @@ -145,10 +145,13 @@ switch_to_speaker (void *cls, return; } fprintf (stderr, "\nPlaying..."); - for (rec=rec_head;NULL != rec; rec = rec->next) + for (rec=rec_head; NULL != rec; rec = rec->next) + { + fprintf (stderr, "<-%u\n", (unsigned int) rec->size); speaker->play (speaker->cls, rec->size, &rec[1]); + } GNUNET_SCHEDULER_cancel (st); st = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shutdown, @@ -170,7 +173,7 @@ record (void *cls, { struct Recording *rec; - fprintf (stderr, "."); + fprintf (stderr, "->%u\n", (unsigned int) data_size); rec = GNUNET_malloc (sizeof (struct Recording) + data_size); rec->size = data_size; memcpy (&rec[1], data, data_size); @@ -202,7 +205,9 @@ run (void *cls, char *const *args, const char *cfgfile, st = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown, NULL); - fprintf (stderr, "Recording..."); + fprintf (stderr, + "Recording for %s...", + GNUNET_STRINGS_relative_time_to_string (TIMEOUT, GNUNET_YES)); if (GNUNET_OK != microphone->enable_microphone (microphone->cls, &record, NULL)) diff --git a/src/conversation/gnunet-helper-audio-playback.c b/src/conversation/gnunet-helper-audio-playback.c index 97664d0f5..5fdc217f2 100644 --- a/src/conversation/gnunet-helper-audio-playback.c +++ b/src/conversation/gnunet-helper-audio-playback.c @@ -41,19 +41,15 @@ #define MAXLINE 4096 /** -* GNUnet Message Tokenizer -*/ -#include "mst.c" - -/** -* Pulseaudio specification. May change in the future. -*/ + * Pulseaudio specification. May change in the future. + */ static pa_sample_spec sample_spec = { .format = PA_SAMPLE_FLOAT32LE, .rate = 48000, .channels = 1 }; + /** * Pulseaudio mainloop api */ @@ -123,40 +119,39 @@ static size_t buffer_index; /** * Message callback */ -static void +static int stdin_receiver (void *cls, + void *client, const struct GNUNET_MessageHeader *msg) { struct AudioMessage *audio; switch (ntohs (msg->type)) - { - case GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO: - audio = (struct AudioMessage *) msg; - - int len = - opus_decode_float (dec, - (const unsigned char *) &audio[1], - ntohs (audio->header.size) - sizeof (struct AudioMessage), - pcm_buffer, - frame_size, 0); - // FIXME: pcm_length != len??? - if (pa_stream_write - (stream_out, (uint8_t *) pcm_buffer, pcm_length, NULL, 0, - PA_SEEK_RELATIVE) < 0) - { - - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("pa_stream_write() failed: %s\n"), - pa_strerror (pa_context_errno (context))); - return; - } - - break; - - default: - break; - } + { + case GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO: + audio = (struct AudioMessage *) msg; + + int len = + opus_decode_float (dec, + (const unsigned char *) &audio[1], + ntohs (audio->header.size) - sizeof (struct AudioMessage), + pcm_buffer, + frame_size, 0); + // FIXME: pcm_length != len??? + if (pa_stream_write + (stream_out, (uint8_t *) pcm_buffer, pcm_length, NULL, 0, + PA_SEEK_RELATIVE) < 0) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("pa_stream_write() failed: %s\n"), + pa_strerror (pa_context_errno (context))); + return GNUNET_OK; + } + break; + default: + break; + } + return GNUNET_OK; } @@ -402,13 +397,14 @@ int main (int argc, char *argv[]) { char readbuf[MAXLINE]; - struct MessageStreamTokenizer *stdin_mst; + struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst; + fprintf (stderr, "HERE!\n"); GNUNET_assert (GNUNET_OK == GNUNET_log_setup ("gnunet-helper-audio-playback", "WARNING", NULL)); - stdin_mst = mst_create (&stdin_receiver, NULL); + stdin_mst = GNUNET_SERVER_mst_create (&stdin_receiver, NULL); opus_init (); pa_init (); while (1) @@ -421,8 +417,10 @@ main (int argc, char *argv[]) strerror (errno)); break; } - mst_receive (stdin_mst, readbuf, ret); + GNUNET_SERVER_mst_receive (stdin_mst, NULL, + readbuf, ret, + GNUNET_NO, GNUNET_NO); } - mst_destroy (stdin_mst); + GNUNET_SERVER_mst_destroy (stdin_mst); return 0; } diff --git a/src/conversation/gnunet-helper-audio-record.c b/src/conversation/gnunet-helper-audio-record.c index a3e760794..2267c764b 100644 --- a/src/conversation/gnunet-helper-audio-record.c +++ b/src/conversation/gnunet-helper-audio-record.c @@ -462,6 +462,8 @@ opus_init () int main (int argc, char *argv[]) { + fprintf (stderr, "HERE2!\n"); + GNUNET_assert (GNUNET_OK == GNUNET_log_setup ("gnunet-helper-audio-record", "WARNING", diff --git a/src/conversation/mst.c b/src/conversation/mst.c deleted file mode 100644 index c348bdea9..000000000 --- a/src/conversation/mst.c +++ /dev/null @@ -1,288 +0,0 @@ -/* - This file is part of GNUnet. - (C) 2008, 2011 Christian Grothoff (and other contributing authors) - - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. -*/ - -/** - * @file conversation/mst.c - * @brief Message tokenizer - * @author Christian Grothoff - */ - -#include -#include - -/** - * To what multiple do we align messages? 8 byte should suffice for everyone - * for now. - */ -#define ALIGN_FACTOR 8 - -/** - * Smallest supported message. - */ -#define MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader) - - -/** - * Functions with this signature are called whenever a - * complete message is received by the tokenizer. - * - * @param cls closure - * @param message the actual message - */ -typedef void (*MessageTokenizerCallback) (void *cls, - const struct - GNUNET_MessageHeader * - message); - -/** - * Handle to a message stream tokenizer. - */ -struct MessageStreamTokenizer -{ - - /** - * Function to call on completed messages. - */ - MessageTokenizerCallback cb; - - /** - * Closure for cb. - */ - void *cb_cls; - - /** - * Size of the buffer (starting at 'hdr'). - */ - size_t curr_buf; - - /** - * How many bytes in buffer have we already processed? - */ - size_t off; - - /** - * How many bytes in buffer are valid right now? - */ - size_t pos; - - /** - * Beginning of the buffer. Typed like this to force alignment. - */ - struct GNUNET_MessageHeader *hdr; - -}; - - -/** - * Create a message stream tokenizer. - * - * @param cb function to call on completed messages - * @param cb_cls closure for cb - * @return handle to tokenizer - */ -static struct MessageStreamTokenizer * -mst_create (MessageTokenizerCallback cb, - void *cb_cls) -{ - struct MessageStreamTokenizer *ret; - - ret = malloc (sizeof (struct MessageStreamTokenizer)); - if (NULL == ret) - { - fprintf (stderr, "Failed to allocate buffer for tokenizer\n"); - exit (1); - } - ret->hdr = malloc (MIN_BUFFER_SIZE); - if (NULL == ret->hdr) - { - fprintf (stderr, "Failed to allocate buffer for alignment\n"); - exit (1); - } - ret->curr_buf = MIN_BUFFER_SIZE; - ret->cb = cb; - ret->cb_cls = cb_cls; - return ret; -} - - -/** - * Add incoming data to the receive buffer and call the - * callback for all complete messages. - * - * @param mst tokenizer to use - * @param buf input data to add - * @param size number of bytes in buf - * @return GNUNET_OK if we are done processing (need more data) - * GNUNET_SYSERR if the data stream is corrupt - */ -static int -mst_receive (struct MessageStreamTokenizer *mst, - const char *buf, size_t size) -{ - const struct GNUNET_MessageHeader *hdr; - size_t delta; - uint16_t want; - char *ibuf; - int need_align; - unsigned long offset; - int ret; - - ret = GNUNET_OK; - ibuf = (char *) mst->hdr; - while (mst->pos > 0) - { -do_align: - if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) || - (0 != (mst->off % ALIGN_FACTOR))) - { - /* need to align or need more space */ - mst->pos -= mst->off; - memmove (ibuf, &ibuf[mst->off], mst->pos); - mst->off = 0; - } - if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader)) - { - delta = - GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) - - (mst->pos - mst->off), size); - memcpy (&ibuf[mst->pos], buf, delta); - mst->pos += delta; - buf += delta; - size -= delta; - } - if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader)) - { - return GNUNET_OK; - } - hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off]; - want = ntohs (hdr->size); - if (want < sizeof (struct GNUNET_MessageHeader)) - { - fprintf (stderr, - "Received invalid message from stdin\n"); - exit (1); - } - if (mst->curr_buf - mst->off < want) - { - /* need more space */ - mst->pos -= mst->off; - memmove (ibuf, &ibuf[mst->off], mst->pos); - mst->off = 0; - } - if (want > mst->curr_buf) - { - mst->hdr = realloc (mst->hdr, want); - if (NULL == mst->hdr) - { - fprintf (stderr, "Failed to allocate buffer for alignment\n"); - exit (1); - } - ibuf = (char *) mst->hdr; - mst->curr_buf = want; - } - hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off]; - if (mst->pos - mst->off < want) - { - delta = GNUNET_MIN (want - (mst->pos - mst->off), size); - memcpy (&ibuf[mst->pos], buf, delta); - mst->pos += delta; - buf += delta; - size -= delta; - } - if (mst->pos - mst->off < want) - { - return GNUNET_OK; - } - mst->cb (mst->cb_cls, hdr); - mst->off += want; - if (mst->off == mst->pos) - { - /* reset to beginning of buffer, it's free right now! */ - mst->off = 0; - mst->pos = 0; - } - } - while (size > 0) - { - if (size < sizeof (struct GNUNET_MessageHeader)) - break; - offset = (unsigned long) buf; - need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO; - if (GNUNET_NO == need_align) - { - /* can try to do zero-copy and process directly from original buffer */ - hdr = (const struct GNUNET_MessageHeader *) buf; - want = ntohs (hdr->size); - if (want < sizeof (struct GNUNET_MessageHeader)) - { - fprintf (stderr, - "Received invalid message from stdin\n"); - exit (1); - } - if (size < want) - break; /* or not, buffer incomplete, so copy to private buffer... */ - mst->cb (mst->cb_cls, hdr); - buf += want; - size -= want; - } - else - { - /* need to copy to private buffer to align; - * yes, we go a bit more spagetti than usual here */ - goto do_align; - } - } - if (size > 0) - { - if (size + mst->pos > mst->curr_buf) - { - mst->hdr = realloc (mst->hdr, size + mst->pos); - if (NULL == mst->hdr) - { - fprintf (stderr, "Failed to allocate buffer for alignment\n"); - exit (1); - } - ibuf = (char *) mst->hdr; - mst->curr_buf = size + mst->pos; - } - if (mst->pos + size > mst->curr_buf) - { - fprintf (stderr, - "Assertion failed\n"); - exit (1); - } - memcpy (&ibuf[mst->pos], buf, size); - mst->pos += size; - } - return ret; -} - - -/** - * Destroys a tokenizer. - * - * @param mst tokenizer to destroy - */ -static void -mst_destroy (struct MessageStreamTokenizer *mst) -{ - free (mst->hdr); - free (mst); -} diff --git a/src/conversation/speaker.c b/src/conversation/speaker.c index 81c62da81..9a0bf910d 100644 --- a/src/conversation/speaker.c +++ b/src/conversation/speaker.c @@ -145,7 +145,7 @@ play (void *cls, am = (const struct AudioMessage *) data; (void) GNUNET_HELPER_send (spe->playback_helper, &am->header, - GNUNET_YES, + GNUNET_NO, NULL, NULL); } -- 2.25.1