RTC: Store the actual size of buffer for RTP packet.

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

@ -290,7 +290,7 @@ public:
// video/audio packet use raw bytes, no video/audio packet.
char* payload;
public:
private:
class SrsSharedPtrPayload
{
public:

@ -766,7 +766,8 @@ SrsRtpPacket2::SrsRtpPacket2()
{
payload_ = NULL;
payload_type_ = SrsRtpPacketPayloadTypeUnknown;
shared_msg = NULL;
shared_buffer = NULL;
actual_buffer_size_ = 0;
nalu_type = SrsAvcNaluTypeReserved;
frame_type = SrsFrameTypeReserved;
@ -779,7 +780,7 @@ SrsRtpPacket2::SrsRtpPacket2()
SrsRtpPacket2::~SrsRtpPacket2()
{
recycle_payload();
recycle_shared_msg();
recycle_shared_buffer();
}
void SrsRtpPacket2::reset()
@ -794,7 +795,7 @@ void SrsRtpPacket2::reset()
// Recyle the payload again, to ensure the packet is new one.
recycle_payload();
recycle_shared_msg();
recycle_shared_buffer();
}
void SrsRtpPacket2::recycle_payload()
@ -805,48 +806,47 @@ void SrsRtpPacket2::recycle_payload()
if (payload_type_ == SrsRtpPacketPayloadTypeRaw && _srs_rtp_raw_cache->enabled()) {
_srs_rtp_raw_cache->recycle((SrsRtpRawPayload*)payload_);
payload_ = NULL;
goto cleanup;
}
if (payload_type_ == SrsRtpPacketPayloadTypeFUA2 && _srs_rtp_fua_cache->enabled()) {
_srs_rtp_fua_cache->recycle((SrsRtpFUAPayload2*)payload_);
payload_ = NULL;
goto cleanup;
}
srs_freep(payload_);
cleanup:
payload_ = NULL;
payload_type_ = SrsRtpPacketPayloadTypeUnknown;
}
void SrsRtpPacket2::recycle_shared_msg()
void SrsRtpPacket2::recycle_shared_buffer()
{
if (!shared_msg) {
if (!shared_buffer) {
return;
}
// 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) {
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_msg);
_srs_rtp_msg_cache_objs->recycle(shared_buffer);
goto cleanup;
}
if (_srs_rtp_msg_cache_buffers->enabled() && shared_msg->count() == 0) {
if (_srs_rtp_msg_cache_buffers->enabled() && shared_buffer->count() == 0) {
// Recycle the UDP large buffer.
_srs_rtp_msg_cache_buffers->recycle(shared_msg);
_srs_rtp_msg_cache_buffers->recycle(shared_buffer);
goto cleanup;
}
}
srs_freep(shared_msg);
return;
srs_freep(shared_buffer);
cleanup:
shared_msg = NULL;
shared_buffer = NULL;
actual_buffer_size_ = 0;
}
bool SrsRtpPacket2::recycle()
@ -859,7 +859,7 @@ bool SrsRtpPacket2::recycle()
// We only recycle the payload and shared messages,
// for header and fields, user will reset or copy it.
recycle_payload();
recycle_shared_msg();
recycle_shared_buffer();
return true;
}
@ -867,28 +867,30 @@ bool SrsRtpPacket2::recycle()
char* SrsRtpPacket2::wrap(int size)
{
// If the buffer is large enough, reuse it.
if (shared_msg && shared_msg->size >= size) {
return shared_msg->payload;
if (shared_buffer && shared_buffer->size >= size) {
return shared_buffer->payload;
}
// Create a large enough message, with under-layer buffer.
while (true) {
srs_freep(shared_msg);
shared_msg = _srs_rtp_msg_cache_buffers->allocate();
srs_freep(shared_buffer);
shared_buffer = _srs_rtp_msg_cache_buffers->allocate();
// The buffer size is larger or equals to the size of packet.
actual_buffer_size_ = size;
// If got a cached message(which has payload), but it's too small,
// we free it and allocate a larger one.
if (shared_msg->payload && shared_msg->size < size) {
if (shared_buffer->payload && shared_buffer->size < size) {
++_srs_pps_objs_rothers->sugar;
continue;
}
// Create under-layer buffer for new message
if (!shared_msg->payload) {
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_msg->wrap(buf, nb_buffer);
shared_buffer->wrap(buf, nb_buffer);
++_srs_pps_objs_rbuf->sugar;
}
@ -896,7 +898,7 @@ char* SrsRtpPacket2::wrap(int size)
break;
}
return shared_msg->payload;
return shared_buffer->payload;
}
char* SrsRtpPacket2::wrap(char* data, int size)
@ -908,11 +910,15 @@ char* SrsRtpPacket2::wrap(char* data, int size)
char* SrsRtpPacket2::wrap(SrsSharedPtrMessage* msg)
{
// Recycle the shared message.
recycle_shared_msg();
// Generally, the wrap(msg) is used for RTMP to RTC, which is not generated by RTC,
// so we do not recycle the msg. It's ok to directly free the msg, event the msg is
// allocated by object cache manager.
srs_freep(shared_buffer);
// Copy from the new message.
shared_msg = msg->copy();
shared_buffer = msg->copy();
// If we wrap a message, the size of packet equals to the message size.
actual_buffer_size_ = shared_buffer->size;
return msg->payload;
}
@ -924,19 +930,20 @@ SrsRtpPacket2* SrsRtpPacket2::copy()
// We got packet from cache, the payload and message MUST be NULL,
// because we had clear it in recycle.
//srs_assert(!cp->payload_);
//srs_assert(!cp->shared_msg);
//srs_assert(!cp->shared_buffer);
cp->header = header;
cp->payload_ = payload_? payload_->copy():NULL;
cp->payload_type_ = payload_type_;
cp->nalu_type = nalu_type;
cp->shared_msg = shared_msg->copy2();
cp->shared_buffer = shared_buffer->copy2();
cp->actual_buffer_size_ = actual_buffer_size_;
cp->frame_type = frame_type;
cp->cached_payload_size = cached_payload_size;
// For performance issue, do not copy the unused field.
//cp->decode_handler = decode_handler;
cp->decode_handler = decode_handler;
return cp;
}

@ -289,7 +289,10 @@ private:
SrsRtpPacketPayloadType payload_type_;
private:
// The original shared message, all RTP packets can refer to its data.
SrsSharedPtrMessage* shared_msg;
// Note that the size of shared msg, is not the packet size, it's a larger aligned buffer.
SrsSharedPtrMessage* shared_buffer;
// The size of original packet.
int actual_buffer_size_;
// Helper fields.
public:
// The first byte as nalu type, for video decoder only.
@ -311,7 +314,7 @@ public:
void reset();
private:
void recycle_payload();
void recycle_shared_msg();
void recycle_shared_buffer();
public:
// Recycle the object to reuse it.
virtual bool recycle();

Loading…
Cancel
Save