diff options
Diffstat (limited to 'toxav/codec.c')
-rw-r--r-- | toxav/codec.c | 59 |
1 files changed, 14 insertions, 45 deletions
diff --git a/toxav/codec.c b/toxav/codec.c index e63ed353..afe4f4aa 100644 --- a/toxav/codec.c +++ b/toxav/codec.c | |||
@@ -369,27 +369,29 @@ void cs_do(CSSession *cs) | |||
369 | Payload *p; | 369 | Payload *p; |
370 | int rc; | 370 | int rc; |
371 | 371 | ||
372 | pthread_mutex_lock(cs->queue_mutex); | 372 | int success = 0; |
373 | 373 | ||
374 | if (cs->abuf_raw && !buffer_empty(cs->abuf_raw)) { | 374 | pthread_mutex_lock(cs->queue_mutex); |
375 | /* Decode audio */ | 375 | RTPMessage *msg; |
376 | buffer_read(cs->abuf_raw, &p); | ||
377 | 376 | ||
378 | /* Leave space for (possibly) other thread to queue more data after we read it here */ | 377 | while ((msg = jbuf_read(cs->j_buf, &success)) || success == 2) { |
379 | pthread_mutex_unlock(cs->queue_mutex); | 378 | pthread_mutex_unlock(cs->queue_mutex); |
380 | 379 | ||
381 | |||
382 | uint16_t fsize = ((cs->audio_decoder_sample_rate * cs->audio_decoder_frame_duration) / 1000); | 380 | uint16_t fsize = ((cs->audio_decoder_sample_rate * cs->audio_decoder_frame_duration) / 1000); |
383 | int16_t tmp[fsize * cs->audio_decoder_channels]; | 381 | int16_t tmp[fsize * cs->audio_decoder_channels]; |
384 | 382 | ||
385 | rc = opus_decode(cs->audio_decoder, p->data, p->size, tmp, fsize, (p->size == 0)); | 383 | if (success == 2) { |
386 | free(p); | 384 | rc = opus_decode(cs->audio_decoder, 0, 0, tmp, fsize, 1); |
385 | } else { | ||
386 | rc = opus_decode(cs->audio_decoder, msg->data, msg->length, tmp, fsize, 0); | ||
387 | } | ||
387 | 388 | ||
388 | if (rc < 0) | 389 | if (rc < 0) { |
389 | LOGGER_WARNING("Decoding error: %s", opus_strerror(rc)); | 390 | LOGGER_WARNING("Decoding error: %s", opus_strerror(rc)); |
390 | else if (cs->acb.first) | 391 | } else if (cs->acb.first) { |
391 | /* Play */ | 392 | /* Play */ |
392 | cs->acb.first(cs->agent, cs->call_idx, tmp, rc, cs->acb.second); | 393 | cs->acb.first(cs->agent, cs->call_idx, tmp, rc, cs->acb.second); |
394 | } | ||
393 | 395 | ||
394 | pthread_mutex_lock(cs->queue_mutex); | 396 | pthread_mutex_lock(cs->queue_mutex); |
395 | } | 397 | } |
@@ -502,8 +504,6 @@ CSSession *cs_new(const ToxAvCSettings *cs_self, const ToxAvCSettings *cs_peer, | |||
502 | 504 | ||
503 | if ( !(cs->capabilities & cs_AudioEncoding) || !(cs->capabilities & cs_AudioDecoding) ) goto error; | 505 | if ( !(cs->capabilities & cs_AudioEncoding) || !(cs->capabilities & cs_AudioDecoding) ) goto error; |
504 | 506 | ||
505 | if ( !(cs->abuf_raw = buffer_new(jbuf_size)) ) goto error; | ||
506 | |||
507 | if ((cs->support_video = has_video)) { | 507 | if ((cs->support_video = has_video)) { |
508 | cs->max_video_frame_size = MAX_VIDEOFRAME_SIZE; | 508 | cs->max_video_frame_size = MAX_VIDEOFRAME_SIZE; |
509 | cs->video_frame_piece_size = VIDEOFRAME_PIECE_SIZE; | 509 | cs->video_frame_piece_size = VIDEOFRAME_PIECE_SIZE; |
@@ -529,8 +529,6 @@ error: | |||
529 | 529 | ||
530 | pthread_mutex_destroy(cs->queue_mutex); | 530 | pthread_mutex_destroy(cs->queue_mutex); |
531 | 531 | ||
532 | buffer_free(cs->abuf_raw); | ||
533 | |||
534 | if ( cs->audio_encoder ) opus_encoder_destroy(cs->audio_encoder); | 532 | if ( cs->audio_encoder ) opus_encoder_destroy(cs->audio_encoder); |
535 | 533 | ||
536 | if ( cs->audio_decoder ) opus_decoder_destroy(cs->audio_decoder); | 534 | if ( cs->audio_decoder ) opus_decoder_destroy(cs->audio_decoder); |
@@ -574,7 +572,6 @@ void cs_kill(CSSession *cs) | |||
574 | vpx_codec_destroy(&cs->v_encoder); | 572 | vpx_codec_destroy(&cs->v_encoder); |
575 | 573 | ||
576 | jbuf_free(cs->j_buf); | 574 | jbuf_free(cs->j_buf); |
577 | buffer_free(cs->abuf_raw); | ||
578 | buffer_free(cs->vbuf_raw); | 575 | buffer_free(cs->vbuf_raw); |
579 | free(cs->frame_buf); | 576 | free(cs->frame_buf); |
580 | 577 | ||
@@ -597,37 +594,9 @@ void queue_message(RTPSession *session, RTPMessage *msg) | |||
597 | 594 | ||
598 | /* Audio */ | 595 | /* Audio */ |
599 | if (session->payload_type == msi_TypeAudio % 128) { | 596 | if (session->payload_type == msi_TypeAudio % 128) { |
597 | pthread_mutex_lock(cs->queue_mutex); | ||
600 | jbuf_write(cs->j_buf, msg); | 598 | jbuf_write(cs->j_buf, msg); |
601 | 599 | pthread_mutex_unlock(cs->queue_mutex); | |
602 | int success = 0; | ||
603 | |||
604 | while ((msg = jbuf_read(cs->j_buf, &success)) || success == 2) { | ||
605 | Payload *p; | ||
606 | |||
607 | if (success == 2) { | ||
608 | p = malloc(sizeof(Payload)); | ||
609 | |||
610 | if (p) p->size = 0; | ||
611 | |||
612 | } else { | ||
613 | p = malloc(sizeof(Payload) + msg->length); | ||
614 | |||
615 | if (p) { | ||
616 | p->size = msg->length; | ||
617 | memcpy(p->data, msg->data, msg->length); | ||
618 | } | ||
619 | |||
620 | rtp_free_msg(NULL, msg); | ||
621 | } | ||
622 | |||
623 | if (p) { | ||
624 | pthread_mutex_lock(cs->queue_mutex); | ||
625 | buffer_write(cs->abuf_raw, p); | ||
626 | pthread_mutex_unlock(cs->queue_mutex); | ||
627 | } else { | ||
628 | LOGGER_WARNING("Allocation failed! Program might misbehave!"); | ||
629 | } | ||
630 | } | ||
631 | } | 600 | } |
632 | /* Video */ | 601 | /* Video */ |
633 | else { | 602 | else { |