代码:
std::string strName = "fengyuzaitu@51.cto";
int nLength = 2 + 2 + 8 + 20 + 4 + 6 + strName.size();
std::string strRTCPBuffer;
//之所以需要增加长度是为了能够在CNAME字符串结尾添加0x0,保证wireshark能够解析出Type :0 (End)标志,%4是为了4个字节对齐
int nRet = (strName.length() + 2) % 4;
if (0 != nRet)
{
nLength = nLength + 4 - nRet;
}
else
{
nLength = nLength + 4;
}
strRTCPBuffer.resize(nLength);
strRTCPBuffer[0] = 0x24;
strRTCPBuffer[1] = 0x01;
//RTCP报文长度,一段是RTCP统计数据内容,一段是RTCP源描述
*(unsigned short*)&strRTCPBuffer[3] = htons(nLength - 4);
//Sender Report
strRTCPBuffer[4] = (char)(2 << 6); // V=2, P=RC=0
strRTCPBuffer[5] = (char)200; // PT=SR=200
*(unsigned short*)&strRTCPBuffer[6] = htons(6); // 7个32比特减一
*(unsigned int*)&strRTCPBuffer[8] = htonl(0x23456);
*(unsigned int*)&strRTCPBuffer[12] = htonl(m_video.sendtime >> 32); // High 32-bits
*(unsigned int*)&strRTCPBuffer[16] = htonl(m_video.sendtime & 0xFFFFFFFF); // Low 32-bits
*(unsigned int*)&strRTCPBuffer[20] = htonl(m_nRtpTime);
*(unsigned int*)&strRTCPBuffer[24] = htonl(m_nSendPacketCount);
*(unsigned int*)&strRTCPBuffer[28] = htonl(m_nSendLength);
//Source description
strRTCPBuffer[32] = (char)(2 << 6) + 1; // V=2, P=0, SC=1
strRTCPBuffer[33] = 202;
//RTCP源描述长度,减去4个字节($+channel+2个字节长度)减去Sender Report报文长度
*(unsigned short*)&strRTCPBuffer[34] = htons((nLength - 4 - 7 * 4) / 4 - 1);
*(unsigned int*)&strRTCPBuffer[39] = htonl(0x23456);
strRTCPBuffer[40] = 1; // CNAME=1
strRTCPBuffer[41] = (char)strName.size();
memcpy(&strRTCPBuffer[42], strName.c_str(), strName.size());
注意:通过htons,htonl等函数转换为网络字节序,否则接收端无法正常解析,这一点可以通过wireshark的分析可以看出来
注意:类似于RTP信息包,每个RTCP信息包以固定部分开始,紧接着的是可变长结构单元,最后以一个32位边界结束
没有进行对齐的报文,WireShark显示异常
RTSP Interleaved Frame, Channel: 0x01, 80 bytes
Magic: 0x24
Channel: 0x01
Length: 80
Real-time Transport Control Protocol (Receiver Report)
10.. .... = Version: RFC 1889 Version (2)
..0. .... = Padding: False
...0 0001 = Reception report count: 1
Packet type: Receiver Report (201)
Length: 7 (32 bytes)
Sender ***C: 0xc009d701 (3221870337)
Source 1
Identifier: 0xbd7266dd (3178391261)
***C contents
Fraction lost: 0 / 256
Cumulative number of packets lost: 0
Extended highest sequence number received: 2175
Sequence number cycles count: 0
Highest sequence number received: 2175
Interarrival jitter: 0
Last SR timestamp: 0 (0x00000000)
Delay since last SR timestamp: 0 (0 milliseconds)
Real-time Transport Control Protocol (Source description)
10.. .... = Version: RFC 1889 Version (2)
..0. .... = Padding: False
...0 0001 = Source count: 1
Packet type: Source description (202)
Length: 11 (48 bytes)
Chunk 1, ***C/CSRC 0xC009D701
Identifier: 0xc009d701 (3221870337)
SDES items
[RTCP frame length check: OK - 80 bytes](实际上这里解析出错)