c++ - 在cpp中使用libpcap打印rtp头信息

标签 c++ rtp libpcap

我正在使用 libpcap 解析 pcap 文件。

我想打印 rtp&rtcp payload type(96 for H264/0 for PCMU)(还有时间戳),这样我就可以区分它是音频/视频。

我可以正确打印那些 rtp/rtcp 数据包序列号,但不能打印有效负载类型。

typedef struct {

   unsigned int version:2;   /* protocol version */
   unsigned int p:1;         /* padding flag */
   unsigned int x:1;         /* header extension flag */
   unsigned int cc:4;        /* CSRC count */
   unsigned int m:1;         /* marker bit */
   unsigned int pt:7;        /* payload type */

       u_int16 seq;              /* sequence number */
       u_int32 ts;               /* timestamp */
       u_int32 ssrc;             /* synchronization source */
       u_int32 csrc[1];          /* optional CSRC list */
   } rtp_hdr_t;

rtp_hdr_t *rtphdr=(rtp_hdr_t *)(packet + sizeof(struct ether_header) +sizeof(struct ip_header) + sizeof(struct udp_header));

cout<< ntohs(rtphdr->pt) << endl;

例如:获取负载类型是 12288 和 0。但我必须获取 96 和 0(如在 wireshark 中)。

cout << ntohs(rtphdr->ts) << endl;

例如:获取时间戳信息如 49892(5 位十进制数) 但我必须获得像 3269770717 这样的值。

最佳答案

ntohs() 函数将无符号短整数从网络字节顺序 转换为主机字节顺序。请注意,它是字节顺序,因此,对于单字节有效负载,您不需要此转换。

对于时间戳,您应该使用 ntohl(),因为您使用的是 32 位值。

更新 我认为这比使用字段更自然:

typedef struct {
   u_int8 version_p_x_cc;
   u_int8 m_pt;
   u_int16 seq; 
   ....
}

// payload type:
cout<< rtphdr->m_pt & 0x7f << endl;
// marker bit
cout<< (rtphdr->m_pt >> 7) & 0x01  << endl;

关于c++ - 在cpp中使用libpcap打印rtp头信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7858088/

相关文章:

c# - 播放解码的RTP数据包的音频

c++ - 将 libpcap 数据包数据从 const u_char* 复制到另一个 const u_char*

gcc - Libnids 64 位系统

c++ - 为什么 GetFullPathName 返回工作目录?

c++ - 文件流析构函数可以在 C++ 中抛出异常吗?

c++ - 什么是 C 中 C++ 的新/删除等价物?

java - Android NDK释放内存

ffmpeg - 如何将多个RTP选项传递给ffmpeg?

ios - 播放描述 UDP/RTP H264 流 iOS 8+ 的 .sdp 文件

我可以使用 pcap 库来接收 ipv6 数据包吗?