我尝试从多播流中计算RTP序列号并确定丢失的包。
请参阅以下C代码:
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;
}
}
}
我收到了以下结果:
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
每个128
RTP程序包引发错误,您可以看到|ESEC - SEQ| = 256
,我还删除了RTP标头并在file.mpg和视频中写入数据,没有错误和伪像。
请解释如何正确计算多播流中的RTP序列号
答案 0 :(得分:3)
由于缓冲区为signed char
数组,转换中出现溢出(左移)。它是UB
您可以使用此代码示例测试/解决它:
#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;
}
输出是:
seq1: 59264
seq2: 59520
seq3: 59520
正如您所看到的那样,使用signed char
(seq1
)缓冲区转换是错误的。
exaple显示了您发布的最后一行结果。