summaryrefslogtreecommitdiff
path: root/toxav/toxmedia.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxav/toxmedia.c')
-rw-r--r--toxav/toxmedia.c76
1 files changed, 35 insertions, 41 deletions
diff --git a/toxav/toxmedia.c b/toxav/toxmedia.c
index 4c9f5261..aff3cf8c 100644
--- a/toxav/toxmedia.c
+++ b/toxav/toxmedia.c
@@ -42,10 +42,7 @@
42#include <opus/opus.h> 42#include <opus/opus.h>
43 43
44#include "toxmsi.h" 44#include "toxmsi.h"
45#include "toxmsi_message.h" 45#include "toxrtp.h"
46#include "../toxrtp/toxrtp_message.h"
47#include "../toxrtp/tests/test_helper.h"
48#include "phone.h"
49#include "toxmedia.h" 46#include "toxmedia.h"
50 47
51SDL_Surface *screen; 48SDL_Surface *screen;
@@ -77,7 +74,7 @@ int display_received_frame(codec_state *cs, AVFrame *r_video_frame)
77} 74}
78 75
79struct jitter_buffer { 76struct jitter_buffer {
80 rtp_msg_t **queue; 77 RTPMessage **queue;
81 uint16_t capacity; 78 uint16_t capacity;
82 uint16_t size; 79 uint16_t size;
83 uint16_t front; 80 uint16_t front;
@@ -92,7 +89,7 @@ struct jitter_buffer *create_queue(int capacity)
92{ 89{
93 struct jitter_buffer *q; 90 struct jitter_buffer *q;
94 q = (struct jitter_buffer *)calloc(sizeof(struct jitter_buffer),1); 91 q = (struct jitter_buffer *)calloc(sizeof(struct jitter_buffer),1);
95 q->queue = (rtp_msg_t **)calloc((sizeof(rtp_msg_t) * capacity),1); 92 q->queue = (RTPMessage **)calloc((sizeof(RTPMessage) * capacity),1);
96 int i = 0; 93 int i = 0;
97 94
98 for (i = 0; i < capacity; ++i) { 95 for (i = 0; i < capacity; ++i) {
@@ -114,11 +111,15 @@ struct jitter_buffer *create_queue(int capacity)
114uint8_t sequence_number_older(uint16_t sn_a, uint16_t sn_b, uint32_t ts_a, uint32_t ts_b) 111uint8_t sequence_number_older(uint16_t sn_a, uint16_t sn_b, uint32_t ts_a, uint32_t ts_b)
115{ 112{
116 /* should be stable enough */ 113 /* should be stable enough */
114
115 /* TODO: There is already this kind of function in toxrtp.c.
116 * Maybe merge?
117 */
117 return (sn_a > sn_b || ts_a > ts_b); 118 return (sn_a > sn_b || ts_a > ts_b);
118} 119}
119 120
120/* success is 0 when there is nothing to dequeue, 1 when there's a good packet, 2 when there's a lost packet */ 121/* success is 0 when there is nothing to dequeue, 1 when there's a good packet, 2 when there's a lost packet */
121rtp_msg_t *dequeue(struct jitter_buffer *q, int *success) 122RTPMessage *dequeue(struct jitter_buffer *q, int *success)
122{ 123{
123 if (q->size == 0 || q->queue_ready == 0) { 124 if (q->size == 0 || q->queue_ready == 0) {
124 q->queue_ready = 0; 125 q->queue_ready = 0;
@@ -129,21 +130,21 @@ rtp_msg_t *dequeue(struct jitter_buffer *q, int *success)
129 int front = q->front; 130 int front = q->front;
130 131
131 if (q->id_set == 0) { 132 if (q->id_set == 0) {
132 q->current_id = q->queue[front]->_header->_sequence_number; 133 q->current_id = q->queue[front]->header->sequnum;
133 q->current_ts = q->queue[front]->_header->_timestamp; 134 q->current_ts = q->queue[front]->header->timestamp;
134 q->id_set = 1; 135 q->id_set = 1;
135 } else { 136 } else {
136 int next_id = q->queue[front]->_header->_sequence_number; 137 int next_id = q->queue[front]->header->sequnum;
137 int next_ts = q->queue[front]->_header->_timestamp; 138 int next_ts = q->queue[front]->header->timestamp;
138 139
139 /* if this packet is indeed the expected packet */ 140 /* if this packet is indeed the expected packet */
140 if (next_id == (q->current_id + 1) % _MAX_SEQU_NUM) { 141 if (next_id == (q->current_id + 1) % MAX_SEQU_NUM) {
141 q->current_id = next_id; 142 q->current_id = next_id;
142 q->current_ts = next_ts; 143 q->current_ts = next_ts;
143 } else { 144 } else {
144 if (sequence_number_older(next_id, q->current_id, next_ts, q->current_ts)) { 145 if (sequence_number_older(next_id, q->current_id, next_ts, q->current_ts)) {
145 printf("nextid: %d current: %d\n", next_id, q->current_id); 146 printf("nextid: %d current: %d\n", next_id, q->current_id);
146 q->current_id = (q->current_id + 1) % _MAX_SEQU_NUM; 147 q->current_id = (q->current_id + 1) % MAX_SEQU_NUM;
147 *success = 2; /* tell the decoder the packet is lost */ 148 *success = 2; /* tell the decoder the packet is lost */
148 return NULL; 149 return NULL;
149 } else { 150 } else {
@@ -162,8 +163,8 @@ rtp_msg_t *dequeue(struct jitter_buffer *q, int *success)
162 q->front = 0; 163 q->front = 0;
163 164
164 *success = 1; 165 *success = 1;
165 q->current_id = q->queue[front]->_header->_sequence_number; 166 q->current_id = q->queue[front]->header->sequnum;
166 q->current_ts = q->queue[front]->_header->_timestamp; 167 q->current_ts = q->queue[front]->header->timestamp;
167 return q->queue[front]; 168 return q->queue[front];
168} 169}
169 170
@@ -184,7 +185,7 @@ int empty_queue(struct jitter_buffer *q)
184 return 0; 185 return 0;
185} 186}
186 187
187int queue(struct jitter_buffer *q, rtp_msg_t *pk) 188int queue(struct jitter_buffer *q, RTPMessage *pk)
188{ 189{
189 if (q->size == q->capacity) { 190 if (q->size == q->capacity) {
190 printf("buffer full, emptying buffer...\n"); 191 printf("buffer full, emptying buffer...\n");
@@ -214,9 +215,9 @@ int queue(struct jitter_buffer *q, rtp_msg_t *pk)
214 if (b < 0) 215 if (b < 0)
215 b += q->capacity; 216 b += q->capacity;
216 217
217 if (sequence_number_older(q->queue[b]->_header->_sequence_number, q->queue[a]->_header->_sequence_number, 218 if (sequence_number_older(q->queue[b]->header->sequnum, q->queue[a]->header->sequnum,
218 q->queue[b]->_header->_timestamp, q->queue[a]->_header->_timestamp)) { 219 q->queue[b]->header->timestamp, q->queue[a]->header->timestamp)) {
219 rtp_msg_t *temp; 220 RTPMessage *temp;
220 temp = q->queue[a]; 221 temp = q->queue[a];
221 q->queue[a] = q->queue[b]; 222 q->queue[a] = q->queue[b];
222 q->queue[b] = temp; 223 q->queue[b] = temp;
@@ -487,7 +488,7 @@ void *encode_video_thread(void *arg)
487 int p = 0; 488 int p = 0;
488 int err; 489 int err;
489 int got_packet; 490 int got_packet;
490 rtp_msg_t *s_video_msg; 491 RTPMessage *s_video_msg;
491 int video_frame_finished; 492 int video_frame_finished;
492 AVFrame *s_video_frame; 493 AVFrame *s_video_frame;
493 AVFrame *webcam_frame; 494 AVFrame *webcam_frame;
@@ -552,18 +553,13 @@ void *encode_video_thread(void *arg)
552 } 553 }
553 554
554 pthread_mutex_lock(&cs->rtp_msg_mutex_lock); 555 pthread_mutex_lock(&cs->rtp_msg_mutex_lock);
555 THREADLOCK()
556 556
557 if (!enc_video_packet.data) fprintf(stderr, "video packet data is NULL\n"); 557 if (!enc_video_packet.data) fprintf(stderr, "video packet data is NULL\n");
558 558
559 s_video_msg = rtp_msg_new ( cs->_rtp_video, enc_video_packet.data, enc_video_packet.size ) ; 559 if ( 0 > rtp_send_msg ( cs->_rtp_video, cs->_messenger, enc_video_packet.data, enc_video_packet.size) ) {
560
561 if (!s_video_msg) {
562 printf("invalid message\n"); 560 printf("invalid message\n");
563 } 561 }
564 562
565 rtp_send_msg ( cs->_rtp_video, s_video_msg, cs->_networking );
566 THREADUNLOCK()
567 pthread_mutex_unlock(&cs->rtp_msg_mutex_lock); 563 pthread_mutex_unlock(&cs->rtp_msg_mutex_lock);
568 av_free_packet(&enc_video_packet); 564 av_free_packet(&enc_video_packet);
569 } 565 }
@@ -587,7 +583,7 @@ void *encode_video_thread(void *arg)
587void *encode_audio_thread(void *arg) 583void *encode_audio_thread(void *arg)
588{ 584{
589 codec_state *cs = (codec_state *)arg; 585 codec_state *cs = (codec_state *)arg;
590 rtp_msg_t *s_audio_msg; 586 RTPMessage *s_audio_msg;
591 unsigned char encoded_data[4096]; 587 unsigned char encoded_data[4096];
592 int encoded_size = 0; 588 int encoded_size = 0;
593 int16_t frame[4096]; 589 int16_t frame[4096];
@@ -606,12 +602,11 @@ void *encode_audio_thread(void *arg)
606 printf("Could not encode audio packet\n"); 602 printf("Could not encode audio packet\n");
607 } else { 603 } else {
608 pthread_mutex_lock(&cs->rtp_msg_mutex_lock); 604 pthread_mutex_lock(&cs->rtp_msg_mutex_lock);
609 THREADLOCK() 605
610 rtp_set_payload_type(cs->_rtp_audio, 96); 606 rtp_send_msg ( cs->_rtp_audio, cs->_messenger, encoded_data, encoded_size );
611 s_audio_msg = rtp_msg_new (cs->_rtp_audio, encoded_data, encoded_size) ; 607
612 rtp_send_msg ( cs->_rtp_audio, s_audio_msg, cs->_networking );
613 pthread_mutex_unlock(&cs->rtp_msg_mutex_lock); 608 pthread_mutex_unlock(&cs->rtp_msg_mutex_lock);
614 THREADUNLOCK() 609
615 } 610 }
616 } else { 611 } else {
617 usleep(1000); 612 usleep(1000);
@@ -646,7 +641,7 @@ void *decode_video_thread(void *arg)
646{ 641{
647 codec_state *cs = (codec_state *)arg; 642 codec_state *cs = (codec_state *)arg;
648 cs->video_stream = 0; 643 cs->video_stream = 0;
649 rtp_msg_t *r_msg; 644 RTPMessage *r_msg;
650 int dec_frame_finished; 645 int dec_frame_finished;
651 AVFrame *r_video_frame; 646 AVFrame *r_video_frame;
652 r_video_frame = avcodec_alloc_frame(); 647 r_video_frame = avcodec_alloc_frame();
@@ -659,8 +654,8 @@ void *decode_video_thread(void *arg)
659 r_msg = rtp_recv_msg ( cs->_rtp_video ); 654 r_msg = rtp_recv_msg ( cs->_rtp_video );
660 655
661 if (r_msg) { 656 if (r_msg) {
662 memcpy(dec_video_packet.data, r_msg->_data, r_msg->_length); 657 memcpy(dec_video_packet.data, r_msg->data, r_msg->length);
663 dec_video_packet.size = r_msg->_length; 658 dec_video_packet.size = r_msg->length;
664 avcodec_decode_video2(cs->video_decoder_ctx, r_video_frame, &dec_frame_finished, &dec_video_packet); 659 avcodec_decode_video2(cs->video_decoder_ctx, r_video_frame, &dec_frame_finished, &dec_video_packet);
665 660
666 if (dec_frame_finished) { 661 if (dec_frame_finished) {
@@ -695,7 +690,7 @@ void *decode_video_thread(void *arg)
695void *decode_audio_thread(void *arg) 690void *decode_audio_thread(void *arg)
696{ 691{
697 codec_state *cs = (codec_state *)arg; 692 codec_state *cs = (codec_state *)arg;
698 rtp_msg_t *r_msg; 693 RTPMessage *r_msg;
699 694
700 int frame_size = AUDIO_FRAME_SIZE; 695 int frame_size = AUDIO_FRAME_SIZE;
701 int data_size; 696 int data_size;
@@ -747,7 +742,7 @@ void *decode_audio_thread(void *arg)
747 opus_int16 PCM[frame_size]; 742 opus_int16 PCM[frame_size];
748 743
749 while (!cs->quit && cs->receive_audio) { 744 while (!cs->quit && cs->receive_audio) {
750 THREADLOCK() 745
751 r_msg = rtp_recv_msg ( cs->_rtp_audio ); 746 r_msg = rtp_recv_msg ( cs->_rtp_audio );
752 747
753 if (r_msg) { 748 if (r_msg) {
@@ -765,7 +760,7 @@ void *decode_audio_thread(void *arg)
765 if (success > 0) { 760 if (success > 0) {
766 /* good packet */ 761 /* good packet */
767 if (success == 1) { 762 if (success == 1) {
768 dec_frame_len = opus_decode(cs->audio_decoder, r_msg->_data, r_msg->_length, PCM, frame_size, 0); 763 dec_frame_len = opus_decode(cs->audio_decoder, r_msg->data, r_msg->length, PCM, frame_size, 0);
769 rtp_free_msg(cs->_rtp_audio, r_msg); 764 rtp_free_msg(cs->_rtp_audio, r_msg);
770 } 765 }
771 766
@@ -806,8 +801,7 @@ void *decode_audio_thread(void *arg)
806 801
807 } 802 }
808 } 803 }
809 804
810 THREADUNLOCK()
811 usleep(1000); 805 usleep(1000);
812 } 806 }
813 807