mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-23 03:10:04 +08:00
优化rtsp/rtmp源生成成功率
This commit is contained in:
parent
e0920536cc
commit
ce44694a43
@ -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,9 +77,26 @@ void MediaSink::inputFrame(const Frame::Ptr &frame) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!_allTrackReady && _trackReadyCallback.empty()){
|
if(!_allTrackReady && (_trackReadyCallback.empty() || _ticker.elapsedTime() > MAX_WAIT_MS)){
|
||||||
_allTrackReady = true;
|
_allTrackReady = true;
|
||||||
onAllTrackReady();
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,11 +104,14 @@ 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){
|
||||||
return pr.second;
|
if(!trackReady){
|
||||||
|
return pr.second;
|
||||||
|
}
|
||||||
|
return pr.second->ready() ? pr.second : nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user