优化rtsp/rtmp源生成成功率

This commit is contained in:
xiongziliang 2018-11-17 15:25:33 +08:00
parent e0920536cc
commit ce44694a43
2 changed files with 34 additions and 7 deletions

View File

@ -25,6 +25,8 @@
*/ */
#include "MediaSink.h" #include "MediaSink.h"
//最多等待未初始化的Track 10秒超时之后会忽略未初始化的Track
#define MAX_WAIT_MS 10000
namespace mediakit{ namespace mediakit{
@ -53,6 +55,7 @@ void MediaSink::addTrack(const Track::Ptr &track_in) {
}else{ }else{
_allTrackReady = false; _allTrackReady = false;
_trackReadyCallback[codec_id] = lam; _trackReadyCallback[codec_id] = lam;
_ticker.resetTime();
} }
} }
@ -64,8 +67,9 @@ void MediaSink::inputFrame(const Frame::Ptr &frame) {
return; return;
} }
it->second->inputFrame(frame); it->second->inputFrame(frame);
if(!_allTrackReady && !_trackReadyCallback.empty() && it->second->ready()){ if(!_allTrackReady && !_trackReadyCallback.empty() && it->second->ready()){
//Track由未就绪状态装换成就绪状态我们就生成sdp以及rtp编码器 //Track由未就绪状态装换成就绪状态我们就触发onTrackReady回调
auto it_callback = _trackReadyCallback.find(codec_id); auto it_callback = _trackReadyCallback.find(codec_id);
if(it_callback != _trackReadyCallback.end()){ if(it_callback != _trackReadyCallback.end()){
it_callback->second(); it_callback->second();
@ -73,22 +77,42 @@ void MediaSink::inputFrame(const Frame::Ptr &frame) {
} }
} }
if(!_allTrackReady && _trackReadyCallback.empty()){ if(!_allTrackReady && (_trackReadyCallback.empty() || _ticker.elapsedTime() > MAX_WAIT_MS)){
_allTrackReady = true; _allTrackReady = true;
if(!_trackReadyCallback.empty()){
//这是超时强制忽略未准备好的Track
_trackReadyCallback.clear();
//移除未准备好的Track
for(auto it = _track_map.begin() ; it != _track_map.end() ; ){
if(!it->second->ready()){
it = _track_map.erase(it);
continue;
}
++it;
}
}
if(!_track_map.empty()){
//最少有一个有效的Track
onAllTrackReady(); onAllTrackReady();
} }
}
} }
bool MediaSink::isAllTrackReady() const { bool MediaSink::isAllTrackReady() const {
return _allTrackReady; return _allTrackReady;
} }
Track::Ptr MediaSink::getTrack(TrackType type) const { Track::Ptr MediaSink::getTrack(TrackType type,bool trackReady) const {
lock_guard<recursive_mutex> lck(_mtx); lock_guard<recursive_mutex> lck(_mtx);
for (auto &pr : _track_map){ for (auto &pr : _track_map){
if(pr.second->getTrackType() == type){ if(pr.second->getTrackType() == type){
if(!trackReady){
return pr.second; return pr.second;
} }
return pr.second->ready() ? pr.second : nullptr;
}
} }
return nullptr; return nullptr;
} }

View File

@ -29,6 +29,7 @@
#include <mutex> #include <mutex>
#include <memory> #include <memory>
#include "Util/TimeTicker.h"
#include "Extension/Frame.h" #include "Extension/Frame.h"
#include "Extension/Track.h" #include "Extension/Track.h"
@ -70,10 +71,11 @@ public:
/** /**
* Track * Track
* @param type * @param type track类型
* @param trackReady Track
* @return * @return
*/ */
Track::Ptr getTrack(TrackType type) const ; Track::Ptr getTrack(TrackType type,bool trackReady = true) const ;
protected: protected:
/** /**
* track已经准备好ready()true * track已经准备好ready()true
@ -97,6 +99,7 @@ private:
map<int,Track::Ptr> _track_map; map<int,Track::Ptr> _track_map;
map<int,function<void()> > _trackReadyCallback; map<int,function<void()> > _trackReadyCallback;
bool _allTrackReady = false; bool _allTrackReady = false;
Ticker _ticker;
}; };