RTC: Fix shared msg cache bug

pull/2204/head
winlin 4 years ago
parent eed98dd85b
commit 171ae5dd2d

@ -466,7 +466,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()) {
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_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;
}

@ -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 err = srs_success;
@ -287,21 +300,6 @@ void SrsSharedPtrMessage::wrap(char* payload, int 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()
{
return ptr? ptr->shared_count : 0;
@ -365,8 +363,6 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy()
copy->timestamp = timestamp;
copy->stream_id = stream_id;
copy->payload = ptr->payload;
copy->size = ptr->size;
return copy;
}
@ -382,6 +378,9 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy2()
copy->ptr = ptr;
ptr->shared_count++;
copy->payload = ptr->payload;
copy->size = ptr->size;
return copy;
}

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

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

Loading…
Cancel
Save