Commit 212a761e by xiongziliang

优化rtsp相关代码

parent 0c5cd624
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <cmath> #include <cmath>
#include <stdarg.h> #include <stdarg.h>
#include <algorithm> #include <algorithm>
#include <iomanip>
#include "Common/config.h" #include "Common/config.h"
#include "RtspPlayer.h" #include "RtspPlayer.h"
...@@ -234,15 +235,11 @@ bool RtspPlayer::sendSetup(unsigned int trackIndex) { ...@@ -234,15 +235,11 @@ bool RtspPlayer::sendSetup(unsigned int trackIndex) {
auto baseUrl = _strContentBase + "/" + track->_control_surffix; auto baseUrl = _strContentBase + "/" + track->_control_surffix;
switch (_eType) { switch (_eType) {
case RTP_TCP: { case RTP_TCP: {
StrCaseMap header; return sendRtspRequest("SETUP",baseUrl,{"Transport",StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2 << "-" << track->_type * 2 + 1});
header["Transport"] = StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2 << "-" << track->_type * 2 + 1;
return sendRtspRequest("SETUP",baseUrl,header);
} }
break; break;
case RTP_MULTICAST: { case RTP_MULTICAST: {
StrCaseMap header; return sendRtspRequest("SETUP",baseUrl,{"Transport","Transport: RTP/AVP;multicast"});
header["Transport"] = "Transport: RTP/AVP;multicast";
return sendRtspRequest("SETUP",baseUrl,header);
} }
break; break;
case RTP_UDP: { case RTP_UDP: {
...@@ -252,9 +249,7 @@ bool RtspPlayer::sendSetup(unsigned int trackIndex) { ...@@ -252,9 +249,7 @@ bool RtspPlayer::sendSetup(unsigned int trackIndex) {
throw std::runtime_error("open udp sock err"); throw std::runtime_error("open udp sock err");
} }
int port = _apUdpSock[trackIndex]->get_local_port(); int port = _apUdpSock[trackIndex]->get_local_port();
StrCaseMap header; return sendRtspRequest("SETUP",baseUrl,{"Transport",StrPrinter << "RTP/AVP;unicast;client_port=" << port << "-" << port + 1});
header["Transport"] = StrPrinter << "RTP/AVP;unicast;client_port=" << port << "-" << port + 1;
return sendRtspRequest("SETUP",baseUrl,header);
} }
break; break;
default: default:
...@@ -364,9 +359,7 @@ bool RtspPlayer::sendOptions() { ...@@ -364,9 +359,7 @@ bool RtspPlayer::sendOptions() {
bool RtspPlayer::sendDescribe() { bool RtspPlayer::sendDescribe() {
//发送DESCRIBE命令后处理函数:handleResDESCRIBE //发送DESCRIBE命令后处理函数:handleResDESCRIBE
_onHandshake = std::bind(&RtspPlayer::handleResDESCRIBE,this, placeholders::_1); _onHandshake = std::bind(&RtspPlayer::handleResDESCRIBE,this, placeholders::_1);
StrCaseMap header; return sendRtspRequest("DESCRIBE",_strUrl,{"Accept","application/sdp"});
header["Accept"] = "application/sdp";
return sendRtspRequest("DESCRIBE",_strUrl,header);
} }
...@@ -383,12 +376,9 @@ bool RtspPlayer::sendPause(bool bPause,uint32_t seekMS){ ...@@ -383,12 +376,9 @@ bool RtspPlayer::sendPause(bool bPause,uint32_t seekMS){
//开启或暂停rtsp //开启或暂停rtsp
_onHandshake = std::bind(&RtspPlayer::handleResPAUSE,this, placeholders::_1,bPause); _onHandshake = std::bind(&RtspPlayer::handleResPAUSE,this, placeholders::_1,bPause);
return sendRtspRequest(bPause ? "PAUSE" : "PLAY",
StrCaseMap header; _strContentBase,
char buf[8]; {"Range",StrPrinter << "npt=" << setiosflags(ios::fixed) << setprecision(2) << seekMS / 1000.0 << "-"});
sprintf(buf,"%.2f",seekMS / 1000.0);
header["Range"] = StrPrinter << "npt=" << buf << "-";
return sendRtspRequest(bPause ? "PAUSE" : "PLAY",_strContentBase,header);
} }
void RtspPlayer::pause(bool bPause) { void RtspPlayer::pause(bool bPause) {
sendPause(bPause, getProgressMilliSecond()); sendPause(bPause, getProgressMilliSecond());
...@@ -513,6 +503,19 @@ void RtspPlayer::seekToMilliSecond(uint32_t ms) { ...@@ -513,6 +503,19 @@ void RtspPlayer::seekToMilliSecond(uint32_t ms) {
sendPause(false,ms); sendPause(false,ms);
} }
bool RtspPlayer::sendRtspRequest(const string &cmd, const string &url, const std::initializer_list<string> &header) {
string key;
StrCaseMap header_map;
int i = 0;
for(auto &val : header){
if(++i % 2 == 0){
header_map.emplace(key,val);
}else{
key = val;
}
}
return sendRtspRequest(cmd,url,header_map);
}
bool RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrCaseMap &header_const) { bool RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrCaseMap &header_const) {
auto header = header_const; auto header = header_const;
header.emplace("CSeq",StrPrinter << _uiCseq++); header.emplace("CSeq",StrPrinter << _uiCseq++);
......
...@@ -108,7 +108,9 @@ private: ...@@ -108,7 +108,9 @@ private:
bool sendPause(bool bPause,uint32_t ms); bool sendPause(bool bPause,uint32_t ms);
bool sendOptions(); bool sendOptions();
bool sendDescribe(); bool sendDescribe();
bool sendRtspRequest(const string &cmd, const string &url ,const StrCaseMap &header = StrCaseMap()); bool sendRtspRequest(const string &cmd, const string &url ,const StrCaseMap &header = StrCaseMap());
bool sendRtspRequest(const string &cmd, const string &url ,const std::initializer_list<string> &header);
private: private:
string _strUrl; string _strUrl;
SdpAttr _sdpAttr; SdpAttr _sdpAttr;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论