From c2e1083493d5b4c1a05e2097fe18950a5eadc7e0 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 28 Feb 2023 22:23:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=8D=E7=94=A8rtp/rtcp=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E8=BF=87=E6=BB=A4=E9=9D=9Ertp=E5=8C=85:=20#2?= =?UTF-8?q?247?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtp/RtpProcess.cpp | 9 ++------- src/Rtp/RtpSession.cpp | 5 +++++ src/Rtsp/Rtsp.cpp | 16 ++++++++++++++++ src/Rtsp/Rtsp.h | 3 +++ webrtc/WebRtcTransport.cpp | 19 +++++-------------- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/Rtp/RtpProcess.cpp b/src/Rtp/RtpProcess.cpp index 993ed158..fac1bd2b 100644 --- a/src/Rtp/RtpProcess.cpp +++ b/src/Rtp/RtpProcess.cpp @@ -70,14 +70,9 @@ RtpProcess::~RtpProcess() { } } -static bool is_rtp(const char *buf) { - RtpHeader *header = (RtpHeader *)buf; - return ((header->pt < 64) || (header->pt >= 96)); -} - bool RtpProcess::inputRtp(bool is_udp, const Socket::Ptr &sock, const char *data, size_t len, const struct sockaddr *addr, uint64_t *dts_out) { - if (!is_rtp(data)) { - WarnL << "Not rtp packet"; + if (!isRtp(data, len)) { + WarnP(this) << "Not rtp packet"; return false; } if (_sock != sock) { diff --git a/src/Rtp/RtpSession.cpp b/src/Rtp/RtpSession.cpp index f99734c8..75d1f9a5 100644 --- a/src/Rtp/RtpSession.cpp +++ b/src/Rtp/RtpSession.cpp @@ -12,6 +12,7 @@ #include "RtpSession.h" #include "RtpSelector.h" #include "Network/TcpServer.h" +#include "Rtsp/Rtsp.h" #include "Rtsp/RtpReceiver.h" #include "Common/config.h" @@ -93,6 +94,10 @@ void RtpSession::onRtpPacket(const char *data, size_t len) { } } if (!_process) { + if (!isRtp(data, len)) { + WarnP(this) << "Not rtp packet"; + return; + } //未设置ssrc时,尝试获取ssrc if (!_ssrc && !RtpSelector::getSSRC(data, len, _ssrc)) { return; diff --git a/src/Rtsp/Rtsp.cpp b/src/Rtsp/Rtsp.cpp index 251b7f65..b80a88f7 100644 --- a/src/Rtsp/Rtsp.cpp +++ b/src/Rtsp/Rtsp.cpp @@ -442,6 +442,22 @@ string printSSRC(uint32_t ui32Ssrc) { return tmp; } +bool isRtp(const char *buf, size_t size) { + if (size < 2) { + return false; + } + RtpHeader *header = (RtpHeader *)buf; + return ((header->pt < 64) || (header->pt >= 96)); +} + +bool isRtcp(const char *buf, size_t size) { + if (size < 2) { + return false; + } + RtpHeader *header = (RtpHeader *)buf; + return ((header->pt >= 64) && (header->pt < 96)); +} + Buffer::Ptr makeRtpOverTcpPrefix(uint16_t size, uint8_t interleaved) { auto rtp_tcp = BufferRaw::create(); rtp_tcp->setCapacity(RtpPacket::kRtpTcpHeaderSize); diff --git a/src/Rtsp/Rtsp.h b/src/Rtsp/Rtsp.h index 9a581b95..5e7370c6 100644 --- a/src/Rtsp/Rtsp.h +++ b/src/Rtsp/Rtsp.h @@ -337,5 +337,8 @@ void makeSockPair(std::pair &pair, c //十六进制方式打印ssrc std::string printSSRC(uint32_t ui32Ssrc); +bool isRtp(const char *buf, size_t size); +bool isRtcp(const char *buf, size_t size); + } //namespace mediakit #endif //RTSP_RTSP_H_ diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index bbb3844a..b4d1cb73 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -15,6 +15,7 @@ #include "Rtcp/Rtcp.h" #include "Rtcp/RtcpFCI.h" #include "Rtcp/RtcpContext.h" +#include "Rtsp/Rtsp.h" #include "Rtsp/RtpReceiver.h" #include "WebRtcTransport.h" @@ -287,20 +288,10 @@ std::string WebRtcTransport::getAnswerSdp(const string &offer) { } } -static bool is_dtls(char *buf) { +static bool isDtls(char *buf) { return ((*buf > 19) && (*buf < 64)); } -static bool is_rtp(char *buf) { - RtpHeader *header = (RtpHeader *)buf; - return ((header->pt < 64) || (header->pt >= 96)); -} - -static bool is_rtcp(char *buf) { - RtpHeader *header = (RtpHeader *)buf; - return ((header->pt >= 64) && (header->pt < 96)); -} - static string getPeerAddress(RTC::TransportTuple *tuple) { return SockUtil::inet_ntoa(tuple); } @@ -315,11 +306,11 @@ void WebRtcTransport::inputSockData(char *buf, int len, RTC::TransportTuple *tup _ice_server->ProcessStunPacket(packet.get(), tuple); return; } - if (is_dtls(buf)) { + if (isDtls(buf)) { _dtls_transport->ProcessDtlsData((uint8_t *)buf, len); return; } - if (is_rtp(buf)) { + if (isRtp(buf, len)) { if (!_srtp_session_recv) { WarnL << "received rtp packet when dtls not completed from:" << getPeerAddress(tuple); return; @@ -329,7 +320,7 @@ void WebRtcTransport::inputSockData(char *buf, int len, RTC::TransportTuple *tup } return; } - if (is_rtcp(buf)) { + if (isRtcp(buf, len)) { if (!_srtp_session_recv) { WarnL << "received rtcp packet when dtls not completed from:" << getPeerAddress(tuple); return;