From bceff73429c2a199131b69b458c7602a01d465e2 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 8 May 2019 17:49:05 +0800 Subject: [PATCH] =?UTF-8?q?rtsp=E6=9C=8D=E5=8A=A1=E5=99=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=8F=91=E9=80=81rtcp=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtsp/Rtsp.h | 1 + src/Rtsp/RtspSession.cpp | 171 ++++++++++++++++++++------------------- src/Rtsp/RtspSession.h | 4 +- 3 files changed, 93 insertions(+), 83 deletions(-) diff --git a/src/Rtsp/Rtsp.h b/src/Rtsp/Rtsp.h index f4b0b2fe..d209800a 100644 --- a/src/Rtsp/Rtsp.h +++ b/src/Rtsp/Rtsp.h @@ -111,6 +111,7 @@ class RtcpCounter { public: uint32_t pktCnt = 0; uint32_t octCount = 0; + //网络字节序 uint32_t timeStamp = 0; }; diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index 68159991..e05275b4 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -976,44 +976,6 @@ inline bool RtspSession::findStream() { } -inline void RtspSession::sendRtpPacket(const RtpPacket::Ptr & pkt) { - //InfoL<<(int)pkt.Interleaved; - switch (_rtpType) { - case Rtsp::RTP_TCP: { - BufferRtp::Ptr buffer(new BufferRtp(pkt)); - send(buffer); -#ifdef RTSP_SEND_RTCP - int iTrackIndex = getTrackIndexByTrackId(pkt.interleaved / 2); - RtcpCounter &counter = _aRtcpCnt[iTrackIndex]; - counter.pktCnt += 1; - counter.octCount += (pkt.length - 12); - auto &_ticker = _aRtcpTicker[iTrackIndex]; - if (_ticker.elapsedTime() > 5 * 1000) { - //send rtcp every 5 second - _ticker.resetTime(); - counter.timeStamp = pkt.timeStamp; - sendRTCP(); - } -#endif - } - break; - case Rtsp::RTP_UDP: { - int iTrackIndex = getTrackIndexByTrackType(pkt->type); - auto &pSock = _apRtpSock[iTrackIndex]; - if (!pSock) { - shutdown(); - return; - } - BufferRtp::Ptr buffer(new BufferRtp(pkt,4)); - _ui64TotalBytes += buffer->size(); - pSock->send(buffer); - } - break; - default: - break; - } -} - void RtspSession::onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx) { _pushSrc->onWrite(rtppt, false); } @@ -1202,63 +1164,108 @@ bool RtspSession::close() { return true; } + +inline void RtspSession::sendRtpPacket(const RtpPacket::Ptr & pkt) { + //InfoL<<(int)pkt.Interleaved; + switch (_rtpType) { + case Rtsp::RTP_TCP: { + BufferRtp::Ptr buffer(new BufferRtp(pkt)); + send(buffer); + } + break; + case Rtsp::RTP_UDP: { + int iTrackIndex = getTrackIndexByTrackType(pkt->type); + auto &pSock = _apRtpSock[iTrackIndex]; + if (!pSock) { + shutdown(); + return; + } + BufferRtp::Ptr buffer(new BufferRtp(pkt,4)); + _ui64TotalBytes += buffer->size(); + pSock->send(buffer); + } + break; + default: + break; + } + #ifdef RTSP_SEND_RTCP -inline void RtspSession::sendRTCP() { - //DebugL; - uint8_t aui8Rtcp[60] = {0}; - uint8_t *pui8Rtcp_SR = aui8Rtcp + 4, *pui8Rtcp_SDES = pui8Rtcp_SR + 28; - for (uint8_t i = 0; i < _uiTrackCnt; i++) { - auto &track = _aTrackInfo[i]; - auto &counter = _aRtcpCnt[i]; + int iTrackIndex = getTrackIndexByInterleaved(pkt->interleaved); + if(iTrackIndex == -1){ + return; + } + RtcpCounter &counter = _aRtcpCnt[iTrackIndex]; + counter.pktCnt += 1; + counter.octCount += (pkt->length - pkt->offset); + auto &ticker = _aRtcpTicker[iTrackIndex]; + if (ticker.elapsedTime() > 5 * 1000) { + //send rtcp every 5 second + ticker.resetTime(); + //直接保存网络字节序 + memcpy(&counter.timeStamp, pkt->payload + 8 , 4); + sendSenderReport(_rtpType == Rtsp::RTP_TCP,iTrackIndex); + } +#endif +} - aui8Rtcp[0] = '$'; - aui8Rtcp[1] = track.trackId * 2 + 1; - aui8Rtcp[2] = 56 / 256; - aui8Rtcp[3] = 56 % 256; +#ifdef RTSP_SEND_RTCP +inline void RtspSession::sendSenderReport(bool overTcp,int iTrackIndex) { + uint8_t aui8Rtcp[4 + 28 + 10 + sizeof(SERVER_NAME) + 1] = {0}; + uint8_t *pui8Rtcp_SR = aui8Rtcp + 4, *pui8Rtcp_SDES = pui8Rtcp_SR + 28; + auto &track = _aTrackInfo[iTrackIndex]; + auto &counter = _aRtcpCnt[iTrackIndex]; - pui8Rtcp_SR[0] = 0x80; - pui8Rtcp_SR[1] = 0xC8; - pui8Rtcp_SR[2] = 0x00; - pui8Rtcp_SR[3] = 0x06; + aui8Rtcp[0] = '$'; + aui8Rtcp[1] = track->_interleaved + 1; + aui8Rtcp[2] = (sizeof(aui8Rtcp) - 4) / 256; + aui8Rtcp[3] = (sizeof(aui8Rtcp) - 4) % 256; - uint32_t ssrc=htonl(track.ssrc); - memcpy(&pui8Rtcp_SR[4], &ssrc, 4); + pui8Rtcp_SR[0] = 0x80; + pui8Rtcp_SR[1] = 0xC8; + pui8Rtcp_SR[2] = 0x00; + pui8Rtcp_SR[3] = 0x06; - uint64_t msw; - uint64_t lsw; - struct timeval tv; - gettimeofday(&tv, NULL); - msw = tv.tv_sec + 0x83AA7E80; /* 0x83AA7E80 is the number of seconds from 1900 to 1970 */ - lsw = (uint32_t) ((double) tv.tv_usec * (double) (((uint64_t) 1) << 32) * 1.0e-6); + uint32_t ssrc=htonl(track->_ssrc); + memcpy(&pui8Rtcp_SR[4], &ssrc, 4); - msw = htonl(msw); - memcpy(&pui8Rtcp_SR[8], &msw, 4); + uint64_t msw; + uint64_t lsw; + struct timeval tv; + gettimeofday(&tv, NULL); + msw = tv.tv_sec + 0x83AA7E80; /* 0x83AA7E80 is the number of seconds from 1900 to 1970 */ + lsw = (uint32_t) ((double) tv.tv_usec * (double) (((uint64_t) 1) << 32) * 1.0e-6); - lsw = htonl(lsw); - memcpy(&pui8Rtcp_SR[12], &lsw, 4); + msw = htonl(msw); + memcpy(&pui8Rtcp_SR[8], &msw, 4); - uint32_t rtpStamp = htonl(counter.timeStamp); - memcpy(&pui8Rtcp_SR[16], &rtpStamp, 4); + lsw = htonl(lsw); + memcpy(&pui8Rtcp_SR[12], &lsw, 4); + //直接使用网络字节序 + memcpy(&pui8Rtcp_SR[16], &counter.timeStamp, 4); - uint32_t pktCnt = htonl(counter.pktCnt); - memcpy(&pui8Rtcp_SR[20], &pktCnt, 4); + uint32_t pktCnt = htonl(counter.pktCnt); + memcpy(&pui8Rtcp_SR[20], &pktCnt, 4); - uint32_t octCount = htonl(counter.octCount); - memcpy(&pui8Rtcp_SR[24], &octCount, 4); + uint32_t octCount = htonl(counter.octCount); + memcpy(&pui8Rtcp_SR[24], &octCount, 4); - pui8Rtcp_SDES[0] = 0x81; - pui8Rtcp_SDES[1] = 0xCA; - pui8Rtcp_SDES[2] = 0x00; - pui8Rtcp_SDES[3] = 0x06; + pui8Rtcp_SDES[0] = 0x81; + pui8Rtcp_SDES[1] = 0xCA; + pui8Rtcp_SDES[2] = 0x00; + pui8Rtcp_SDES[3] = 0x06; - memcpy(&pui8Rtcp_SDES[4], &ssrc, 4); + memcpy(&pui8Rtcp_SDES[4], &ssrc, 4); - pui8Rtcp_SDES[8] = 0x01; - pui8Rtcp_SDES[9] = 0x0f; - memcpy(&pui8Rtcp_SDES[10], "_ZL_RtspServer_", 15); - pui8Rtcp_SDES[25] = 0x00; - send((char *) aui8Rtcp, 60); - } + pui8Rtcp_SDES[8] = 0x01; + pui8Rtcp_SDES[9] = 0x0f; + memcpy(&pui8Rtcp_SDES[10], SERVER_NAME, sizeof(SERVER_NAME)); + pui8Rtcp_SDES[10 + sizeof(SERVER_NAME)] = 0x00; + + if(overTcp){ + send(obtainBuffer((char *) aui8Rtcp, sizeof(aui8Rtcp))); + }else { + _apRtcpSock[iTrackIndex]->send((char *) aui8Rtcp + 4, sizeof(aui8Rtcp) - 4); + } } #endif diff --git a/src/Rtsp/RtspSession.h b/src/Rtsp/RtspSession.h index 6c5c4376..085fb416 100644 --- a/src/Rtsp/RtspSession.h +++ b/src/Rtsp/RtspSession.h @@ -46,6 +46,8 @@ using namespace std; using namespace toolkit; +#define RTSP_SEND_RTCP + namespace mediakit { class RtspSession; @@ -201,7 +203,7 @@ private: #ifdef RTSP_SEND_RTCP RtcpCounter _aRtcpCnt[2]; //rtcp统计,trackid idx 为数组下标 Ticker _aRtcpTicker[2]; //rtcp发送时间,trackid idx 为数组下标 - inline void sendRTCP(); + inline void sendSenderReport(bool overTcp,int iTrackIndex); #endif };