添加bom、完善rtsp客户端兼容性

This commit is contained in:
xiongziliang 2018-03-20 11:28:13 +08:00
parent 906fb0613b
commit 88e5a199eb
8 changed files with 34 additions and 18 deletions

View File

@ -1,4 +1,4 @@
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2016 xiongziliang <771730766@qq.com> * Copyright (c) 2016 xiongziliang <771730766@qq.com>

View File

@ -1,4 +1,4 @@
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2016 xiongziliang <771730766@qq.com> * Copyright (c) 2016 xiongziliang <771730766@qq.com>

View File

@ -71,7 +71,13 @@ int parserSDP(const string& sdp, RtspTrack Track[2]) {
if (IDStr == "") { if (IDStr == "") {
break; break;
} }
TrackID = atoi(IDStr.c_str()); if(strcasecmp(IDStr.data(),"video") == 0){
TrackID = 0;
}else if(strcasecmp(IDStr.data(),"audio") == 0){
TrackID = 1;
}else{
TrackID = atoi(IDStr.c_str());
}
pos_end = sdp.find("m=", pos_head + 2); pos_end = sdp.find("m=", pos_head + 2);
if (pos_end == string::npos) { if (pos_end == string::npos) {
pos_end = sdp.size(); pos_end = sdp.size();
@ -81,8 +87,8 @@ int parserSDP(const string& sdp, RtspTrack Track[2]) {
Track[track_cnt].trackStyle = track_str; Track[track_cnt].trackStyle = track_str;
Track[track_cnt].inited = false; Track[track_cnt].inited = false;
Track[track_cnt].trackId = TrackID; Track[track_cnt].trackId = TrackID;
Track[track_cnt].PT = atoi( Track[track_cnt].trackIdStr = IDStr;
FindField(mid.c_str(), "rtpmap:", " ").c_str()); Track[track_cnt].PT = atoi(FindField(mid.c_str(), "rtpmap:", " ").c_str());
if (mid.find("m=video") != string::npos) { if (mid.find("m=video") != string::npos) {
//视频通道 //视频通道
Track[track_cnt].type = TrackVideo; Track[track_cnt].type = TrackVideo;

View File

@ -44,7 +44,8 @@ class RtspTrack{
public: public:
uint8_t PT; uint8_t PT;
uint8_t trackId; uint8_t trackId;
uint8_t interleaved; string trackIdStr;
uint8_t interleaved;
TrackType type = (TrackType) -1; TrackType type = (TrackType) -1;
string trackSdp; string trackSdp;
string trackStyle; string trackStyle;

View File

@ -225,14 +225,19 @@ inline void RtspPlayer::HandleResDESCRIBE(const Parser& parser) {
} }
auto strSdp = parser.Content(); auto strSdp = parser.Content();
m_strContentBase = parser["Content-Base"]; m_strContentBase = parser["Content-Base"];
if(m_strContentBase.empty()){
m_strContentBase = m_strUrl;
}
if (m_strContentBase[m_strContentBase.length() - 1] == '/') {
m_strContentBase.pop_back();
}
auto iLen = atoi(parser["Content-Length"].data()); auto iLen = atoi(parser["Content-Length"].data());
if(iLen > 0){ if(iLen > 0){
strSdp.erase(iLen); strSdp.erase(iLen);
} }
if (m_strContentBase[m_strContentBase.length() - 1] == '/') {
m_strContentBase.pop_back();
}
//解析sdp //解析sdp
m_uiTrackCnt = parserSDP(strSdp, m_aTrackInfo); m_uiTrackCnt = parserSDP(strSdp, m_aTrackInfo);
for (unsigned int i=0; i<m_uiTrackCnt; i++) { for (unsigned int i=0; i<m_uiTrackCnt; i++) {
@ -256,21 +261,21 @@ inline void RtspPlayer::sendSetup(unsigned int trackIndex) {
auto &track = m_aTrackInfo[trackIndex]; auto &track = m_aTrackInfo[trackIndex];
switch (m_eType) { switch (m_eType) {
case RTP_TCP: { case RTP_TCP: {
iLen = sprintf(acRtspbuf, "SETUP %s/%s%d RTSP/1.0\r\n" iLen = sprintf(acRtspbuf, "SETUP %s/%s%s RTSP/1.0\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Transport: RTP/AVP/TCP;unicast;interleaved=%d-%d\r\n" "Transport: RTP/AVP/TCP;unicast;interleaved=%d-%d\r\n"
"Authorization: Basic %s\r\n\r\n", m_strContentBase.c_str(), "Authorization: Basic %s\r\n\r\n", m_strContentBase.c_str(),
track.trackStyle.c_str(), track.trackId, m_uiCseq++, track.trackStyle.c_str(), track.trackIdStr.data(), m_uiCseq++,
track.trackId * 2, track.trackId * 2 + 1, track.trackId * 2, track.trackId * 2 + 1,
m_strAuthorization.c_str()); m_strAuthorization.c_str());
} }
break; break;
case RTP_MULTICAST: { case RTP_MULTICAST: {
iLen = sprintf(acRtspbuf, "SETUP %s/%s%d RTSP/1.0\r\n" iLen = sprintf(acRtspbuf, "SETUP %s/%s%s RTSP/1.0\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Transport: RTP/AVP;multicast\r\n" "Transport: RTP/AVP;multicast\r\n"
"Authorization: Basic %s\r\n\r\n", m_strContentBase.c_str(), "Authorization: Basic %s\r\n\r\n", m_strContentBase.c_str(),
track.trackStyle.c_str(), track.trackId, m_uiCseq++, track.trackStyle.c_str(), track.trackIdStr.data(), m_uiCseq++,
m_strAuthorization.c_str()); m_strAuthorization.c_str());
} }
break; break;
@ -281,11 +286,11 @@ inline void RtspPlayer::sendSetup(unsigned int trackIndex) {
throw std::runtime_error("open udp sock err"); throw std::runtime_error("open udp sock err");
} }
int port = m_apUdpSock[trackIndex]->get_local_port(); int port = m_apUdpSock[trackIndex]->get_local_port();
iLen = sprintf(acRtspbuf, "SETUP %s/%s%d RTSP/1.0\r\n" iLen = sprintf(acRtspbuf, "SETUP %s/%s%s RTSP/1.0\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Transport: RTP/AVP;unicast;client_port=%d-%d\r\n" "Transport: RTP/AVP;unicast;client_port=%d-%d\r\n"
"Authorization: Basic %s\r\n\r\n", m_strContentBase.c_str(), "Authorization: Basic %s\r\n\r\n", m_strContentBase.c_str(),
track.trackStyle.c_str(), track.trackId, m_uiCseq++, port, track.trackStyle.c_str(), track.trackIdStr.data(), m_uiCseq++, port,
port + 1, m_strAuthorization.c_str()); port + 1, m_strAuthorization.c_str());
} }
break; break;

View File

@ -734,9 +734,9 @@ bool RtspSession::handleReq_Play() {
shutdown(); shutdown();
return; return;
} }
iLen += sprintf(m_pcBuf + iLen, "url=%s/%s%d;seq=%d;rtptime=%u,", iLen += sprintf(m_pcBuf + iLen, "url=%s/%s%s;seq=%d;rtptime=%u,",
m_strUrl.data(), track.trackStyle.data(), m_strUrl.data(), track.trackStyle.data(),
track.trackId, track.seq,track.timeStamp); track.trackIdStr.data(), track.seq,track.timeStamp);
} }
iLen -= 1; iLen -= 1;
(m_pcBuf)[iLen] = '\0'; (m_pcBuf)[iLen] = '\0';

View File

@ -1,4 +1,4 @@
// //
// Created by xzl on 2017/12/1. // Created by xzl on 2017/12/1.
// //

View File

@ -135,6 +135,10 @@ static onceToken s_token([](){
}, nullptr); }, nullptr);
#if !defined(SIGHUP)
#defined SIGHUP 1
#endif
int main(int argc,char *argv[]) { int main(int argc,char *argv[]) {
//设置退出信号处理函数 //设置退出信号处理函数
signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); }); signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); });