Skip to content

Commit 7f42c7d

Browse files
Merge branch 'v0.22'
2 parents 59ce380 + 12a1949 commit 7f42c7d

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/impl/icetransport.cpp

+24-14
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,22 @@ void IceTransport::LogCallback(juice_log_level_t level, const char *message) {
392392

393393
#else // USE_NICE == 1
394394

395-
unique_ptr<GMainLoop, void (*)(GMainLoop *)> IceTransport::MainLoop(nullptr, nullptr);
396-
std::thread IceTransport::MainLoopThread;
395+
IceTransport::MainLoopWrapper *IceTransport::MainLoop = nullptr;
396+
397+
IceTransport::MainLoopWrapper::MainLoopWrapper()
398+
: mMainLoop(g_main_loop_new(nullptr, FALSE), g_main_loop_unref) {
399+
if (!mMainLoop)
400+
throw std::runtime_error("Failed to create the glib main loop");
401+
402+
mThread = std::thread(g_main_loop_run, mMainLoop.get());
403+
}
404+
405+
IceTransport::MainLoopWrapper::~MainLoopWrapper() {
406+
g_main_loop_quit(mMainLoop.get());
407+
mThread.join();
408+
}
409+
410+
GMainLoop *IceTransport::MainLoopWrapper::get() const { return mMainLoop.get(); }
397411

398412
void IceTransport::Init() {
399413
g_log_set_handler("libnice", G_LOG_LEVEL_MASK, LogCallback, nullptr);
@@ -402,17 +416,12 @@ void IceTransport::Init() {
402416
nice_debug_enable(false); // do not output STUN debug messages
403417
}
404418

405-
MainLoop = decltype(MainLoop)(g_main_loop_new(nullptr, FALSE), g_main_loop_unref);
406-
if (!MainLoop)
407-
throw std::runtime_error("Failed to create the main loop");
408-
409-
MainLoopThread = std::thread(g_main_loop_run, MainLoop.get());
419+
MainLoop = new MainLoopWrapper;
410420
}
411421

412422
void IceTransport::Cleanup() {
413-
g_main_loop_quit(MainLoop.get());
414-
MainLoopThread.join();
415-
MainLoop.reset();
423+
delete MainLoop;
424+
MainLoop = nullptr;
416425
}
417426

418427
static void closeNiceAgentCallback(GObject *niceAgent, GAsyncResult *, gpointer) {
@@ -447,7 +456,7 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi
447456
// Create agent
448457
mNiceAgent = decltype(mNiceAgent)(
449458
nice_agent_new_full(
450-
g_main_loop_get_context(MainLoop.get()),
459+
g_main_loop_get_context(MainLoop->get()),
451460
NICE_COMPATIBILITY_RFC5245, // RFC 5245 was obsoleted by RFC 8445 but this should be OK
452461
flags),
453462
closeNiceAgent);
@@ -580,12 +589,13 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi
580589
nice_agent_set_port_range(mNiceAgent.get(), mStreamId, 1, config.portRangeBegin,
581590
config.portRangeEnd);
582591

583-
nice_agent_attach_recv(mNiceAgent.get(), mStreamId, 1, g_main_loop_get_context(MainLoop.get()),
592+
nice_agent_attach_recv(mNiceAgent.get(), mStreamId, 1, g_main_loop_get_context(MainLoop->get()),
584593
RecvCallback, this);
585594
}
586595

587596
void IceTransport::setIceAttributes([[maybe_unused]] string uFrag, [[maybe_unused]] string pwd) {
588-
PLOG_WARNING << "Setting custom ICE attributes is not supported with libnice, please use libjuice";
597+
PLOG_WARNING
598+
<< "Setting custom ICE attributes is not supported with libnice, please use libjuice";
589599
}
590600

591601
void IceTransport::addIceServer(IceServer server) {
@@ -647,7 +657,7 @@ void IceTransport::addIceServer(IceServer server) {
647657

648658
IceTransport::~IceTransport() {
649659
PLOG_DEBUG << "Destroying ICE transport";
650-
nice_agent_attach_recv(mNiceAgent.get(), mStreamId, 1, g_main_loop_get_context(MainLoop.get()),
660+
nice_agent_attach_recv(mNiceAgent.get(), mStreamId, 1, g_main_loop_get_context(MainLoop->get()),
651661
NULL, NULL);
652662
nice_agent_remove_stream(mNiceAgent.get(), mStreamId);
653663
mNiceAgent.reset();

src/impl/icetransport.hpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,17 @@ class IceTransport : public Transport {
9090
static void RecvCallback(juice_agent_t *agent, const char *data, size_t size, void *user_ptr);
9191
static void LogCallback(juice_log_level_t level, const char *message);
9292
#else
93-
static unique_ptr<GMainLoop, void (*)(GMainLoop *)> MainLoop;
94-
static std::thread MainLoopThread;
93+
class MainLoopWrapper {
94+
public:
95+
MainLoopWrapper();
96+
~MainLoopWrapper();
97+
GMainLoop *get() const;
98+
99+
private:
100+
unique_ptr<GMainLoop, void (*)(GMainLoop *)> mMainLoop;
101+
std::thread mThread;
102+
};
103+
static MainLoopWrapper *MainLoop;
95104

96105
unique_ptr<NiceAgent, void (*)(NiceAgent *)> mNiceAgent;
97106
uint32_t mStreamId = 0;

0 commit comments

Comments
 (0)