aboutsummaryrefslogtreecommitdiff
path: root/src/conversation
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-10-02 21:19:33 +0000
committerChristian Grothoff <christian@grothoff.org>2013-10-02 21:19:33 +0000
commit517263c3f5134e5dba57a25e745cb276937602bc (patch)
treec373fee79171adde26b138631bd8d0c371825b69 /src/conversation
parent875342e4d0114e626dcf502362787d048d5e12fc (diff)
downloadgnunet-517263c3f5134e5dba57a25e745cb276937602bc.tar.gz
gnunet-517263c3f5134e5dba57a25e745cb276937602bc.zip
-use util's mst, remove broken mst.c
Diffstat (limited to 'src/conversation')
-rw-r--r--src/conversation/gnunet-conversation-test.c13
-rw-r--r--src/conversation/gnunet-helper-audio-playback.c76
-rw-r--r--src/conversation/gnunet-helper-audio-record.c2
-rw-r--r--src/conversation/mst.c288
-rw-r--r--src/conversation/speaker.c2
5 files changed, 49 insertions, 332 deletions
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 @@
31/** 31/**
32 * How long do we record before we replay? 32 * How long do we record before we replay?
33 */ 33 */
34#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) 34#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
35 35
36 36
37/** 37/**
@@ -145,10 +145,13 @@ switch_to_speaker (void *cls,
145 return; 145 return;
146 } 146 }
147 fprintf (stderr, "\nPlaying..."); 147 fprintf (stderr, "\nPlaying...");
148 for (rec=rec_head;NULL != rec; rec = rec->next) 148 for (rec=rec_head; NULL != rec; rec = rec->next)
149 {
150 fprintf (stderr, "<-%u\n", (unsigned int) rec->size);
149 speaker->play (speaker->cls, 151 speaker->play (speaker->cls,
150 rec->size, 152 rec->size,
151 &rec[1]); 153 &rec[1]);
154 }
152 GNUNET_SCHEDULER_cancel (st); 155 GNUNET_SCHEDULER_cancel (st);
153 st = GNUNET_SCHEDULER_add_delayed (TIMEOUT, 156 st = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
154 &do_shutdown, 157 &do_shutdown,
@@ -170,7 +173,7 @@ record (void *cls,
170{ 173{
171 struct Recording *rec; 174 struct Recording *rec;
172 175
173 fprintf (stderr, "."); 176 fprintf (stderr, "->%u\n", (unsigned int) data_size);
174 rec = GNUNET_malloc (sizeof (struct Recording) + data_size); 177 rec = GNUNET_malloc (sizeof (struct Recording) + data_size);
175 rec->size = data_size; 178 rec->size = data_size;
176 memcpy (&rec[1], data, data_size); 179 memcpy (&rec[1], data, data_size);
@@ -202,7 +205,9 @@ run (void *cls, char *const *args, const char *cfgfile,
202 st = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 205 st = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
203 &do_shutdown, 206 &do_shutdown,
204 NULL); 207 NULL);
205 fprintf (stderr, "Recording..."); 208 fprintf (stderr,
209 "Recording for %s...",
210 GNUNET_STRINGS_relative_time_to_string (TIMEOUT, GNUNET_YES));
206 if (GNUNET_OK != 211 if (GNUNET_OK !=
207 microphone->enable_microphone (microphone->cls, 212 microphone->enable_microphone (microphone->cls,
208 &record, NULL)) 213 &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 @@
41#define MAXLINE 4096 41#define MAXLINE 4096
42 42
43/** 43/**
44* GNUnet Message Tokenizer 44 * Pulseaudio specification. May change in the future.
45*/ 45 */
46#include "mst.c"
47
48/**
49* Pulseaudio specification. May change in the future.
50*/
51static pa_sample_spec sample_spec = { 46static pa_sample_spec sample_spec = {
52 .format = PA_SAMPLE_FLOAT32LE, 47 .format = PA_SAMPLE_FLOAT32LE,
53 .rate = 48000, 48 .rate = 48000,
54 .channels = 1 49 .channels = 1
55}; 50};
56 51
52
57/** 53/**
58 * Pulseaudio mainloop api 54 * Pulseaudio mainloop api
59 */ 55 */
@@ -123,40 +119,39 @@ static size_t buffer_index;
123/** 119/**
124 * Message callback 120 * Message callback
125 */ 121 */
126static void 122static int
127stdin_receiver (void *cls, 123stdin_receiver (void *cls,
124 void *client,
128 const struct GNUNET_MessageHeader *msg) 125 const struct GNUNET_MessageHeader *msg)
129{ 126{
130 struct AudioMessage *audio; 127 struct AudioMessage *audio;
131 128
132 switch (ntohs (msg->type)) 129 switch (ntohs (msg->type))
133 { 130 {
134 case GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO: 131 case GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO:
135 audio = (struct AudioMessage *) msg; 132 audio = (struct AudioMessage *) msg;
136 133
137 int len = 134 int len =
138 opus_decode_float (dec, 135 opus_decode_float (dec,
139 (const unsigned char *) &audio[1], 136 (const unsigned char *) &audio[1],
140 ntohs (audio->header.size) - sizeof (struct AudioMessage), 137 ntohs (audio->header.size) - sizeof (struct AudioMessage),
141 pcm_buffer, 138 pcm_buffer,
142 frame_size, 0); 139 frame_size, 0);
143 // FIXME: pcm_length != len??? 140 // FIXME: pcm_length != len???
144 if (pa_stream_write 141 if (pa_stream_write
145 (stream_out, (uint8_t *) pcm_buffer, pcm_length, NULL, 0, 142 (stream_out, (uint8_t *) pcm_buffer, pcm_length, NULL, 0,
146 PA_SEEK_RELATIVE) < 0) 143 PA_SEEK_RELATIVE) < 0)
147 { 144 {
148 145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 146 _("pa_stream_write() failed: %s\n"),
150 _("pa_stream_write() failed: %s\n"), 147 pa_strerror (pa_context_errno (context)));
151 pa_strerror (pa_context_errno (context))); 148 return GNUNET_OK;
152 return; 149 }
153 } 150 break;
154 151 default:
155 break; 152 break;
156 153 }
157 default: 154 return GNUNET_OK;
158 break;
159 }
160} 155}
161 156
162 157
@@ -402,13 +397,14 @@ int
402main (int argc, char *argv[]) 397main (int argc, char *argv[])
403{ 398{
404 char readbuf[MAXLINE]; 399 char readbuf[MAXLINE];
405 struct MessageStreamTokenizer *stdin_mst; 400 struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst;
406 401
402 fprintf (stderr, "HERE!\n");
407 GNUNET_assert (GNUNET_OK == 403 GNUNET_assert (GNUNET_OK ==
408 GNUNET_log_setup ("gnunet-helper-audio-playback", 404 GNUNET_log_setup ("gnunet-helper-audio-playback",
409 "WARNING", 405 "WARNING",
410 NULL)); 406 NULL));
411 stdin_mst = mst_create (&stdin_receiver, NULL); 407 stdin_mst = GNUNET_SERVER_mst_create (&stdin_receiver, NULL);
412 opus_init (); 408 opus_init ();
413 pa_init (); 409 pa_init ();
414 while (1) 410 while (1)
@@ -421,8 +417,10 @@ main (int argc, char *argv[])
421 strerror (errno)); 417 strerror (errno));
422 break; 418 break;
423 } 419 }
424 mst_receive (stdin_mst, readbuf, ret); 420 GNUNET_SERVER_mst_receive (stdin_mst, NULL,
421 readbuf, ret,
422 GNUNET_NO, GNUNET_NO);
425 } 423 }
426 mst_destroy (stdin_mst); 424 GNUNET_SERVER_mst_destroy (stdin_mst);
427 return 0; 425 return 0;
428} 426}
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 ()
462int 462int
463main (int argc, char *argv[]) 463main (int argc, char *argv[])
464{ 464{
465 fprintf (stderr, "HERE2!\n");
466
465 GNUNET_assert (GNUNET_OK == 467 GNUNET_assert (GNUNET_OK ==
466 GNUNET_log_setup ("gnunet-helper-audio-record", 468 GNUNET_log_setup ("gnunet-helper-audio-record",
467 "WARNING", 469 "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 @@
1/*
2 This file is part of GNUnet.
3 (C) 2008, 2011 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file conversation/mst.c
23 * @brief Message tokenizer
24 * @author Christian Grothoff
25 */
26
27#include <gnunet/platform.h>
28#include <gnunet/gnunet_constants.h>
29
30/**
31 * To what multiple do we align messages? 8 byte should suffice for everyone
32 * for now.
33 */
34#define ALIGN_FACTOR 8
35
36/**
37 * Smallest supported message.
38 */
39#define MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
40
41
42/**
43 * Functions with this signature are called whenever a
44 * complete message is received by the tokenizer.
45 *
46 * @param cls closure
47 * @param message the actual message
48 */
49typedef void (*MessageTokenizerCallback) (void *cls,
50 const struct
51 GNUNET_MessageHeader *
52 message);
53
54/**
55 * Handle to a message stream tokenizer.
56 */
57struct MessageStreamTokenizer
58{
59
60 /**
61 * Function to call on completed messages.
62 */
63 MessageTokenizerCallback cb;
64
65 /**
66 * Closure for cb.
67 */
68 void *cb_cls;
69
70 /**
71 * Size of the buffer (starting at 'hdr').
72 */
73 size_t curr_buf;
74
75 /**
76 * How many bytes in buffer have we already processed?
77 */
78 size_t off;
79
80 /**
81 * How many bytes in buffer are valid right now?
82 */
83 size_t pos;
84
85 /**
86 * Beginning of the buffer. Typed like this to force alignment.
87 */
88 struct GNUNET_MessageHeader *hdr;
89
90};
91
92
93/**
94 * Create a message stream tokenizer.
95 *
96 * @param cb function to call on completed messages
97 * @param cb_cls closure for cb
98 * @return handle to tokenizer
99 */
100static struct MessageStreamTokenizer *
101mst_create (MessageTokenizerCallback cb,
102 void *cb_cls)
103{
104 struct MessageStreamTokenizer *ret;
105
106 ret = malloc (sizeof (struct MessageStreamTokenizer));
107 if (NULL == ret)
108 {
109 fprintf (stderr, "Failed to allocate buffer for tokenizer\n");
110 exit (1);
111 }
112 ret->hdr = malloc (MIN_BUFFER_SIZE);
113 if (NULL == ret->hdr)
114 {
115 fprintf (stderr, "Failed to allocate buffer for alignment\n");
116 exit (1);
117 }
118 ret->curr_buf = MIN_BUFFER_SIZE;
119 ret->cb = cb;
120 ret->cb_cls = cb_cls;
121 return ret;
122}
123
124
125/**
126 * Add incoming data to the receive buffer and call the
127 * callback for all complete messages.
128 *
129 * @param mst tokenizer to use
130 * @param buf input data to add
131 * @param size number of bytes in buf
132 * @return GNUNET_OK if we are done processing (need more data)
133 * GNUNET_SYSERR if the data stream is corrupt
134 */
135static int
136mst_receive (struct MessageStreamTokenizer *mst,
137 const char *buf, size_t size)
138{
139 const struct GNUNET_MessageHeader *hdr;
140 size_t delta;
141 uint16_t want;
142 char *ibuf;
143 int need_align;
144 unsigned long offset;
145 int ret;
146
147 ret = GNUNET_OK;
148 ibuf = (char *) mst->hdr;
149 while (mst->pos > 0)
150 {
151do_align:
152 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
153 (0 != (mst->off % ALIGN_FACTOR)))
154 {
155 /* need to align or need more space */
156 mst->pos -= mst->off;
157 memmove (ibuf, &ibuf[mst->off], mst->pos);
158 mst->off = 0;
159 }
160 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
161 {
162 delta =
163 GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
164 (mst->pos - mst->off), size);
165 memcpy (&ibuf[mst->pos], buf, delta);
166 mst->pos += delta;
167 buf += delta;
168 size -= delta;
169 }
170 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
171 {
172 return GNUNET_OK;
173 }
174 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
175 want = ntohs (hdr->size);
176 if (want < sizeof (struct GNUNET_MessageHeader))
177 {
178 fprintf (stderr,
179 "Received invalid message from stdin\n");
180 exit (1);
181 }
182 if (mst->curr_buf - mst->off < want)
183 {
184 /* need more space */
185 mst->pos -= mst->off;
186 memmove (ibuf, &ibuf[mst->off], mst->pos);
187 mst->off = 0;
188 }
189 if (want > mst->curr_buf)
190 {
191 mst->hdr = realloc (mst->hdr, want);
192 if (NULL == mst->hdr)
193 {
194 fprintf (stderr, "Failed to allocate buffer for alignment\n");
195 exit (1);
196 }
197 ibuf = (char *) mst->hdr;
198 mst->curr_buf = want;
199 }
200 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
201 if (mst->pos - mst->off < want)
202 {
203 delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
204 memcpy (&ibuf[mst->pos], buf, delta);
205 mst->pos += delta;
206 buf += delta;
207 size -= delta;
208 }
209 if (mst->pos - mst->off < want)
210 {
211 return GNUNET_OK;
212 }
213 mst->cb (mst->cb_cls, hdr);
214 mst->off += want;
215 if (mst->off == mst->pos)
216 {
217 /* reset to beginning of buffer, it's free right now! */
218 mst->off = 0;
219 mst->pos = 0;
220 }
221 }
222 while (size > 0)
223 {
224 if (size < sizeof (struct GNUNET_MessageHeader))
225 break;
226 offset = (unsigned long) buf;
227 need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
228 if (GNUNET_NO == need_align)
229 {
230 /* can try to do zero-copy and process directly from original buffer */
231 hdr = (const struct GNUNET_MessageHeader *) buf;
232 want = ntohs (hdr->size);
233 if (want < sizeof (struct GNUNET_MessageHeader))
234 {
235 fprintf (stderr,
236 "Received invalid message from stdin\n");
237 exit (1);
238 }
239 if (size < want)
240 break; /* or not, buffer incomplete, so copy to private buffer... */
241 mst->cb (mst->cb_cls, hdr);
242 buf += want;
243 size -= want;
244 }
245 else
246 {
247 /* need to copy to private buffer to align;
248 * yes, we go a bit more spagetti than usual here */
249 goto do_align;
250 }
251 }
252 if (size > 0)
253 {
254 if (size + mst->pos > mst->curr_buf)
255 {
256 mst->hdr = realloc (mst->hdr, size + mst->pos);
257 if (NULL == mst->hdr)
258 {
259 fprintf (stderr, "Failed to allocate buffer for alignment\n");
260 exit (1);
261 }
262 ibuf = (char *) mst->hdr;
263 mst->curr_buf = size + mst->pos;
264 }
265 if (mst->pos + size > mst->curr_buf)
266 {
267 fprintf (stderr,
268 "Assertion failed\n");
269 exit (1);
270 }
271 memcpy (&ibuf[mst->pos], buf, size);
272 mst->pos += size;
273 }
274 return ret;
275}
276
277
278/**
279 * Destroys a tokenizer.
280 *
281 * @param mst tokenizer to destroy
282 */
283static void
284mst_destroy (struct MessageStreamTokenizer *mst)
285{
286 free (mst->hdr);
287 free (mst);
288}
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,
145 am = (const struct AudioMessage *) data; 145 am = (const struct AudioMessage *) data;
146 (void) GNUNET_HELPER_send (spe->playback_helper, 146 (void) GNUNET_HELPER_send (spe->playback_helper,
147 &am->header, 147 &am->header,
148 GNUNET_YES, 148 GNUNET_NO,
149 NULL, NULL); 149 NULL, NULL);
150} 150}
151 151