优化代码

This commit is contained in:
xiongziliang 2018-07-05 18:48:08 +08:00
parent 7c7e9a7ea4
commit 2edc4b1bd4
11 changed files with 46 additions and 37 deletions

View File

@ -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; };

View File

@ -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"

View File

@ -45,7 +45,7 @@ public:
typedef std::shared_ptr<RtpMaker_AAC> 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() {

View File

@ -44,7 +44,7 @@ class RtpMaker_H264: public RtpMaker {
public:
typedef std::shared_ptr<RtpMaker_H264> 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() {

View File

@ -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;

View File

@ -43,7 +43,6 @@ typedef enum {
class RtspTrack{
public:
uint8_t PT;
uint8_t trackId;
uint8_t interleaved;
TrackType type = (TrackType) -1;
string trackSdp;

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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();

View File

@ -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;
}
}