gpt4 book ai didi

从多播流中计算 RTP 序列号

转载 作者:太空宇宙 更新时间:2023-11-04 01:54:32 28 4
gpt4 key购买 nike

我尝试从多播流中计算 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 header 并将数据写入 file.mpg 和视频播放正常,没有错误和伪影。

请解释我如何正确计算多播流中的 RTP 序列号

最佳答案

由于缓冲区是 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) 缓冲区转换是错误的。该示例显示了您发布的最后一行结果发生了什么。

关于从多播流中计算 RTP 序列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36796952/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com