srtp: Use shared_ptr to manage srtp and srtcp memory

This commit is contained in:
Joni Räsänen 2022-03-03 10:44:04 +02:00
parent 32839127f4
commit 97317fed22
4 changed files with 16 additions and 18 deletions

View File

@ -308,15 +308,15 @@ namespace uvgrtp {
/* free all allocated resources */
rtp_error_t free_resources(rtp_error_t ret);
rtp_error_t init_srtp_with_zrtp(int flags, int type, uvgrtp::base_srtp* srtp,
rtp_error_t init_srtp_with_zrtp(int flags, int type, std::shared_ptr<uvgrtp::base_srtp> srtp,
std::shared_ptr<uvgrtp::zrtp> zrtp);
uint32_t key_;
uvgrtp::srtp *srtp_;
uvgrtp::srtcp *srtcp_;
std::shared_ptr<uvgrtp::srtp> srtp_;
std::shared_ptr<uvgrtp::srtcp> srtcp_;
std::shared_ptr<uvgrtp::socket> socket_;
std::shared_ptr<uvgrtp::rtp> rtp_;
std::shared_ptr<uvgrtp::rtp> rtp_;
uvgrtp::rtcp *rtcp_;
sockaddr_in addr_out_;

View File

@ -77,7 +77,7 @@ namespace uvgrtp {
public:
/// \cond DO_NOT_DOCUMENT
rtcp(std::shared_ptr<uvgrtp::rtp> rtp, int flags);
rtcp(std::shared_ptr<uvgrtp::rtp> rtp, uvgrtp::srtcp *srtcp, int flags);
rtcp(std::shared_ptr<uvgrtp::rtp> rtp, std::shared_ptr<uvgrtp::srtcp> srtcp, int flags);
~rtcp();
/* start the RTCP runner thread
@ -368,7 +368,7 @@ namespace uvgrtp {
std::shared_ptr<uvgrtp::rtp> rtp_;
/* Secure RTCP context */
uvgrtp::srtcp *srtcp_;
std::shared_ptr<uvgrtp::srtcp> srtcp_;
/* RTP context flags */
int flags_;

View File

@ -194,12 +194,10 @@ rtp_error_t uvgrtp::media_stream::free_resources(rtp_error_t ret)
}
if (srtp_)
{
delete srtp_;
srtp_ = nullptr;
}
if (srtcp_)
{
delete srtcp_;
srtcp_ = nullptr;
}
if (reception_flow_)
@ -279,24 +277,24 @@ rtp_error_t uvgrtp::media_stream::init(std::shared_ptr<uvgrtp::zrtp> zrtp)
return free_resources(ret);
}
srtp_ = new uvgrtp::srtp(ctx_config_.flags);
srtp_ = std::shared_ptr<uvgrtp::srtp>(new uvgrtp::srtp(ctx_config_.flags));
if ((ret = init_srtp_with_zrtp(ctx_config_.flags, SRTP, srtp_, zrtp)) != RTP_OK)
return free_resources(ret);
srtcp_ = new uvgrtp::srtcp();
srtcp_ = std::shared_ptr<uvgrtp::srtcp> (new uvgrtp::srtcp());
if ((ret = init_srtp_with_zrtp(ctx_config_.flags, SRTCP, srtcp_, zrtp)) != RTP_OK)
return free_resources(ret);
rtcp_ = new uvgrtp::rtcp(rtp_, srtcp_, ctx_config_.flags);
socket_->install_handler(rtcp_, rtcp_->send_packet_handler_vec);
socket_->install_handler(srtp_, srtp_->send_packet_handler);
socket_->install_handler(srtp_.get(), srtp_->send_packet_handler);
rtp_handler_key_ = reception_flow_->install_handler(rtp_->packet_handler);
zrtp_handler_key_ = reception_flow_->install_handler(zrtp->packet_handler);
reception_flow_->install_aux_handler(rtp_handler_key_, rtcp_, rtcp_->recv_packet_handler, nullptr);
reception_flow_->install_aux_handler(rtp_handler_key_, srtp_, srtp_->recv_packet_handler, nullptr);
reception_flow_->install_aux_handler(rtp_handler_key_, srtp_.get(), srtp_->recv_packet_handler, nullptr);
if (create_media(fmt_) != RTP_OK)
return free_resources(RTP_MEMORY_ERROR);
@ -338,7 +336,7 @@ rtp_error_t uvgrtp::media_stream::add_srtp_ctx(uint8_t *key, uint8_t *salt)
rtp_ = std::shared_ptr<uvgrtp::rtp> (new uvgrtp::rtp(fmt_));
srtp_ = new uvgrtp::srtp(ctx_config_.flags);
srtp_ = std::shared_ptr<uvgrtp::srtp> (new uvgrtp::srtp(ctx_config_.flags));
// why are they local and remote key/salt the same?
if ((ret = srtp_->init(SRTP, ctx_config_.flags, key, key, salt, salt)) != RTP_OK) {
@ -346,7 +344,7 @@ rtp_error_t uvgrtp::media_stream::add_srtp_ctx(uint8_t *key, uint8_t *salt)
return free_resources(ret);
}
srtcp_ = new uvgrtp::srtcp();
srtcp_ = std::shared_ptr<uvgrtp::srtcp> (new uvgrtp::srtcp());
if ((ret = srtcp_->init(SRTCP, ctx_config_.flags, key, key, salt, salt)) != RTP_OK) {
LOG_WARN("Failed to initialize SRTCP for media stream!");
@ -356,12 +354,12 @@ rtp_error_t uvgrtp::media_stream::add_srtp_ctx(uint8_t *key, uint8_t *salt)
rtcp_ = new uvgrtp::rtcp(rtp_, srtcp_, ctx_config_.flags);
socket_->install_handler(rtcp_, rtcp_->send_packet_handler_vec);
socket_->install_handler(srtp_, srtp_->send_packet_handler);
socket_->install_handler(srtp_.get(), srtp_->send_packet_handler);
rtp_handler_key_ = reception_flow_->install_handler(rtp_->packet_handler);
reception_flow_->install_aux_handler(rtp_handler_key_, rtcp_, rtcp_->recv_packet_handler, nullptr);
reception_flow_->install_aux_handler(rtp_handler_key_, srtp_, srtp_->recv_packet_handler, nullptr);
reception_flow_->install_aux_handler(rtp_handler_key_, srtp_.get(), srtp_->recv_packet_handler, nullptr);
if (create_media(fmt_) != RTP_OK)
return free_resources(RTP_MEMORY_ERROR);
@ -614,7 +612,7 @@ uvgrtp::rtcp *uvgrtp::media_stream::get_rtcp()
return rtcp_;
}
rtp_error_t uvgrtp::media_stream::init_srtp_with_zrtp(int flags, int type, uvgrtp::base_srtp* srtp,
rtp_error_t uvgrtp::media_stream::init_srtp_with_zrtp(int flags, int type, std::shared_ptr<uvgrtp::base_srtp> srtp,
std::shared_ptr<uvgrtp::zrtp> zrtp)
{
size_t key_size = srtp->get_key_size(flags);

View File

@ -64,7 +64,7 @@ uvgrtp::rtcp::rtcp(std::shared_ptr<uvgrtp::rtp> rtp, int flags):
zero_stats(&our_stats);
}
uvgrtp::rtcp::rtcp(std::shared_ptr<uvgrtp::rtp> rtp, uvgrtp::srtcp *srtcp, int flags):
uvgrtp::rtcp::rtcp(std::shared_ptr<uvgrtp::rtp> rtp, std::shared_ptr<uvgrtp::srtcp> srtcp, int flags):
rtcp(rtp, flags)
{
srtcp_ = srtcp;