mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
SquashSRS4: Remove object cache and stat api
This commit is contained in:
parent
f711eb79ed
commit
6a980683f7
44 changed files with 141 additions and 1277 deletions
|
@ -229,19 +229,6 @@ SrsSharedPtrMessage::~SrsSharedPtrMessage()
|
|||
}
|
||||
}
|
||||
|
||||
bool SrsSharedPtrMessage::recycle()
|
||||
{
|
||||
// When recycle, unwrap if not the last reference.
|
||||
if (ptr && ptr->shared_count > 0) {
|
||||
ptr->shared_count--;
|
||||
ptr = NULL;
|
||||
payload = NULL;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
srs_error_t SrsSharedPtrMessage::create(SrsCommonMessage* msg)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
@ -369,7 +356,7 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy()
|
|||
|
||||
SrsSharedPtrMessage* SrsSharedPtrMessage::copy2()
|
||||
{
|
||||
SrsSharedPtrMessage* copy = _srs_rtp_msg_cache_objs->allocate();
|
||||
SrsSharedPtrMessage* copy = new SrsSharedPtrMessage();
|
||||
|
||||
// We got an object from cache, the ptr might exists, so unwrap it.
|
||||
//srs_assert(!copy->ptr);
|
||||
|
|
|
@ -312,8 +312,6 @@ public:
|
|||
SrsSharedPtrMessage();
|
||||
virtual ~SrsSharedPtrMessage();
|
||||
public:
|
||||
// For object cache to reset and reuse it.
|
||||
bool recycle();
|
||||
// Create shared ptr message,
|
||||
// copy header, manage the payload of msg,
|
||||
// set the payload to NULL to prevent double free.
|
||||
|
|
|
@ -71,7 +71,7 @@ public:
|
|||
/**
|
||||
* The reader and seeker.
|
||||
*/
|
||||
class ISrsReadSeeker : virtual public ISrsReader, virtual public ISrsSeeker
|
||||
class ISrsReadSeeker : public ISrsReader, public ISrsSeeker
|
||||
{
|
||||
public:
|
||||
ISrsReadSeeker();
|
||||
|
@ -115,7 +115,7 @@ public:
|
|||
/**
|
||||
* The generally writer, stream and vector writer.
|
||||
*/
|
||||
class ISrsWriter : virtual public ISrsStreamWriter, virtual public ISrsVectorWriter
|
||||
class ISrsWriter : public ISrsStreamWriter, public ISrsVectorWriter
|
||||
{
|
||||
public:
|
||||
ISrsWriter();
|
||||
|
@ -125,7 +125,7 @@ public:
|
|||
/**
|
||||
* The writer and seeker.
|
||||
*/
|
||||
class ISrsWriteSeeker : virtual public ISrsWriter, virtual public ISrsSeeker
|
||||
class ISrsWriteSeeker : public ISrsWriter, public ISrsSeeker
|
||||
{
|
||||
public:
|
||||
ISrsWriteSeeker();
|
||||
|
|
|
@ -41,7 +41,6 @@ SrsPps* _srs_pps_objs_rraw = NULL;
|
|||
SrsPps* _srs_pps_objs_rfua = NULL;
|
||||
SrsPps* _srs_pps_objs_rbuf = NULL;
|
||||
SrsPps* _srs_pps_objs_rothers = NULL;
|
||||
SrsPps* _srs_pps_objs_drop = NULL;
|
||||
|
||||
/* @see https://tools.ietf.org/html/rfc1889#section-5.1
|
||||
0 1 2 3
|
||||
|
@ -786,89 +785,8 @@ SrsRtpPacket2::SrsRtpPacket2()
|
|||
|
||||
SrsRtpPacket2::~SrsRtpPacket2()
|
||||
{
|
||||
recycle_payload();
|
||||
recycle_shared_buffer();
|
||||
}
|
||||
|
||||
void SrsRtpPacket2::reset()
|
||||
{
|
||||
nalu_type = SrsAvcNaluTypeReserved;
|
||||
frame_type = SrsFrameTypeReserved;
|
||||
cached_payload_size = 0;
|
||||
decode_handler = NULL;
|
||||
|
||||
// It's important to reset the header.
|
||||
header = SrsRtpHeader();
|
||||
|
||||
// Recyle the payload again, to ensure the packet is new one.
|
||||
recycle_payload();
|
||||
recycle_shared_buffer();
|
||||
}
|
||||
|
||||
void SrsRtpPacket2::recycle_payload()
|
||||
{
|
||||
if (!payload_) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload_type_ == SrsRtpPacketPayloadTypeRaw && _srs_rtp_raw_cache->enabled()) {
|
||||
_srs_rtp_raw_cache->recycle((SrsRtpRawPayload*)payload_);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (payload_type_ == SrsRtpPacketPayloadTypeFUA2 && _srs_rtp_fua_cache->enabled()) {
|
||||
_srs_rtp_fua_cache->recycle((SrsRtpFUAPayload2*)payload_);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
srs_freep(payload_);
|
||||
|
||||
cleanup:
|
||||
payload_ = NULL;
|
||||
payload_type_ = SrsRtpPacketPayloadTypeUnknown;
|
||||
}
|
||||
|
||||
void SrsRtpPacket2::recycle_shared_buffer()
|
||||
{
|
||||
if (!shared_buffer_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only recycle the message for UDP packets.
|
||||
if (shared_buffer_->payload && shared_buffer_->size == kRtpPacketSize) {
|
||||
if (_srs_rtp_msg_cache_objs->enabled() && shared_buffer_->count() > 0) {
|
||||
// Recycle the small shared message objects.
|
||||
_srs_rtp_msg_cache_objs->recycle(shared_buffer_);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (_srs_rtp_msg_cache_buffers->enabled() && shared_buffer_->count() == 0) {
|
||||
// Recycle the UDP large buffer.
|
||||
_srs_rtp_msg_cache_buffers->recycle(shared_buffer_);
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
srs_freep(shared_buffer_);
|
||||
|
||||
cleanup:
|
||||
shared_buffer_ = NULL;
|
||||
actual_buffer_size_ = 0;
|
||||
}
|
||||
|
||||
bool SrsRtpPacket2::recycle()
|
||||
{
|
||||
// Clear the cache size, it may change when reuse it.
|
||||
cached_payload_size = 0;
|
||||
// Reset the handler, for decode only.
|
||||
decode_handler = NULL;
|
||||
|
||||
// We only recycle the payload and shared messages,
|
||||
// for header and fields, user will reset or copy it.
|
||||
recycle_payload();
|
||||
recycle_shared_buffer();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
char* SrsRtpPacket2::wrap(int size)
|
||||
|
@ -882,29 +800,16 @@ char* SrsRtpPacket2::wrap(int size)
|
|||
}
|
||||
|
||||
// Create a large enough message, with under-layer buffer.
|
||||
while (true) {
|
||||
srs_freep(shared_buffer_);
|
||||
shared_buffer_ = _srs_rtp_msg_cache_buffers->allocate();
|
||||
srs_freep(shared_buffer_);
|
||||
shared_buffer_ = new SrsSharedPtrMessage();
|
||||
|
||||
// If got a cached message(which has payload), but it's too small,
|
||||
// we free it and allocate a larger one.
|
||||
if (shared_buffer_->payload && shared_buffer_->size < size) {
|
||||
++_srs_pps_objs_rothers->sugar;
|
||||
continue;
|
||||
}
|
||||
// Create under-layer buffer for new message
|
||||
// For RTC, we use larger under-layer buffer for each packet.
|
||||
int nb_buffer = srs_max(size, kRtpPacketSize);
|
||||
char* buf = new char[nb_buffer];
|
||||
shared_buffer_->wrap(buf, nb_buffer);
|
||||
|
||||
// Create under-layer buffer for new message
|
||||
if (!shared_buffer_->payload) {
|
||||
// For RTC, we use larger under-layer buffer for each packet.
|
||||
int nb_buffer = srs_max(size, kRtpPacketSize);
|
||||
char* buf = new char[nb_buffer];
|
||||
shared_buffer_->wrap(buf, nb_buffer);
|
||||
|
||||
++_srs_pps_objs_rbuf->sugar;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
++_srs_pps_objs_rbuf->sugar;
|
||||
|
||||
return shared_buffer_->payload;
|
||||
}
|
||||
|
@ -933,7 +838,7 @@ char* SrsRtpPacket2::wrap(SrsSharedPtrMessage* msg)
|
|||
|
||||
SrsRtpPacket2* SrsRtpPacket2::copy()
|
||||
{
|
||||
SrsRtpPacket2* cp = _srs_rtp_cache->allocate();
|
||||
SrsRtpPacket2* cp = new SrsRtpPacket2();
|
||||
|
||||
// We got packet from cache, the payload and message MUST be NULL,
|
||||
// because we had clear it in recycle.
|
||||
|
@ -1043,7 +948,7 @@ srs_error_t SrsRtpPacket2::decode(SrsBuffer* buf)
|
|||
|
||||
// By default, we always use the RAW payload.
|
||||
if (!payload_) {
|
||||
payload_ = _srs_rtp_raw_cache->allocate();
|
||||
payload_ = new SrsRtpRawPayload();
|
||||
payload_type_ = SrsRtpPacketPayloadTypeRaw;
|
||||
}
|
||||
|
||||
|
@ -1081,13 +986,6 @@ bool SrsRtpPacket2::is_keyframe()
|
|||
return false;
|
||||
}
|
||||
|
||||
SrsRtpObjectCacheManager<SrsRtpPacket2>* _srs_rtp_cache = NULL;
|
||||
SrsRtpObjectCacheManager<SrsRtpRawPayload>* _srs_rtp_raw_cache = NULL;
|
||||
SrsRtpObjectCacheManager<SrsRtpFUAPayload2>* _srs_rtp_fua_cache = NULL;
|
||||
|
||||
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_buffers = NULL;
|
||||
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_objs = NULL;
|
||||
|
||||
SrsRtpRawPayload::SrsRtpRawPayload()
|
||||
{
|
||||
payload = NULL;
|
||||
|
@ -1100,12 +998,6 @@ SrsRtpRawPayload::~SrsRtpRawPayload()
|
|||
{
|
||||
}
|
||||
|
||||
bool SrsRtpRawPayload::recycle()
|
||||
{
|
||||
payload=NULL; nn_payload=0;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t SrsRtpRawPayload::nb_bytes()
|
||||
{
|
||||
return nn_payload;
|
||||
|
@ -1140,7 +1032,7 @@ srs_error_t SrsRtpRawPayload::decode(SrsBuffer* buf)
|
|||
|
||||
ISrsRtpPayloader* SrsRtpRawPayload::copy()
|
||||
{
|
||||
SrsRtpRawPayload* cp = _srs_rtp_raw_cache->allocate();
|
||||
SrsRtpRawPayload* cp = new SrsRtpRawPayload();
|
||||
|
||||
cp->payload = payload;
|
||||
cp->nn_payload = nn_payload;
|
||||
|
@ -1566,16 +1458,6 @@ SrsRtpFUAPayload2::~SrsRtpFUAPayload2()
|
|||
{
|
||||
}
|
||||
|
||||
bool SrsRtpFUAPayload2::recycle()
|
||||
{
|
||||
start = end = false;
|
||||
nri = nalu_type = (SrsAvcNaluType)0;
|
||||
|
||||
payload = NULL;
|
||||
size = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t SrsRtpFUAPayload2::nb_bytes()
|
||||
{
|
||||
return 2 + size;
|
||||
|
@ -1643,7 +1525,7 @@ srs_error_t SrsRtpFUAPayload2::decode(SrsBuffer* buf)
|
|||
|
||||
ISrsRtpPayloader* SrsRtpFUAPayload2::copy()
|
||||
{
|
||||
SrsRtpFUAPayload2* cp = _srs_rtp_fua_cache->allocate();
|
||||
SrsRtpFUAPayload2* cp = new SrsRtpFUAPayload2();
|
||||
|
||||
cp->nri = nri;
|
||||
cp->start = start;
|
||||
|
|
|
@ -315,15 +315,6 @@ public:
|
|||
SrsRtpPacket2();
|
||||
virtual ~SrsRtpPacket2();
|
||||
public:
|
||||
// User MUST reset the packet if got from cache,
|
||||
// except copy(we will assign the header and copy payload).
|
||||
void reset();
|
||||
private:
|
||||
void recycle_payload();
|
||||
void recycle_shared_buffer();
|
||||
public:
|
||||
// Recycle the object to reuse it.
|
||||
virtual bool recycle();
|
||||
// Wrap buffer to shared_message, which is managed by us.
|
||||
char* wrap(int size);
|
||||
char* wrap(char* data, int size);
|
||||
|
@ -357,112 +348,6 @@ public:
|
|||
bool is_keyframe();
|
||||
};
|
||||
|
||||
// For object cache manager to stat the object dropped.
|
||||
#include <srs_kernel_kbps.hpp>
|
||||
extern SrsPps* _srs_pps_objs_drop;
|
||||
|
||||
// The RTP packet or message cache manager.
|
||||
template<typename T>
|
||||
class SrsRtpObjectCacheManager
|
||||
{
|
||||
private:
|
||||
bool enabled_;
|
||||
std::vector<T*> cache_objs_;
|
||||
size_t capacity_;
|
||||
size_t object_size_;
|
||||
public:
|
||||
// SrsRtpObjectCacheManager::SrsRtpObjectCacheManager
|
||||
SrsRtpObjectCacheManager(size_t size_of_object) {
|
||||
enabled_ = false;
|
||||
capacity_ = 0;
|
||||
object_size_ = size_of_object;
|
||||
}
|
||||
// SrsRtpObjectCacheManager::~SrsRtpObjectCacheManager
|
||||
virtual ~SrsRtpObjectCacheManager() {
|
||||
typedef typename std::vector<T*>::iterator iterator;
|
||||
for (iterator it = cache_objs_.begin(); it != cache_objs_.end(); ++it) {
|
||||
T* obj = *it;
|
||||
srs_freep(obj);
|
||||
}
|
||||
}
|
||||
public:
|
||||
// Setup the object cache, shrink if capacity changed.
|
||||
// SrsRtpObjectCacheManager::setup
|
||||
void setup(bool v, uint64_t memory) {
|
||||
enabled_ = v;
|
||||
capacity_ = (size_t)(memory / object_size_);
|
||||
|
||||
|
||||
if (!enabled_) {
|
||||
capacity_ = 0;
|
||||
}
|
||||
|
||||
// Shrink the cache.
|
||||
while (cache_objs_.size() > capacity_) {
|
||||
T* obj = cache_objs_.back();
|
||||
cache_objs_.pop_back();
|
||||
srs_freep(obj);
|
||||
}
|
||||
}
|
||||
// Get the status of object cache.
|
||||
// SrsRtpObjectCacheManager::enabled
|
||||
inline bool enabled() {
|
||||
return enabled_;
|
||||
}
|
||||
// SrsRtpObjectCacheManager::size
|
||||
int size() {
|
||||
return (int)cache_objs_.size();
|
||||
}
|
||||
// SrsRtpObjectCacheManager::capacity
|
||||
int capacity() {
|
||||
return (int)capacity_;
|
||||
}
|
||||
// Try to allocate from cache, create new object if no cache.
|
||||
// SrsRtpObjectCacheManager::allocate
|
||||
T* allocate() {
|
||||
if (!enabled_ || cache_objs_.empty()) {
|
||||
return new T();
|
||||
}
|
||||
|
||||
T* obj = cache_objs_.back();
|
||||
cache_objs_.pop_back();
|
||||
|
||||
return obj;
|
||||
}
|
||||
// Recycle the object to cache.
|
||||
// @remark User can directly free the packet.
|
||||
// SrsRtpObjectCacheManager::recycle
|
||||
void recycle(T* p) {
|
||||
// The p may be NULL, because srs_freep(NULL) is ok.
|
||||
if (!p) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If disabled, drop the object.
|
||||
if (!enabled_) {
|
||||
srs_freep(p);
|
||||
return;
|
||||
}
|
||||
|
||||
// If recycle the object fail, drop the cached object.
|
||||
if (!p->recycle()) {
|
||||
srs_freep(p);
|
||||
return;
|
||||
}
|
||||
|
||||
// If exceed the capacity, drop the object.
|
||||
if (cache_objs_.size() > capacity_) {
|
||||
++_srs_pps_objs_drop->sugar;
|
||||
|
||||
srs_freep(p);
|
||||
return;
|
||||
}
|
||||
|
||||
// Recycle it.
|
||||
cache_objs_.push_back(p);
|
||||
}
|
||||
};
|
||||
|
||||
// Single payload data.
|
||||
class SrsRtpRawPayload : public ISrsRtpPayloader
|
||||
{
|
||||
|
@ -474,8 +359,6 @@ public:
|
|||
public:
|
||||
SrsRtpRawPayload();
|
||||
virtual ~SrsRtpRawPayload();
|
||||
public:
|
||||
bool recycle();
|
||||
// interface ISrsRtpPayloader
|
||||
public:
|
||||
virtual uint64_t nb_bytes();
|
||||
|
@ -574,8 +457,6 @@ public:
|
|||
public:
|
||||
SrsRtpFUAPayload2();
|
||||
virtual ~SrsRtpFUAPayload2();
|
||||
public:
|
||||
bool recycle();
|
||||
// interface ISrsRtpPayloader
|
||||
public:
|
||||
virtual uint64_t nb_bytes();
|
||||
|
@ -584,15 +465,4 @@ public:
|
|||
virtual ISrsRtpPayloader* copy();
|
||||
};
|
||||
|
||||
// For RTP packets cache.
|
||||
extern SrsRtpObjectCacheManager<SrsRtpPacket2>* _srs_rtp_cache;
|
||||
extern SrsRtpObjectCacheManager<SrsRtpRawPayload>* _srs_rtp_raw_cache;
|
||||
extern SrsRtpObjectCacheManager<SrsRtpFUAPayload2>* _srs_rtp_fua_cache;
|
||||
|
||||
// For shared message cache, with payload.
|
||||
extern SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_buffers;
|
||||
// For shared message cache, without payload.
|
||||
// Note that user must unwrap the shared message, before recycle it.
|
||||
extern SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_objs;
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue