Commit 29410687 by ziyue

优化rtsp客户端代码

parent bc125e65
......@@ -137,9 +137,13 @@ StrCaseMap Parser::parseArgs(const string &str, const char *pair_delim, const ch
StrCaseMap ret;
auto arg_vec = split(str, pair_delim);
for (string &key_val : arg_vec) {
auto key = FindField(key_val.data(), NULL, key_delim);
auto val = FindField(key_val.data(), key_delim, NULL);
ret.emplace_force(trim(key), trim(val));
auto key = trim(FindField(key_val.data(), NULL, key_delim));
if (!key.empty()) {
auto val = trim(FindField(key_val.data(), key_delim, NULL));
ret.emplace_force(key, val);
} else {
ret.emplace_force(key_val, "");
}
}
return ret;
}
......
......@@ -152,13 +152,16 @@ void RtpReceiver::setPoolSize(int size) {
_rtp_pool.setSize(size);
}
int RtpReceiver::getJitterSize(int track_index){
int RtpReceiver::getJitterSize(int track_index) const{
return _rtp_sortor[track_index].getJitterSize();
}
int RtpReceiver::getCycleCount(int track_index){
int RtpReceiver::getCycleCount(int track_index) const{
return _rtp_sortor[track_index].getCycleCount();
}
uint32_t RtpReceiver::getSSRC(int track_index) const{
return _ssrc[track_index];
}
}//namespace mediakit
......@@ -44,14 +44,14 @@ public:
/**
* 获取排序缓存长度
*/
int getJitterSize() {
int getJitterSize() const{
return _rtp_sort_cache_map.size();
}
/**
* 获取seq回环次数
*/
int getCycleCount() {
int getCycleCount() const{
return _seq_cycle_count;
}
......@@ -184,8 +184,9 @@ protected:
void clear();
void setPoolSize(int size);
int getJitterSize(int track_index);
int getCycleCount(int track_index);
int getJitterSize(int track_index) const;
int getCycleCount(int track_index) const;
uint32_t getSSRC(int track_index) const;
private:
uint32_t _ssrc[2] = {0, 0};
......
......@@ -252,40 +252,43 @@ void RtspPlayer::sendSetup(unsigned int track_idx) {
void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) {
if (parser.Url() != "200") {
throw std::runtime_error(
StrPrinter << "SETUP:" << parser.Url() << " " << parser.Tail() << endl);
throw std::runtime_error(StrPrinter << "SETUP:" << parser.Url() << " " << parser.Tail() << endl);
}
if (track_idx == 0) {
_session_id = parser["Session"];
_session_id.append(";");
_session_id = FindField(_session_id.data(), nullptr, ";");
}
auto strTransport = parser["Transport"];
if(strTransport.find("TCP") != string::npos || strTransport.find("interleaved") != string::npos){
if (strTransport.find("TCP") != string::npos || strTransport.find("interleaved") != string::npos) {
_rtp_type = Rtsp::RTP_TCP;
}else if(strTransport.find("multicast") != string::npos){
} else if (strTransport.find("multicast") != string::npos) {
_rtp_type = Rtsp::RTP_MULTICAST;
}else{
} else {
_rtp_type = Rtsp::RTP_UDP;
}
auto transport_map = Parser::parseArgs(strTransport, ";", "=");
RtspSplitter::enableRecvRtp(_rtp_type == Rtsp::RTP_TCP);
string ssrc = transport_map["ssrc"];
if(!ssrc.empty()){
sscanf(ssrc.data(), "%x", &_sdp_track[track_idx]->_ssrc);
} else{
_sdp_track[track_idx]->_ssrc = 0;
}
if(_rtp_type == Rtsp::RTP_TCP) {
string interleaved = FindField( FindField((strTransport + ";").data(), "interleaved=", ";").data(), NULL, "-");
_sdp_track[track_idx]->_interleaved = atoi(interleaved.data());
}else{
const char *strPos = (_rtp_type == Rtsp::RTP_MULTICAST ? "port=" : "server_port=") ;
auto port_str = FindField((strTransport + ";").data(), strPos, ";");
uint16_t rtp_port = atoi(FindField(port_str.data(), NULL, "-").data());
uint16_t rtcp_port = atoi(FindField(port_str.data(), "-",NULL).data());
if (_rtp_type == Rtsp::RTP_TCP) {
int interleaved_rtp, interleaved_rtcp;
sscanf(transport_map["interleaved"].data(), "%d-%d", &interleaved_rtp, &interleaved_rtcp);
_sdp_track[track_idx]->_interleaved = interleaved_rtp;
} else {
auto port_str = transport_map[(_rtp_type == Rtsp::RTP_MULTICAST ? "port" : "server_port")];
int rtp_port, rtcp_port;
sscanf(port_str.data(), "%d-%d", &rtp_port, &rtcp_port);
auto &pRtpSockRef = _rtp_sock[track_idx];
auto &pRtcpSockRef = _rtcp_sock[track_idx];
if (_rtp_type == Rtsp::RTP_MULTICAST) {
//udp组播
auto multiAddr = FindField((strTransport + ";").data(), "destination=", ";");
auto multiAddr = transport_map["destination"];
pRtpSockRef = createSocket();
if (!pRtpSockRef->bindUdpSock(rtp_port, multiAddr.data())) {
pRtpSockRef.reset();
......@@ -383,7 +386,7 @@ void RtspPlayer::sendKeepAlive(){
_on_response = [this](const Parser& parser){};
if(_supported_cmd.find("GET_PARAMETER") != _supported_cmd.end()){
//支持GET_PARAMETER,用此命令保活
sendRtspRequest("GET_PARAMETER", _play_url);
sendRtspRequest("GET_PARAMETER", _content_base);
}else{
//不支持GET_PARAMETER,用OPTIONS命令保活
sendRtspRequest("OPTIONS", _play_url);
......@@ -549,7 +552,7 @@ void RtspPlayer::sendReceiverReport(bool over_tcp, int track_idx){
aui8Rtcp[0] = '$';
aui8Rtcp[1] = track->_interleaved + 1;
aui8Rtcp[2] = (sizeof(aui8Rtcp) - 4) >> 8;
aui8Rtcp[2] = (sizeof(aui8Rtcp) - 4) >> 8;
aui8Rtcp[3] = (sizeof(aui8Rtcp) - 4) & 0xFF;
pui8Rtcp_RR[0] = 0x81;/* 1 report block */
......@@ -557,11 +560,13 @@ void RtspPlayer::sendReceiverReport(bool over_tcp, int track_idx){
pui8Rtcp_RR[2] = 0x00;
pui8Rtcp_RR[3] = 0x07;/* length in words - 1 */
uint32_t ssrc=htonl(track->_ssrc + 1);
auto track_ssrc = track->_ssrc ? track->_ssrc : getSSRC(track_idx);
// our own SSRC: we use the server's SSRC + 1 to avoid conflicts
uint32_t ssrc = htonl(track_ssrc + 1);
memcpy(&pui8Rtcp_RR[4], &ssrc, 4);
ssrc=htonl(track->_ssrc);
// server SSRC
ssrc = htonl(track_ssrc);
memcpy(&pui8Rtcp_RR[8], &ssrc, 4);
//FIXME: 8 bits of fraction, 24 bits of total packets lost
......@@ -577,9 +582,9 @@ void RtspPlayer::sendReceiverReport(bool over_tcp, int track_idx){
pui8Rtcp_RR[18] = counter.pktCnt >> 8;
pui8Rtcp_RR[19] = counter.pktCnt & 0xFF;
uint32_t jitter = htonl(getJitterSize(track_idx));
uint32_t jitter = htonl(getJitterSize(track_idx));
//FIXME: jitter
memcpy(pui8Rtcp_RR + 20, &jitter , 4);
memcpy(pui8Rtcp_RR + 20, &jitter, 4);
/* last SR timestamp */
memcpy(pui8Rtcp_RR + 24, &counter.lastTimeStamp, 4);
uint32_t msInc = htonl(ntohl(counter.timeStamp) - ntohl(counter.lastTimeStamp));
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论