Commit db0611de by xiongziliang

修复rtsp注册前未设置sdp的bug

rtsp鉴权放置在查找流之前,防止无谓的按需拉流
parent 6d08e2a2
Subproject commit eb3e28a6188fc8a7e5007215cc10a2b8c35c2cdd Subproject commit e03997435ad388a7c169de7a4b17b76d773ab69b
...@@ -46,7 +46,7 @@ RtmpSession::~RtmpSession() { ...@@ -46,7 +46,7 @@ RtmpSession::~RtmpSession() {
} }
void RtmpSession::onError(const SockException& err) { void RtmpSession::onError(const SockException& err) {
DebugP(this) << err.what(); WarnP(this) << err.what();
//流量统计事件广播 //流量统计事件广播
GET_CONFIG(uint32_t,iFlowThreshold,General::kFlowThreshold); GET_CONFIG(uint32_t,iFlowThreshold,General::kFlowThreshold);
...@@ -143,6 +143,7 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { ...@@ -143,6 +143,7 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) {
})); }));
dec.load<AMFValue>();/* NULL */ dec.load<AMFValue>();/* NULL */
_mediaInfo.parse(_strTcUrl + "/" + dec.load<std::string>()); _mediaInfo.parse(_strTcUrl + "/" + dec.load<std::string>());
_mediaInfo._schema = RTMP_SCHEMA;
auto onRes = [this,pToken](const string &err){ auto onRes = [this,pToken](const string &err){
auto src = dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA, auto src = dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA,
...@@ -305,7 +306,6 @@ void RtmpSession::doPlayResponse(const string &err,const std::function<void(bool ...@@ -305,7 +306,6 @@ void RtmpSession::doPlayResponse(const string &err,const std::function<void(bool
} }
//鉴权成功,查找媒体源并回复 //鉴权成功,查找媒体源并回复
_mediaInfo._schema = RTMP_SCHEMA;
weak_ptr<RtmpSession> weakSelf = dynamic_pointer_cast<RtmpSession>(shared_from_this()); weak_ptr<RtmpSession> weakSelf = dynamic_pointer_cast<RtmpSession>(shared_from_this());
MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,[weakSelf,cb](const MediaSource::Ptr &src){ MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,[weakSelf,cb](const MediaSource::Ptr &src){
auto rtmp_src = dynamic_pointer_cast<RtmpMediaSource>(src); auto rtmp_src = dynamic_pointer_cast<RtmpMediaSource>(src);
...@@ -351,6 +351,7 @@ void RtmpSession::onCmd_play2(AMFDecoder &dec) { ...@@ -351,6 +351,7 @@ void RtmpSession::onCmd_play2(AMFDecoder &dec) {
void RtmpSession::onCmd_play(AMFDecoder &dec) { void RtmpSession::onCmd_play(AMFDecoder &dec) {
dec.load<AMFValue>();/* NULL */ dec.load<AMFValue>();/* NULL */
_mediaInfo.parse(_strTcUrl + "/" + dec.load<std::string>()); _mediaInfo.parse(_strTcUrl + "/" + dec.load<std::string>());
_mediaInfo._schema = RTMP_SCHEMA;
doPlay(dec); doPlay(dec);
} }
......
...@@ -117,6 +117,9 @@ public: ...@@ -117,6 +117,9 @@ public:
//派生类设置该媒体源媒体描述信息 //派生类设置该媒体源媒体描述信息
_strSdp = sdp; _strSdp = sdp;
_sdpAttr.load(sdp); _sdpAttr.load(sdp);
if(_pRing){
regist();
}
} }
void onWrite(const RtpPacket::Ptr &rtppt, bool keyPos) override { void onWrite(const RtpPacket::Ptr &rtppt, bool keyPos) override {
...@@ -136,8 +139,10 @@ public: ...@@ -136,8 +139,10 @@ public:
strongSelf->onReaderChanged(size); strongSelf->onReaderChanged(size);
}); });
onReaderChanged(0); onReaderChanged(0);
if(!_strSdp.empty()){
regist(); regist();
} }
}
_pRing->write(rtppt,keyPos); _pRing->write(rtppt,keyPos);
checkNoneReader(); checkNoneReader();
} }
......
...@@ -147,6 +147,7 @@ void RtspSession::onWholeRtspPacket(Parser &parser) { ...@@ -147,6 +147,7 @@ void RtspSession::onWholeRtspPacket(Parser &parser) {
if(_strContentBase.empty() && strCmd != "GET"){ if(_strContentBase.empty() && strCmd != "GET"){
_strContentBase = parser.Url(); _strContentBase = parser.Url();
_mediaInfo.parse(parser.FullUrl()); _mediaInfo.parse(parser.FullUrl());
_mediaInfo._schema = RTSP_SCHEMA;
} }
typedef void (RtspSession::*rtsp_request_handler)(const Parser &parser); typedef void (RtspSession::*rtsp_request_handler)(const Parser &parser);
...@@ -257,7 +258,6 @@ void RtspSession::handleReq_RECORD(const Parser &parser){ ...@@ -257,7 +258,6 @@ void RtspSession::handleReq_RECORD(const Parser &parser){
auto onRes = [this](const string &err){ auto onRes = [this](const string &err){
bool authSuccess = err.empty(); bool authSuccess = err.empty();
if(!authSuccess){ if(!authSuccess){
//第一次play是播放,否则是恢复播放。只对播放鉴权
sendRtspResponse("401 Unauthorized", {"Content-Type", "text/plain"}, err); sendRtspResponse("401 Unauthorized", {"Content-Type", "text/plain"}, err);
shutdown(SockException(Err_shutdown,StrPrinter << "401 Unauthorized:" << err)); shutdown(SockException(Err_shutdown,StrPrinter << "401 Unauthorized:" << err));
return; return;
...@@ -307,41 +307,9 @@ void RtspSession::handleReq_RECORD(const Parser &parser){ ...@@ -307,41 +307,9 @@ void RtspSession::handleReq_RECORD(const Parser &parser){
} }
void RtspSession::handleReq_Describe(const Parser &parser) { void RtspSession::handleReq_Describe(const Parser &parser) {
_mediaInfo._schema = RTSP_SCHEMA;
auto authorization = parser["Authorization"];
weak_ptr<RtspSession> weakSelf = dynamic_pointer_cast<RtspSession>(shared_from_this()); weak_ptr<RtspSession> weakSelf = dynamic_pointer_cast<RtspSession>(shared_from_this());
MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,[weakSelf,authorization](const MediaSource::Ptr &src){
auto strongSelf = weakSelf.lock();
if(!strongSelf){
return;
}
auto rtsp_src = dynamic_pointer_cast<RtspMediaSource>(src);
if (!rtsp_src) {
//未找到相应的MediaSource
string err = StrPrinter << "no such stream:" << strongSelf->_mediaInfo._vhost << " " << strongSelf->_mediaInfo._app << " " << strongSelf->_mediaInfo._streamid;
strongSelf->send_StreamNotFound();
strongSelf->shutdown(SockException(Err_shutdown,err));
return;
}
//找到了响应的rtsp流
strongSelf->_strSdp = rtsp_src->getSdp();
SdpAttr sdpAttr(strongSelf->_strSdp);
strongSelf->_aTrackInfo = sdpAttr.getAvailableTrack();
if (strongSelf->_aTrackInfo.empty()) {
//该流无效
strongSelf->send_StreamNotFound();
strongSelf->shutdown(SockException(Err_shutdown,"can not find any availabe track in sdp"));
return;
}
strongSelf->_strSession = makeRandStr(12);
strongSelf->_pMediaSrc = rtsp_src;
for(auto &track : strongSelf->_aTrackInfo){
track->_ssrc = rtsp_src->getSsrc(track->_type);
track->_seq = rtsp_src->getSeqence(track->_type);
track->_time_stamp = rtsp_src->getTimeStamp(track->_type);
}
//该请求中的认证信息 //该请求中的认证信息
auto authorization = parser["Authorization"];
onGetRealm invoker = [weakSelf,authorization](const string &realm){ onGetRealm invoker = [weakSelf,authorization](const string &realm){
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
if(!strongSelf){ if(!strongSelf){
...@@ -368,21 +336,53 @@ void RtspSession::handleReq_Describe(const Parser &parser) { ...@@ -368,21 +336,53 @@ void RtspSession::handleReq_Describe(const Parser &parser) {
//广播是否需要认证事件 //广播是否需要认证事件
if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnGetRtspRealm, if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnGetRtspRealm,
strongSelf->_mediaInfo, _mediaInfo,
invoker, invoker,
*strongSelf)){ *this)){
//无人监听此事件,说明无需认证 //无人监听此事件,说明无需认证
invoker(""); invoker("");
} }
});
} }
void RtspSession::onAuthSuccess() { void RtspSession::onAuthSuccess() {
TraceP(this); TraceP(this);
sendRtspResponse("200 OK", weak_ptr<RtspSession> weakSelf = dynamic_pointer_cast<RtspSession>(shared_from_this());
{"Content-Base",_strContentBase + "/", MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,[weakSelf](const MediaSource::Ptr &src){
auto strongSelf = weakSelf.lock();
if(!strongSelf){
return;
}
auto rtsp_src = dynamic_pointer_cast<RtspMediaSource>(src);
if (!rtsp_src) {
//未找到相应的MediaSource
string err = StrPrinter << "no such stream:" << strongSelf->_mediaInfo._vhost << " " << strongSelf->_mediaInfo._app << " " << strongSelf->_mediaInfo._streamid;
strongSelf->send_StreamNotFound();
strongSelf->shutdown(SockException(Err_shutdown,err));
return;
}
//找到了响应的rtsp流
strongSelf->_strSdp = rtsp_src->getSdp();
SdpAttr sdpAttr(strongSelf->_strSdp);
strongSelf->_aTrackInfo = sdpAttr.getAvailableTrack();
if (strongSelf->_aTrackInfo.empty()) {
//该流无效
strongSelf->send_StreamNotFound();
strongSelf->shutdown(SockException(Err_shutdown,"can not find any availabe track in sdp:" << strongSelf->_strSdp));
return;
}
strongSelf->_strSession = makeRandStr(12);
strongSelf->_pMediaSrc = rtsp_src;
for(auto &track : strongSelf->_aTrackInfo){
track->_ssrc = rtsp_src->getSsrc(track->_type);
track->_seq = rtsp_src->getSeqence(track->_type);
track->_time_stamp = rtsp_src->getTimeStamp(track->_type);
}
strongSelf->sendRtspResponse("200 OK",
{"Content-Base",strongSelf->_strContentBase + "/",
"x-Accept-Retransmit","our-retransmit", "x-Accept-Retransmit","our-retransmit",
"x-Accept-Dynamic-Rate","1" "x-Accept-Dynamic-Rate","1"
},_strSdp); },strongSelf->_strSdp);
});
} }
void RtspSession::onAuthFailed(const string &realm,const string &why,bool close) { void RtspSession::onAuthFailed(const string &realm,const string &why,bool close) {
GET_CONFIG(bool,authBasic,Rtsp::kAuthBasic); GET_CONFIG(bool,authBasic,Rtsp::kAuthBasic);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论