Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions browser-mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ bool ExecuteNextBrowserTask();
void ExecuteTask(MessageTask task);
void ExecuteSyncTask(MessageTask task);
void DoCefMessageLoop(int ms);
void DoCefMessageLoopTimer(float ms);
void StopCefMessageLoopTimer();
void Process();
#if 0 // REMOVE_DUPLICATE
void QueueBrowserTask(CefRefPtr<CefBrowser> browser, BrowserFunc func);
Expand Down
35 changes: 31 additions & 4 deletions browser-mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

std::mutex browserTaskMutex;
std::deque<Task> browserTasks;
static NSTimer *cefTimer = nil;

bool ExecuteNextBrowserTask()
{
Expand All @@ -45,16 +46,26 @@ bool ExecuteNextBrowserTask()

void ExecuteTask(MessageTask task)
{
dispatch_async(dispatch_get_main_queue(), ^{
// Protect against exception if already on main thread
if ([NSThread isMainThread]) {
task();
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
task();
});
}
}

void ExecuteSyncTask(MessageTask task)
{
dispatch_sync(dispatch_get_main_queue(), ^{
// Protect against exception if already on main thread
if ([NSThread isMainThread]) {
task();
});
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
task();
});
}
}

void DoCefMessageLoop(int)
Expand All @@ -64,6 +75,22 @@ void DoCefMessageLoop(int)
});
}

void DoCefMessageLoopTimer(float ms)
{
cefTimer = [NSTimer
scheduledTimerWithTimeInterval:ms
repeats:YES
block:^(NSTimer *) {
CefDoMessageLoopWork();
}];
}

void StopCefMessageLoopTimer()
{
[cefTimer invalidate];
cefTimer = nil;
}

void Process()
{
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down
26 changes: 16 additions & 10 deletions obs-browser-plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ static obs_data_array_t *browser_source_get_messages(void *data)
messages = obs_data_array_create();
for (const auto &message : bs->messagesToApp) {
obs_data_t *msg_data = obs_data_create();
obs_data_set_string(msg_data, "message", message.c_str());
obs_data_set_string(msg_data, "message",
message.c_str());
obs_data_array_push_back(messages, msg_data);
obs_data_release(msg_data);
}
Expand Down Expand Up @@ -529,15 +530,9 @@ static void BrowserShutdown(void)
#ifndef ENABLE_BROWSER_QT_LOOP
static void BrowserManagerThread(obs_data_t *settings)
{
#ifdef __APPLE__
ExecuteSyncTask([&settings]() {
#endif
BrowserInit(settings);
CefRunMessageLoop();
BrowserShutdown();
#ifdef __APPLE__
});
#endif
BrowserInit(settings);
CefRunMessageLoop();
BrowserShutdown();
}
#endif

Expand All @@ -546,6 +541,12 @@ extern "C" EXPORT void obs_browser_initialize(obs_data_t *settings)
if (!os_atomic_set_bool(&manager_initialized, true)) {
#ifdef ENABLE_BROWSER_QT_LOOP
BrowserInit(settings);
#elif __APPLE__

ExecuteTask([&settings]() {
BrowserInit(settings);
DoCefMessageLoopTimer(0.01f); // Do not block the main queue so we avoid calling CefRunMessageLoop()
});
#else
auto binded_fn = bind(BrowserManagerThread, settings);
manager_thread = thread(binded_fn);
Expand Down Expand Up @@ -956,6 +957,11 @@ void obs_module_unload(void)
{
#ifdef USE_UI_LOOP
BrowserShutdown();
#elif __APPLE__
ExecuteSyncTask([]() {
StopCefMessageLoopTimer();
BrowserShutdown();
});
#else
if (manager_thread.joinable()) {
while (!QueueCEFTask([]() { CefQuitMessageLoop(); }))
Expand Down
Loading