diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index d548a7d1..1c14508a 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -646,7 +646,7 @@ bool MediaSourceEventInterceptor::stopSendRtp(MediaSource &sender){ /////////////////////////////////////FlushPolicy////////////////////////////////////// -static bool isFlushAble_default(bool is_video, uint32_t last_stamp, uint32_t new_stamp, int cache_size) { +static bool isFlushAble_default(bool is_video, uint64_t last_stamp, uint64_t new_stamp, int cache_size) { if (new_stamp + 500 < last_stamp) { //时间戳回退比较大(可能seek中),由于rtp中时间戳是pts,是可能存在一定程度的回退的 return true; @@ -656,7 +656,7 @@ static bool isFlushAble_default(bool is_video, uint32_t last_stamp, uint32_t new return last_stamp != new_stamp || cache_size >= 1024; } -static bool isFlushAble_merge(bool is_video, uint32_t last_stamp, uint32_t new_stamp, int cache_size, int merge_ms) { +static bool isFlushAble_merge(bool is_video, uint64_t last_stamp, uint64_t new_stamp, int cache_size, int merge_ms) { if (new_stamp + 500 < last_stamp) { //时间戳回退比较大(可能seek中),由于rtp中时间戳是pts,是可能存在一定程度的回退的 return true; @@ -672,7 +672,7 @@ static bool isFlushAble_merge(bool is_video, uint32_t last_stamp, uint32_t new_s return cache_size >= 1024; } -bool FlushPolicy::isFlushAble(bool is_video, bool is_key, uint32_t new_stamp, int cache_size) { +bool FlushPolicy::isFlushAble(bool is_video, bool is_key, uint64_t new_stamp, int cache_size) { bool flush_flag = false; if (is_key && is_video) { //遇到关键帧flush掉前面的数据,确保关键帧为该组数据的第一帧,确保GOP缓存有效 diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 44f8d3be..ed7d23e6 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -301,18 +301,10 @@ public: FlushPolicy() = default; ~FlushPolicy() = default; - uint32_t getStamp(const RtpPacket::Ptr &packet) { - return packet->timeStamp; - } - - uint32_t getStamp(const RtmpPacket::Ptr &packet) { - return packet->time_stamp; - } - - bool isFlushAble(bool is_video, bool is_key, uint32_t new_stamp, int cache_size); + bool isFlushAble(bool is_video, bool is_key, uint64_t new_stamp, int cache_size); private: - uint32_t _last_stamp[2] = {0, 0}; + uint64_t _last_stamp[2] = {0, 0}; }; /// 合并写缓存模板 @@ -328,8 +320,8 @@ public: virtual ~PacketCache() = default; - void inputPacket(bool is_video, std::shared_ptr pkt, bool key_pos) { - if (_policy.isFlushAble(is_video, key_pos, _policy.getStamp(pkt), _cache->size())) { + void inputPacket(uint64_t stamp, bool is_video, std::shared_ptr pkt, bool key_pos) { + if (_policy.isFlushAble(is_video, key_pos, stamp, _cache->size())) { flushAll(); } diff --git a/src/FMP4/FMP4MediaSource.h b/src/FMP4/FMP4MediaSource.h index 23b58fee..ef4af868 100644 --- a/src/FMP4/FMP4MediaSource.h +++ b/src/FMP4/FMP4MediaSource.h @@ -30,19 +30,8 @@ public: uint32_t time_stamp = 0; }; -//FMP4直播合并写策略类 -class FMP4FlushPolicy : public FlushPolicy{ -public: - FMP4FlushPolicy() = default; - ~FMP4FlushPolicy() = default; - - uint32_t getStamp(const FMP4Packet::Ptr &packet) { - return packet->time_stamp; - } -}; - //FMP4直播源 -class FMP4MediaSource : public MediaSource, public RingDelegate, public PacketCache{ +class FMP4MediaSource : public MediaSource, public RingDelegate, public PacketCache{ public: using Ptr = std::shared_ptr; using RingDataType = std::shared_ptr >; @@ -100,14 +89,15 @@ public: _have_video = true; } _speed += packet->size(); - PacketCache::inputPacket(true, std::move(packet), key); + auto stamp = packet->time_stamp; + PacketCache::inputPacket(stamp, true, std::move(packet), key); } /** * 情况GOP缓存 */ void clearCache() override { - PacketCache::clearCache(); + PacketCache::clearCache(); _ring->clearCache(); } diff --git a/src/Rtmp/RtmpMediaSource.h b/src/Rtmp/RtmpMediaSource.h index b538c003..8d309654 100644 --- a/src/Rtmp/RtmpMediaSource.h +++ b/src/Rtmp/RtmpMediaSource.h @@ -154,7 +154,8 @@ public: } bool key = pkt->isVideoKeyFrame(); bool is_video = pkt->type_id == MSG_VIDEO; - PacketCache::inputPacket(is_video, std::move(pkt), key); + auto stamp = pkt->time_stamp; + PacketCache::inputPacket(stamp, is_video, std::move(pkt), key); } /** diff --git a/src/Rtsp/RtspMediaSource.h b/src/Rtsp/RtspMediaSource.h index cafeaf33..869eae73 100644 --- a/src/Rtsp/RtspMediaSource.h +++ b/src/Rtsp/RtspMediaSource.h @@ -183,7 +183,8 @@ public: } } bool is_video = rtp->type == TrackVideo; - PacketCache::inputPacket(is_video, std::move(rtp), keyPos); + auto stamp = rtp->timeStamp; + PacketCache::inputPacket(stamp, is_video, std::move(rtp), keyPos); } void clearCache() override{ diff --git a/src/Rtsp/RtspSession.h b/src/Rtsp/RtspSession.h index f681e874..cd691c7b 100644 --- a/src/Rtsp/RtspSession.h +++ b/src/Rtsp/RtspSession.h @@ -37,17 +37,19 @@ class RtspSession; class BufferRtp : public Buffer{ public: typedef std::shared_ptr Ptr; - BufferRtp(const RtpPacket::Ptr & pkt,uint32_t offset = 0 ):_rtp(pkt),_offset(offset){} - virtual ~BufferRtp(){} + BufferRtp(Buffer::Ptr pkt, uint32_t offset = 0) : _rtp(std::move(pkt)), _offset(offset) {} + ~BufferRtp() override{} char *data() const override { return (char *)_rtp->data() + _offset; } + uint32_t size() const override { return _rtp->size() - _offset; } + private: - RtpPacket::Ptr _rtp; + Buffer::Ptr _rtp; uint32_t _offset; }; diff --git a/src/TS/TSMediaSource.h b/src/TS/TSMediaSource.h index 8b8c57f7..f13ad1c1 100644 --- a/src/TS/TSMediaSource.h +++ b/src/TS/TSMediaSource.h @@ -30,19 +30,8 @@ public: uint32_t time_stamp = 0; }; -//TS直播合并写策略类 -class TSFlushPolicy : public FlushPolicy{ -public: - TSFlushPolicy() = default; - ~TSFlushPolicy() = default; - - uint32_t getStamp(const TSPacket::Ptr &packet) { - return packet->time_stamp; - } -}; - //TS直播源 -class TSMediaSource : public MediaSource, public RingDelegate, public PacketCache{ +class TSMediaSource : public MediaSource, public RingDelegate, public PacketCache{ public: using PoolType = ResourcePool; using Ptr = std::shared_ptr; @@ -83,14 +72,15 @@ public: if (key) { _have_video = true; } - PacketCache::inputPacket(true, std::move(packet), key); + auto stamp = packet->time_stamp; + PacketCache::inputPacket(stamp, true, std::move(packet), key); } /** * 情况GOP缓存 */ void clearCache() override { - PacketCache::clearCache(); + PacketCache::clearCache(); _ring->clearCache(); }