summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-12-12 17:47:57 -0500
committerirungentoo <irungentoo@gmail.com>2014-12-12 17:47:57 -0500
commit12f396fcc26d7633bcaeb9c02658acb5c68acba8 (patch)
treeabb347cf0b06b1e5a2357e70a5a7bea97f145d1d
parent36851e7b3843de1b9ce260c02a9da6b685338df6 (diff)
Fixed video packet assembling.
Video should look better now.
-rw-r--r--toxav/codec.c64
1 files changed, 40 insertions, 24 deletions
diff --git a/toxav/codec.c b/toxav/codec.c
index baaad47a..e63ed353 100644
--- a/toxav/codec.c
+++ b/toxav/codec.c
@@ -637,47 +637,63 @@ void queue_message(RTPSession *session, RTPMessage *msg)
637 if (packet_size < VIDEOFRAME_HEADER_SIZE) 637 if (packet_size < VIDEOFRAME_HEADER_SIZE)
638 goto end; 638 goto end;
639 639
640 if (packet[0] > cs->frameid_in || (msg->header->timestamp > cs->last_timestamp)) { /* New frame */ 640 uint8_t diff = packet[0] - cs->frameid_in;
641 /* Flush last frames' data and get ready for this frame */
642 Payload *p = malloc(sizeof(Payload) + cs->frame_size);
643 641
644 if (p) { 642 if (diff != 0) {
645 pthread_mutex_lock(cs->queue_mutex); 643 if (diff < 225) { /* New frame */
644 /* Flush last frames' data and get ready for this frame */
645 Payload *p = malloc(sizeof(Payload) + cs->frame_size);
646 646
647 if (buffer_full(cs->vbuf_raw)) { 647 if (p) {
648 LOGGER_DEBUG("Dropped video frame"); 648 pthread_mutex_lock(cs->queue_mutex);
649 Payload *tp; 649
650 buffer_read(cs->vbuf_raw, &tp); 650 if (buffer_full(cs->vbuf_raw)) {
651 free(tp); 651 LOGGER_DEBUG("Dropped video frame");
652 Payload *tp;
653 buffer_read(cs->vbuf_raw, &tp);
654 free(tp);
655 } else {
656 p->size = cs->frame_size;
657 memcpy(p->data, cs->frame_buf, cs->frame_size);
658 }
659
660 buffer_write(cs->vbuf_raw, p);
661 pthread_mutex_unlock(cs->queue_mutex);
652 } else { 662 } else {
653 p->size = cs->frame_size; 663 LOGGER_WARNING("Allocation failed! Program might misbehave!");
654 memcpy(p->data, cs->frame_buf, cs->frame_size); 664 goto end;
655 } 665 }
656 666
657 buffer_write(cs->vbuf_raw, p); 667 cs->last_timestamp = msg->header->timestamp;
658 pthread_mutex_unlock(cs->queue_mutex); 668 cs->frameid_in = packet[0];
659 } else { 669 memset(cs->frame_buf, 0, cs->frame_size);
660 LOGGER_WARNING("Allocation failed! Program might misbehave!"); 670 cs->frame_size = 0;
671
672 } else { /* Old frame; drop */
673 LOGGER_DEBUG("Old packet: %u", packet[0]);
661 goto end; 674 goto end;
662 } 675 }
676 }
677
678 uint8_t piece_number = packet[1];
663 679
664 cs->last_timestamp = msg->header->timestamp; 680 uint32_t length_before_piece = ((piece_number - 1) * cs->video_frame_piece_size);
665 cs->frameid_in = packet[0]; 681 uint32_t framebuf_new_length = length_before_piece + (packet_size - VIDEOFRAME_HEADER_SIZE);
666 memset(cs->frame_buf, 0, cs->frame_size);
667 cs->frame_size = 0;
668 682
669 } else if (packet[0] < cs->frameid_in) { /* Old frame; drop */ 683 if (framebuf_new_length > cs->max_video_frame_size) {
670 LOGGER_DEBUG("Old packet: %u", packet[0]);
671 goto end; 684 goto end;
672 } 685 }
673 686
674 /* Otherwise it's part of the frame so just process */ 687 /* Otherwise it's part of the frame so just process */
675 /* LOGGER_DEBUG("Video Packet: %u %u", packet[0], packet[1]); */ 688 /* LOGGER_DEBUG("Video Packet: %u %u", packet[0], packet[1]); */
676 memcpy(cs->frame_buf + cs->frame_size, 689
690 memcpy(cs->frame_buf + length_before_piece,
677 packet + VIDEOFRAME_HEADER_SIZE, 691 packet + VIDEOFRAME_HEADER_SIZE,
678 packet_size - VIDEOFRAME_HEADER_SIZE); 692 packet_size - VIDEOFRAME_HEADER_SIZE);
679 693
680 cs->frame_size += packet_size - VIDEOFRAME_HEADER_SIZE; 694 if (framebuf_new_length > cs->frame_size) {
695 cs->frame_size = framebuf_new_length;
696 }
681 697
682end: 698end:
683 rtp_free_msg(NULL, msg); 699 rtp_free_msg(NULL, msg);