rtmp未找到流后延迟重试

This commit is contained in:
xiongziliang 2018-03-14 22:35:54 +08:00
parent 36f688f118
commit b5c1726ce7
2 changed files with 136 additions and 98 deletions

View File

@ -75,6 +75,12 @@ void RtmpSession::onManager() {
shutdown();
}
}
if(m_delayTask){
if(time(NULL) > m_iTaskTimeLine){
m_delayTask();
m_delayTask = nullptr;
}
}
}
void RtmpSession::onRecv(const Buffer::Ptr &pBuf) {
@ -196,18 +202,35 @@ void RtmpSession::onCmd_deleteStream(AMFDecoder &dec) {
throw std::runtime_error(StrPrinter << "Stop publishing." << endl);
}
void RtmpSession::doPlay(AMFDecoder &dec){
auto onRes = [this](const string &err) {
void RtmpSession::doPlayResponse(const string &err,bool tryDelay) {
//获取流对象
auto src = dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA,
m_mediaInfo.m_vhost,
m_mediaInfo.m_app,
m_mediaInfo.m_streamid,
true));
bool authSuccess = err.empty();
bool ok = (src.operator bool() && authSuccess);
if(ok){
ok = ok && src->ready();
if(src ){
if(!src->ready()){
//流未准备好那么相当于没有
src = nullptr;
}
}
//是否鉴权成功
bool authSuccess = err.empty();
if(authSuccess && !src && tryDelay ){
//校验成功,但是流不存在而导致的不能播放,我们看看该流延时几秒后是否确实不能播放
doDelay(3,[this](){
//延时后就不再延时重试了
doPlayResponse("",false);
});
return;
}
///////回复流程///////
//是否播放成功
bool ok = (src.operator bool() && authSuccess);
if (ok) {
//stream begin
sendUserControl(CONTROL_STREAM_BEGIN, STREAM_MEDIA);
@ -298,26 +321,27 @@ void RtmpSession::doPlay(AMFDecoder &dec){
if (src->getRing()->readerCount() == 1) {
src->seekTo(0);
}
};
}
void RtmpSession::doPlay(AMFDecoder &dec){
weak_ptr<RtmpSession> weakSelf = dynamic_pointer_cast<RtmpSession>(shared_from_this());
Broadcast::AuthInvoker invoker = [weakSelf,onRes](const string &err){
Broadcast::AuthInvoker invoker = [weakSelf](const string &err){
auto strongSelf = weakSelf.lock();
if(!strongSelf){
return;
}
strongSelf->async([weakSelf,onRes,err](){
strongSelf->async([weakSelf,err](){
auto strongSelf = weakSelf.lock();
if(!strongSelf){
return;
}
onRes(err);
strongSelf->doPlayResponse(err,true);
});
};
auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,m_mediaInfo,invoker,*this);
if(!flag){
//该事件无人监听,默认不鉴权
onRes("");
doPlayResponse("",true);
}
}
void RtmpSession::onCmd_play2(AMFDecoder &dec) {
@ -411,6 +435,7 @@ void RtmpSession::onRtmpChunk(RtmpPacket &chunkData) {
if (!m_pPublisherSrc) {
throw std::runtime_error("Not a rtmp publisher!");
}
chunkData.timeStamp = m_stampTicker[chunkData.typeId % 2].elapsedTime();
m_pPublisherSrc->onGetMedia(std::make_shared<RtmpPacket>(chunkData));
if(!m_bPublisherSrcRegisted && m_pPublisherSrc->ready()){
m_bPublisherSrcRegisted = true;
@ -457,5 +482,11 @@ void RtmpSession::onSendMedia(const RtmpPacket::Ptr &pkt) {
sendRtmp(pkt->typeId, pkt->streamId, pkt->strBuf, modifiedStamp, pkt->chunkId , true);
}
void RtmpSession::doDelay(int delaySec, const std::function<void()> &fun) {
m_delayTask = fun;
m_iTaskTimeLine = time(NULL) + delaySec;
}
} /* namespace Rtmp */
} /* namespace ZL */

View File

@ -57,6 +57,7 @@ private:
MediaInfo m_mediaInfo;
double m_dNowReqID = 0;
Ticker m_ticker;//数据接收时间
SmoothTicker m_stampTicker[2];//时间戳生产器
typedef void (RtmpSession::*rtmpCMDHandle)(AMFDecoder &dec);
static unordered_map<string, rtmpCMDHandle> g_mapCmd;
@ -77,6 +78,7 @@ private:
void onCmd_play(AMFDecoder &dec);
void onCmd_play2(AMFDecoder &dec);
void doPlay(AMFDecoder &dec);
void doPlayResponse(const string &err,bool tryDelay);
void onCmd_seek(AMFDecoder &dec);
void onCmd_pause(AMFDecoder &dec);
void setMetaData(AMFDecoder &dec);
@ -104,6 +106,11 @@ private:
safeShutdown();
return true;
}
void doDelay(int delaySec,const std::function<void()> &fun);
std::function<void()> m_delayTask;
uint32_t m_iTaskTimeLine = 0;
};
} /* namespace Rtmp */