Refine code

pull/2252/head
winlin 4 years ago
parent 563b99701a
commit 2b4b6a8e65

@ -766,7 +766,7 @@ SrsRtpPacket2::SrsRtpPacket2()
{ {
payload_ = NULL; payload_ = NULL;
payload_type_ = SrsRtpPacketPayloadTypeUnknown; payload_type_ = SrsRtpPacketPayloadTypeUnknown;
shared_buffer = NULL; shared_buffer_ = NULL;
actual_buffer_size_ = 0; actual_buffer_size_ = 0;
nalu_type = SrsAvcNaluTypeReserved; nalu_type = SrsAvcNaluTypeReserved;
@ -823,29 +823,29 @@ cleanup:
void SrsRtpPacket2::recycle_shared_buffer() void SrsRtpPacket2::recycle_shared_buffer()
{ {
if (!shared_buffer) { if (!shared_buffer_) {
return; return;
} }
// Only recycle the message for UDP packets. // Only recycle the message for UDP packets.
if (shared_buffer->payload && shared_buffer->size == kRtpPacketSize) { if (shared_buffer_->payload && shared_buffer_->size == kRtpPacketSize) {
if (_srs_rtp_msg_cache_objs->enabled() && shared_buffer->count() > 0) { if (_srs_rtp_msg_cache_objs->enabled() && shared_buffer_->count() > 0) {
// Recycle the small shared message objects. // Recycle the small shared message objects.
_srs_rtp_msg_cache_objs->recycle(shared_buffer); _srs_rtp_msg_cache_objs->recycle(shared_buffer_);
goto cleanup; goto cleanup;
} }
if (_srs_rtp_msg_cache_buffers->enabled() && shared_buffer->count() == 0) { if (_srs_rtp_msg_cache_buffers->enabled() && shared_buffer_->count() == 0) {
// Recycle the UDP large buffer. // Recycle the UDP large buffer.
_srs_rtp_msg_cache_buffers->recycle(shared_buffer); _srs_rtp_msg_cache_buffers->recycle(shared_buffer_);
goto cleanup; goto cleanup;
} }
} }
srs_freep(shared_buffer); srs_freep(shared_buffer_);
cleanup: cleanup:
shared_buffer = NULL; shared_buffer_ = NULL;
actual_buffer_size_ = 0; actual_buffer_size_ = 0;
} }
@ -870,28 +870,28 @@ char* SrsRtpPacket2::wrap(int size)
actual_buffer_size_ = size; actual_buffer_size_ = size;
// If the buffer is large enough, reuse it. // If the buffer is large enough, reuse it.
if (shared_buffer && shared_buffer->size >= size) { if (shared_buffer_ && shared_buffer_->size >= size) {
return shared_buffer->payload; return shared_buffer_->payload;
} }
// Create a large enough message, with under-layer buffer. // Create a large enough message, with under-layer buffer.
while (true) { while (true) {
srs_freep(shared_buffer); srs_freep(shared_buffer_);
shared_buffer = _srs_rtp_msg_cache_buffers->allocate(); shared_buffer_ = _srs_rtp_msg_cache_buffers->allocate();
// If got a cached message(which has payload), but it's too small, // If got a cached message(which has payload), but it's too small,
// we free it and allocate a larger one. // we free it and allocate a larger one.
if (shared_buffer->payload && shared_buffer->size < size) { if (shared_buffer_->payload && shared_buffer_->size < size) {
++_srs_pps_objs_rothers->sugar; ++_srs_pps_objs_rothers->sugar;
continue; continue;
} }
// Create under-layer buffer for new message // Create under-layer buffer for new message
if (!shared_buffer->payload) { if (!shared_buffer_->payload) {
// For RTC, we use larger under-layer buffer for each packet. // For RTC, we use larger under-layer buffer for each packet.
int nb_buffer = srs_max(size, kRtpPacketSize); int nb_buffer = srs_max(size, kRtpPacketSize);
char* buf = new char[nb_buffer]; char* buf = new char[nb_buffer];
shared_buffer->wrap(buf, nb_buffer); shared_buffer_->wrap(buf, nb_buffer);
++_srs_pps_objs_rbuf->sugar; ++_srs_pps_objs_rbuf->sugar;
} }
@ -899,7 +899,7 @@ char* SrsRtpPacket2::wrap(int size)
break; break;
} }
return shared_buffer->payload; return shared_buffer_->payload;
} }
char* SrsRtpPacket2::wrap(char* data, int size) char* SrsRtpPacket2::wrap(char* data, int size)
@ -914,12 +914,12 @@ char* SrsRtpPacket2::wrap(SrsSharedPtrMessage* msg)
// Generally, the wrap(msg) is used for RTMP to RTC, which is not generated by RTC, // 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 // so we do not recycle the msg. It's ok to directly free the msg, event the msg is
// allocated by object cache manager. // allocated by object cache manager.
srs_freep(shared_buffer); srs_freep(shared_buffer_);
// Copy from the new message. // Copy from the new message.
shared_buffer = msg->copy(); shared_buffer_ = msg->copy();
// If we wrap a message, the size of packet equals to the message size. // If we wrap a message, the size of packet equals to the message size.
actual_buffer_size_ = shared_buffer->size; actual_buffer_size_ = shared_buffer_->size;
return msg->payload; return msg->payload;
} }
@ -931,14 +931,14 @@ SrsRtpPacket2* SrsRtpPacket2::copy()
// We got packet from cache, the payload and message MUST be NULL, // We got packet from cache, the payload and message MUST be NULL,
// because we had clear it in recycle. // because we had clear it in recycle.
//srs_assert(!cp->payload_); //srs_assert(!cp->payload_);
//srs_assert(!cp->shared_buffer); //srs_assert(!cp->shared_buffer_);
cp->header = header; cp->header = header;
cp->payload_ = payload_? payload_->copy():NULL; cp->payload_ = payload_? payload_->copy():NULL;
cp->payload_type_ = payload_type_; cp->payload_type_ = payload_type_;
cp->nalu_type = nalu_type; cp->nalu_type = nalu_type;
cp->shared_buffer = shared_buffer->copy2(); cp->shared_buffer_ = shared_buffer_->copy2();
cp->actual_buffer_size_ = actual_buffer_size_; cp->actual_buffer_size_ = actual_buffer_size_;
cp->frame_type = frame_type; cp->frame_type = frame_type;

@ -290,7 +290,7 @@ private:
private: private:
// The original shared message, all RTP packets can refer to its data. // The original shared message, all RTP packets can refer to its data.
// Note that the size of shared msg, is not the packet size, it's a larger aligned buffer. // Note that the size of shared msg, is not the packet size, it's a larger aligned buffer.
SrsSharedPtrMessage* shared_buffer; SrsSharedPtrMessage* shared_buffer_;
// The size of original packet. // The size of original packet.
int actual_buffer_size_; int actual_buffer_size_;
// Helper fields. // Helper fields.

Loading…
Cancel
Save