Commit a19f6767 by xia-chu

兼容一些rtsp流 #735

parent 19f3b1bd
...@@ -86,6 +86,14 @@ string SdpTrack::getName() const{ ...@@ -86,6 +86,14 @@ string SdpTrack::getName() const{
} }
} }
string SdpTrack::getControlUrl(const string &base_url) const{
if (_control.find("://") != string::npos) {
//以rtsp://开头
return _control;
}
return base_url +"/" + _control;
}
string SdpTrack::toString() const { string SdpTrack::toString() const {
_StrPrinter _printer; _StrPrinter _printer;
switch (_type){ switch (_type){
...@@ -246,8 +254,6 @@ void SdpParser::load(const string &sdp) { ...@@ -246,8 +254,6 @@ void SdpParser::load(const string &sdp) {
it = track._attr.find("control"); it = track._attr.find("control");
if(it != track._attr.end()) { if(it != track._attr.end()) {
track._control = it->second; track._control = it->second;
auto surffix = string("/") + track._control;
track._control_surffix = surffix.substr(1 + surffix.rfind('/'));
} }
} }
} }
......
...@@ -213,6 +213,7 @@ public: ...@@ -213,6 +213,7 @@ public:
string toString() const; string toString() const;
string getName() const; string getName() const;
string getControlUrl(const string &base_url) const;
public: public:
int _pt; int _pt;
...@@ -222,7 +223,6 @@ public: ...@@ -222,7 +223,6 @@ public:
string _codec; string _codec;
string _fmtp; string _fmtp;
string _control; string _control;
string _control_surffix;
public: public:
bool _inited = false; bool _inited = false;
......
...@@ -226,23 +226,20 @@ void RtspPlayer::createUdpSockIfNecessary(int track_idx){ ...@@ -226,23 +226,20 @@ void RtspPlayer::createUdpSockIfNecessary(int track_idx){
void RtspPlayer::sendSetup(unsigned int track_idx) { void RtspPlayer::sendSetup(unsigned int track_idx) {
_on_response = std::bind(&RtspPlayer::handleResSETUP, this, placeholders::_1, track_idx); _on_response = std::bind(&RtspPlayer::handleResSETUP, this, placeholders::_1, track_idx);
auto &track = _sdp_track[track_idx]; auto &track = _sdp_track[track_idx];
auto baseUrl = _content_base + "/" + track->_control_surffix; auto control_url = track->getControlUrl(_content_base);
if (track->_control.find("://") != string::npos) {
baseUrl = track->_control;
}
switch (_rtp_type) { switch (_rtp_type) {
case Rtsp::RTP_TCP: { case Rtsp::RTP_TCP: {
sendRtspRequest("SETUP",baseUrl,{"Transport",StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2 << "-" << track->_type * 2 + 1}); sendRtspRequest("SETUP", control_url, {"Transport", StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2 << "-" << track->_type * 2 + 1});
} }
break; break;
case Rtsp::RTP_MULTICAST: { case Rtsp::RTP_MULTICAST: {
sendRtspRequest("SETUP",baseUrl,{"Transport","RTP/AVP;multicast"}); sendRtspRequest("SETUP", control_url, {"Transport", "RTP/AVP;multicast"});
} }
break; break;
case Rtsp::RTP_UDP: { case Rtsp::RTP_UDP: {
createUdpSockIfNecessary(track_idx); createUdpSockIfNecessary(track_idx);
sendRtspRequest("SETUP", baseUrl, {"Transport", sendRtspRequest("SETUP", control_url, {"Transport",
StrPrinter << "RTP/AVP;unicast;client_port=" StrPrinter << "RTP/AVP;unicast;client_port="
<< _rtp_sock[track_idx]->get_local_port() << "-" << _rtp_sock[track_idx]->get_local_port() << "-"
<< _rtcp_sock[track_idx]->get_local_port()}); << _rtcp_sock[track_idx]->get_local_port()});
} }
......
...@@ -261,21 +261,18 @@ void RtspPusher::createUdpSockIfNecessary(int track_idx){ ...@@ -261,21 +261,18 @@ void RtspPusher::createUdpSockIfNecessary(int track_idx){
void RtspPusher::sendSetup(unsigned int track_idx) { void RtspPusher::sendSetup(unsigned int track_idx) {
_on_res_func = std::bind(&RtspPusher::handleResSetup, this, placeholders::_1, track_idx); _on_res_func = std::bind(&RtspPusher::handleResSetup, this, placeholders::_1, track_idx);
auto &track = _track_vec[track_idx]; auto &track = _track_vec[track_idx];
auto base_url = _content_base + "/" + track->_control_surffix; auto control_url = track->getControlUrl(_content_base);
if (track->_control.find("://") != string::npos) {
base_url = track->_control;
}
switch (_rtp_type) { switch (_rtp_type) {
case Rtsp::RTP_TCP: { case Rtsp::RTP_TCP: {
sendRtspRequest("SETUP", base_url, {"Transport", sendRtspRequest("SETUP", control_url, {"Transport",
StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2 StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2
<< "-" << track->_type * 2 + 1}); << "-" << track->_type * 2 + 1});
} }
break; break;
case Rtsp::RTP_UDP: { case Rtsp::RTP_UDP: {
createUdpSockIfNecessary(track_idx); createUdpSockIfNecessary(track_idx);
int port = _rtp_sock[track_idx]->get_local_port(); int port = _rtp_sock[track_idx]->get_local_port();
sendRtspRequest("SETUP", base_url, sendRtspRequest("SETUP", control_url,
{"Transport", StrPrinter << "RTP/AVP;unicast;client_port=" << port << "-" << port + 1}); {"Transport", StrPrinter << "RTP/AVP;unicast;client_port=" << port << "-" << port + 1});
} }
break; break;
......
...@@ -268,7 +268,7 @@ void RtspSession::handleReq_RECORD(const Parser &parser){ ...@@ -268,7 +268,7 @@ void RtspSession::handleReq_RECORD(const Parser &parser){
shutdown(SockException(Err_shutdown,"track not setuped")); shutdown(SockException(Err_shutdown,"track not setuped"));
return; return;
} }
rtp_info << "url=" << _content_base << "/" << track->_control_surffix << ","; rtp_info << "url=" << track->getControlUrl(_content_base) << ",";
} }
rtp_info.pop_back(); rtp_info.pop_back();
...@@ -614,11 +614,7 @@ void RtspSession::send_SessionNotFound() { ...@@ -614,11 +614,7 @@ void RtspSession::send_SessionNotFound() {
void RtspSession::handleReq_Setup(const Parser &parser) { void RtspSession::handleReq_Setup(const Parser &parser) {
//处理setup命令,该函数可能进入多次 //处理setup命令,该函数可能进入多次
auto controlSuffix = split(parser.FullUrl(),"/").back(); int trackIdx = getTrackIndexByControlUrl(parser.Url());
if(controlSuffix.front() == '/'){
controlSuffix = controlSuffix.substr(1);
}
int trackIdx = getTrackIndexByControlSuffix(controlSuffix);
SdpTrack::Ptr &trackRef = _sdp_track[trackIdx]; SdpTrack::Ptr &trackRef = _sdp_track[trackIdx];
if (trackRef->_inited) { if (trackRef->_inited) {
//已经初始化过该Track //已经初始化过该Track
...@@ -796,7 +792,7 @@ void RtspSession::handleReq_Play(const Parser &parser) { ...@@ -796,7 +792,7 @@ void RtspSession::handleReq_Play(const Parser &parser) {
track->_seq = play_src->getSeqence(track->_type); track->_seq = play_src->getSeqence(track->_type);
track->_time_stamp = play_src->getTimeStamp(track->_type); track->_time_stamp = play_src->getTimeStamp(track->_type);
rtp_info << "url=" << _content_base << "/" << track->_control_surffix << ";" rtp_info << "url=" << track->getControlUrl(_content_base) << ";"
<< "seq=" << track->_seq << ";" << "seq=" << track->_seq << ";"
<< "rtptime=" << (int) (track->_time_stamp * (track->_samplerate / 1000)) << ","; << "rtptime=" << (int) (track->_time_stamp * (track->_samplerate / 1000)) << ",";
} }
...@@ -1068,16 +1064,16 @@ int RtspSession::getTrackIndexByTrackType(TrackType type) { ...@@ -1068,16 +1064,16 @@ int RtspSession::getTrackIndexByTrackType(TrackType type) {
throw SockException(Err_shutdown, StrPrinter << "no such track with type:" << (int) type); throw SockException(Err_shutdown, StrPrinter << "no such track with type:" << (int) type);
} }
int RtspSession::getTrackIndexByControlSuffix(const string &controlSuffix) { int RtspSession::getTrackIndexByControlUrl(const string &control_url) {
for (unsigned int i = 0; i < _sdp_track.size(); i++) { for (unsigned int i = 0; i < _sdp_track.size(); i++) {
if (controlSuffix == _sdp_track[i]->_control_surffix) { if (control_url == _sdp_track[i]->getControlUrl(_content_base)) {
return i; return i;
} }
} }
if(_sdp_track.size() == 1){ if(_sdp_track.size() == 1){
return 0; return 0;
} }
throw SockException(Err_shutdown, StrPrinter << "no such track with suffix:" << controlSuffix); throw SockException(Err_shutdown, StrPrinter << "no such track with control url:" << control_url);
} }
int RtspSession::getTrackIndexByInterleaved(int interleaved){ int RtspSession::getTrackIndexByInterleaved(int interleaved){
......
...@@ -132,7 +132,7 @@ private: ...@@ -132,7 +132,7 @@ private:
void send_NotAcceptable(); void send_NotAcceptable();
//获取track下标 //获取track下标
int getTrackIndexByTrackType(TrackType type); int getTrackIndexByTrackType(TrackType type);
int getTrackIndexByControlSuffix(const string &control_suffix); int getTrackIndexByControlUrl(const string &control_url);
int getTrackIndexByInterleaved(int interleaved); int getTrackIndexByInterleaved(int interleaved);
//一般用于接收udp打洞包,也用于rtsp推流 //一般用于接收udp打洞包,也用于rtsp推流
void onRcvPeerUdpData(int interleaved, const Buffer::Ptr &buf, const struct sockaddr &addr); void onRcvPeerUdpData(int interleaved, const Buffer::Ptr &buf, const struct sockaddr &addr);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论