1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

RTC: Fix shared msg cache bug

This commit is contained in:
winlin 2021-02-28 11:26:50 +08:00
parent 75a4c8d9e5
commit f6589aa370
4 changed files with 31 additions and 31 deletions

View file

@ -397,7 +397,8 @@ srs_error_t SrsHybridServer::notify(int event, srs_utime_t interval, srs_utime_t
if (_srs_rtp_cache->size() || _srs_rtp_raw_cache->size() || _srs_rtp_fua_cache->size() || _srs_rtp_msg_cache_buffers->size() || _srs_rtp_msg_cache_objs->size()) { if (_srs_rtp_cache->size() || _srs_rtp_raw_cache->size() || _srs_rtp_fua_cache->size() || _srs_rtp_msg_cache_buffers->size() || _srs_rtp_msg_cache_objs->size()) {
snprintf(buf, sizeof(buf), ", cache=(pkt:%d-%dw,raw:%d-%dw,fua:%d-%dw,msg:%d-%dw,buf:%d-%dw)", snprintf(buf, sizeof(buf), ", cache=(pkt:%d-%dw,raw:%d-%dw,fua:%d-%dw,msg:%d-%dw,buf:%d-%dw)",
_srs_rtp_cache->size(), _srs_rtp_cache->capacity()/10000, _srs_rtp_raw_cache->size(), _srs_rtp_raw_cache->capacity()/10000, _srs_rtp_cache->size(), _srs_rtp_cache->capacity()/10000, _srs_rtp_raw_cache->size(), _srs_rtp_raw_cache->capacity()/10000,
_srs_rtp_fua_cache->size(), _srs_rtp_fua_cache->capacity()/10000, _srs_rtp_msg_cache_buffers->size(), _srs_rtp_msg_cache_buffers->capacity()/10000, _srs_rtp_msg_cache_objs->size(), _srs_rtp_msg_cache_objs->capacity()/10000); _srs_rtp_fua_cache->size(), _srs_rtp_fua_cache->capacity()/10000, _srs_rtp_msg_cache_objs->size(), _srs_rtp_msg_cache_objs->capacity()/10000,
_srs_rtp_msg_cache_buffers->size(), _srs_rtp_msg_cache_buffers->capacity()/10000);
cache_desc = buf; cache_desc = buf;
} }

View file

@ -229,6 +229,19 @@ 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 SrsSharedPtrMessage::create(SrsCommonMessage* msg)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
@ -287,21 +300,6 @@ void SrsSharedPtrMessage::wrap(char* payload, int size)
this->size = ptr->size; this->size = ptr->size;
} }
void SrsSharedPtrMessage::unwrap()
{
if (ptr) {
if (ptr->shared_count > 0) {
ptr->shared_count--;
ptr = NULL;
} else {
srs_freep(ptr);
}
}
payload = NULL;
size = 0;
}
int SrsSharedPtrMessage::count() int SrsSharedPtrMessage::count()
{ {
return ptr? ptr->shared_count : 0; return ptr? ptr->shared_count : 0;
@ -365,8 +363,6 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy()
copy->timestamp = timestamp; copy->timestamp = timestamp;
copy->stream_id = stream_id; copy->stream_id = stream_id;
copy->payload = ptr->payload;
copy->size = ptr->size;
return copy; return copy;
} }
@ -382,6 +378,9 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy2()
copy->ptr = ptr; copy->ptr = ptr;
ptr->shared_count++; ptr->shared_count++;
copy->payload = ptr->payload;
copy->size = ptr->size;
return copy; return copy;
} }

View file

@ -313,7 +313,7 @@ public:
virtual ~SrsSharedPtrMessage(); virtual ~SrsSharedPtrMessage();
public: public:
// For object cache to reset and reuse it. // For object cache to reset and reuse it.
bool recycle() { return true; } bool recycle();
// Create shared ptr message, // Create shared ptr message,
// copy header, manage the payload of msg, // copy header, manage the payload of msg,
// set the payload to NULL to prevent double free. // set the payload to NULL to prevent double free.
@ -328,8 +328,6 @@ public:
// Create shared ptr message from RAW payload. // Create shared ptr message from RAW payload.
// @remark Note that the header is set to zero. // @remark Note that the header is set to zero.
virtual void wrap(char* payload, int size); virtual void wrap(char* payload, int size);
// Decrease the reference, if the last one, free it.
void unwrap();
// Get current reference count. // Get current reference count.
// when this object created, count set to 0. // when this object created, count set to 0.
// if copy() this object, count increase 1. // if copy() this object, count increase 1.

View file

@ -827,18 +827,20 @@ void SrsRtpPacket2::recycle_shared_msg()
return; return;
} }
if (!shared_msg->payload || shared_msg->size != kRtpPacketSize || shared_msg->count() > 0) { // Only recycle the message for UDP packets.
// Note that we must unwrap the shared message, because this object pool only cache the if (shared_msg->payload && shared_msg->size == kRtpPacketSize) {
// shared message itself without payload. if (_srs_rtp_msg_cache_objs->enabled() && shared_msg->count() > 0) {
shared_msg->unwrap(); // Recycle the small shared message objects.
_srs_rtp_msg_cache_objs->recycle(shared_msg); _srs_rtp_msg_cache_objs->recycle(shared_msg);
goto cleanup; goto cleanup;
} }
if (_srs_rtp_msg_cache_buffers->enabled()) { if (_srs_rtp_msg_cache_buffers->enabled() && shared_msg->count() == 0) {
// Recycle the UDP large buffer.
_srs_rtp_msg_cache_buffers->recycle(shared_msg); _srs_rtp_msg_cache_buffers->recycle(shared_msg);
goto cleanup; goto cleanup;
} }
}
srs_freep(shared_msg); srs_freep(shared_msg);
return; return;