简化RTMP的注册逻辑

This commit is contained in:
xiongziliang 2019-04-09 11:31:47 +08:00
parent 507eba76c7
commit 4908ab4b47

View File

@ -76,36 +76,19 @@ public:
} }
} }
virtual void onGetMetaData(const AMFValue &metadata) { virtual void onGetMetaData(const AMFValue &metadata) {
lock_guard<recursive_mutex> lock(_mtxMap); lock_guard<recursive_mutex> lock(_mtxMap);
_metadata = metadata; _metadata = metadata;
_iCfgFrameSize = RtmpDemuxer::getTrackCount(metadata);
if(ready()){
MediaSource::regist();
_bRegisted = true;
} else{
_bAsyncRegist = true;
}
} }
void onWrite(const RtmpPacket::Ptr &pkt,bool isKey = true) override { void onWrite(const RtmpPacket::Ptr &pkt,bool isKey = true) override {
lock_guard<recursive_mutex> lock(_mtxMap); lock_guard<recursive_mutex> lock(_mtxMap);
if (pkt->isCfgFrame()) { if (pkt->isCfgFrame()) {
_mapCfgFrame[pkt->typeId] = pkt; _mapCfgFrame[pkt->typeId] = pkt;
if(_bAsyncRegist && !_bRegisted && _mapCfgFrame.size() == _iCfgFrameSize){
_bAsyncRegist = false;
MediaSource::regist();
_bRegisted = true;
}
} else{ } else{
if(!_bRegisted){ if(!_bRegisted){
//强制在3秒后注册不管是否有metedata或config包 MediaSource::regist();
if(_ticker.createdTime() > 3 * 1000){ _bRegisted = true;
MediaSource::regist();
_bRegisted = true;
}
} }
_mapStamp[pkt->typeId] = pkt->timeStamp; _mapStamp[pkt->typeId] = pkt->timeStamp;
_pRing->write(pkt,pkt->isVideoKeyFrame()); _pRing->write(pkt,pkt->isVideoKeyFrame());
@ -123,21 +106,13 @@ public:
return MAX(_mapStamp[MSG_VIDEO],_mapStamp[MSG_AUDIO]); return MAX(_mapStamp[MSG_VIDEO],_mapStamp[MSG_AUDIO]);
} }
} }
private:
bool ready(){
lock_guard<recursive_mutex> lock(_mtxMap);
return _iCfgFrameSize != -1 && _iCfgFrameSize == _mapCfgFrame.size();
}
protected: protected:
AMFValue _metadata; AMFValue _metadata;
map<int, RtmpPacket::Ptr> _mapCfgFrame; map<int, RtmpPacket::Ptr> _mapCfgFrame;
map<int,uint32_t> _mapStamp; map<int,uint32_t> _mapStamp;
mutable recursive_mutex _mtxMap; mutable recursive_mutex _mtxMap;
RingBuffer<RtmpPacket::Ptr>::Ptr _pRing; //rtp环形缓冲 RingBuffer<RtmpPacket::Ptr>::Ptr _pRing; //rtp环形缓冲
int _iCfgFrameSize = -1;
bool _bAsyncRegist = false;
bool _bRegisted = false; bool _bRegisted = false;
Ticker _ticker;
}; };
} /* namespace mediakit */ } /* namespace mediakit */