Implement free_resources()
This commit is contained in:
parent
238cf77f64
commit
2dadd7b937
|
@ -293,6 +293,9 @@ namespace uvgrtp {
|
|||
/* Create the media object for the stream */
|
||||
rtp_error_t create_media(rtp_format_t fmt);
|
||||
|
||||
/* free all allocated resources */
|
||||
rtp_error_t free_resources(rtp_error_t ret);
|
||||
|
||||
uint32_t key_;
|
||||
|
||||
uvgrtp::srtp *srtp_;
|
||||
|
@ -324,7 +327,6 @@ namespace uvgrtp {
|
|||
|
||||
/* RTP packet dispatcher for the receiver */
|
||||
uvgrtp::pkt_dispatcher *pkt_dispatcher_;
|
||||
std::thread *dispatcher_thread_;
|
||||
|
||||
/* Media object associated with this media stream. */
|
||||
uvgrtp::formats::media *media_;
|
||||
|
|
|
@ -22,7 +22,6 @@ uvgrtp::media_stream::media_stream(std::string addr, int src_port, int dst_port,
|
|||
initialized_(false),
|
||||
rtp_handler_key_(0),
|
||||
pkt_dispatcher_(nullptr),
|
||||
dispatcher_thread_(nullptr),
|
||||
media_(nullptr),
|
||||
holepuncher_(nullptr)
|
||||
{
|
||||
|
@ -57,15 +56,7 @@ uvgrtp::media_stream::~media_stream()
|
|||
if (ctx_config_.flags & RCE_HOLEPUNCH_KEEPALIVE)
|
||||
holepuncher_->stop();
|
||||
|
||||
delete socket_;
|
||||
delete rtcp_;
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete srtcp_;
|
||||
delete pkt_dispatcher_;
|
||||
delete dispatcher_thread_;
|
||||
delete holepuncher_;
|
||||
delete media_;
|
||||
(void)free_resources(RTP_OK);
|
||||
}
|
||||
|
||||
rtp_error_t uvgrtp::media_stream::init_connection()
|
||||
|
@ -166,47 +157,46 @@ rtp_error_t uvgrtp::media_stream::create_media(rtp_format_t fmt)
|
|||
}
|
||||
}
|
||||
|
||||
rtp_error_t uvgrtp::media_stream::free_resources(rtp_error_t ret)
|
||||
{
|
||||
delete socket_;
|
||||
delete rtcp_;
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete srtcp_;
|
||||
delete pkt_dispatcher_;
|
||||
delete holepuncher_;
|
||||
delete media_;
|
||||
return ret;
|
||||
}
|
||||
|
||||
rtp_error_t uvgrtp::media_stream::init()
|
||||
{
|
||||
if (init_connection() != RTP_OK) {
|
||||
log_platform_error("Failed to initialize the underlying socket");
|
||||
return RTP_GENERIC_ERROR;
|
||||
LOG_ERROR("Failed to initialize the underlying socket");
|
||||
return free_resources(RTP_GENERIC_ERROR);
|
||||
}
|
||||
|
||||
if (!(pkt_dispatcher_ = new uvgrtp::pkt_dispatcher()))
|
||||
return RTP_MEMORY_ERROR;
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if (!(rtp_ = new uvgrtp::rtp(fmt_))) {
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(rtp_ = new uvgrtp::rtp(fmt_)))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if (!(rtcp_ = new uvgrtp::rtcp(rtp_, ctx_config_.flags))) {
|
||||
delete rtp_;
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(rtcp_ = new uvgrtp::rtcp(rtp_, ctx_config_.flags)))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
socket_->install_handler(rtcp_, rtcp_->send_packet_handler_vec);
|
||||
|
||||
rtp_handler_key_ = pkt_dispatcher_->install_handler(rtp_->packet_handler);
|
||||
pkt_dispatcher_->install_aux_handler(rtp_handler_key_, rtcp_, rtcp_->recv_packet_handler, nullptr);
|
||||
|
||||
if (create_media(fmt_) != RTP_OK) {
|
||||
delete rtp_;
|
||||
delete rtcp_;
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (create_media(fmt_) != RTP_OK)
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if (ctx_config_.flags & RCE_HOLEPUNCH_KEEPALIVE) {
|
||||
if (!(holepuncher_ = new uvgrtp::holepuncher(socket_))) {
|
||||
delete rtp_;
|
||||
delete rtcp_;
|
||||
delete pkt_dispatcher_;
|
||||
delete media_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(holepuncher_ = new uvgrtp::holepuncher(socket_)))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
holepuncher_->start();
|
||||
}
|
||||
|
||||
|
@ -229,57 +219,34 @@ rtp_error_t uvgrtp::media_stream::init(uvgrtp::zrtp *zrtp)
|
|||
}
|
||||
|
||||
if (!(pkt_dispatcher_ = new uvgrtp::pkt_dispatcher()))
|
||||
return RTP_MEMORY_ERROR;
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if (!(rtp_ = new uvgrtp::rtp(fmt_))) {
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(rtp_ = new uvgrtp::rtp(fmt_)))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if ((ret = zrtp->init(rtp_->get_ssrc(), socket_, addr_out_)) != RTP_OK) {
|
||||
LOG_WARN("Failed to initialize ZRTP for media stream!");
|
||||
delete rtp_;
|
||||
delete pkt_dispatcher_;
|
||||
return ret;
|
||||
return free_resources(ret);
|
||||
}
|
||||
|
||||
if (!(srtp_ = new uvgrtp::srtp())) {
|
||||
delete rtp_;
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(srtp_ = new uvgrtp::srtp()))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if ((ret = srtp_->init_zrtp(SRTP, ctx_config_.flags, rtp_, zrtp)) != RTP_OK) {
|
||||
LOG_WARN("Failed to initialize SRTP for media stream!");
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete pkt_dispatcher_;
|
||||
return ret;
|
||||
return free_resources(ret);
|
||||
}
|
||||
|
||||
if (!(srtcp_ = new uvgrtp::srtcp())) {
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(srtcp_ = new uvgrtp::srtcp()))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if ((ret = srtcp_->init_zrtp(SRTCP, ctx_config_.flags, rtp_, zrtp)) != RTP_OK) {
|
||||
LOG_ERROR("Failed to initialize SRTCP for media stream!");
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete srtcp_;
|
||||
delete pkt_dispatcher_;
|
||||
return ret;
|
||||
return free_resources(ret);
|
||||
}
|
||||
|
||||
if (!(rtcp_ = new uvgrtp::rtcp(rtp_, srtcp_, ctx_config_.flags))) {
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete srtcp_;
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(rtcp_ = new uvgrtp::rtcp(rtp_, srtcp_, ctx_config_.flags)))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
socket_->install_handler(rtcp_, rtcp_->send_packet_handler_vec);
|
||||
socket_->install_handler(srtp_, srtp_->send_packet_handler);
|
||||
|
@ -290,25 +257,12 @@ rtp_error_t uvgrtp::media_stream::init(uvgrtp::zrtp *zrtp)
|
|||
pkt_dispatcher_->install_aux_handler(rtp_handler_key_, rtcp_, rtcp_->recv_packet_handler, nullptr);
|
||||
pkt_dispatcher_->install_aux_handler(rtp_handler_key_, srtp_, srtp_->recv_packet_handler, nullptr);
|
||||
|
||||
if (create_media(fmt_) != RTP_OK) {
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete srtcp_;
|
||||
delete rtcp_;
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (create_media(fmt_) != RTP_OK)
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if (ctx_config_.flags & RCE_HOLEPUNCH_KEEPALIVE) {
|
||||
if (!(holepuncher_ = new uvgrtp::holepuncher(socket_))) {
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete srtcp_;
|
||||
delete rtcp_;
|
||||
delete pkt_dispatcher_;
|
||||
delete media_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(holepuncher_ = new uvgrtp::holepuncher(socket_)))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
holepuncher_->start();
|
||||
}
|
||||
|
||||
|
@ -333,51 +287,37 @@ rtp_error_t uvgrtp::media_stream::add_srtp_ctx(uint8_t *key, uint8_t *salt)
|
|||
rtp_error_t ret = RTP_OK;
|
||||
|
||||
if (init_connection() != RTP_OK) {
|
||||
log_platform_error("Failed to initialize the underlying socket");
|
||||
return RTP_GENERIC_ERROR;
|
||||
LOG_ERROR("Failed to initialize the underlying socket");
|
||||
return free_resources(RTP_GENERIC_ERROR);
|
||||
}
|
||||
|
||||
if ((flags_ & srtp_flags) != srtp_flags)
|
||||
return RTP_NOT_SUPPORTED;
|
||||
return free_resources(RTP_NOT_SUPPORTED);
|
||||
|
||||
if (!(pkt_dispatcher_ = new uvgrtp::pkt_dispatcher()))
|
||||
return RTP_MEMORY_ERROR;
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if (!(rtp_ = new uvgrtp::rtp(fmt_))) {
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(rtp_ = new uvgrtp::rtp(fmt_)))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if (!(srtp_ = new uvgrtp::srtp())) {
|
||||
delete rtp_;
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(srtp_ = new uvgrtp::srtp()))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if ((ret = srtp_->init_user(SRTP, ctx_config_.flags, key, salt)) != RTP_OK) {
|
||||
LOG_WARN("Failed to initialize SRTP for media stream!");
|
||||
return ret;
|
||||
return free_resources(ret);
|
||||
}
|
||||
|
||||
if (!(srtcp_ = new uvgrtp::srtcp())) {
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(srtcp_ = new uvgrtp::srtcp()))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if ((ret = srtcp_->init_user(SRTCP, ctx_config_.flags, key, salt)) != RTP_OK) {
|
||||
LOG_WARN("Failed to initialize SRTCP for media stream!");
|
||||
return ret;
|
||||
return free_resources(ret);
|
||||
}
|
||||
|
||||
if (!(rtcp_ = new uvgrtp::rtcp(rtp_, srtcp_, ctx_config_.flags))) {
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete srtcp_;
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(rtcp_ = new uvgrtp::rtcp(rtp_, srtcp_, ctx_config_.flags)))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
socket_->install_handler(rtcp_, rtcp_->send_packet_handler_vec);
|
||||
socket_->install_handler(srtp_, srtp_->send_packet_handler);
|
||||
|
@ -387,25 +327,12 @@ rtp_error_t uvgrtp::media_stream::add_srtp_ctx(uint8_t *key, uint8_t *salt)
|
|||
pkt_dispatcher_->install_aux_handler(rtp_handler_key_, rtcp_, rtcp_->recv_packet_handler, nullptr);
|
||||
pkt_dispatcher_->install_aux_handler(rtp_handler_key_, srtp_, srtp_->recv_packet_handler, nullptr);
|
||||
|
||||
if (create_media(fmt_) != RTP_OK) {
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete srtcp_;
|
||||
delete rtcp_;
|
||||
delete pkt_dispatcher_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (create_media(fmt_) != RTP_OK)
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
|
||||
if (ctx_config_.flags & RCE_HOLEPUNCH_KEEPALIVE) {
|
||||
if (!(holepuncher_ = new uvgrtp::holepuncher(socket_))) {
|
||||
delete rtp_;
|
||||
delete srtp_;
|
||||
delete srtcp_;
|
||||
delete rtcp_;
|
||||
delete pkt_dispatcher_;
|
||||
delete media_;
|
||||
return RTP_MEMORY_ERROR;
|
||||
}
|
||||
if (!(holepuncher_ = new uvgrtp::holepuncher(socket_)))
|
||||
return free_resources(RTP_MEMORY_ERROR);
|
||||
holepuncher_->start();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue