From 9039f8c1f61ea3205cd27139a02af3d8b3e8b899 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 24 Jun 2019 16:07:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=B6=85=E5=A4=A7rtp?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Android/app/src/main/cpp/native-lib.cpp | 2 +- src/Rtsp/RtpReceiver.cpp | 30 ++++++++++++------------- src/Rtsp/RtspPlayer.cpp | 2 +- src/Rtsp/RtspSession.h | 4 ++-- src/RtspMuxer/AACRtpCodec.cpp | 4 ++-- src/RtspMuxer/H264RtpCodec.cpp | 4 ++-- src/RtspMuxer/H265RtpCodec.cpp | 4 ++-- src/RtspMuxer/RtpCodec.cpp | 6 +++-- src/RtspMuxer/RtpCodec.h | 11 +-------- 9 files changed, 30 insertions(+), 37 deletions(-) diff --git a/Android/app/src/main/cpp/native-lib.cpp b/Android/app/src/main/cpp/native-lib.cpp index 12e95eb6..79538cfd 100644 --- a/Android/app/src/main/cpp/native-lib.cpp +++ b/Android/app/src/main/cpp/native-lib.cpp @@ -203,7 +203,7 @@ JNI_API(jlong,createMediaPlayer,jstring url,jobject callback){ emitEvent((jobject)globalWeakRef,"onShutdown","(ILjava/lang/String;)V",(jint)ex.getErrCode(),env->NewStringUTF(ex.what())); }); - (*player)[Client::kRtpType] = Rtsp::RTP_UDP; + (*player)[Client::kRtpType] = Rtsp::RTP_TCP; player->play(stringFromJstring(env,url)); return (jlong)(ret); diff --git a/src/Rtsp/RtpReceiver.cpp b/src/Rtsp/RtpReceiver.cpp index 08adcef2..8dd0c929 100644 --- a/src/Rtsp/RtpReceiver.cpp +++ b/src/Rtsp/RtpReceiver.cpp @@ -45,9 +45,9 @@ RtpReceiver::~RtpReceiver() {} bool RtpReceiver::handleOneRtp(int iTrackidx,SdpTrack::Ptr &track, unsigned char *pucData, unsigned int uiLen) { auto pt_ptr=_pktPool.obtain(); auto &rtppt=*pt_ptr; - rtppt.interleaved = track->_interleaved; - rtppt.length = uiLen + 4; + auto length = uiLen + 4; + rtppt.interleaved = track->_interleaved; rtppt.mark = pucData[1] >> 7; rtppt.PT = pucData[1] & 0x7F; //序列号 @@ -81,12 +81,8 @@ bool RtpReceiver::handleOneRtp(int iTrackidx,SdpTrack::Ptr &track, unsigned char } _aui32SsrcErrorCnt[iTrackidx] = 0; - rtppt.payload[0] = '$'; - rtppt.payload[1] = rtppt.interleaved; - rtppt.payload[2] = uiLen >> 8; - rtppt.payload[3] = (uiLen & 0x00FF); - - rtppt.offset = 16; + //获取rtp中媒体数据偏移量 + rtppt.offset = 12 + 4; int csrc = pucData[0] & 0x0f; int ext = pucData[0] & 0x10; rtppt.offset += 4 * csrc; @@ -99,16 +95,20 @@ bool RtpReceiver::handleOneRtp(int iTrackidx,SdpTrack::Ptr &track, unsigned char rtppt.offset += ext; } - if(rtppt.length - rtppt.offset <= 0){ - WarnL << "无有效负载的rtp包:" << rtppt.length << "<=" << (int)rtppt.offset; + if(length - rtppt.offset <= 0){ + WarnL << "无有效负载的rtp包:" << length << "<=" << (int)rtppt.offset; return false; } - if(uiLen > sizeof(rtppt.payload) - 4){ - WarnL << "超长的rtp包:" << uiLen << ">" << sizeof(rtppt.payload) - 4; - return false; - } - memcpy(rtppt.payload + 4, pucData, uiLen); + rtppt.setCapacity(length); + rtppt.setSize(length); + uint8_t *payload_ptr = (uint8_t *)rtppt.data(); + payload_ptr[0] = '$'; + payload_ptr[1] = rtppt.interleaved; + payload_ptr[2] = uiLen >> 8; + payload_ptr[3] = (uiLen & 0x00FF); + //拷贝rtp负载 + memcpy(payload_ptr + 4, pucData, uiLen); /////////////////////////////////RTP排序逻辑/////////////////////////////////// if(rtppt.sequence != _aui16LastSeq[iTrackidx] + 1 && _aui16LastSeq[iTrackidx] != 0){ diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index ee641d51..6853116d 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -732,7 +732,7 @@ void RtspPlayer::onRecvRTP_l(const RtpPacket::Ptr &pkt, const SdpTrack::Ptr &tra //send rtcp every 5 second counter.lastTimeStamp = counter.timeStamp; //直接保存网络字节序 - memcpy(&counter.timeStamp, pkt->payload + 8 , 4); + memcpy(&counter.timeStamp, pkt->data() + 8 , 4); if(counter.lastTimeStamp != 0){ sendReceiverReport(_eType == Rtsp::RTP_TCP,iTrackIndex); ticker.resetTime(); diff --git a/src/Rtsp/RtspSession.h b/src/Rtsp/RtspSession.h index 6d5b064f..433c13f7 100644 --- a/src/Rtsp/RtspSession.h +++ b/src/Rtsp/RtspSession.h @@ -57,10 +57,10 @@ public: virtual ~BufferRtp(){} char *data() const override { - return (char *)_rtp->payload + _offset; + return (char *)_rtp->data() + _offset; } uint32_t size() const override { - return _rtp->length - _offset; + return _rtp->size() - _offset; } private: RtpPacket::Ptr _rtp; diff --git a/src/RtspMuxer/AACRtpCodec.cpp b/src/RtspMuxer/AACRtpCodec.cpp index 58474324..a05f8228 100644 --- a/src/RtspMuxer/AACRtpCodec.cpp +++ b/src/RtspMuxer/AACRtpCodec.cpp @@ -105,10 +105,10 @@ bool AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) { RtpCodec::inputRtp(rtppack, false); // 获取rtp数据长度 - int length = rtppack->length - rtppack->offset; + int length = rtppack->size() - rtppack->offset; // 获取rtp数据 - const uint8_t *rtp_packet_buf = (uint8_t *)rtppack->payload + rtppack->offset; + const uint8_t *rtp_packet_buf = (uint8_t *)rtppack->data() + rtppack->offset; do { diff --git a/src/RtspMuxer/H264RtpCodec.cpp b/src/RtspMuxer/H264RtpCodec.cpp index 09260793..5ebd3100 100644 --- a/src/RtspMuxer/H264RtpCodec.cpp +++ b/src/RtspMuxer/H264RtpCodec.cpp @@ -91,8 +91,8 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) { * Type==8:PPS frame */ - const uint8_t *frame = (uint8_t *) rtppack->payload + rtppack->offset; - int length = rtppack->length - rtppack->offset; + const uint8_t *frame = (uint8_t *) rtppack->data() + rtppack->offset; + int length = rtppack->size() - rtppack->offset; NALU nal; MakeNalu(*frame, nal); diff --git a/src/RtspMuxer/H265RtpCodec.cpp b/src/RtspMuxer/H265RtpCodec.cpp index 343b7ef8..19d81b7f 100644 --- a/src/RtspMuxer/H265RtpCodec.cpp +++ b/src/RtspMuxer/H265RtpCodec.cpp @@ -82,8 +82,8 @@ bool H265RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) { } bool H265RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) { - const uint8_t *frame = (uint8_t *) rtppack->payload + rtppack->offset; - int length = rtppack->length - rtppack->offset; + const uint8_t *frame = (uint8_t *) rtppack->data() + rtppack->offset; + int length = rtppack->size() - rtppack->offset; int nal = H265_TYPE(frame[0]); if (nal > 50){ diff --git a/src/RtspMuxer/RtpCodec.cpp b/src/RtspMuxer/RtpCodec.cpp index 5b0b3a14..fe1b6a5a 100644 --- a/src/RtspMuxer/RtpCodec.cpp +++ b/src/RtspMuxer/RtpCodec.cpp @@ -35,7 +35,10 @@ RtpPacket::Ptr RtpInfo::makeRtp(TrackType type, const void* data, unsigned int l uint32_t sc = htonl(_ui32Ssrc); auto rtppkt = ResourcePoolHelper::obtainObj(); - unsigned char *pucRtp = rtppkt->payload; + rtppkt->setCapacity(len + 16); + rtppkt->setSize(len + 16); + + unsigned char *pucRtp = (unsigned char *)rtppkt->data(); pucRtp[0] = '$'; pucRtp[1] = _ui8Interleaved; pucRtp[2] = ui16RtpLen >> 8; @@ -52,7 +55,6 @@ RtpPacket::Ptr RtpInfo::makeRtp(TrackType type, const void* data, unsigned int l rtppkt->PT = _ui8PlayloadType; rtppkt->interleaved = _ui8Interleaved; rtppkt->mark = mark; - rtppkt->length = len + 16; rtppkt->sequence = _ui16Sequence; rtppkt->timeStamp = uiStamp; rtppkt->ssrc = _ui32Ssrc; diff --git a/src/RtspMuxer/RtpCodec.h b/src/RtspMuxer/RtpCodec.h index 3e172477..49c96544 100644 --- a/src/RtspMuxer/RtpCodec.h +++ b/src/RtspMuxer/RtpCodec.h @@ -35,27 +35,18 @@ using namespace toolkit; namespace mediakit{ -class RtpPacket : public Buffer{ +class RtpPacket : public BufferRaw{ public: typedef std::shared_ptr Ptr; uint8_t interleaved; uint8_t PT; bool mark; - uint32_t length; //时间戳,单位毫秒 uint32_t timeStamp; uint16_t sequence; uint32_t ssrc; - uint8_t payload[1604]; uint8_t offset; TrackType type; - - char *data() const override { - return (char *)payload; - } - uint32_t size() const override { - return length; - } }; class RtpRingInterface {