aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/gnunet-helper-audio-record.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/conversation/gnunet-helper-audio-record.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/conversation/gnunet-helper-audio-record.c')
-rw-r--r--src/conversation/gnunet-helper-audio-record.c658
1 files changed, 331 insertions, 327 deletions
diff --git a/src/conversation/gnunet-helper-audio-record.c b/src/conversation/gnunet-helper-audio-record.c
index 0cfc04578..16311cf69 100644
--- a/src/conversation/gnunet-helper-audio-record.c
+++ b/src/conversation/gnunet-helper-audio-record.c
@@ -149,7 +149,8 @@ static pa_sample_spec sample_spec = {
149GNUNET_NETWORK_STRUCT_BEGIN 149GNUNET_NETWORK_STRUCT_BEGIN
150 150
151/* OggOpus spec says the numbers must be in little-endian order */ 151/* OggOpus spec says the numbers must be in little-endian order */
152struct OpusHeadPacket { 152struct OpusHeadPacket
153{
153 uint8_t magic[8]; 154 uint8_t magic[8];
154 uint8_t version; 155 uint8_t version;
155 uint8_t channels; 156 uint8_t channels;
@@ -159,7 +160,8 @@ struct OpusHeadPacket {
159 uint8_t channel_mapping; 160 uint8_t channel_mapping;
160}; 161};
161 162
162struct OpusCommentsPacket { 163struct OpusCommentsPacket
164{
163 uint8_t magic[8]; 165 uint8_t magic[8];
164 uint32_t vendor_length; 166 uint32_t vendor_length;
165 /* followed by: 167 /* followed by:
@@ -265,63 +267,64 @@ static int dump_pure_ogg;
265 * Pulseaudio shutdown task 267 * Pulseaudio shutdown task
266 */ 268 */
267static void 269static void
268quit(int ret) 270quit (int ret)
269{ 271{
270 mainloop_api->quit(mainloop_api, 272 mainloop_api->quit (mainloop_api,
271 ret); 273 ret);
272 exit(ret); 274 exit (ret);
273} 275}
274 276
275 277
276static void 278static void
277write_data(const char *ptr, 279write_data (const char *ptr,
278 size_t msg_size) 280 size_t msg_size)
279{ 281{
280 ssize_t ret; 282 ssize_t ret;
281 size_t off; 283 size_t off;
282 284
283 off = 0; 285 off = 0;
284 while (off < msg_size) 286 while (off < msg_size)
287 {
288 ret = write (STDOUT_FILENO,
289 &ptr[off],
290 msg_size - off);
291 if (0 >= ret)
285 { 292 {
286 ret = write(STDOUT_FILENO, 293 if (-1 == ret)
287 &ptr[off], 294 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
288 msg_size - off); 295 "write");
289 if (0 >= ret) 296 quit (2);
290 {
291 if (-1 == ret)
292 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR,
293 "write");
294 quit(2);
295 }
296 off += ret;
297 } 297 }
298 off += ret;
299 }
298} 300}
299 301
300 302
301static void 303static void
302write_page(ogg_page *og) 304write_page (ogg_page *og)
303{ 305{
304 static unsigned long long toff; 306 static unsigned long long toff;
305 size_t msg_size; 307 size_t msg_size;
306 308
307 msg_size = sizeof(struct AudioMessage) + og->header_len + og->body_len; 309 msg_size = sizeof(struct AudioMessage) + og->header_len + og->body_len;
308 audio_message->header.size = htons((uint16_t)msg_size); 310 audio_message->header.size = htons ((uint16_t) msg_size);
309 GNUNET_memcpy(&audio_message[1], og->header, og->header_len); 311 GNUNET_memcpy (&audio_message[1], og->header, og->header_len);
310 GNUNET_memcpy(((char *)&audio_message[1]) + og->header_len, og->body, og->body_len); 312 GNUNET_memcpy (((char *) &audio_message[1]) + og->header_len, og->body,
313 og->body_len);
311 314
312 toff += msg_size; 315 toff += msg_size;
313 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Sending %u bytes of audio data (total: %llu)\n", 317 "Sending %u bytes of audio data (total: %llu)\n",
315 (unsigned int)msg_size, 318 (unsigned int) msg_size,
316 toff); 319 toff);
317#ifdef DEBUG_RECORD_PURE_OGG 320#ifdef DEBUG_RECORD_PURE_OGG
318 if (dump_pure_ogg) 321 if (dump_pure_ogg)
319 write_data((const char *)&audio_message[1], 322 write_data ((const char *) &audio_message[1],
320 og->header_len + og->body_len); 323 og->header_len + og->body_len);
321 else 324 else
322#endif 325#endif
323 write_data((const char *)audio_message, 326 write_data ((const char *) audio_message,
324 msg_size); 327 msg_size);
325} 328}
326 329
327 330
@@ -329,7 +332,7 @@ write_page(ogg_page *og)
329 * Creates OPUS packets from PCM data 332 * Creates OPUS packets from PCM data
330 */ 333 */
331static void 334static void
332packetizer() 335packetizer ()
333{ 336{
334 char *nbuf; 337 char *nbuf;
335 size_t new_size; 338 size_t new_size;
@@ -338,68 +341,68 @@ packetizer()
338 ogg_page og; 341 ogg_page og;
339 342
340 while (transmit_buffer_length >= transmit_buffer_index + pcm_length) 343 while (transmit_buffer_length >= transmit_buffer_index + pcm_length)
344 {
345 GNUNET_memcpy (pcm_buffer,
346 &transmit_buffer[transmit_buffer_index],
347 pcm_length);
348 transmit_buffer_index += pcm_length;
349 len =
350 opus_encode_float (enc, pcm_buffer, FRAME_SIZE, opus_data,
351 MAX_PAYLOAD_BYTES);
352
353 if (len < 0)
341 { 354 {
342 GNUNET_memcpy(pcm_buffer, 355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
343 &transmit_buffer[transmit_buffer_index], 356 _ ("opus_encode_float() failed: %s. Aborting\n"),
344 pcm_length); 357 opus_strerror (len));
345 transmit_buffer_index += pcm_length; 358 quit (5);
346 len =
347 opus_encode_float(enc, pcm_buffer, FRAME_SIZE, opus_data,
348 MAX_PAYLOAD_BYTES);
349
350 if (len < 0)
351 {
352 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
353 _("opus_encode_float() failed: %s. Aborting\n"),
354 opus_strerror(len));
355 quit(5);
356 }
357 if (((uint32_t)len) > UINT16_MAX - sizeof(struct AudioMessage))
358 {
359 GNUNET_break(0);
360 continue;
361 }
362
363 /* As per OggOpus spec, granule is calculated as if the audio
364 had 48kHz sampling rate. */
365 enc_granulepos += FRAME_SIZE * 48000 / SAMPLING_RATE;
366
367 op.packet = (unsigned char *)opus_data;
368 op.bytes = len;
369 op.b_o_s = 0;
370 op.e_o_s = 0;
371 op.granulepos = enc_granulepos;
372 op.packetno = packet_id++;
373 ogg_stream_packetin(&os, &op);
374
375 while (ogg_stream_flush_fill(&os, &og, PAGE_WATERLINE))
376 {
377 if (((unsigned long long)og.header_len) +
378 ((unsigned long long)og.body_len) >
379 UINT16_MAX - sizeof(struct AudioMessage))
380 {
381 GNUNET_assert(0);
382 continue;
383 }
384 write_page(&og);
385 }
386 } 359 }
387 360 if (((uint32_t) len) > UINT16_MAX - sizeof(struct AudioMessage))
388 new_size = transmit_buffer_length - transmit_buffer_index;
389 if (0 != new_size)
390 { 361 {
391 nbuf = pa_xmalloc(new_size); 362 GNUNET_break (0);
392 memmove(nbuf, 363 continue;
393 &transmit_buffer[transmit_buffer_index],
394 new_size);
395 pa_xfree(transmit_buffer);
396 transmit_buffer = nbuf;
397 } 364 }
398 else 365
366 /* As per OggOpus spec, granule is calculated as if the audio
367 had 48kHz sampling rate. */
368 enc_granulepos += FRAME_SIZE * 48000 / SAMPLING_RATE;
369
370 op.packet = (unsigned char *) opus_data;
371 op.bytes = len;
372 op.b_o_s = 0;
373 op.e_o_s = 0;
374 op.granulepos = enc_granulepos;
375 op.packetno = packet_id++;
376 ogg_stream_packetin (&os, &op);
377
378 while (ogg_stream_flush_fill (&os, &og, PAGE_WATERLINE))
399 { 379 {
400 pa_xfree(transmit_buffer); 380 if (((unsigned long long) og.header_len)
401 transmit_buffer = NULL; 381 + ((unsigned long long) og.body_len) >
382 UINT16_MAX - sizeof(struct AudioMessage))
383 {
384 GNUNET_assert (0);
385 continue;
386 }
387 write_page (&og);
402 } 388 }
389 }
390
391 new_size = transmit_buffer_length - transmit_buffer_index;
392 if (0 != new_size)
393 {
394 nbuf = pa_xmalloc (new_size);
395 memmove (nbuf,
396 &transmit_buffer[transmit_buffer_index],
397 new_size);
398 pa_xfree (transmit_buffer);
399 transmit_buffer = nbuf;
400 }
401 else
402 {
403 pa_xfree (transmit_buffer);
404 transmit_buffer = NULL;
405 }
403 transmit_buffer_index = 0; 406 transmit_buffer_index = 0;
404 transmit_buffer_length = new_size; 407 transmit_buffer_length = new_size;
405} 408}
@@ -409,51 +412,51 @@ packetizer()
409 * Pulseaudio callback when new data is available. 412 * Pulseaudio callback when new data is available.
410 */ 413 */
411static void 414static void
412stream_read_callback(pa_stream * s, 415stream_read_callback (pa_stream *s,
413 size_t length, 416 size_t length,
414 void *userdata) 417 void *userdata)
415{ 418{
416 const void *data; 419 const void *data;
417 420
418 (void)userdata; 421 (void) userdata;
419 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "Got %u/%d bytes of PCM data\n", 423 "Got %u/%d bytes of PCM data\n",
421 (unsigned int)length, 424 (unsigned int) length,
422 pcm_length); 425 pcm_length);
423 426
424 GNUNET_assert(NULL != s); 427 GNUNET_assert (NULL != s);
425 GNUNET_assert(length > 0); 428 GNUNET_assert (length > 0);
426 if (stdio_event) 429 if (stdio_event)
427 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT); 430 mainloop_api->io_enable (stdio_event, PA_IO_EVENT_OUTPUT);
428 431
429 if (pa_stream_peek(s, (const void **)&data, &length) < 0) 432 if (pa_stream_peek (s, (const void **) &data, &length) < 0)
430 { 433 {
431 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
432 _("pa_stream_peek() failed: %s\n"), 435 _ ("pa_stream_peek() failed: %s\n"),
433 pa_strerror(pa_context_errno(context))); 436 pa_strerror (pa_context_errno (context)));
434 quit(1); 437 quit (1);
435 return; 438 return;
436 } 439 }
437 GNUNET_assert(NULL != data); 440 GNUNET_assert (NULL != data);
438 GNUNET_assert(length > 0); 441 GNUNET_assert (length > 0);
439 if (NULL != transmit_buffer) 442 if (NULL != transmit_buffer)
440 { 443 {
441 transmit_buffer = pa_xrealloc(transmit_buffer, 444 transmit_buffer = pa_xrealloc (transmit_buffer,
442 transmit_buffer_length + length); 445 transmit_buffer_length + length);
443 GNUNET_memcpy(&transmit_buffer[transmit_buffer_length], 446 GNUNET_memcpy (&transmit_buffer[transmit_buffer_length],
444 data, 447 data,
445 length); 448 length);
446 transmit_buffer_length += length; 449 transmit_buffer_length += length;
447 } 450 }
448 else 451 else
449 { 452 {
450 transmit_buffer = pa_xmalloc(length); 453 transmit_buffer = pa_xmalloc (length);
451 GNUNET_memcpy(transmit_buffer, data, length); 454 GNUNET_memcpy (transmit_buffer, data, length);
452 transmit_buffer_length = length; 455 transmit_buffer_length = length;
453 transmit_buffer_index = 0; 456 transmit_buffer_index = 0;
454 } 457 }
455 pa_stream_drop(s); 458 pa_stream_drop (s);
456 packetizer(); 459 packetizer ();
457} 460}
458 461
459 462
@@ -461,18 +464,18 @@ stream_read_callback(pa_stream * s,
461 * Exit callback for SIGTERM and SIGINT 464 * Exit callback for SIGTERM and SIGINT
462 */ 465 */
463static void 466static void
464exit_signal_callback(pa_mainloop_api * m, 467exit_signal_callback (pa_mainloop_api *m,
465 pa_signal_event * e, 468 pa_signal_event *e,
466 int sig, 469 int sig,
467 void *userdata) 470 void *userdata)
468{ 471{
469 (void)m; 472 (void) m;
470 (void)e; 473 (void) e;
471 (void)sig; 474 (void) sig;
472 (void)userdata; 475 (void) userdata;
473 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 476 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
474 _("Got signal, exiting.\n")); 477 _ ("Got signal, exiting.\n"));
475 quit(1); 478 quit (1);
476} 479}
477 480
478 481
@@ -480,61 +483,61 @@ exit_signal_callback(pa_mainloop_api * m,
480 * Pulseaudio stream state callback 483 * Pulseaudio stream state callback
481 */ 484 */
482static void 485static void
483stream_state_callback(pa_stream * s, 486stream_state_callback (pa_stream *s,
484 void *userdata) 487 void *userdata)
485{ 488{
486 (void)userdata; 489 (void) userdata;
487 GNUNET_assert(NULL != s); 490 GNUNET_assert (NULL != s);
488 switch (pa_stream_get_state(s)) 491 switch (pa_stream_get_state (s))
489 { 492 {
490 case PA_STREAM_CREATING: 493 case PA_STREAM_CREATING:
491 case PA_STREAM_TERMINATED: 494 case PA_STREAM_TERMINATED:
492 break; 495 break;
493 496
494 case PA_STREAM_READY: 497 case PA_STREAM_READY:
495 { 498 {
496 const pa_buffer_attr *a; 499 const pa_buffer_attr *a;
497 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX]; 500 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX];
498 char sst[PA_SAMPLE_SPEC_SNPRINT_MAX]; 501 char sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
499 502
500 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 503 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
501 _("Stream successfully created.\n")); 504 _ ("Stream successfully created.\n"));
502 505
503 if (!(a = pa_stream_get_buffer_attr(s))) 506 if (! (a = pa_stream_get_buffer_attr (s)))
504 { 507 {
505 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 508 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
506 _("pa_stream_get_buffer_attr() failed: %s\n"), 509 _ ("pa_stream_get_buffer_attr() failed: %s\n"),
507 pa_strerror(pa_context_errno 510 pa_strerror (pa_context_errno
508 (pa_stream_get_context(s)))); 511 (pa_stream_get_context (s))));
509 } 512 }
510 else 513 else
511 { 514 {
512 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 515 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
513 _("Buffer metrics: maxlength=%u, fragsize=%u\n"), 516 _ ("Buffer metrics: maxlength=%u, fragsize=%u\n"),
514 a->maxlength, a->fragsize); 517 a->maxlength, a->fragsize);
515 } 518 }
516 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 519 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
517 _("Using sample spec '%s', channel map '%s'.\n"), 520 _ ("Using sample spec '%s', channel map '%s'.\n"),
518 pa_sample_spec_snprint(sst, sizeof(sst), 521 pa_sample_spec_snprint (sst, sizeof(sst),
519 pa_stream_get_sample_spec(s)), 522 pa_stream_get_sample_spec (s)),
520 pa_channel_map_snprint(cmt, sizeof(cmt), 523 pa_channel_map_snprint (cmt, sizeof(cmt),
521 pa_stream_get_channel_map(s))); 524 pa_stream_get_channel_map (s)));
522 525
523 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 526 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
524 _("Connected to device %s (%u, %ssuspended).\n"), 527 _ ("Connected to device %s (%u, %ssuspended).\n"),
525 pa_stream_get_device_name(s), 528 pa_stream_get_device_name (s),
526 pa_stream_get_device_index(s), 529 pa_stream_get_device_index (s),
527 pa_stream_is_suspended(s) ? "" : "not "); 530 pa_stream_is_suspended (s) ? "" : "not ");
528 } 531 }
529 break; 532 break;
530 533
531 case PA_STREAM_FAILED: 534 case PA_STREAM_FAILED:
532 default: 535 default:
533 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
534 _("Stream error: %s\n"), 537 _ ("Stream error: %s\n"),
535 pa_strerror(pa_context_errno(pa_stream_get_context(s)))); 538 pa_strerror (pa_context_errno (pa_stream_get_context (s))));
536 quit(1); 539 quit (1);
537 } 540 }
538} 541}
539 542
540 543
@@ -542,67 +545,67 @@ stream_state_callback(pa_stream * s,
542 * Pulseaudio context state callback 545 * Pulseaudio context state callback
543 */ 546 */
544static void 547static void
545context_state_callback(pa_context * c, 548context_state_callback (pa_context *c,
546 void *userdata) 549 void *userdata)
547{ 550{
548 (void)userdata; 551 (void) userdata;
549 GNUNET_assert(c); 552 GNUNET_assert (c);
550 553
551 switch (pa_context_get_state(c)) 554 switch (pa_context_get_state (c))
552 { 555 {
553 case PA_CONTEXT_CONNECTING: 556 case PA_CONTEXT_CONNECTING:
554 case PA_CONTEXT_AUTHORIZING: 557 case PA_CONTEXT_AUTHORIZING:
555 case PA_CONTEXT_SETTING_NAME: 558 case PA_CONTEXT_SETTING_NAME:
556 break; 559 break;
557 560
558 case PA_CONTEXT_READY: 561 case PA_CONTEXT_READY:
559 { 562 {
560 int r; 563 int r;
561 pa_buffer_attr na; 564 pa_buffer_attr na;
562 565
563 GNUNET_assert(!stream_in); 566 GNUNET_assert (! stream_in);
564 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 567 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
565 _("Connection established.\n")); 568 _ ("Connection established.\n"));
566 if (!(stream_in = 569 if (! (stream_in =
567 pa_stream_new(c, "GNUNET_VoIP recorder", &sample_spec, NULL))) 570 pa_stream_new (c, "GNUNET_VoIP recorder", &sample_spec, NULL)))
568 { 571 {
569 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
570 _("pa_stream_new() failed: %s\n"), 573 _ ("pa_stream_new() failed: %s\n"),
571 pa_strerror(pa_context_errno(c))); 574 pa_strerror (pa_context_errno (c)));
572 goto fail; 575 goto fail;
573 } 576 }
574 pa_stream_set_state_callback(stream_in, &stream_state_callback, NULL); 577 pa_stream_set_state_callback (stream_in, &stream_state_callback, NULL);
575 pa_stream_set_read_callback(stream_in, &stream_read_callback, NULL); 578 pa_stream_set_read_callback (stream_in, &stream_read_callback, NULL);
576 memset(&na, 0, sizeof(na)); 579 memset (&na, 0, sizeof(na));
577 na.maxlength = UINT32_MAX; 580 na.maxlength = UINT32_MAX;
578 na.fragsize = pcm_length; 581 na.fragsize = pcm_length;
579 if ((r = pa_stream_connect_record(stream_in, NULL, &na, 582 if ((r = pa_stream_connect_record (stream_in, NULL, &na,
580 PA_STREAM_ADJUST_LATENCY)) < 0) 583 PA_STREAM_ADJUST_LATENCY)) < 0)
581 { 584 {
582 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 585 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
583 _("pa_stream_connect_record() failed: %s\n"), 586 _ ("pa_stream_connect_record() failed: %s\n"),
584 pa_strerror(pa_context_errno(c))); 587 pa_strerror (pa_context_errno (c)));
585 goto fail; 588 goto fail;
586 } 589 }
587 590
588 break; 591 break;
589 } 592 }
590 593
591 case PA_CONTEXT_TERMINATED: 594 case PA_CONTEXT_TERMINATED:
592 quit(0); 595 quit (0);
593 break; 596 break;
594 597
595 case PA_CONTEXT_FAILED: 598 case PA_CONTEXT_FAILED:
596 default: 599 default:
597 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
598 _("Connection failure: %s\n"), 601 _ ("Connection failure: %s\n"),
599 pa_strerror(pa_context_errno(c))); 602 pa_strerror (pa_context_errno (c)));
600 goto fail; 603 goto fail;
601 } 604 }
602 return; 605 return;
603 606
604fail: 607fail:
605 quit(1); 608 quit (1);
606} 609}
607 610
608 611
@@ -610,49 +613,49 @@ fail:
610 * Pulsaudio init 613 * Pulsaudio init
611 */ 614 */
612static void 615static void
613pa_init() 616pa_init ()
614{ 617{
615 int r; 618 int r;
616 int i; 619 int i;
617 620
618 if (!pa_sample_spec_valid(&sample_spec)) 621 if (! pa_sample_spec_valid (&sample_spec))
619 { 622 {
620 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 623 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
621 _("Wrong Spec\n")); 624 _ ("Wrong Spec\n"));
622 } 625 }
623 /* set up main record loop */ 626 /* set up main record loop */
624 if (!(m = pa_mainloop_new())) 627 if (! (m = pa_mainloop_new ()))
625 { 628 {
626 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 629 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
627 _("pa_mainloop_new() failed.\n")); 630 _ ("pa_mainloop_new() failed.\n"));
628 } 631 }
629 mainloop_api = pa_mainloop_get_api(m); 632 mainloop_api = pa_mainloop_get_api (m);
630 633
631 /* listen to signals */ 634 /* listen to signals */
632 r = pa_signal_init(mainloop_api); 635 r = pa_signal_init (mainloop_api);
633 GNUNET_assert(r == 0); 636 GNUNET_assert (r == 0);
634 pa_signal_new(SIGINT, &exit_signal_callback, NULL); 637 pa_signal_new (SIGINT, &exit_signal_callback, NULL);
635 pa_signal_new(SIGTERM, &exit_signal_callback, NULL); 638 pa_signal_new (SIGTERM, &exit_signal_callback, NULL);
636 639
637 /* connect to the main pulseaudio context */ 640 /* connect to the main pulseaudio context */
638 641
639 if (!(context = pa_context_new(mainloop_api, "GNUNET VoIP"))) 642 if (! (context = pa_context_new (mainloop_api, "GNUNET VoIP")))
640 { 643 {
641 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
642 _("pa_context_new() failed.\n")); 645 _ ("pa_context_new() failed.\n"));
643 } 646 }
644 pa_context_set_state_callback(context, &context_state_callback, NULL); 647 pa_context_set_state_callback (context, &context_state_callback, NULL);
645 if (pa_context_connect(context, NULL, 0, NULL) < 0) 648 if (pa_context_connect (context, NULL, 0, NULL) < 0)
646 { 649 {
647 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 650 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
648 _("pa_context_connect() failed: %s\n"), 651 _ ("pa_context_connect() failed: %s\n"),
649 pa_strerror(pa_context_errno(context))); 652 pa_strerror (pa_context_errno (context)));
650 } 653 }
651 if (pa_mainloop_run(m, &i) < 0) 654 if (pa_mainloop_run (m, &i) < 0)
652 { 655 {
653 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 656 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
654 _("pa_mainloop_run() failed.\n")); 657 _ ("pa_mainloop_run() failed.\n"));
655 } 658 }
656} 659}
657 660
658 661
@@ -660,45 +663,46 @@ pa_init()
660 * OPUS init 663 * OPUS init
661 */ 664 */
662static void 665static void
663opus_init() 666opus_init ()
664{ 667{
665 int err; 668 int err;
666 669
667 pcm_length = FRAME_SIZE * CHANNELS * sizeof(float); 670 pcm_length = FRAME_SIZE * CHANNELS * sizeof(float);
668 pcm_buffer = pa_xmalloc(pcm_length); 671 pcm_buffer = pa_xmalloc (pcm_length);
669 opus_data = GNUNET_malloc(MAX_PAYLOAD_BYTES); 672 opus_data = GNUNET_malloc (MAX_PAYLOAD_BYTES);
670 enc = opus_encoder_create(SAMPLING_RATE, 673 enc = opus_encoder_create (SAMPLING_RATE,
671 CHANNELS, 674 CHANNELS,
672 CONV_OPUS_APP_TYPE, 675 CONV_OPUS_APP_TYPE,
673 &err); 676 &err);
674 opus_encoder_ctl(enc, 677 opus_encoder_ctl (enc,
675 OPUS_SET_PACKET_LOSS_PERC(CONV_OPUS_PACKET_LOSS_PERCENTAGE)); 678 OPUS_SET_PACKET_LOSS_PERC (
676 opus_encoder_ctl(enc, 679 CONV_OPUS_PACKET_LOSS_PERCENTAGE));
677 OPUS_SET_COMPLEXITY(CONV_OPUS_ENCODING_COMPLEXITY)); 680 opus_encoder_ctl (enc,
678 opus_encoder_ctl(enc, 681 OPUS_SET_COMPLEXITY (CONV_OPUS_ENCODING_COMPLEXITY));
679 OPUS_SET_INBAND_FEC(CONV_OPUS_INBAND_FEC)); 682 opus_encoder_ctl (enc,
680 opus_encoder_ctl(enc, 683 OPUS_SET_INBAND_FEC (CONV_OPUS_INBAND_FEC));
681 OPUS_SET_SIGNAL(CONV_OPUS_SIGNAL)); 684 opus_encoder_ctl (enc,
685 OPUS_SET_SIGNAL (CONV_OPUS_SIGNAL));
682} 686}
683 687
684 688
685static void 689static void
686ogg_init() 690ogg_init ()
687{ 691{
688 int serialno; 692 int serialno;
689 struct OpusHeadPacket headpacket; 693 struct OpusHeadPacket headpacket;
690 struct OpusCommentsPacket *commentspacket; 694 struct OpusCommentsPacket *commentspacket;
691 size_t commentspacket_len; 695 size_t commentspacket_len;
692 696
693 serialno = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 697 serialno = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
694 0x7FFFFFFF); 698 0x7FFFFFFF);
695 /*Initialize Ogg stream struct*/ 699 /*Initialize Ogg stream struct*/
696 if (-1 == ogg_stream_init(&os, serialno)) 700 if (-1 == ogg_stream_init (&os, serialno))
697 { 701 {
698 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 702 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
699 _("ogg_stream_init() failed.\n")); 703 _ ("ogg_stream_init() failed.\n"));
700 exit(3); 704 exit (3);
701 } 705 }
702 706
703 packet_id = 0; 707 packet_id = 0;
704 708
@@ -709,64 +713,64 @@ ogg_init()
709 const char *opusver; 713 const char *opusver;
710 int vendor_length; 714 int vendor_length;
711 715
712 GNUNET_memcpy(headpacket.magic, "OpusHead", 8); 716 GNUNET_memcpy (headpacket.magic, "OpusHead", 8);
713 headpacket.version = 1; 717 headpacket.version = 1;
714 headpacket.channels = CHANNELS; 718 headpacket.channels = CHANNELS;
715 headpacket.preskip = GNUNET_htole16(0); 719 headpacket.preskip = GNUNET_htole16 (0);
716 headpacket.sampling_rate = GNUNET_htole32(SAMPLING_RATE); 720 headpacket.sampling_rate = GNUNET_htole32 (SAMPLING_RATE);
717 headpacket.gain = GNUNET_htole16(0); 721 headpacket.gain = GNUNET_htole16 (0);
718 headpacket.channel_mapping = 0; /* Mono or stereo */ 722 headpacket.channel_mapping = 0; /* Mono or stereo */
719 723
720 op.packet = (unsigned char *)&headpacket; 724 op.packet = (unsigned char *) &headpacket;
721 op.bytes = sizeof(headpacket); 725 op.bytes = sizeof(headpacket);
722 op.b_o_s = 1; 726 op.b_o_s = 1;
723 op.e_o_s = 0; 727 op.e_o_s = 0;
724 op.granulepos = 0; 728 op.granulepos = 0;
725 op.packetno = packet_id++; 729 op.packetno = packet_id++;
726 ogg_stream_packetin(&os, &op); 730 ogg_stream_packetin (&os, &op);
727 731
728 /* Head packet must be alone on its page */ 732 /* Head packet must be alone on its page */
729 while (ogg_stream_flush(&os, &og)) 733 while (ogg_stream_flush (&os, &og))
730 { 734 {
731 write_page(&og); 735 write_page (&og);
732 } 736 }
733 737
734 commentspacket_len = sizeof(*commentspacket); 738 commentspacket_len = sizeof(*commentspacket);
735 opusver = opus_get_version_string(); 739 opusver = opus_get_version_string ();
736 vendor_length = strlen(opusver); 740 vendor_length = strlen (opusver);
737 commentspacket_len += vendor_length; 741 commentspacket_len += vendor_length;
738 commentspacket_len += sizeof(uint32_t); 742 commentspacket_len += sizeof(uint32_t);
739 743
740 commentspacket = (struct OpusCommentsPacket *)malloc(commentspacket_len); 744 commentspacket = (struct OpusCommentsPacket *) malloc (commentspacket_len);
741 if (NULL == commentspacket) 745 if (NULL == commentspacket)
742 { 746 {
743 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 747 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
744 _("Failed to allocate %u bytes for second packet\n"), 748 _ ("Failed to allocate %u bytes for second packet\n"),
745 (unsigned int)commentspacket_len); 749 (unsigned int) commentspacket_len);
746 exit(5); 750 exit (5);
747 } 751 }
748 752
749 GNUNET_memcpy(commentspacket->magic, "OpusTags", 8); 753 GNUNET_memcpy (commentspacket->magic, "OpusTags", 8);
750 commentspacket->vendor_length = GNUNET_htole32(vendor_length); 754 commentspacket->vendor_length = GNUNET_htole32 (vendor_length);
751 GNUNET_memcpy(&commentspacket[1], opusver, vendor_length); 755 GNUNET_memcpy (&commentspacket[1], opusver, vendor_length);
752 *(uint32_t *)&((char *)&commentspacket[1])[vendor_length] = \ 756 *(uint32_t *) &((char *) &commentspacket[1])[vendor_length] = \
753 GNUNET_htole32(0); /* no tags */ 757 GNUNET_htole32 (0); /* no tags */
754 758
755 op.packet = (unsigned char *)commentspacket; 759 op.packet = (unsigned char *) commentspacket;
756 op.bytes = commentspacket_len; 760 op.bytes = commentspacket_len;
757 op.b_o_s = 0; 761 op.b_o_s = 0;
758 op.e_o_s = 0; 762 op.e_o_s = 0;
759 op.granulepos = 0; 763 op.granulepos = 0;
760 op.packetno = packet_id++; 764 op.packetno = packet_id++;
761 ogg_stream_packetin(&os, &op); 765 ogg_stream_packetin (&os, &op);
762 766
763 /* Comment packets must not be mixed with audio packets on their pages */ 767 /* Comment packets must not be mixed with audio packets on their pages */
764 while (ogg_stream_flush(&os, &og)) 768 while (ogg_stream_flush (&os, &og))
765 { 769 {
766 write_page(&og); 770 write_page (&og);
767 } 771 }
768 772
769 free(commentspacket); 773 free (commentspacket);
770 } 774 }
771} 775}
772 776
@@ -778,25 +782,25 @@ ogg_init()
778 * @return 0 ok, 1 on error 782 * @return 0 ok, 1 on error
779 */ 783 */
780int 784int
781main(int argc, 785main (int argc,
782 char *argv[]) 786 char *argv[])
783{ 787{
784 (void)argc; 788 (void) argc;
785 (void)argv; 789 (void) argv;
786 GNUNET_assert(GNUNET_OK == 790 GNUNET_assert (GNUNET_OK ==
787 GNUNET_log_setup("gnunet-helper-audio-record", 791 GNUNET_log_setup ("gnunet-helper-audio-record",
788 "WARNING", 792 "WARNING",
789 NULL)); 793 NULL));
790 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Audio source starts\n"); 795 "Audio source starts\n");
792 audio_message = GNUNET_malloc(UINT16_MAX); 796 audio_message = GNUNET_malloc (UINT16_MAX);
793 audio_message->header.type = htons(GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO); 797 audio_message->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
794 798
795#ifdef DEBUG_RECORD_PURE_OGG 799#ifdef DEBUG_RECORD_PURE_OGG
796 dump_pure_ogg = getenv("GNUNET_RECORD_PURE_OGG") ? 1 : 0; 800 dump_pure_ogg = getenv ("GNUNET_RECORD_PURE_OGG") ? 1 : 0;
797#endif 801#endif
798 ogg_init(); 802 ogg_init ();
799 opus_init(); 803 opus_init ();
800 pa_init(); 804 pa_init ();
801 return 0; 805 return 0;
802} 806}