diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index a350704f..e2b4aee1 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -486,14 +486,22 @@ static bool isFlushAble_merge(bool is_audio, uint32_t last_stamp, uint32_t new_s return cache_size > 20; } -bool FlushPolicy::isFlushAble(uint32_t last_stamp, uint32_t new_stamp, int cache_size) { - GET_CONFIG(bool,ultraLowDelay, General::kUltraLowDelay); - GET_CONFIG(int,mergeWriteMS, General::kMergeWriteMS); - if(ultraLowDelay || mergeWriteMS <= 0){ +bool FlushPolicy::isFlushAble(uint32_t new_stamp, int cache_size) { + bool ret = false; + GET_CONFIG(bool, ultraLowDelay, General::kUltraLowDelay); + GET_CONFIG(int, mergeWriteMS, General::kMergeWriteMS); + if (ultraLowDelay || mergeWriteMS <= 0) { //关闭了合并写或者合并写阈值小于等于0 - return isFlushAble_default(_is_audio, last_stamp, new_stamp, cache_size); + ret = isFlushAble_default(_is_audio, _last_stamp, new_stamp, cache_size); + } else { + ret = isFlushAble_merge(_is_audio, _last_stamp, new_stamp, cache_size, mergeWriteMS); } - return isFlushAble_merge(_is_audio, last_stamp, new_stamp, cache_size,mergeWriteMS); + + if (ret) { +// DebugL << _is_audio << " " << _last_stamp << " " << new_stamp; + _last_stamp = new_stamp; + } + return ret; } } /* namespace mediakit */ \ No newline at end of file diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index cddfb272..ab0a0cfa 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -173,9 +173,10 @@ public: return packet->timeStamp; } - bool isFlushAble(uint32_t last_stamp, uint32_t new_stamp, int cache_size); + bool isFlushAble(uint32_t new_stamp, int cache_size); private: bool _is_audio; + uint32_t _last_stamp= 0; }; /// 视频合并写缓存模板 @@ -192,14 +193,12 @@ public: virtual ~VideoPacketCache() = default; void inputVideo(const std::shared_ptr &rtp, bool key_pos) { - auto new_stamp = _policy.getStamp(rtp); - if (_policy.isFlushAble(_last_stamp, new_stamp, _cache->size())) { + if (_policy.isFlushAble(_policy.getStamp(rtp), _cache->size())) { flushAll(); } //追加数据到最后 _cache->emplace_back(rtp); - _last_stamp = new_stamp; if (key_pos) { _key_pos = key_pos; } @@ -220,7 +219,6 @@ private: private: policy _policy; std::shared_ptr _cache; - uint32_t _last_stamp = 0; bool _key_pos = false; }; @@ -238,13 +236,11 @@ public: virtual ~AudioPacketCache() = default; void inputAudio(const std::shared_ptr &rtp) { - auto new_stamp = _policy.getStamp(rtp); - if (_policy.isFlushAble(_last_stamp, new_stamp, _cache->size())) { + if (_policy.isFlushAble(_policy.getStamp(rtp), _cache->size())) { flushAll(); } //追加数据到最后 _cache->emplace_back(rtp); - _last_stamp = new_stamp; } virtual void onFlushAudio(std::shared_ptr &) = 0; @@ -261,7 +257,6 @@ private: private: policy _policy; std::shared_ptr _cache; - uint32_t _last_stamp = 0; }; } /* namespace mediakit */