summaryrefslogtreecommitdiff
path: root/toxav/rtp.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-05-25 12:27:48 -0400
committerirungentoo <irungentoo@gmail.com>2014-05-25 12:27:48 -0400
commit82e38883a239f265089982bc255de0f9db618ce7 (patch)
tree1d38b2ff978e86246695cc7e473af309eacdba2a /toxav/rtp.c
parent9d53bc5d1dabd3142f58cafd9fffa18f783f96df (diff)
parent08ca08dcd952d55a0df75e5efa25a5d8afa70e3f (diff)
Merge branch 'mannol1-Multicalls' into multi-av
Diffstat (limited to 'toxav/rtp.c')
-rw-r--r--[-rwxr-xr-x]toxav/rtp.c161
1 files changed, 102 insertions, 59 deletions
diff --git a/toxav/rtp.c b/toxav/rtp.c
index d96712b3..f44b2bfe 100755..100644
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -25,6 +25,8 @@
25#include "config.h" 25#include "config.h"
26#endif /* HAVE_CONFIG_H */ 26#endif /* HAVE_CONFIG_H */
27 27
28#include "../toxcore/logger.h"
29
28#include "rtp.h" 30#include "rtp.h"
29#include <assert.h> 31#include <assert.h>
30#include <stdlib.h> 32#include <stdlib.h>
@@ -227,6 +229,7 @@ static const uint32_t payload_table[] = {
227RTPHeader *extract_header ( const uint8_t *payload, int length ) 229RTPHeader *extract_header ( const uint8_t *payload, int length )
228{ 230{
229 if ( !payload || !length ) { 231 if ( !payload || !length ) {
232 LOGGER_WARNING("No payload to extract!");
230 return NULL; 233 return NULL;
231 } 234 }
232 235
@@ -245,6 +248,7 @@ RTPHeader *extract_header ( const uint8_t *payload, int length )
245 248
246 if ( GET_FLAG_VERSION(_retu) != RTP_VERSION ) { 249 if ( GET_FLAG_VERSION(_retu) != RTP_VERSION ) {
247 /* Deallocate */ 250 /* Deallocate */
251 LOGGER_WARNING("Invalid version!");
248 free(_retu); 252 free(_retu);
249 return NULL; 253 return NULL;
250 } 254 }
@@ -258,12 +262,13 @@ RTPHeader *extract_header ( const uint8_t *payload, int length )
258 262
259 if ( length < _length ) { 263 if ( length < _length ) {
260 /* Deallocate */ 264 /* Deallocate */
265 LOGGER_WARNING("Length invalid!");
261 free(_retu); 266 free(_retu);
262 return NULL; 267 return NULL;
263 } 268 }
264 269
265 memset(_retu->csrc, 0, 16 * sizeof (uint32_t)); 270 memset(_retu->csrc, 0, 16 * sizeof (uint32_t));
266 271
267 _retu->marker_payloadt = *_it; 272 _retu->marker_payloadt = *_it;
268 ++_it; 273 ++_it;
269 _retu->length = _length; 274 _retu->length = _length;
@@ -304,6 +309,7 @@ RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length )
304 309
305 310
306 if ( length < ( _ext_length * sizeof(uint32_t) ) ) { 311 if ( length < ( _ext_length * sizeof(uint32_t) ) ) {
312 LOGGER_WARNING("Length invalid!");
307 free(_retu); 313 free(_retu);
308 return NULL; 314 return NULL;
309 } 315 }
@@ -415,9 +421,9 @@ RTPHeader *build_header ( RTPSession *session )
415 421
416 int i; 422 int i;
417 423
418 for ( i = 0; i < session->cc; i++ ) 424 for ( i = 0; i < session->cc; i++ )
419 _retu->csrc[i] = session->csrc[i]; 425 _retu->csrc[i] = session->csrc[i];
420 426
421 _retu->length = 12 /* Minimum header len */ + ( session->cc * size_32 ); 427 _retu->length = 12 /* Minimum header len */ + ( session->cc * size_32 );
422 428
423 return _retu; 429 return _retu;
@@ -443,6 +449,7 @@ RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length )
443 _retu->header = extract_header ( data, length ); /* It allocates memory and all */ 449 _retu->header = extract_header ( data, length ); /* It allocates memory and all */
444 450
445 if ( !_retu->header ) { 451 if ( !_retu->header ) {
452 LOGGER_WARNING("Header failed to extract!");
446 free(_retu); 453 free(_retu);
447 return NULL; 454 return NULL;
448 } 455 }
@@ -461,6 +468,7 @@ RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length )
461 _retu->length -= ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); 468 _retu->length -= ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
462 _from_pos += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); 469 _from_pos += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
463 } else { /* Error */ 470 } else { /* Error */
471 LOGGER_WARNING("Ext Header failed to extract!");
464 rtp_free_msg(NULL, _retu); 472 rtp_free_msg(NULL, _retu);
465 return NULL; 473 return NULL;
466 } 474 }
@@ -471,6 +479,7 @@ RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length )
471 if ( length - _from_pos <= MAX_RTP_SIZE ) 479 if ( length - _from_pos <= MAX_RTP_SIZE )
472 memcpy ( _retu->data, data + _from_pos, length - _from_pos ); 480 memcpy ( _retu->data, data + _from_pos, length - _from_pos );
473 else { 481 else {
482 LOGGER_WARNING("Invalid length!");
474 rtp_free_msg(NULL, _retu); 483 rtp_free_msg(NULL, _retu);
475 return NULL; 484 return NULL;
476 } 485 }
@@ -496,8 +505,15 @@ int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
496 RTPSession *_session = object; 505 RTPSession *_session = object;
497 RTPMessage *_msg; 506 RTPMessage *_msg;
498 507
499 if ( !_session || length < 13 + crypto_box_MACBYTES) /* 12 is the minimum length for rtp + desc. byte */ 508 if ( !_session || length < 13 + crypto_box_MACBYTES) { /* 12 is the minimum length for rtp + desc. byte */
509 LOGGER_WARNING("No session or invalid length of received buffer!");
500 return -1; 510 return -1;
511 }
512
513 if ( _session->queue_limit <= _session->queue_size ) {
514 LOGGER_WARNING("Queue limit reached!");
515 return -1;
516 }
501 517
502 uint8_t _plain[MAX_UDP_PACKET_SIZE]; 518 uint8_t _plain[MAX_UDP_PACKET_SIZE];
503 519
@@ -524,7 +540,10 @@ int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
524 _decrypted_length = decrypt_data_symmetric( 540 _decrypted_length = decrypt_data_symmetric(
525 (uint8_t *)_session->decrypt_key, _session->nonce_cycle, data + 3, length - 3, _plain ); 541 (uint8_t *)_session->decrypt_key, _session->nonce_cycle, data + 3, length - 3, _plain );
526 542
527 if ( _decrypted_length == -1 ) return -1; /* This packet is not encrypted properly */ 543 if ( _decrypted_length == -1 ) {
544 LOGGER_WARNING("Packet not ecrypted properly!");
545 return -1; /* This packet is not encrypted properly */
546 }
528 547
529 /* Otherwise, if decryption is ok with new cycle, set new cycle 548 /* Otherwise, if decryption is ok with new cycle, set new cycle
530 */ 549 */
@@ -533,7 +552,10 @@ int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
533 _decrypted_length = decrypt_data_symmetric( 552 _decrypted_length = decrypt_data_symmetric(
534 (uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain ); 553 (uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
535 554
536 if ( _decrypted_length == -1 ) return -1; /* This is just an error */ 555 if ( _decrypted_length == -1 ) {
556 LOGGER_WARNING("Error decrypting!");
557 return -1; /* This is just an error */
558 }
537 559
538 /* A new cycle setting. */ 560 /* A new cycle setting. */
539 memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_box_NONCEBYTES); 561 memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_box_NONCEBYTES);
@@ -543,7 +565,10 @@ int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
543 565
544 _msg = msg_parse ( _sequnum, _plain, _decrypted_length ); 566 _msg = msg_parse ( _sequnum, _plain, _decrypted_length );
545 567
546 if ( !_msg ) return -1; 568 if ( !_msg ) {
569 LOGGER_WARNING("Could not parse message!");
570 return -1;
571 }
547 572
548 /* Check if message came in late */ 573 /* Check if message came in late */
549 if ( check_late_message(_session, _msg) < 0 ) { /* Not late */ 574 if ( check_late_message(_session, _msg) < 0 ) { /* Not late */
@@ -560,6 +585,8 @@ int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
560 _session->last_msg = _session->oldest_msg = _msg; 585 _session->last_msg = _session->oldest_msg = _msg;
561 } 586 }
562 587
588 _session->queue_size++;
589
563 pthread_mutex_unlock(&_session->mutex); 590 pthread_mutex_unlock(&_session->mutex);
564 591
565 return 0; 592 return 0;
@@ -579,8 +606,10 @@ int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
579 */ 606 */
580RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t length ) 607RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t length )
581{ 608{
582 if ( !session ) 609 if ( !session ) {
610 LOGGER_WARNING("No session!");
583 return NULL; 611 return NULL;
612 }
584 613
585 uint8_t *_from_pos; 614 uint8_t *_from_pos;
586 RTPMessage *_retu = calloc(1, sizeof (RTPMessage)); 615 RTPMessage *_retu = calloc(1, sizeof (RTPMessage));
@@ -619,36 +648,6 @@ RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t
619 648
620 649
621 650
622
623
624
625
626/********************************************************************************************************************
627 ********************************************************************************************************************
628 ********************************************************************************************************************
629 ********************************************************************************************************************
630 ********************************************************************************************************************
631 *
632 *
633 *
634 * PUBLIC API FUNCTIONS IMPLEMENTATIONS
635 *
636 *
637 *
638 ********************************************************************************************************************
639 ********************************************************************************************************************
640 ********************************************************************************************************************
641 ********************************************************************************************************************
642 ********************************************************************************************************************/
643
644
645
646
647
648
649
650
651
652/** 651/**
653 * @brief Release all messages held by session. 652 * @brief Release all messages held by session.
654 * 653 *
@@ -660,6 +659,7 @@ RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t
660int rtp_release_session_recv ( RTPSession *session ) 659int rtp_release_session_recv ( RTPSession *session )
661{ 660{
662 if ( !session ) { 661 if ( !session ) {
662 LOGGER_WARNING("No session!");
663 return -1; 663 return -1;
664 } 664 }
665 665
@@ -673,6 +673,7 @@ int rtp_release_session_recv ( RTPSession *session )
673 } 673 }
674 674
675 session->last_msg = session->oldest_msg = NULL; 675 session->last_msg = session->oldest_msg = NULL;
676 session->queue_size = 0;
676 677
677 pthread_mutex_unlock(&session->mutex); 678 pthread_mutex_unlock(&session->mutex);
678 679
@@ -681,6 +682,31 @@ int rtp_release_session_recv ( RTPSession *session )
681 682
682 683
683/** 684/**
685 * @brief Call this to change queue limit
686 *
687 * @param session The session
688 * @param limit new limit
689 * @return void
690 */
691void rtp_queue_adjust_limit(RTPSession *session, uint64_t limit)
692{
693 RTPMessage *_tmp, * _it;
694 pthread_mutex_lock(&session->mutex);
695
696 for ( _it = session->oldest_msg; session->queue_size > limit; _it = _tmp ) {
697 _tmp = _it->next;
698 rtp_free_msg( session, _it);
699 session->queue_size --;
700 }
701
702 session->oldest_msg = _it;
703 session->queue_limit = limit;
704
705 pthread_mutex_unlock(&session->mutex);
706}
707
708
709/**
684 * @brief Gets oldest message in the list. 710 * @brief Gets oldest message in the list.
685 * 711 *
686 * @param session Where the list is. 712 * @param session Where the list is.
@@ -689,21 +715,32 @@ int rtp_release_session_recv ( RTPSession *session )
689 */ 715 */
690RTPMessage *rtp_recv_msg ( RTPSession *session ) 716RTPMessage *rtp_recv_msg ( RTPSession *session )
691{ 717{
692 if ( !session ) 718 if ( !session ) {
719 LOGGER_WARNING("No session!");
693 return NULL; 720 return NULL;
694 721 }
695 RTPMessage *_retu = session->oldest_msg;
696 722
697 pthread_mutex_lock(&session->mutex); 723 pthread_mutex_lock(&session->mutex);
698 724
699 if ( _retu ) 725 if ( session->queue_size == 0 ) {
700 session->oldest_msg = _retu->next; 726 pthread_mutex_unlock(&session->mutex);
727 return NULL;
728 }
729
730
731 RTPMessage *_retu = session->oldest_msg;
732
733 /*if (_retu)*/
734 session->oldest_msg = _retu->next;
701 735
702 if ( !session->oldest_msg ) 736 if ( !session->oldest_msg )
703 session->last_msg = NULL; 737 session->last_msg = NULL;
704 738
739 session->queue_size --;
740
705 pthread_mutex_unlock(&session->mutex); 741 pthread_mutex_unlock(&session->mutex);
706 742
743
707 return _retu; 744 return _retu;
708} 745}
709 746
@@ -723,7 +760,10 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat
723{ 760{
724 RTPMessage *msg = rtp_new_message (session, data, length); 761 RTPMessage *msg = rtp_new_message (session, data, length);
725 762
726 if ( !msg ) return -1; 763 if ( !msg ) {
764 LOGGER_WARNING("No session!");
765 return -1;
766 }
727 767
728 uint8_t _send_data [ MAX_UDP_PACKET_SIZE ]; 768 uint8_t _send_data [ MAX_UDP_PACKET_SIZE ];
729 769
@@ -738,15 +778,13 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat
738 int encrypted_length = encrypt_data_symmetric( /* TODO: msg->length - 2 (fix this properly)*/ 778 int encrypted_length = encrypt_data_symmetric( /* TODO: msg->length - 2 (fix this properly)*/
739 (uint8_t *) session->encrypt_key, _calculated, msg->data + 2, msg->length, _send_data + 3 ); 779 (uint8_t *) session->encrypt_key, _calculated, msg->data + 2, msg->length, _send_data + 3 );
740 780
741 int full_length = encrypted_length + 3;
742 781
743 _send_data[1] = msg->data[0]; 782 _send_data[1] = msg->data[0];
744 _send_data[2] = msg->data[1]; 783 _send_data[2] = msg->data[1];
745 784
746 785
747 /*if ( full_length != sendpacket ( messenger->net, *((IP_Port*) &session->dest), _send_data, full_length) ) {*/ 786 if ( -1 == send_custom_lossy_packet(messenger, session->dest, _send_data, encrypted_length + 3) ) {
748 if ( 0 != send_custom_lossy_packet(messenger, session->dest, _send_data, full_length) ) { 787 LOGGER_WARNING("Failed to send full packet! std error: %s", strerror(errno));
749 /*fprintf(stderr, "Rtp error: %s\n", strerror(errno));*/
750 rtp_free_msg ( session, msg ); 788 rtp_free_msg ( session, msg );
751 return -1; 789 return -1;
752 } 790 }
@@ -761,6 +799,7 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat
761 } 799 }
762 800
763 rtp_free_msg ( session, msg ); 801 rtp_free_msg ( session, msg );
802
764 return 0; 803 return 0;
765} 804}
766 805
@@ -817,13 +856,15 @@ RTPSession *rtp_init_session ( int payload_type,
817 RTPSession *_retu = calloc(1, sizeof(RTPSession)); 856 RTPSession *_retu = calloc(1, sizeof(RTPSession));
818 assert(_retu); 857 assert(_retu);
819 858
820 /*networking_registerhandler(messenger->net, payload_type, rtp_handle_packet, _retu);*/ 859 if ( -1 == custom_lossy_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, _retu) ||
821 if ( -1 == custom_lossy_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, _retu) ) { 860 !encrypt_key || !decrypt_key || !encrypt_nonce || !decrypt_nonce) {
822 /*fprintf(stderr, "Error setting custom register handler for rtp session\n");*/ 861 LOGGER_ERROR("Error setting custom register handler for rtp session");
823 free(_retu); 862 free(_retu);
824 return NULL; 863 return NULL;
825 } 864 }
826 865
866 LOGGER_DEBUG("Registered packet handler: pt: %d; fid: %d", payload_type, friend_num);
867
827 _retu->version = RTP_VERSION; /* It's always 2 */ 868 _retu->version = RTP_VERSION; /* It's always 2 */
828 _retu->padding = 0; /* If some additional data is needed about the packet */ 869 _retu->padding = 0; /* If some additional data is needed about the packet */
829 _retu->extension = 0; /* If extension to header is needed */ 870 _retu->extension = 0; /* If extension to header is needed */
@@ -838,8 +879,6 @@ RTPSession *rtp_init_session ( int payload_type,
838 _retu->rsequnum = _retu->sequnum = 1; 879 _retu->rsequnum = _retu->sequnum = 1;
839 880
840 _retu->ext_header = NULL; /* When needed allocate */ 881 _retu->ext_header = NULL; /* When needed allocate */
841 _retu->framerate = -1;
842 _retu->resolution = -1;
843 882
844 _retu->encrypt_key = encrypt_key; 883 _retu->encrypt_key = encrypt_key;
845 _retu->decrypt_key = decrypt_key; 884 _retu->decrypt_key = decrypt_key;
@@ -865,6 +904,8 @@ RTPSession *rtp_init_session ( int payload_type,
865 _retu->prefix = payload_type; 904 _retu->prefix = payload_type;
866 905
867 _retu->oldest_msg = _retu->last_msg = NULL; 906 _retu->oldest_msg = _retu->last_msg = NULL;
907 _retu->queue_limit = 100; /* Default */
908 _retu->queue_size = 0;
868 909
869 pthread_mutex_init(&_retu->mutex, NULL); 910 pthread_mutex_init(&_retu->mutex, NULL);
870 /* 911 /*
@@ -885,15 +926,17 @@ RTPSession *rtp_init_session ( int payload_type,
885 */ 926 */
886int rtp_terminate_session ( RTPSession *session, Messenger *messenger ) 927int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
887{ 928{
888 if ( !session ) 929 if ( !session ) {
930 LOGGER_WARNING("No session!");
889 return -1; 931 return -1;
890 932 }
933
891 custom_lossy_packet_registerhandler(messenger, session->dest, session->prefix, NULL, NULL); 934 custom_lossy_packet_registerhandler(messenger, session->dest, session->prefix, NULL, NULL);
892 935
893 rtp_release_session_recv(session); 936 rtp_release_session_recv(session);
894 937
895 pthread_mutex_lock(&session->mutex); 938 pthread_mutex_lock(&session->mutex);
896 939
897 free ( session->ext_header ); 940 free ( session->ext_header );
898 free ( session->csrc ); 941 free ( session->csrc );
899 free ( session->decrypt_nonce ); 942 free ( session->decrypt_nonce );
@@ -901,7 +944,7 @@ int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
901 free ( session->nonce_cycle ); 944 free ( session->nonce_cycle );
902 945
903 pthread_mutex_unlock(&session->mutex); 946 pthread_mutex_unlock(&session->mutex);
904 947
905 pthread_mutex_destroy(&session->mutex); 948 pthread_mutex_destroy(&session->mutex);
906 949
907 /* And finally free session */ 950 /* And finally free session */