Reputation: 1811
I trying count RTP Sequence number from multicast stream and a determine lost packages.
Please see the following C code:
int sock, bytesRead;
char buffer[MAXBUFSIZE];
uint16_t seq = 0;
uint16_t eseq = 0;
struct sockaddr_in saddr;
socklen_t socklen;
for(;;) {
bytesRead = recvfrom(sock, buffer, MAXBUFSIZE, 0,
(struct sockaddr *)&saddr, &socklen);
if (bytesRead) {
seq = (buffer[2] << 8)+buffer[3];
eseq++;
if (seq != eseq) {
std::cerr << " SEQ = " << seq << " ESEC = " << eseq << "\n";
eseq = seq;
}
}
}
And I received the following results:
12:47:44 SEQ = 58112 ESEC = 57856
12:47:45 SEQ = 57984 ESEC = 58240
12:47:45 SEQ = 58368 ESEC = 58112
12:47:45 SEQ = 58240 ESEC = 58496
12:47:45 SEQ = 58624 ESEC = 58368
12:47:46 SEQ = 58496 ESEC = 58752
12:47:46 SEQ = 58880 ESEC = 58624
12:47:46 SEQ = 58752 ESEC = 59008
12:47:46 SEQ = 59136 ESEC = 58880
12:47:47 SEQ = 59008 ESEC = 59264
12:47:47 SEQ = 59392 ESEC = 59136
12:47:47 SEQ = 59264 ESEC = 59520
Error raised with each 128
RTP package and as you can see |ESEC - SEQ| = 256
, I also removed RTP headers and write data in a file.mpg and video playing fine without errors and artefacts.
Please explain how can I properly count RTP Sequence number from multicast stream
Upvotes: 1
Views: 269
Reputation: 16243
You have an overflow in your conversion(left-shift) due to buffer that is a signed char
array. It is UB
You could test/solve it using this code sample:
#include <stdio.h>
#include <stdint.h>
int main()
{
uint16_t seq1;
uint16_t seq2;
uint16_t seq3;
char buffer1[] = {0, 0, 0xE8, 0x80};
unsigned char buffer2[] = {0, 0, 0xE8, 0x80};
seq1 = (buffer1[2] << 8)+buffer1[3];
printf("seq1: %d\n", seq1);
seq2 = (buffer2[2] << 8)+buffer2[3];
printf("seq2: %d\n", seq2);
seq3 = buffer1[2];
seq3 = (seq3 << 8)+buffer2[3];
printf("seq3: %d\n", seq3);
return 0;
}
Output is:
seq1: 59264
seq2: 59520
seq3: 59520
As you can see using a signed char
(seq1
) buffer the conversion is wrong.
The exaple shows what happened with last line of result you posted.
Upvotes: 3