diff --git a/daemon/src/ice_transport.cpp b/daemon/src/ice_transport.cpp index f075dff61bafbb07a4d5d6576c6149b1df3d02e5..c9ba5a3d671ca9bd4a6d1bfdd8982965cc730a50 100644 --- a/daemon/src/ice_transport.cpp +++ b/daemon/src/ice_transport.cpp @@ -45,11 +45,6 @@ namespace sfl { -// GLOBALS (blame PJSIP) - -static pj_caching_pool g_cp_; -static pj_pool_t* g_pool_ = nullptr; - static void register_thread() { @@ -101,19 +96,25 @@ IceTransport::cb_on_ice_complete(pj_ice_strans* ice_st, IceTransport::IceTransport(const char* name, int component_count, IceTransportCompleteCb on_initdone_cb, IceTransportCompleteCb on_negodone_cb) - : on_initdone_cb_(on_initdone_cb) + : pool_(nullptr, pj_pool_release) + , on_initdone_cb_(on_initdone_cb) , on_negodone_cb_(on_negodone_cb) , component_count_(component_count) , compIO_(component_count) { auto& iceTransportFactory = Manager::instance().getIceTransportFactory(); - pj_ice_strans_cb icecb; + pool_.reset(pj_pool_create(iceTransportFactory.getPoolFactory(), + "IceTransport.pool", 512, 512, NULL)); + if (not pool_) + throw std::runtime_error("pj_pool_create() failed"); + + pj_ice_strans_cb icecb; pj_bzero(&icecb, sizeof(icecb)); icecb.on_rx_data = cb_on_rx_data; icecb.on_ice_complete = cb_on_ice_complete; - pj_ice_strans *icest = nullptr; + pj_ice_strans* icest = nullptr; pj_status_t status = pj_ice_strans_create(name, iceTransportFactory.getIceCfg(), component_count, this, &icecb, @@ -454,7 +455,8 @@ IceTransport::getCandidateFromSDP(const std::string& line, IceCandidate& cand) } pj_sockaddr_set_port(&cand.addr, (pj_uint16_t)port); - pj_strdup2(g_pool_, &cand.foundation, foundation); + pj_strdup2(pool_.get(), &cand.foundation, foundation); + return true; } @@ -532,27 +534,30 @@ IceTransport::waitForData(int comp_id, unsigned int timeout) return io.queue.front().datalen; } -IceTransportFactory::IceTransportFactory() : - ice_cfg_(), thread_(nullptr, pj_thread_destroy) +IceTransportFactory::IceTransportFactory() + : cp_() + , pool_(nullptr, pj_pool_release) + , thread_(nullptr, pj_thread_destroy) + , ice_cfg_() { - pj_caching_pool_init(&g_cp_, NULL, 0); - g_pool_ = pj_pool_create(&g_cp_.factory, "icetransportpool", - 512, 512, NULL); - if (not g_pool_) + pj_caching_pool_init(&cp_, NULL, 0); + pool_.reset(pj_pool_create(&cp_.factory, "IceTransportFactory.pool", + 512, 512, NULL)); + if (not pool_) throw std::runtime_error("pj_pool_create() failed"); pj_ice_strans_cfg_default(&ice_cfg_); - ice_cfg_.stun_cfg.pf = &g_cp_.factory; + ice_cfg_.stun_cfg.pf = &cp_.factory; - TRY( pj_timer_heap_create(g_pool_, 100, &ice_cfg_.stun_cfg.timer_heap) ); - TRY( pj_ioqueue_create(g_pool_, 16, &ice_cfg_.stun_cfg.ioqueue) ); + TRY( pj_timer_heap_create(pool_.get(), 100, &ice_cfg_.stun_cfg.timer_heap) ); + TRY( pj_ioqueue_create(pool_.get(), 16, &ice_cfg_.stun_cfg.ioqueue) ); pj_thread_t* thread = nullptr; const auto& thread_work = [](void* udata) { register_thread(); return static_cast<IceTransportFactory*>(udata)->processThread(); }; - TRY( pj_thread_create(g_pool_, "icetransportpool", + TRY( pj_thread_create(pool_.get(), "icetransportpool", thread_work, this, 0, 0, &thread) ); thread_.reset(thread); @@ -582,8 +587,8 @@ IceTransportFactory::~IceTransportFactory() if (ice_cfg_.stun_cfg.timer_heap) pj_timer_heap_destroy(ice_cfg_.stun_cfg.timer_heap); - pj_pool_release(g_pool_); - pj_caching_pool_destroy(&g_cp_); + pool_.reset(); + pj_caching_pool_destroy(&cp_); } int diff --git a/daemon/src/ice_transport.h b/daemon/src/ice_transport.h index d782f2cc62747bba4a7399a882935792132ae14c..61d4ef3ec62a72e093baad99cb9338ef310d82b3 100644 --- a/daemon/src/ice_transport.h +++ b/daemon/src/ice_transport.h @@ -176,6 +176,7 @@ class IceTransport { void getDefaultCanditates(); + std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&> pool_; IceTransportCompleteCb on_initdone_cb_; IceTransportCompleteCb on_negodone_cb_; std::unique_ptr<pj_ice_strans, IceSTransDeleter> icest_; @@ -211,13 +212,19 @@ class IceTransportFactory { int processThread(); + /** + * PJSIP specifics + */ const pj_ice_strans_cfg* getIceCfg() const { return &ice_cfg_; } + pj_pool_factory* getPoolFactory() { return &cp_.factory; } private: int handleEvents(unsigned max_msec, unsigned *p_count); + pj_caching_pool cp_; + std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&> pool_; + std::unique_ptr<pj_thread_t, decltype(pj_thread_destroy)&> thread_; pj_ice_strans_cfg ice_cfg_; - std::unique_ptr<pj_thread_t, decltype(*pj_thread_destroy)> thread_; pj_bool_t thread_quit_flag_ {PJ_FALSE}; };