优化单Track情况下等待3秒的情况

This commit is contained in:
xiongziliang 2020-02-25 14:59:40 +08:00
parent f76109c629
commit d60101b93c
4 changed files with 23 additions and 5 deletions

View File

@ -59,6 +59,7 @@ void MediaSink::resetTracks() {
_track_map.clear();
_trackReadyCallback.clear();
_ticker.resetTime();
_max_track_size = 2;
}
void MediaSink::inputFrame(const Frame::Ptr &frame) {
@ -82,7 +83,7 @@ void MediaSink::inputFrame(const Frame::Ptr &frame) {
if(!_allTrackReady){
if(_ticker.elapsedTime() > MAX_WAIT_MS_READY){
//如果超过规定时间那么不再等待并忽略未准备好的Track
addTrackCompleted();
emitAllTrackReady();
return;
}
@ -91,21 +92,26 @@ void MediaSink::inputFrame(const Frame::Ptr &frame) {
return;
}
if(_track_map.size() == 2){
if(_track_map.size() == _max_track_size){
//如果已经添加了音视频Track并且不存在未准备好的Track那么说明所有Track都准备好了
addTrackCompleted();
emitAllTrackReady();
return;
}
if(_track_map.size() == 1 && _ticker.elapsedTime() > MAX_WAIT_MS_ADD_TRACK){
//如果只有一个Track那么在该Track添加后我们最多还等待若干时间(可能后面还会添加Track)
addTrackCompleted();
emitAllTrackReady();
return;
}
}
}
void MediaSink::addTrackCompleted() {
void MediaSink::addTrackCompleted(){
lock_guard<recursive_mutex> lck(_mtx);
_max_track_size = _track_map.size();
}
void MediaSink::emitAllTrackReady() {
if (_allTrackReady) {
return;
}

View File

@ -116,12 +116,18 @@ protected:
* @param frame
*/
virtual void onTrackFrame(const Frame::Ptr &frame) {};
private:
/**
* onAllTrackReady事件
*/
void emitAllTrackReady();
private:
mutable recursive_mutex _mtx;
map<int,Track::Ptr> _track_map;
map<int,function<void()> > _trackReadyCallback;
bool _allTrackReady = false;
Ticker _ticker;
int _max_track_size = 2;
};

View File

@ -291,6 +291,9 @@ void PlayerProxy::onPlaySuccess() {
audioMaker->addDelegate(_mediaMuxer);
}
//添加完毕所有track防止单track情况下最大等待3秒
_mediaMuxer->addTrackCompleted();
if(_pMediaSrc){
_pMediaSrc->setTrackSource(_mediaMuxer);
}

View File

@ -151,6 +151,9 @@ MP4Reader::MP4Reader(const string &strVhost,const string &strApp, const string &
H264Track::Ptr track = std::make_shared<H264Track>(_strSps,_strPps);
_mediaMuxer->addTrack(track);
}
//添加完毕所有track防止单track情况下最大等待3秒
_mediaMuxer->addTrackCompleted();
}