场景
RTP over TCP中 RTCP的实现代码
std::string strName = "fengyuzaitu@51.cto";
int nLength = 2 + 2 + 8 + 20 + 4 + 6 + strName.size();
std::string strRTCPBuffer;
//对齐四个字节,并且让最后的字符是0x00,保证wireshark抓包最后显示Type: END (0)
int nRet = (strName.length() + 2) % 4;
if (0 != nRet)
{
nLength = nLength + 4 - nRet;
}
else
{
nLength = nLength + 4;
}
strRTCPBuffer.resize(nLength);
//$+channel
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(m_n***c);
*(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(m_n***c);
strRTCPBuffer[40] = 1; // CNAME=1
strRTCPBuffer[41] = (char)strName.size();
memcpy(&strRTCPBuffer[42], strName.c_str(), strName.size());
wireshark抓包展示内容:
Frame 151081: 118 bytes on wire (944 bits), 118 bytes captured (944 bits) on interface 0
Ethernet II, Src: Clevo_5a:e2:a6 (80:fa:5b:5a:e2:a6), Dst: c4:b8:b4:01:56:de (c4:b8:b4:01:56:de)
Internet Protocol Version 4, Src: 192.168.58.173, Dst: 192.168.10.162
Transmission Control Protocol, Src Port: 554, Dst Port: 6470, Seq: 29276514, Ack: 1506, Len: 64
RTSP Interleaved Frame, Channel: 0x01, 60 bytes
Magic: 0x24
Channel: 0x01
Length: 60
Real-time Transport Control Protocol (Sender Report)
10.. .... = Version: RFC 1889 Version (2)
..0. .... = Padding: False
...0 0000 = Reception report count: 0
Packet type: Sender Report (200)
Length: 6 (28 bytes)
Sender ***C: 0xdb7c74bd (3682366653)
Timestamp, MSW: 0 (0x00000000)
Timestamp, LSW: 449468312 (0x1aca5798)
[MSW and LSW as NTP timestamp: Jan 1, 1970 00:00:00.104649996 UTC]
RTP timestamp: 897337600
Sender's packet count: 24924
Sender's octet count: 28834770
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: 7 (32 bytes)
Chunk 1, ***C/CSRC 0xDB
Identifier: 0x000000db (219)
SDES items
Type: CNAME (user and domain) (1)
Length: 18
Text: fengyuzaitu@51.cto
Type: END (0)
[RTCP frame length check: OK - 60 bytes]
来源:oschina
链接:https://my.oschina.net/u/4296417/blog/4277216