Skip to content

Commit ea61c23

Browse files
committed
create browser source on main thread
* create 1st browser_source on main thread to satisfy requirement CEFInitialize() must be called on main thread (obs-browser plugin -> CEF)
1 parent e441565 commit ea61c23

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

obs-studio-server/source/osn-input.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,22 @@ void osn::Input::Create(void *data, const int64_t id, const std::vector<ipc::val
111111
break;
112112
}
113113

114+
#if defined(__APPLE__)
115+
static bool hasInitializedBrowserSources = false;
116+
obs_source_t *source = nullptr;
117+
118+
if (!hasInitializedBrowserSources && sourceId == "browser_source") {
119+
// CEFInitialize must be invoked on main thread (first time a browser source is created)
120+
hasInitializedBrowserSources = true;
121+
122+
g_util_osx->runOnMainThreadSync(
123+
[&source, &sourceId, &name, &settings, &hotkeys]() { source = obs_source_create(sourceId.c_str(), name.c_str(), settings, hotkeys); });
124+
} else {
125+
source = obs_source_create(sourceId.c_str(), name.c_str(), settings, hotkeys);
126+
}
127+
#else
114128
obs_source_t *source = obs_source_create(sourceId.c_str(), name.c_str(), settings, hotkeys);
129+
#endif
115130
obs_data_release(hotkeys);
116131
obs_data_release(settings);
117132

@@ -660,4 +675,4 @@ void osn::Input::GetMediaState(void *data, const int64_t id, const std::vector<i
660675
rval.push_back(ipc::value((uint64_t)ErrorCode::Ok));
661676
rval.push_back(ipc::value(obs_source_media_get_state(input)));
662677
AUTO_DEBUG;
663-
}
678+
}

obs-studio-server/source/util-osx-impl.mm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,11 @@ @implementation UtilImplObj
170170
NSString *workindDirPath = [[NSProcessInfo processInfo] environment][@"PWD"];
171171
return std::string([workindDirPath UTF8String]);
172172
}
173+
174+
void UtilObjCInt::runOnMainThreadSync(std::function<void()> func)
175+
{
176+
dispatch_sync(dispatch_get_main_queue(), ^{
177+
func();
178+
});
179+
}
173180
@end

obs-studio-server/source/util-osx-int.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class UtilObjCInt {
3939
std::string getWorkingDirectory(void);
4040
void wait_terminate(void);
4141

42+
// Runs a function on the main thread and waits for the function to finish before proceeding.
43+
void runOnMainThreadSync(std::function<void()> func);
44+
4245
private:
4346
void *self;
4447
std::atomic<bool> appRunning;

obs-studio-server/source/util-osx.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ std::string UtilInt::getUserDataPath(void)
7272
std::string UtilInt::getWorkingDirectory(void)
7373
{
7474
return _impl->getWorkingDirectory();
75-
}
75+
}
76+
77+
void UtilInt::runOnMainThreadSync(std::function<void()> func)
78+
{
79+
_impl->runOnMainThreadSync(func);
80+
}

obs-studio-server/source/util-osx.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef __UTIL_CLASS_H__
2020
#define __UTIL_CLASS_H__
2121

22+
#include <functional>
2223
#include <string>
2324
#include <vector>
2425

@@ -38,6 +39,8 @@ class UtilInt {
3839
std::vector<std::pair<uint32_t, uint32_t>> getAvailableScreenResolutions(void);
3940
std::string getUserDataPath(void);
4041
std::string getWorkingDirectory(void);
42+
// Runs a function on the main thread and waits for the function to finish before proceeding.
43+
void runOnMainThreadSync(std::function<void()> func);
4144

4245
private:
4346
UtilObjCInt *_impl;

0 commit comments

Comments
 (0)