mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-24 11:40:37 +08:00
保证不丢帧
This commit is contained in:
parent
b2fe3cd05d
commit
75cc34814f
@ -25,36 +25,30 @@ void MediaSink::addTrack(const Track::Ptr &track_in) {
|
|||||||
auto track = track_in->clone();
|
auto track = track_in->clone();
|
||||||
auto codec_id = track->getCodecId();
|
auto codec_id = track->getCodecId();
|
||||||
_track_map[codec_id] = track;
|
_track_map[codec_id] = track;
|
||||||
_allTrackReady = false;
|
_all_track_ready = false;
|
||||||
_trackReadyCallback[codec_id] = [this, track]() {
|
_track_ready_callback[codec_id] = [this, track]() {
|
||||||
onTrackReady(track);
|
onTrackReady(track);
|
||||||
};
|
};
|
||||||
_ticker.resetTime();
|
_ticker.resetTime();
|
||||||
|
|
||||||
track->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
|
track->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
|
||||||
if (_allTrackReady) {
|
if (_all_track_ready) {
|
||||||
onTrackFrame(frame);
|
onTrackFrame(frame);
|
||||||
return;
|
} else {
|
||||||
}
|
//还有Track未就绪,先缓存之
|
||||||
|
_frame_unread[frame->getCodecId()].emplace_back(Frame::getCacheAbleFrame(frame));
|
||||||
//还有track未准备好,如果是视频的话,如果直接丢帧可能导致丢失I帧
|
|
||||||
checkTrackIfReady(nullptr);
|
|
||||||
if (_allTrackReady) {
|
|
||||||
//运行至这里说明Track状态由未就绪切换为已就绪状态,那么这帧就不应该丢弃
|
|
||||||
onTrackFrame(frame);
|
|
||||||
} else if(frame->keyFrame()){
|
|
||||||
WarnL << "some track is unready,drop key frame of: " << frame->getCodecName();
|
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MediaSink::resetTracks() {
|
void MediaSink::resetTracks() {
|
||||||
lock_guard<recursive_mutex> lck(_mtx);
|
lock_guard<recursive_mutex> lck(_mtx);
|
||||||
_allTrackReady = false;
|
_all_track_ready = false;
|
||||||
_track_map.clear();
|
_track_map.clear();
|
||||||
_trackReadyCallback.clear();
|
_track_ready_callback.clear();
|
||||||
_ticker.resetTime();
|
_ticker.resetTime();
|
||||||
_max_track_size = 2;
|
_max_track_size = 2;
|
||||||
|
_frame_unread.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MediaSink::inputFrame(const Frame::Ptr &frame) {
|
void MediaSink::inputFrame(const Frame::Ptr &frame) {
|
||||||
@ -63,22 +57,21 @@ void MediaSink::inputFrame(const Frame::Ptr &frame) {
|
|||||||
if (it == _track_map.end()) {
|
if (it == _track_map.end()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
checkTrackIfReady(it->second);
|
|
||||||
it->second->inputFrame(frame);
|
it->second->inputFrame(frame);
|
||||||
|
checkTrackIfReady(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MediaSink::checkTrackIfReady_l(const Track::Ptr &track){
|
void MediaSink::checkTrackIfReady_l(const Track::Ptr &track){
|
||||||
//Track由未就绪状态转换成就绪状态,我们就触发onTrackReady回调
|
//Track由未就绪状态转换成就绪状态,我们就触发onTrackReady回调
|
||||||
auto it_callback = _trackReadyCallback.find(track->getCodecId());
|
auto it_callback = _track_ready_callback.find(track->getCodecId());
|
||||||
if (it_callback != _trackReadyCallback.end() && track->ready()) {
|
if (it_callback != _track_ready_callback.end() && track->ready()) {
|
||||||
it_callback->second();
|
it_callback->second();
|
||||||
_trackReadyCallback.erase(it_callback);
|
_track_ready_callback.erase(it_callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MediaSink::checkTrackIfReady(const Track::Ptr &track){
|
void MediaSink::checkTrackIfReady(const Track::Ptr &track){
|
||||||
lock_guard<recursive_mutex> lck(_mtx);
|
if (!_all_track_ready && !_track_ready_callback.empty()) {
|
||||||
if (!_allTrackReady && !_trackReadyCallback.empty()) {
|
|
||||||
if (track) {
|
if (track) {
|
||||||
checkTrackIfReady_l(track);
|
checkTrackIfReady_l(track);
|
||||||
} else {
|
} else {
|
||||||
@ -88,14 +81,14 @@ void MediaSink::checkTrackIfReady(const Track::Ptr &track){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!_allTrackReady){
|
if(!_all_track_ready){
|
||||||
if(_ticker.elapsedTime() > MAX_WAIT_MS_READY){
|
if(_ticker.elapsedTime() > MAX_WAIT_MS_READY){
|
||||||
//如果超过规定时间,那么不再等待并忽略未准备好的Track
|
//如果超过规定时间,那么不再等待并忽略未准备好的Track
|
||||||
emitAllTrackReady();
|
emitAllTrackReady();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!_trackReadyCallback.empty()){
|
if(!_track_ready_callback.empty()){
|
||||||
//在超时时间内,如果存在未准备好的Track,那么继续等待
|
//在超时时间内,如果存在未准备好的Track,那么继续等待
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -115,22 +108,20 @@ void MediaSink::checkTrackIfReady(const Track::Ptr &track){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MediaSink::addTrackCompleted(){
|
void MediaSink::addTrackCompleted(){
|
||||||
{
|
|
||||||
lock_guard<recursive_mutex> lck(_mtx);
|
lock_guard<recursive_mutex> lck(_mtx);
|
||||||
_max_track_size = _track_map.size();
|
_max_track_size = _track_map.size();
|
||||||
}
|
|
||||||
checkTrackIfReady(nullptr);
|
checkTrackIfReady(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MediaSink::emitAllTrackReady() {
|
void MediaSink::emitAllTrackReady() {
|
||||||
if (_allTrackReady) {
|
if (_all_track_ready) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DebugL << "all track ready use " << _ticker.elapsedTime() << "ms";
|
DebugL << "all track ready use " << _ticker.elapsedTime() << "ms";
|
||||||
if (!_trackReadyCallback.empty()) {
|
if (!_track_ready_callback.empty()) {
|
||||||
//这是超时强制忽略未准备好的Track
|
//这是超时强制忽略未准备好的Track
|
||||||
_trackReadyCallback.clear();
|
_track_ready_callback.clear();
|
||||||
//移除未准备好的Track
|
//移除未准备好的Track
|
||||||
for (auto it = _track_map.begin(); it != _track_map.end();) {
|
for (auto it = _track_map.begin(); it != _track_map.end();) {
|
||||||
if (!it->second->ready()) {
|
if (!it->second->ready()) {
|
||||||
@ -144,8 +135,20 @@ void MediaSink::emitAllTrackReady() {
|
|||||||
|
|
||||||
if (!_track_map.empty()) {
|
if (!_track_map.empty()) {
|
||||||
//最少有一个有效的Track
|
//最少有一个有效的Track
|
||||||
_allTrackReady = true;
|
_all_track_ready = true;
|
||||||
onAllTrackReady();
|
onAllTrackReady();
|
||||||
|
|
||||||
|
//全部Track就绪,我们一次性把之前的帧输出
|
||||||
|
for(auto &pr : _frame_unread){
|
||||||
|
if (_track_map.find(pr.first) == _track_map.end()) {
|
||||||
|
//该Track已经被移除
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
pr.second.for_each([&](const Frame::Ptr &frame) {
|
||||||
|
onTrackFrame(frame);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
_frame_unread.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,8 +114,9 @@ private:
|
|||||||
private:
|
private:
|
||||||
mutable recursive_mutex _mtx;
|
mutable recursive_mutex _mtx;
|
||||||
unordered_map<int,Track::Ptr> _track_map;
|
unordered_map<int,Track::Ptr> _track_map;
|
||||||
unordered_map<int,function<void()> > _trackReadyCallback;
|
unordered_map<int,List<Frame::Ptr> > _frame_unread;
|
||||||
bool _allTrackReady = false;
|
unordered_map<int,function<void()> > _track_ready_callback;
|
||||||
|
bool _all_track_ready = false;
|
||||||
Ticker _ticker;
|
Ticker _ticker;
|
||||||
int _max_track_size = 2;
|
int _max_track_size = 2;
|
||||||
};
|
};
|
||||||
|
@ -223,13 +223,13 @@ public:
|
|||||||
* @param frame 数据帧
|
* @param frame 数据帧
|
||||||
*/
|
*/
|
||||||
void inputFrame(const Frame::Ptr &frame) override{
|
void inputFrame(const Frame::Ptr &frame) override{
|
||||||
if(_cfg.empty()){
|
if (_cfg.empty()) {
|
||||||
//未获取到aac_cfg信息
|
//未获取到aac_cfg信息
|
||||||
if(frame->prefixSize() >= 7) {
|
if (frame->prefixSize() >= 7) {
|
||||||
//7个字节的adts头
|
//7个字节的adts头
|
||||||
_cfg = makeAdtsConfig(reinterpret_cast<const uint8_t *>(frame->data()));
|
_cfg = makeAdtsConfig((uint8_t *)(frame->data()));
|
||||||
onReady();
|
onReady();
|
||||||
}else{
|
} else {
|
||||||
WarnL << "无法获取adts头!";
|
WarnL << "无法获取adts头!";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ void AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
|||||||
if (_aac_cfg.empty()) {
|
if (_aac_cfg.empty()) {
|
||||||
if (frame->prefixSize() >= 7) {
|
if (frame->prefixSize() >= 7) {
|
||||||
//包含adts头,从adts头获取aac配置信息
|
//包含adts头,从adts头获取aac配置信息
|
||||||
_aac_cfg = makeAdtsConfig(reinterpret_cast<const uint8_t *>(frame->data()));
|
_aac_cfg = makeAdtsConfig((uint8_t *)(frame->data()));
|
||||||
}
|
}
|
||||||
makeConfigPacket();
|
makeConfigPacket();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user