mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-23 11:17:09 +08:00
优化代码
This commit is contained in:
parent
46ee7f369b
commit
02c4aa3f4b
@ -646,7 +646,7 @@ bool MediaSourceEventInterceptor::stopSendRtp(MediaSource &sender){
|
|||||||
|
|
||||||
/////////////////////////////////////FlushPolicy//////////////////////////////////////
|
/////////////////////////////////////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) {
|
if (new_stamp + 500 < last_stamp) {
|
||||||
//时间戳回退比较大(可能seek中),由于rtp中时间戳是pts,是可能存在一定程度的回退的
|
//时间戳回退比较大(可能seek中),由于rtp中时间戳是pts,是可能存在一定程度的回退的
|
||||||
return true;
|
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;
|
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) {
|
if (new_stamp + 500 < last_stamp) {
|
||||||
//时间戳回退比较大(可能seek中),由于rtp中时间戳是pts,是可能存在一定程度的回退的
|
//时间戳回退比较大(可能seek中),由于rtp中时间戳是pts,是可能存在一定程度的回退的
|
||||||
return true;
|
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;
|
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;
|
bool flush_flag = false;
|
||||||
if (is_key && is_video) {
|
if (is_key && is_video) {
|
||||||
//遇到关键帧flush掉前面的数据,确保关键帧为该组数据的第一帧,确保GOP缓存有效
|
//遇到关键帧flush掉前面的数据,确保关键帧为该组数据的第一帧,确保GOP缓存有效
|
||||||
|
@ -301,18 +301,10 @@ public:
|
|||||||
FlushPolicy() = default;
|
FlushPolicy() = default;
|
||||||
~FlushPolicy() = default;
|
~FlushPolicy() = default;
|
||||||
|
|
||||||
uint32_t getStamp(const RtpPacket::Ptr &packet) {
|
bool isFlushAble(bool is_video, bool is_key, uint64_t new_stamp, int cache_size);
|
||||||
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);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t _last_stamp[2] = {0, 0};
|
uint64_t _last_stamp[2] = {0, 0};
|
||||||
};
|
};
|
||||||
|
|
||||||
/// 合并写缓存模板
|
/// 合并写缓存模板
|
||||||
@ -328,8 +320,8 @@ public:
|
|||||||
|
|
||||||
virtual ~PacketCache() = default;
|
virtual ~PacketCache() = default;
|
||||||
|
|
||||||
void inputPacket(bool is_video, std::shared_ptr<packet> pkt, bool key_pos) {
|
void inputPacket(uint64_t stamp, bool is_video, std::shared_ptr<packet> pkt, bool key_pos) {
|
||||||
if (_policy.isFlushAble(is_video, key_pos, _policy.getStamp(pkt), _cache->size())) {
|
if (_policy.isFlushAble(is_video, key_pos, stamp, _cache->size())) {
|
||||||
flushAll();
|
flushAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,19 +30,8 @@ public:
|
|||||||
uint32_t time_stamp = 0;
|
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直播源
|
//FMP4直播源
|
||||||
class FMP4MediaSource : public MediaSource, public RingDelegate<FMP4Packet::Ptr>, public PacketCache<FMP4Packet, FMP4FlushPolicy>{
|
class FMP4MediaSource : public MediaSource, public RingDelegate<FMP4Packet::Ptr>, public PacketCache<FMP4Packet>{
|
||||||
public:
|
public:
|
||||||
using Ptr = std::shared_ptr<FMP4MediaSource>;
|
using Ptr = std::shared_ptr<FMP4MediaSource>;
|
||||||
using RingDataType = std::shared_ptr<List<FMP4Packet::Ptr> >;
|
using RingDataType = std::shared_ptr<List<FMP4Packet::Ptr> >;
|
||||||
@ -100,14 +89,15 @@ public:
|
|||||||
_have_video = true;
|
_have_video = true;
|
||||||
}
|
}
|
||||||
_speed += packet->size();
|
_speed += packet->size();
|
||||||
PacketCache<FMP4Packet, FMP4FlushPolicy>::inputPacket(true, std::move(packet), key);
|
auto stamp = packet->time_stamp;
|
||||||
|
PacketCache<FMP4Packet>::inputPacket(stamp, true, std::move(packet), key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 情况GOP缓存
|
* 情况GOP缓存
|
||||||
*/
|
*/
|
||||||
void clearCache() override {
|
void clearCache() override {
|
||||||
PacketCache<FMP4Packet, FMP4FlushPolicy>::clearCache();
|
PacketCache<FMP4Packet>::clearCache();
|
||||||
_ring->clearCache();
|
_ring->clearCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,7 +154,8 @@ public:
|
|||||||
}
|
}
|
||||||
bool key = pkt->isVideoKeyFrame();
|
bool key = pkt->isVideoKeyFrame();
|
||||||
bool is_video = pkt->type_id == MSG_VIDEO;
|
bool is_video = pkt->type_id == MSG_VIDEO;
|
||||||
PacketCache<RtmpPacket>::inputPacket(is_video, std::move(pkt), key);
|
auto stamp = pkt->time_stamp;
|
||||||
|
PacketCache<RtmpPacket>::inputPacket(stamp, is_video, std::move(pkt), key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -183,7 +183,8 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool is_video = rtp->type == TrackVideo;
|
bool is_video = rtp->type == TrackVideo;
|
||||||
PacketCache<RtpPacket>::inputPacket(is_video, std::move(rtp), keyPos);
|
auto stamp = rtp->timeStamp;
|
||||||
|
PacketCache<RtpPacket>::inputPacket(stamp, is_video, std::move(rtp), keyPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearCache() override{
|
void clearCache() override{
|
||||||
|
@ -37,17 +37,19 @@ class RtspSession;
|
|||||||
class BufferRtp : public Buffer{
|
class BufferRtp : public Buffer{
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<BufferRtp> Ptr;
|
typedef std::shared_ptr<BufferRtp> Ptr;
|
||||||
BufferRtp(const RtpPacket::Ptr & pkt,uint32_t offset = 0 ):_rtp(pkt),_offset(offset){}
|
BufferRtp(Buffer::Ptr pkt, uint32_t offset = 0) : _rtp(std::move(pkt)), _offset(offset) {}
|
||||||
virtual ~BufferRtp(){}
|
~BufferRtp() override{}
|
||||||
|
|
||||||
char *data() const override {
|
char *data() const override {
|
||||||
return (char *)_rtp->data() + _offset;
|
return (char *)_rtp->data() + _offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t size() const override {
|
uint32_t size() const override {
|
||||||
return _rtp->size() - _offset;
|
return _rtp->size() - _offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RtpPacket::Ptr _rtp;
|
Buffer::Ptr _rtp;
|
||||||
uint32_t _offset;
|
uint32_t _offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -30,19 +30,8 @@ public:
|
|||||||
uint32_t time_stamp = 0;
|
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直播源
|
//TS直播源
|
||||||
class TSMediaSource : public MediaSource, public RingDelegate<TSPacket::Ptr>, public PacketCache<TSPacket, TSFlushPolicy>{
|
class TSMediaSource : public MediaSource, public RingDelegate<TSPacket::Ptr>, public PacketCache<TSPacket>{
|
||||||
public:
|
public:
|
||||||
using PoolType = ResourcePool<TSPacket>;
|
using PoolType = ResourcePool<TSPacket>;
|
||||||
using Ptr = std::shared_ptr<TSMediaSource>;
|
using Ptr = std::shared_ptr<TSMediaSource>;
|
||||||
@ -83,14 +72,15 @@ public:
|
|||||||
if (key) {
|
if (key) {
|
||||||
_have_video = true;
|
_have_video = true;
|
||||||
}
|
}
|
||||||
PacketCache<TSPacket, TSFlushPolicy>::inputPacket(true, std::move(packet), key);
|
auto stamp = packet->time_stamp;
|
||||||
|
PacketCache<TSPacket>::inputPacket(stamp, true, std::move(packet), key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 情况GOP缓存
|
* 情况GOP缓存
|
||||||
*/
|
*/
|
||||||
void clearCache() override {
|
void clearCache() override {
|
||||||
PacketCache<TSPacket, TSFlushPolicy>::clearCache();
|
PacketCache<TSPacket>::clearCache();
|
||||||
_ring->clearCache();
|
_ring->clearCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user