RTC: Cache the RTP payload objects

pull/2252/head
winlin 4 years ago
parent d5b210abc8
commit bffe9c5f4b

@ -286,6 +286,8 @@ srs_error_t SrsRtcServer::initialize()
bool rtp_cache = _srs_config->get_rtc_server_rtp_cache();
_srs_rtp_cache->set_enabled(rtp_cache);
_srs_rtp_raw_cache->set_enabled(rtp_cache);
_srs_rtp_fua_cache->set_enabled(rtp_cache);
bool rtp_msg_cache = _srs_config->get_rtc_server_rtp_msg_cache();
_srs_rtp_msg_cache->set_enabled(rtp_msg_cache);

@ -825,7 +825,7 @@ srs_error_t SrsRtcFromRtmpBridger::package_opus(char* data, int size, SrsRtpPack
// TODO: FIXME: Why 960? Need Refactoring?
audio_timestamp += 960;
SrsRtpRawPayload* raw = new SrsRtpRawPayload();
SrsRtpRawPayload* raw = _srs_rtp_raw_cache->allocate();
pkt->payload = raw;
raw->payload = pkt->wrap(data, size);
@ -1107,7 +1107,7 @@ srs_error_t SrsRtcFromRtmpBridger::package_single_nalu(SrsSharedPtrMessage* msg,
pkt->header.set_sequence(video_sequence++);
pkt->header.set_timestamp(msg->timestamp * 90);
SrsRtpRawPayload* raw = new SrsRtpRawPayload();
SrsRtpRawPayload* raw = _srs_rtp_raw_cache->allocate();
pkt->payload = raw;
raw->payload = sample->bytes;
@ -1141,7 +1141,7 @@ srs_error_t SrsRtcFromRtmpBridger::package_fu_a(SrsSharedPtrMessage* msg, SrsSam
pkt->header.set_sequence(video_sequence++);
pkt->header.set_timestamp(msg->timestamp * 90);
SrsRtpFUAPayload2* fua = new SrsRtpFUAPayload2();
SrsRtpFUAPayload2* fua = _srs_rtp_fua_cache->allocate();
pkt->payload = fua;
fua->nri = (SrsAvcNaluType)header;
@ -1861,7 +1861,7 @@ void SrsRtcAudioRecvTrack::on_before_decode_payload(SrsRtpPacket2* pkt, SrsBuffe
return;
}
*ppayload = new SrsRtpRawPayload();
*ppayload = _srs_rtp_raw_cache->allocate();
}
srs_error_t SrsRtcAudioRecvTrack::on_rtp(SrsRtcStream* source, SrsRtpPacket2* pkt, bool nack_enabled)
@ -1921,9 +1921,9 @@ void SrsRtcVideoRecvTrack::on_before_decode_payload(SrsRtpPacket2* pkt, SrsBuffe
if (v == kStapA) {
*ppayload = new SrsRtpSTAPPayload();
} else if (v == kFuA) {
*ppayload = new SrsRtpFUAPayload2();
*ppayload = _srs_rtp_fua_cache->allocate();
} else {
*ppayload = new SrsRtpRawPayload();
*ppayload = _srs_rtp_raw_cache->allocate();
}
}

@ -839,10 +839,19 @@ bool SrsRtpPacket2::reset()
header.reset();
// We do not cache the payload, it will be created even
// we cache it, because it only stores pointers to the shared message,
// and it's different for each packet.
// Only recycle some common payloads.
SrsRtpRawPayload* raw_payload;
SrsRtpFUAPayload2* fua_payload;
if ((raw_payload = dynamic_cast<SrsRtpRawPayload*>(payload)) != NULL) {
_srs_rtp_raw_cache->recycle(raw_payload);
payload = NULL;
} else if ((fua_payload = dynamic_cast<SrsRtpFUAPayload2*>(payload)) != NULL) {
_srs_rtp_fua_cache->recycle(fua_payload);
payload = NULL;
} else {
srs_freep(payload);
}
// Recyle the real owner of message, no other reference object.
if (shared_msg && shared_msg->count() == 0) {
@ -1012,7 +1021,7 @@ srs_error_t SrsRtpPacket2::decode(SrsBuffer* buf)
// By default, we always use the RAW payload.
if (!payload) {
payload = new SrsRtpRawPayload();
payload = _srs_rtp_raw_cache->allocate();
}
if ((err = payload->decode(buf)) != srs_success) {
@ -1023,6 +1032,8 @@ srs_error_t SrsRtpPacket2::decode(SrsBuffer* buf)
}
SrsRtpObjectCacheManager<SrsRtpPacket2>* _srs_rtp_cache = new SrsRtpObjectCacheManager<SrsRtpPacket2>();
SrsRtpObjectCacheManager<SrsRtpRawPayload>* _srs_rtp_raw_cache = new SrsRtpObjectCacheManager<SrsRtpRawPayload>();
SrsRtpObjectCacheManager<SrsRtpFUAPayload2>* _srs_rtp_fua_cache = new SrsRtpObjectCacheManager<SrsRtpFUAPayload2>();
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>();
@ -1072,7 +1083,7 @@ srs_error_t SrsRtpRawPayload::decode(SrsBuffer* buf)
ISrsRtpPayloader* SrsRtpRawPayload::copy()
{
SrsRtpRawPayload* cp = new SrsRtpRawPayload();
SrsRtpRawPayload* cp = _srs_rtp_raw_cache->allocate();
cp->payload = payload;
cp->nn_payload = nn_payload;
@ -1561,7 +1572,7 @@ srs_error_t SrsRtpFUAPayload2::decode(SrsBuffer* buf)
ISrsRtpPayloader* SrsRtpFUAPayload2::copy()
{
SrsRtpFUAPayload2* cp = new SrsRtpFUAPayload2();
SrsRtpFUAPayload2* cp = _srs_rtp_fua_cache->allocate();
cp->nri = nri;
cp->start = start;

@ -394,12 +394,6 @@ public:
}
};
// For RTP packets cache.
extern SrsRtpObjectCacheManager<SrsRtpPacket2>* _srs_rtp_cache;
// For RTP packet shared messages cache.
extern SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache;
// Single payload data.
class SrsRtpRawPayload : public ISrsRtpPayloader
{
@ -411,6 +405,8 @@ public:
public:
SrsRtpRawPayload();
virtual ~SrsRtpRawPayload();
public:
bool reset() { return true; }
// interface ISrsRtpPayloader
public:
virtual uint64_t nb_bytes();
@ -509,6 +505,8 @@ public:
public:
SrsRtpFUAPayload2();
virtual ~SrsRtpFUAPayload2();
public:
bool reset() { return true; }
// interface ISrsRtpPayloader
public:
virtual uint64_t nb_bytes();
@ -517,4 +515,12 @@ 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 RTP packet shared messages cache.
extern SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache;
#endif

Loading…
Cancel
Save