summaryrefslogtreecommitdiff
path: root/toxav/codec.c
diff options
context:
space:
mode:
authormannol <eniz_vukovic@hotmail.com>2015-04-08 01:00:19 +0200
committermannol <eniz_vukovic@hotmail.com>2015-04-08 01:00:19 +0200
commit9c003c9dd215d5f6bb2c1a0fbdc2c0f7fd9def7c (patch)
treead682cf784489259d6ccc65c2d116cb2bbd2ba6d /toxav/codec.c
parentbf9f7e2ae8dc5f1b83c45e383ec70cb08c4caf5f (diff)
Video works now
Diffstat (limited to 'toxav/codec.c')
-rw-r--r--toxav/codec.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/toxav/codec.c b/toxav/codec.c
index c3f5d740..cd26d1e3 100644
--- a/toxav/codec.c
+++ b/toxav/codec.c
@@ -38,7 +38,7 @@
38#include "rtp.h" 38#include "rtp.h"
39#include "codec.h" 39#include "codec.h"
40 40
41#define DEFAULT_JBUF 3 41#define DEFAULT_JBUF 6
42 42
43/* Good quality encode. */ 43/* Good quality encode. */
44#define MAX_DECODE_TIME_US 0 44#define MAX_DECODE_TIME_US 0
@@ -324,17 +324,17 @@ void cs_do(CSession *cs)
324 324
325 int success = 0; 325 int success = 0;
326 326
327 pthread_mutex_lock(cs->queue_mutex); 327 LOGGED_LOCK(cs->queue_mutex);
328 328
329 /********************* AUDIO *********************/ 329 /********************* AUDIO *********************/
330 if (cs->audio_decoder) { /* If receiving enabled */ 330 if (cs->audio_decoder) {
331 RTPMessage *msg; 331 RTPMessage *msg;
332 332
333 /* The maximum for 120 ms 48 KHz audio */ 333 /* The maximum for 120 ms 48 KHz audio */
334 int16_t tmp[5760]; 334 int16_t tmp[5760];
335 335
336 while ((msg = jbuf_read(cs->j_buf, &success)) || success == 2) { 336 while ((msg = jbuf_read(cs->j_buf, &success)) || success == 2) {
337 pthread_mutex_unlock(cs->queue_mutex); 337 LOGGED_UNLOCK(cs->queue_mutex);
338 338
339 if (success == 2) { 339 if (success == 2) {
340 LOGGER_DEBUG("OPUS correction"); 340 LOGGER_DEBUG("OPUS correction");
@@ -377,7 +377,7 @@ void cs_do(CSession *cs)
377 cs->last_packet_channel_count, cs->last_packet_sampling_rate, cs->acb.second); 377 cs->last_packet_channel_count, cs->last_packet_sampling_rate, cs->acb.second);
378 } 378 }
379 379
380 pthread_mutex_lock(cs->queue_mutex); 380 LOGGED_LOCK(cs->queue_mutex);
381 } 381 }
382 } 382 }
383 383
@@ -387,7 +387,7 @@ void cs_do(CSession *cs)
387 buffer_read(cs->vbuf_raw, &p); 387 buffer_read(cs->vbuf_raw, &p);
388 388
389 /* Leave space for (possibly) other thread to queue more data after we read it here */ 389 /* Leave space for (possibly) other thread to queue more data after we read it here */
390 pthread_mutex_unlock(cs->queue_mutex); 390 LOGGED_UNLOCK(cs->queue_mutex);
391 391
392 rc = vpx_codec_decode(cs->v_decoder, p->data, p->size, NULL, MAX_DECODE_TIME_US); 392 rc = vpx_codec_decode(cs->v_decoder, p->data, p->size, NULL, MAX_DECODE_TIME_US);
393 free(p); 393 free(p);
@@ -411,7 +411,7 @@ void cs_do(CSession *cs)
411 return; 411 return;
412 } 412 }
413 413
414 pthread_mutex_unlock(cs->queue_mutex); 414 LOGGED_UNLOCK(cs->queue_mutex);
415} 415}
416 416
417CSession *cs_new(uint32_t peer_video_frame_piece_size) 417CSession *cs_new(uint32_t peer_video_frame_piece_size)
@@ -423,7 +423,7 @@ CSession *cs_new(uint32_t peer_video_frame_piece_size)
423 return NULL; 423 return NULL;
424 } 424 }
425 425
426 if (pthread_mutex_init(cs->queue_mutex, NULL) != 0) { 426 if (create_recursive_mutex(cs->queue_mutex) != 0) {
427 LOGGER_WARNING("Failed to create recursive mutex!"); 427 LOGGER_WARNING("Failed to create recursive mutex!");
428 free(cs); 428 free(cs);
429 return NULL; 429 return NULL;
@@ -481,6 +481,8 @@ CSession *cs_new(uint32_t peer_video_frame_piece_size)
481 if ( !(cs->split_video_frame = calloc(VIDEOFRAME_PIECE_SIZE + VIDEOFRAME_HEADER_SIZE, 1)) ) 481 if ( !(cs->split_video_frame = calloc(VIDEOFRAME_PIECE_SIZE + VIDEOFRAME_HEADER_SIZE, 1)) )
482 goto FAILURE; 482 goto FAILURE;
483 483
484 cs->linfts = current_time_monotonic();
485 cs->lcfd = 10;
484 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 486 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
485 487
486 /* Initialize encoders with default values */ 488 /* Initialize encoders with default values */
@@ -621,6 +623,8 @@ int cs_reconfigure_audio_encoder(CSession* cs, int32_t bitrate, int32_t sampling
621 cs->last_encoding_bitrate = bitrate; 623 cs->last_encoding_bitrate = bitrate;
622 cs->last_encoding_sampling_rate = sampling_rate; 624 cs->last_encoding_sampling_rate = sampling_rate;
623 cs->last_encoding_channel_count = channels; 625 cs->last_encoding_channel_count = channels;
626
627 LOGGER_DEBUG ("Reconfigured audio encoder br: %d sr: %d cc:%d", bitrate, sampling_rate, channels);
624 return 0; 628 return 0;
625} 629}
626 630
@@ -628,9 +632,6 @@ int cs_reconfigure_audio_encoder(CSession* cs, int32_t bitrate, int32_t sampling
628/* Called from RTP */ 632/* Called from RTP */
629void queue_message(RTPSession *session, RTPMessage *msg) 633void queue_message(RTPSession *session, RTPMessage *msg)
630{ 634{
631 /* This function is unregistered during call termination befor destroying
632 * Codec session so no need to check for validity of cs TODO properly check video cycle
633 */
634 CSession *cs = session->cs; 635 CSession *cs = session->cs;
635 636
636 if (!cs) 637 if (!cs)
@@ -638,9 +639,9 @@ void queue_message(RTPSession *session, RTPMessage *msg)
638 639
639 /* Audio */ 640 /* Audio */
640 if (session->payload_type == rtp_TypeAudio % 128) { 641 if (session->payload_type == rtp_TypeAudio % 128) {
641 pthread_mutex_lock(cs->queue_mutex); 642 LOGGED_LOCK(cs->queue_mutex);
642 int ret = jbuf_write(cs->j_buf, msg); 643 int ret = jbuf_write(cs->j_buf, msg);
643 pthread_mutex_unlock(cs->queue_mutex); 644 LOGGED_UNLOCK(cs->queue_mutex);
644 645
645 if (ret == -1) { 646 if (ret == -1) {
646 rtp_free_msg(NULL, msg); 647 rtp_free_msg(NULL, msg);
@@ -662,7 +663,7 @@ void queue_message(RTPSession *session, RTPMessage *msg)
662 Payload *p = malloc(sizeof(Payload) + cs->frame_size); 663 Payload *p = malloc(sizeof(Payload) + cs->frame_size);
663 664
664 if (p) { 665 if (p) {
665 pthread_mutex_lock(cs->queue_mutex); 666 LOGGED_LOCK(cs->queue_mutex);
666 667
667 if (buffer_full(cs->vbuf_raw)) { 668 if (buffer_full(cs->vbuf_raw)) {
668 LOGGER_DEBUG("Dropped video frame"); 669 LOGGER_DEBUG("Dropped video frame");
@@ -673,15 +674,19 @@ void queue_message(RTPSession *session, RTPMessage *msg)
673 p->size = cs->frame_size; 674 p->size = cs->frame_size;
674 memcpy(p->data, cs->frame_buf, cs->frame_size); 675 memcpy(p->data, cs->frame_buf, cs->frame_size);
675 } 676 }
676 677
678 /* Calculate time took for peer to send us this frame */
679 uint32_t t_lcfd = current_time_monotonic() - cs->linfts;
680 cs->lcfd = t_lcfd > 100 ? cs->lcfd : t_lcfd;
681 cs->linfts = current_time_monotonic();
682
677 buffer_write(cs->vbuf_raw, p); 683 buffer_write(cs->vbuf_raw, p);
678 pthread_mutex_unlock(cs->queue_mutex); 684 LOGGED_UNLOCK(cs->queue_mutex);
679 } else { 685 } else {
680 LOGGER_WARNING("Allocation failed! Program might misbehave!"); 686 LOGGER_WARNING("Allocation failed! Program might misbehave!");
681 goto end; 687 goto end;
682 } 688 }
683 689
684 cs->last_timestamp = msg->header->timestamp;
685 cs->frameid_in = packet[0]; 690 cs->frameid_in = packet[0];
686 memset(cs->frame_buf, 0, cs->frame_size); 691 memset(cs->frame_buf, 0, cs->frame_size);
687 cs->frame_size = 0; 692 cs->frame_size = 0;