修复rtsp时间戳相关bug

This commit is contained in:
xiongziliang 2018-10-26 22:32:50 +08:00
parent 1629d1f63a
commit 09c1ae87f3
1 changed files with 150 additions and 141 deletions

View File

@ -205,7 +205,8 @@ void RtspSession::inputRtspOrRtcp(const char *data,uint64_t len) {
bool RtspSession::handleReq_Options() { bool RtspSession::handleReq_Options() {
//支持这些命令 //支持这些命令
int n = sprintf(_pcBuf, "RTSP/1.0 200 OK\r\n" int n = sprintf(_pcBuf,
"RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
@ -467,7 +468,8 @@ void RtspSession::onAuthUser(const weak_ptr<RtspSession> &weakSelf,const string
} }
} }
inline void RtspSession::send_StreamNotFound() { inline void RtspSession::send_StreamNotFound() {
int n = sprintf(_pcBuf, "RTSP/1.0 404 Stream Not Found\r\n" int n = sprintf(_pcBuf,
"RTSP/1.0 404 Stream Not Found\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
@ -478,7 +480,8 @@ inline void RtspSession::send_StreamNotFound() {
SocketHelper::send(_pcBuf, n); SocketHelper::send(_pcBuf, n);
} }
inline void RtspSession::send_UnsupportedTransport() { inline void RtspSession::send_UnsupportedTransport() {
int n = sprintf(_pcBuf, "RTSP/1.0 461 Unsupported Transport\r\n" int n = sprintf(_pcBuf,
"RTSP/1.0 461 Unsupported Transport\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
@ -490,7 +493,8 @@ inline void RtspSession::send_UnsupportedTransport() {
} }
inline void RtspSession::send_SessionNotFound() { inline void RtspSession::send_SessionNotFound() {
int n = sprintf(_pcBuf, "RTSP/1.0 454 Session Not Found\r\n" int n = sprintf(_pcBuf,
"RTSP/1.0 454 Session Not Found\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
@ -551,7 +555,8 @@ bool RtspSession::handleReq_Setup() {
switch (_rtpType) { switch (_rtpType) {
case PlayerBase::RTP_TCP: { case PlayerBase::RTP_TCP: {
int iLen = sprintf(_pcBuf, "RTSP/1.0 200 OK\r\n" int iLen = sprintf(_pcBuf,
"RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
@ -598,7 +603,8 @@ bool RtspSession::handleReq_Setup() {
//尝试获取客户端nat映射地址 //尝试获取客户端nat映射地址
startListenPeerUdpData(); startListenPeerUdpData();
//InfoL << "分配端口:" << srv_port; //InfoL << "分配端口:" << srv_port;
int n = sprintf(_pcBuf, "RTSP/1.0 200 OK\r\n" int n = sprintf(_pcBuf,
"RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
@ -641,7 +647,8 @@ bool RtspSession::handleReq_Setup() {
} }
startListenPeerUdpData(); startListenPeerUdpData();
GET_CONFIG_AND_REGISTER(uint32_t,udpTTL,MultiCast::kUdpTTL); GET_CONFIG_AND_REGISTER(uint32_t,udpTTL,MultiCast::kUdpTTL);
int n = sprintf(_pcBuf, "RTSP/1.0 200 OK\r\n" int n = sprintf(_pcBuf,
"RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
@ -713,8 +720,12 @@ bool RtspSession::handleReq_Play() {
} }
auto pMediaSrc = _pMediaSrc.lock(); auto pMediaSrc = _pMediaSrc.lock();
uint32_t iStamp = 0; if(!pMediaSrc){
if(pMediaSrc){ send_StreamNotFound();
shutdown();
return;
}
if (strRange.size() && !_bFirstPlay) { if (strRange.size() && !_bFirstPlay) {
auto strStart = FindField(strRange.data(), "npt=", "-"); auto strStart = FindField(strRange.data(), "npt=", "-");
if (strStart == "now") { if (strStart == "now") {
@ -723,30 +734,20 @@ bool RtspSession::handleReq_Play() {
auto iStartTime = 1000 * atof(strStart.data()); auto iStartTime = 1000 * atof(strStart.data());
InfoL << "rtsp seekTo(ms):" << iStartTime; InfoL << "rtsp seekTo(ms):" << iStartTime;
pMediaSrc->seekTo(iStartTime); pMediaSrc->seekTo(iStartTime);
iStamp = pMediaSrc->getTimeStamp(TrackInvalid);
}else if(pMediaSrc->getRing()->readerCount() == 1){ }else if(pMediaSrc->getRing()->readerCount() == 1){
//第一个消费者 //第一个消费者
pMediaSrc->seekTo(0); pMediaSrc->seekTo(0);
iStamp = 0;
}else{
iStamp = pMediaSrc->getTimeStamp(TrackInvalid);
}
for(auto &track : _aTrackInfo){
track->_ssrc = pMediaSrc->getSsrc(track->_type);
track->_seq = pMediaSrc->getSeqence(track->_type);
track->_time_stamp = pMediaSrc->getTimeStamp(track->_type);
}
} }
_bFirstPlay = false; _bFirstPlay = false;
int iLen = sprintf(_pcBuf, "RTSP/1.0 200 OK\r\n" int iLen = sprintf(_pcBuf,
"RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
"Session: %s\r\n" "Session: %s\r\n"
"Range: npt=%.2f-\r\n" "Range: npt=%.2f-\r\n"
"RTP-Info: ", _iCseq, SERVER_NAME, RTSP_VERSION, RTSP_BUILDTIME, "RTP-Info: ", _iCseq, SERVER_NAME, RTSP_VERSION, RTSP_BUILDTIME,
dateHeader().data(), _strSession.data(),iStamp/1000.0); dateHeader().data(), _strSession.data(), pMediaSrc->getTimeStamp(TrackInvalid) / 1000.0);
for(auto &track : _aTrackInfo){ for(auto &track : _aTrackInfo){
if (track->_inited == false) { if (track->_inited == false) {
@ -754,11 +755,15 @@ bool RtspSession::handleReq_Play() {
shutdown(); shutdown();
return; return;
} }
track->_ssrc = pMediaSrc->getSsrc(track->_type);
track->_seq = pMediaSrc->getSeqence(track->_type);
track->_time_stamp = pMediaSrc->getTimeStamp(track->_type);
iLen += sprintf(_pcBuf + iLen, "url=%s/%s;seq=%d;rtptime=%u,", iLen += sprintf(_pcBuf + iLen, "url=%s/%s;seq=%d;rtptime=%u,",
_strUrl.data(), _strUrl.data(),
track->_control_surffix.data(), track->_control_surffix.data(),
track->_seq, track->_seq,
track->_time_stamp * track->_samplerate / 1000); track->_time_stamp * (track->_samplerate / 1000));
} }
iLen -= 1; iLen -= 1;
@ -798,7 +803,8 @@ bool RtspSession::handleReq_Pause() {
send_SessionNotFound(); send_SessionNotFound();
return false; return false;
} }
int n = sprintf(_pcBuf, "RTSP/1.0 200 OK\r\n" int n = sprintf(_pcBuf,
"RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
@ -813,13 +819,13 @@ bool RtspSession::handleReq_Pause() {
} }
bool RtspSession::handleReq_Teardown() { bool RtspSession::handleReq_Teardown() {
int n = sprintf(_pcBuf, "RTSP/1.0 200 OK\r\n" int n = sprintf(_pcBuf,
"RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
"Session: %s\r\n\r\n", _iCseq, SERVER_NAME, RTSP_VERSION, RTSP_BUILDTIME, "Session: %s\r\n\r\n", _iCseq, SERVER_NAME, RTSP_VERSION, RTSP_BUILDTIME,
dateHeader().data(), _strSession.data()); dateHeader().data(), _strSession.data());
SocketHelper::send(_pcBuf, n); SocketHelper::send(_pcBuf, n);
TraceL << "播放器断开连接!"; TraceL << "播放器断开连接!";
return false; return false;
@ -827,7 +833,8 @@ bool RtspSession::handleReq_Teardown() {
bool RtspSession::handleReq_Get() { bool RtspSession::handleReq_Get() {
_strSessionCookie = _parser["x-sessioncookie"]; _strSessionCookie = _parser["x-sessioncookie"];
int n = sprintf(_pcBuf, "HTTP/1.0 200 OK\r\n" int n = sprintf(_pcBuf,
"HTTP/1.0 200 OK\r\n"
"%s" "%s"
"Connection: close\r\n" "Connection: close\r\n"
"Cache-Control: no-store\r\n" "Cache-Control: no-store\r\n"
@ -867,7 +874,8 @@ bool RtspSession::handleReq_Post() {
bool RtspSession::handleReq_SET_PARAMETER() { bool RtspSession::handleReq_SET_PARAMETER() {
//TraceL<<endl; //TraceL<<endl;
int n = sprintf(_pcBuf, "RTSP/1.0 200 OK\r\n" int n = sprintf(_pcBuf,
"RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"
@ -878,7 +886,8 @@ bool RtspSession::handleReq_SET_PARAMETER() {
} }
inline void RtspSession::send_NotAcceptable() { inline void RtspSession::send_NotAcceptable() {
int n = sprintf(_pcBuf, "RTSP/1.0 406 Not Acceptable\r\n" int n = sprintf(_pcBuf,
"RTSP/1.0 406 Not Acceptable\r\n"
"CSeq: %d\r\n" "CSeq: %d\r\n"
"Server: %s-%0.2f(build in %s)\r\n" "Server: %s-%0.2f(build in %s)\r\n"
"%s" "%s"