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