From 2edc4b1bd42a500d98480c17454c8010ca3e68f3 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 5 Jul 2018 18:48:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Player/PlayerBase.h | 3 ++- src/RTP/RtpMaker.h | 1 + src/RTP/RtpMakerAAC.h | 2 +- src/RTP/RtpMakerH264.h | 2 +- src/Rtsp/Rtsp.cpp | 2 -- src/Rtsp/Rtsp.h | 1 - src/Rtsp/RtspMediaSource.h | 14 +++++++------- src/Rtsp/RtspPlayer.cpp | 27 ++++++++++++++++++--------- src/Rtsp/RtspPlayer.h | 7 ++++--- src/Rtsp/RtspSession.cpp | 20 ++++++++++---------- src/Rtsp/RtspSession.h | 4 ++-- 11 files changed, 46 insertions(+), 37 deletions(-) diff --git a/src/Player/PlayerBase.h b/src/Player/PlayerBase.h index b30beccc..79faa718 100644 --- a/src/Player/PlayerBase.h +++ b/src/Player/PlayerBase.h @@ -81,7 +81,8 @@ public: virtual int getAudioSampleRate() const { return 0; }; virtual int getAudioSampleBit() const { return 0; }; virtual int getAudioChannel() const { return 0; }; - virtual float getRtpLossRate(int iTrackId) const {return 0; }; + //TrackVideo = 0, TrackAudio = 1 + virtual float getRtpLossRate(int trackType) const {return 0; }; virtual const string& getPps() const { static string null;return null; }; virtual const string& getSps() const { static string null;return null; }; virtual const string& getAudioCfg() const { static string null;return null; }; diff --git a/src/RTP/RtpMaker.h b/src/RTP/RtpMaker.h index 42ca212d..dfb64690 100644 --- a/src/RTP/RtpMaker.h +++ b/src/RTP/RtpMaker.h @@ -28,6 +28,7 @@ #define RTP_RTPMAKER_H_ #include "Rtsp/RtspMediaSource.h" +#include "Rtsp/Rtsp.h" #include "Util/logger.h" #include "Util/RingBuffer.h" #include "Util/TimeTicker.h" diff --git a/src/RTP/RtpMakerAAC.h b/src/RTP/RtpMakerAAC.h index 7a532418..cc146b15 100644 --- a/src/RTP/RtpMakerAAC.h +++ b/src/RTP/RtpMakerAAC.h @@ -45,7 +45,7 @@ public: typedef std::shared_ptr Ptr; RtpMaker_AAC(const onGetRTP &cb, uint32_t ui32Ssrc, int iMtuSize , int iSampleRate, uint8_t ui8PlayloadType = 97, - uint8_t ui8Interleaved = 2) : + uint8_t ui8Interleaved = TrackAudio * 2) : RtpMaker(cb, ui32Ssrc, iMtuSize,iSampleRate, ui8PlayloadType, ui8Interleaved) { } virtual ~RtpMaker_AAC() { diff --git a/src/RTP/RtpMakerH264.h b/src/RTP/RtpMakerH264.h index 6a15c7e5..88bfa77a 100644 --- a/src/RTP/RtpMakerH264.h +++ b/src/RTP/RtpMakerH264.h @@ -44,7 +44,7 @@ class RtpMaker_H264: public RtpMaker { public: typedef std::shared_ptr Ptr; RtpMaker_H264(const onGetRTP &cb, uint32_t ui32Ssrc,int iMtuSize = 1400,int iSampleRate = 90000, - uint8_t ui8PlayloadType = 96, uint8_t ui8Interleaved = 0) : + uint8_t ui8PlayloadType = 96, uint8_t ui8Interleaved = TrackVideo * 2) : RtpMaker(cb, ui32Ssrc, iMtuSize,iSampleRate, ui8PlayloadType, ui8Interleaved) { } virtual ~RtpMaker_H264() { diff --git a/src/Rtsp/Rtsp.cpp b/src/Rtsp/Rtsp.cpp index 1e41165c..75b75f8a 100644 --- a/src/Rtsp/Rtsp.cpp +++ b/src/Rtsp/Rtsp.cpp @@ -67,11 +67,9 @@ int parserSDP(const string& sdp, RtspTrack Track[2]) { if (sdp_mid.find("m=video") != string::npos) { //视频通道 Track[track_cnt].type = TrackVideo; - Track[track_cnt].trackId = 0; } else if (sdp_mid.find("m=audio") != string::npos) { //音频通道 Track[track_cnt].type = TrackAudio; - Track[track_cnt].trackId = 1; } else { //不识别的track return track_cnt; diff --git a/src/Rtsp/Rtsp.h b/src/Rtsp/Rtsp.h index 03b8f133..31f29d51 100644 --- a/src/Rtsp/Rtsp.h +++ b/src/Rtsp/Rtsp.h @@ -43,7 +43,6 @@ typedef enum { class RtspTrack{ public: uint8_t PT; - uint8_t trackId; uint8_t interleaved; TrackType type = (TrackType) -1; string trackSdp; diff --git a/src/Rtsp/RtspMediaSource.h b/src/Rtsp/RtspMediaSource.h index 30850ec8..0fbedb1f 100644 --- a/src/Rtsp/RtspMediaSource.h +++ b/src/Rtsp/RtspMediaSource.h @@ -74,14 +74,14 @@ public: return m_strSdp; } - virtual uint32_t getSsrc(int trackId) { - return m_mapTracks[trackId].ssrc; + virtual uint32_t getSsrc(TrackType trackType) { + return m_mapTracks[trackType].ssrc; } - virtual uint16_t getSeqence(int trackId) { - return m_mapTracks[trackId].seq; + virtual uint16_t getSeqence(TrackType trackType) { + return m_mapTracks[trackType].seq; } - virtual uint32_t getTimestamp(int trackId) { - return m_mapTracks[trackId].timeStamp; + virtual uint32_t getTimestamp(TrackType trackType) { + return m_mapTracks[trackType].timeStamp; } virtual void onGetSDP(const string& sdp) { @@ -89,7 +89,7 @@ public: m_strSdp = sdp; } virtual void onGetRTP(const RtpPacket::Ptr &rtppt, bool keyPos) { - auto &trackRef = m_mapTracks[rtppt->interleaved / 2]; + auto &trackRef = m_mapTracks[rtppt->type]; trackRef.seq = rtppt->sequence; trackRef.timeStamp = rtppt->timeStamp; trackRef.ssrc = rtppt->ssrc; diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index 08baf900..d8b5cff5 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -297,7 +297,7 @@ bool RtspPlayer::sendSetup(unsigned int trackIndex) { switch (m_eType) { case RTP_TCP: { StrCaseMap header; - header["Transport"] = StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track.trackId * 2 << "-" << track.trackId * 2 + 1; + header["Transport"] = StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track.type * 2 << "-" << track.type * 2 + 1; return sendRtspRequest("SETUP",baseUrl,header); } break; @@ -485,7 +485,7 @@ void RtspPlayer::handleResPAUSE(const Parser& parser, bool bPause) { strTrack.append(";"); auto strControlSuffix = strTrack.substr(1 + strTrack.rfind('/'),strTrack.find(';') - strTrack.rfind('/') - 1); auto strRtpTime = FindField(strTrack.data(), "rtptime=", ";"); - auto iIdx = getTrackIndex(strControlSuffix); + auto iIdx = getTrackIndexByControlSuffix(strControlSuffix); m_adFistStamp[iIdx] = atoll(strRtpTime.data()); m_adNowStamp[iIdx] = m_adFistStamp[iIdx]; DebugL << "rtptime:" << strControlSuffix <<" " << strRtpTime; @@ -552,7 +552,7 @@ void RtspPlayer::splitRtp(unsigned char* pucRtp, unsigned int uiLen) { } int trackIdx = -1; if(interleaved %2 ==0){ - trackIdx = getTrackIndex(interleaved/2); + trackIdx = getTrackIndexByInterleaved(interleaved); } if (trackIdx != -1) { handleOneRtp(trackIdx, rtp_ptr + 4, length); @@ -582,7 +582,7 @@ bool RtspPlayer::handleOneRtp(int iTrackidx, unsigned char *pucData, unsigned in auto &track = m_aTrackInfo[iTrackidx]; auto pt_ptr=m_pktPool.obtain(); auto &rtppt=*pt_ptr; - rtppt.interleaved = track.trackId * 2; + rtppt.interleaved = track.interleaved; rtppt.length = uiLen + 4; rtppt.mark = pucData[1] >> 7; @@ -667,8 +667,8 @@ void RtspPlayer::onRecvRTP_l(const RtpPacket::Ptr &rtppt, int trackidx){ onRecvRTP_l(rtppt,m_aTrackInfo[trackidx]); } -float RtspPlayer::getRtpLossRate(int iTrackId) const{ - int iTrackIdx = getTrackIndex(iTrackId); +float RtspPlayer::getRtpLossRate(int iTrackType) const{ + int iTrackIdx = getTrackIndexByTrackType((TrackType)iTrackType); if(iTrackIdx == -1){ uint64_t totalRecv = 0; uint64_t totalSend = 0; @@ -788,7 +788,7 @@ void RtspPlayer::onPlayResult_l(const SockException &ex) { onPlayResult(ex); } -int RtspPlayer::getTrackIndex(const string &controlSuffix) const{ +int RtspPlayer::getTrackIndexByControlSuffix(const string &controlSuffix) const{ for (unsigned int i = 0; i < m_uiTrackCnt; i++) { if (m_aTrackInfo[i].controlSuffix == controlSuffix) { return i; @@ -796,9 +796,18 @@ int RtspPlayer::getTrackIndex(const string &controlSuffix) const{ } return -1; } -int RtspPlayer::getTrackIndex(int iTrackId) const{ +int RtspPlayer::getTrackIndexByInterleaved(int interleaved) const{ for (unsigned int i = 0; i < m_uiTrackCnt; i++) { - if (m_aTrackInfo[i].trackId == iTrackId) { + if (m_aTrackInfo[i].interleaved == interleaved) { + return i; + } + } + return -1; +} + +int RtspPlayer::getTrackIndexByTrackType(TrackType trackType) const { + for (unsigned int i = 0; i < m_uiTrackCnt; i++) { + if (m_aTrackInfo[i].type == trackType) { return i; } } diff --git a/src/Rtsp/RtspPlayer.h b/src/Rtsp/RtspPlayer.h index 5c9d4689..c7f387df 100644 --- a/src/Rtsp/RtspPlayer.h +++ b/src/Rtsp/RtspPlayer.h @@ -60,7 +60,7 @@ public: void play(const char* strUrl) override; void pause(bool bPause) override; void teardown() override; - float getRtpLossRate(int iTrackId) const override; + float getRtpLossRate(int iTrackType) const override; protected: //派生类回调函数 virtual bool onCheckSDP(const string &strSdp, const RtspTrack *pTrack, int iTrackCnt) = 0; @@ -73,8 +73,9 @@ private: void onRecvRTP_l(const RtpPacket::Ptr &pRtppt, const RtspTrack &track); void onPlayResult_l(const SockException &ex); - int getTrackIndex(const string &controlSuffix) const; - int getTrackIndex(int iTrackId) const; + int getTrackIndexByControlSuffix(const string &controlSuffix) const; + int getTrackIndexByInterleaved(int interleaved) const; + int getTrackIndexByTrackType(TrackType trackId) const; void play(const char* strUrl, const char *strUser, const char *strPwd, eRtpType eType); void onConnect(const SockException &err) override; diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index 67568e88..cf9baaf5 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -538,8 +538,8 @@ bool RtspSession::handleReq_Setup() { "x-Dynamic-Rate: 1\r\n\r\n", m_iCseq, SERVER_NAME, RTSP_VERSION, RTSP_BUILDTIME, - dateHeader().data(), trackRef.trackId * 2, - trackRef.trackId * 2 + 1, + dateHeader().data(), trackRef.type * 2, + trackRef.type * 2 + 1, printSSRC(trackRef.ssrc).data(), m_strSession.data()); send(m_pcBuf, iLen); @@ -606,7 +606,7 @@ bool RtspSession::handleReq_Setup() { strongSelf->safeShutdown(); }); } - int iSrvPort = m_pBrdcaster->getPort(trackRef.trackId); + int iSrvPort = m_pBrdcaster->getPort(trackRef.type); //我们用trackIdx区分rtp和rtcp包 auto pSockRtcp = UDPServer::Instance().getSock(get_local_ip().data(),2*trackIdx + 1,iSrvPort + 1); if (!pSockRtcp) { @@ -709,9 +709,9 @@ bool RtspSession::handleReq_Play() { } for (unsigned int i = 0; i < m_uiTrackCnt; i++) { auto &track = m_aTrackInfo[i]; - track.ssrc = pMediaSrc->getSsrc(track.trackId); - track.seq = pMediaSrc->getSeqence(track.trackId); - track.timeStamp = pMediaSrc->getTimestamp(track.trackId); + track.ssrc = pMediaSrc->getSsrc(track.type); + track.seq = pMediaSrc->getSeqence(track.type); + track.timeStamp = pMediaSrc->getTimestamp(track.type); } } m_bFirstPlay = false; @@ -873,9 +873,9 @@ inline bool RtspSession::findStream() { for (unsigned int i = 0; i < m_uiTrackCnt; i++) { auto &track = m_aTrackInfo[i]; - track.ssrc = pMediaSrc->getSsrc(track.trackId); - track.seq = pMediaSrc->getSeqence(track.trackId); - track.timeStamp = pMediaSrc->getTimestamp(track.trackId); + track.ssrc = pMediaSrc->getSsrc(track.type); + track.seq = pMediaSrc->getSeqence(track.type); + track.timeStamp = pMediaSrc->getTimestamp(track.type); } return true; @@ -904,7 +904,7 @@ inline void RtspSession::sendRtpPacket(const RtpPacket::Ptr & pkt) { } break; case PlayerBase::RTP_UDP: { - int iTrackIndex = getTrackIndexByTrackId(pkt->interleaved / 2); + int iTrackIndex = getTrackIndexByTrackType(pkt->type); auto pSock = m_apUdpSock[iTrackIndex].lock(); if (!pSock) { shutdown(); diff --git a/src/Rtsp/RtspSession.h b/src/Rtsp/RtspSession.h index 7ad9e003..19f99ac9 100644 --- a/src/Rtsp/RtspSession.h +++ b/src/Rtsp/RtspSession.h @@ -126,9 +126,9 @@ private: } return tmp; } - inline int getTrackIndexByTrackId(int iTrackId) { + inline int getTrackIndexByTrackType(TrackType type) { for (unsigned int i = 0; i < m_uiTrackCnt; i++) { - if (iTrackId == m_aTrackInfo[i].trackId) { + if (type == m_aTrackInfo[i].type) { return i; } }