aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/gnunet-helper-audio-record.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-10-02 21:05:48 +0000
committerChristian Grothoff <christian@grothoff.org>2013-10-02 21:05:48 +0000
commit875342e4d0114e626dcf502362787d048d5e12fc (patch)
tree9ae7e303b3310a892c219b702f36005336ad5eff /src/conversation/gnunet-helper-audio-record.c
parent18b62beec7902acecaad00c0470f2f1d4d98b244 (diff)
downloadgnunet-875342e4d0114e626dcf502362787d048d5e12fc.tar.gz
gnunet-875342e4d0114e626dcf502362787d048d5e12fc.zip
-fix logging
Diffstat (limited to 'src/conversation/gnunet-helper-audio-record.c')
-rw-r--r--src/conversation/gnunet-helper-audio-record.c151
1 files changed, 78 insertions, 73 deletions
diff --git a/src/conversation/gnunet-helper-audio-record.c b/src/conversation/gnunet-helper-audio-record.c
index 5d4c0399a..a3e760794 100644
--- a/src/conversation/gnunet-helper-audio-record.c
+++ b/src/conversation/gnunet-helper-audio-record.c
@@ -74,11 +74,6 @@ static pa_stream *stream_in;
74static pa_io_event *stdio_event; 74static pa_io_event *stdio_event;
75 75
76/** 76/**
77 * Message tokenizer
78 */
79static struct MessageStreamTokenizer *stdin_mst;
80
81/**
82 * OPUS encoder 77 * OPUS encoder
83 */ 78 */
84static OpusEncoder *enc; 79static OpusEncoder *enc;
@@ -148,55 +143,57 @@ packetizer ()
148{ 143{
149 while (transmit_buffer_length >= transmit_buffer_index + pcm_length) 144 while (transmit_buffer_length >= transmit_buffer_index + pcm_length)
150 { 145 {
151 int ret; 146 ssize_t ret;
152 int len; 147 int len; // FIXME: int?
153 148 size_t msg_size;
154 size_t msg_size = sizeof (struct AudioMessage); 149
155 150 memcpy (pcm_buffer,
156 memcpy (pcm_buffer, 151 (float *) transmit_buffer +
157 (float *) transmit_buffer + 152 (transmit_buffer_index / sizeof (float)), pcm_length);
158 (transmit_buffer_index / sizeof (float)), pcm_length); 153 len =
159 len = 154 opus_encode_float (enc, pcm_buffer, frame_size, opus_data,
160 opus_encode_float (enc, pcm_buffer, frame_size, opus_data, 155 max_payload_bytes);
161 max_payload_bytes); 156 if (len > UINT16_MAX - sizeof (struct AudioMessage))
162 157 {
163 audio_message->length = len; 158 GNUNET_break (0);
164 memcpy (audio_message->audio, opus_data, len); 159 len = UINT16_MAX - sizeof (struct AudioMessage);
165
166 if ((ret = write (1, audio_message, msg_size)) != msg_size)
167 {
168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("write"));
169 return;
170 }
171
172 transmit_buffer_index += pcm_length;
173 } 160 }
161 msg_size = sizeof (struct AudioMessage) + len;
162 audio_message->header.size = htons ((uint16_t) msg_size);
163 memcpy (&audio_message[1], opus_data, len);
174 164
175 int new_size = transmit_buffer_length - transmit_buffer_index; 165 // FIXME: handle partial writes better...
176 166 if ((ret = write (1, audio_message, msg_size)) != msg_size)
177 if (0 != new_size)
178 { 167 {
168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("write"));
169 return;
170 }
171 transmit_buffer_index += pcm_length;
172 }
179 173
180 transmit_buffer = pa_xrealloc (transmit_buffer, new_size); 174 int new_size = transmit_buffer_length - transmit_buffer_index;
181 memcpy (transmit_buffer, transmit_buffer + transmit_buffer_index, 175 if (0 != new_size)
182 new_size); 176 {
183 177 transmit_buffer = pa_xrealloc (transmit_buffer, new_size);
184 transmit_buffer_index = 0; 178 memcpy (transmit_buffer, transmit_buffer + transmit_buffer_index,
185 transmit_buffer_length = new_size; 179 new_size);
186 } 180
187 181 transmit_buffer_index = 0;
182 transmit_buffer_length = new_size;
183 }
188} 184}
189 185
186
190/** 187/**
191* Pulseaudio callback when new data is available. 188 * Pulseaudio callback when new data is available.
192*/ 189 */
193static void 190static void
194stream_read_callback (pa_stream * s, size_t length, void *userdata) 191stream_read_callback (pa_stream * s, size_t length, void *userdata)
195{ 192{
196 const void *data; 193 const void *data;
194
197 GNUNET_assert (s); 195 GNUNET_assert (s);
198 GNUNET_assert (length > 0); 196 GNUNET_assert (length > 0);
199
200 if (stdio_event) 197 if (stdio_event)
201 mainloop_api->io_enable (stdio_event, PA_IO_EVENT_OUTPUT); 198 mainloop_api->io_enable (stdio_event, PA_IO_EVENT_OUTPUT);
202 199
@@ -212,39 +209,43 @@ stream_read_callback (pa_stream * s, size_t length, void *userdata)
212 GNUNET_assert (length > 0); 209 GNUNET_assert (length > 0);
213 210
214 if (transmit_buffer) 211 if (transmit_buffer)
215 { 212 {
216 transmit_buffer = 213 transmit_buffer =
217 pa_xrealloc (transmit_buffer, transmit_buffer_length + length); 214 pa_xrealloc (transmit_buffer, transmit_buffer_length + length);
218 memcpy ((uint8_t *) transmit_buffer + transmit_buffer_length, data, 215 memcpy ((uint8_t *) transmit_buffer + transmit_buffer_length, data,
219 length); 216 length);
220 transmit_buffer_length += length; 217 transmit_buffer_length += length;
221 } 218 }
222 else 219 else
223 { 220 {
224 transmit_buffer = pa_xmalloc (length); 221 transmit_buffer = pa_xmalloc (length);
225 memcpy (transmit_buffer, data, length); 222 memcpy (transmit_buffer, data, length);
226 transmit_buffer_length = length; 223 transmit_buffer_length = length;
227 transmit_buffer_index = 0; 224 transmit_buffer_index = 0;
228 } 225 }
229
230 pa_stream_drop (s); 226 pa_stream_drop (s);
231 packetizer (); 227 packetizer ();
232} 228}
233 229
230
234/** 231/**
235* Exit callback for SIGTERM and SIGINT 232 * Exit callback for SIGTERM and SIGINT
236*/ 233 */
237static void 234static void
238exit_signal_callback (pa_mainloop_api * m, pa_signal_event * e, int sig, 235exit_signal_callback (pa_mainloop_api * m,
236 pa_signal_event * e,
237 int sig,
239 void *userdata) 238 void *userdata)
240{ 239{
241 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Got signal, exiting.\n")); 240 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
241 _("Got signal, exiting.\n"));
242 quit (1); 242 quit (1);
243} 243}
244 244
245
245/** 246/**
246* Pulseaudio stream state callback 247 * Pulseaudio stream state callback
247*/ 248 */
248static void 249static void
249stream_state_callback (pa_stream * s, void *userdata) 250stream_state_callback (pa_stream * s, void *userdata)
250{ 251{
@@ -305,11 +306,13 @@ stream_state_callback (pa_stream * s, void *userdata)
305 } 306 }
306} 307}
307 308
309
308/** 310/**
309* Pulseaudio context state callback 311 * Pulseaudio context state callback
310*/ 312 */
311static void 313static void
312context_state_callback (pa_context * c, void *userdata) 314context_state_callback (pa_context * c,
315 void *userdata)
313{ 316{
314 GNUNET_assert (c); 317 GNUNET_assert (c);
315 318
@@ -370,13 +373,13 @@ context_state_callback (pa_context * c, void *userdata)
370 373
371fail: 374fail:
372 quit (1); 375 quit (1);
373
374} 376}
375 377
378
376/** 379/**
377 * Pulsaudio init 380 * Pulsaudio init
378 */ 381 */
379void 382static void
380pa_init () 383pa_init ()
381{ 384{
382 int r; 385 int r;
@@ -425,28 +428,26 @@ pa_init ()
425 } 428 }
426} 429}
427 430
431
428/** 432/**
429 * OPUS init 433 * OPUS init
430 */ 434 */
431void 435static void
432opus_init () 436opus_init ()
433{ 437{
434 opus_int32 sampling_rate = 48000; 438 opus_int32 sampling_rate = 48000;
435 frame_size = sampling_rate / 50;
436 int channels = 1; 439 int channels = 1;
437
438 pcm_length = frame_size * channels * sizeof (float);
439
440 int err; 440 int err;
441 441
442 frame_size = sampling_rate / 50;
443 pcm_length = frame_size * channels * sizeof (float);
442 enc = 444 enc =
443 opus_encoder_create (sampling_rate, channels, OPUS_APPLICATION_VOIP, 445 opus_encoder_create (sampling_rate, channels, OPUS_APPLICATION_VOIP,
444 &err); 446 &err);
445 pcm_buffer = (float *) pa_xmalloc (pcm_length); 447 pcm_buffer = (float *) pa_xmalloc (pcm_length);
446 opus_data = (unsigned char *) calloc (max_payload_bytes, sizeof (char)); 448 opus_data = (unsigned char *) calloc (max_payload_bytes, sizeof (char));
447 449
448 audio_message = pa_xmalloc (sizeof (struct AudioMessage)); 450 audio_message = pa_xmalloc (UINT16_MAX);
449 audio_message->header.size = htons (sizeof (struct AudioMessage));
450 audio_message->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO); 451 audio_message->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
451} 452}
452 453
@@ -461,6 +462,10 @@ opus_init ()
461int 462int
462main (int argc, char *argv[]) 463main (int argc, char *argv[])
463{ 464{
465 GNUNET_assert (GNUNET_OK ==
466 GNUNET_log_setup ("gnunet-helper-audio-record",
467 "WARNING",
468 NULL));
464 opus_init (); 469 opus_init ();
465 pa_init (); 470 pa_init ();
466 471