Skip to content

Commit

Permalink
feat: support timestamp revert & wait for first rtp seq
Browse files Browse the repository at this point in the history
  • Loading branch information
ireader committed Dec 3, 2022
1 parent 10063f9 commit 1e373ae
Showing 1 changed file with 85 additions and 4 deletions.
89 changes: 85 additions & 4 deletions librtp/source/rtp-queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct rtp_queue_t* rtp_queue_create(int threshold, int frequency, void(*freepkt
return NULL;

rtp_queue_reset(q);
q->probation = 1;
q->threshold = threshold;
q->frequency = frequency;
q->free = freepkt;
Expand Down Expand Up @@ -193,10 +194,15 @@ int rtp_queue_write(struct rtp_queue_t* q, struct rtp_packet_t* pkt)
{
if (q->size > 0 && (uint16_t)pkt->rtp.seq == q->last_seq + 1)
{
q->probation--;
if (0 == q->probation)
if (0 == --q->probation)
q->first_seq = (uint16_t)q->items[q->pos].pkt->rtp.seq;
}
else if (q->size == 0 && q->probation == 1)
{
// init
q->first_seq = (uint16_t)pkt->rtp.seq;
--q->probation;
}
else
{
rtp_queue_reset(q);
Expand Down Expand Up @@ -267,6 +273,7 @@ int rtp_queue_write(struct rtp_queue_t* q, struct rtp_packet_t* pkt)
}

// for safety
assert(0);
return -1;
}

Expand All @@ -288,7 +295,9 @@ struct rtp_packet_t* rtp_queue_read(struct rtp_queue_t* q)
}
else
{
threshold = (q->items[(q->pos + q->size - 1) % q->capacity].pkt->rtp.timestamp - pkt->rtp.timestamp) / ((uint32_t)q->frequency / 1000);
threshold = (q->items[(q->pos + q->size - 1) % q->capacity].pkt->rtp.timestamp - pkt->rtp.timestamp);
threshold = (int32_t)threshold < 0 ? (uint32_t)(-(int32_t)threshold) : threshold; // fix h.264 b-frames pts order
threshold = (uint32_t)(((uint64_t)threshold) * 1000 / (uint64_t)q->frequency);
if (threshold < (uint32_t)q->threshold)
return NULL;

Expand Down Expand Up @@ -343,7 +352,7 @@ static void rtp_queue_test2(void)
q = rtp_queue_create(100, 90000, rtp_packet_free, NULL);

for(i = 0; i < sizeof(s_seq)/sizeof(s_seq[0]); i++)
s_seq[i] = 45000 + i;
s_seq[i] = (uint16_t)(45000 + i);

// 45460, 45461, 45462, 45464, 45465, 45466, ...,
// 45490, 45491, 45492, 45503, 45504, 45505, 45463,
Expand Down Expand Up @@ -384,6 +393,76 @@ static void rtp_queue_test2(void)
rtp_queue_destroy(q);
}

static void rtp_queue_test3(void)
{
int i;
uint16_t seq;
rtp_queue_t* q;
struct rtp_packet_t* pkt;

static uint16_t s_seq[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11,12,13,14,15,16,17,18,19,20,
21,22,
31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,
23,24,25,26,27,28,29,30,
46,47,48,49,50
};
q = rtp_queue_create(100, 90000, rtp_packet_free, NULL);

seq = s_seq[0];
for (i = 0; i < sizeof(s_seq) / sizeof(s_seq[0]); i++)
{
rtp_queue_packet(q, s_seq[i]);
pkt = rtp_queue_read(q);
if (pkt)
{
//printf("%u ", pkt->rtp.seq);
assert(0 == pkt->rtp.seq - seq++);
free(pkt);
}
}

rtp_queue_destroy(q);
}

static void rtp_queue_test4(void)
{
int i;
uint16_t seq;
rtp_queue_t* q;
struct rtp_packet_t* pkt;

// first packet
static uint16_t s_seq[] = {
1,
17,18,19,20,
21,22,
2, 3, 4, 5, 6, 7, 8, 9, 10,
11,12,13,14,15,16,
23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50
};
q = rtp_queue_create(100, 90000, rtp_packet_free, NULL);

seq = s_seq[0];
for (i = 0; i < sizeof(s_seq) / sizeof(s_seq[0]); i++)
{
rtp_queue_packet(q, s_seq[i]);
pkt = rtp_queue_read(q);
if (pkt)
{
//printf("%u ", pkt->rtp.seq);
assert(0 == pkt->rtp.seq - seq++);
free(pkt);
}
}

rtp_queue_destroy(q);
}

void rtp_queue_test(void)
{
int i;
Expand All @@ -398,6 +477,8 @@ void rtp_queue_test(void)
};

rtp_queue_test2();
rtp_queue_test3();
rtp_queue_test4();

q = rtp_queue_create(100, 90000, rtp_packet_free, NULL);

Expand Down

0 comments on commit 1e373ae

Please sign in to comment.