Skip to content

Commit ebcb12f

Browse files
authored
Merge pull request #37 from DDRBoxman/scenecallback
Scene change js callback
2 parents cd0f8c1 + ba4f0a8 commit ebcb12f

14 files changed

+167
-24
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ project(cef-isolation)
55
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}")
66
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
77

8+
include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/UI/obs-frontend-api")
9+
810
include(BSUtils)
911

1012
find_package(LibObs REQUIRED)
@@ -108,7 +110,8 @@ else (APPLE)
108110
endif(APPLE)
109111

110112
set(obs-browser_LIBRARIES
111-
libobs)
113+
libobs
114+
obs-frontend-api)
112115

113116
if (APPLE)
114117
list(APPEND obs-browser_LIBRARIES

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,19 @@ window.obsstudio.pluginVersion
1919
*
2020
* @param {bool} visiblity - True -> visible, False -> hidden
2121
*/
22-
window.obsstudio.onVisibilityChange(visiblity) {
22+
window.obsstudio.onVisibilityChange = function(visiblity) {
23+
24+
};
25+
```
26+
27+
### Register for scene change callbacks
28+
```
29+
/**
30+
* onSceneChange gets callbacks when the scene is changed
31+
*
32+
* @param {string} sceneName - The name of the scene that was changed to.
33+
*/
34+
window.obsstudio.onSceneChange = function(sceneName) {
2335
2436
};
2537
```

cef-isolation/cef-isolated-client.mm

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ - (void)sendEvent:(const int)browserIdentifier
133133
}
134134
}
135135

136+
- (void)sendEventToAllBrowsers:(event_block_t)event
137+
{
138+
for (auto& x: map) {
139+
SharedBrowserHandle browserHandle = x.second;
140+
sync_on_cef_ui(^{
141+
event(browserHandle);
142+
});
143+
}
144+
}
145+
136146
- (void)sendMouseClick:(const int)browserIdentifier
137147
event:(ObsMouseEventBridge *)event type:(const int)type
138148
mouseUp:(BOOL)mouseUp clickCount:(const int)clickCount
@@ -301,6 +311,19 @@ - (void)executeVisiblityJSCallback:(const int)browserIdentifier visible:(BOOL)vi
301311
}];
302312
}
303313

314+
- (void)executeSceneChangeJSCallback:(const char *)name
315+
{
316+
[self sendEventToAllBrowsers:^(SharedBrowserHandle browserHandle)
317+
{
318+
CefRefPtr<CefBrowser> browser = browserHandle->GetBrowser();
319+
320+
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("SceneChange");
321+
CefRefPtr<CefListValue> args = msg->GetArgumentList();
322+
args->SetString(0, name);
323+
browser->SendProcessMessage(PID_RENDERER, msg);
324+
}];
325+
}
326+
304327
- (void)destroyBrowser:(const int)browserIdentifier {
305328
if (map.count(browserIdentifier) == 1) {
306329
std::shared_ptr<BrowserHandle> browserHandle =
@@ -312,4 +335,4 @@ - (void)destroyBrowser:(const int)browserIdentifier {
312335
}
313336
}
314337

315-
@end
338+
@end

obs-browser/apple/browser-manager-mac.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class BrowserManager::Impl {
5252

5353
void ExecuteVisiblityJSCallback(int browserIdentifier, bool visible);
5454

55+
void ExecuteSceneChangeJSCallback(const char *name);
56+
5557
private:
5658
std::unique_ptr<CEFIsolationServiceManager> cefIsolationServiceManager;
5759
};

obs-browser/apple/browser-manager-mac.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
pimpl->ExecuteVisiblityJSCallback(browserIdentifier, visible);
8787
}
8888

89+
void BrowserManager::ExecuteSceneChangeJSCallback(const char *name)
90+
{
91+
pimpl->ExecuteSceneChangeJSCallback(name);
92+
}
93+
8994
int BrowserManager::CreateBrowser(const BrowserSettings &browserSettings,
9095
const std::shared_ptr<BrowserListener> &browserListener)
9196
{
@@ -171,6 +176,11 @@
171176
cefIsolationServiceManager->ExecuteVisiblityJSCallback(browserIdentifier, visible);
172177
}
173178

179+
void BrowserManager::Impl::ExecuteSceneChangeJSCallback(const char *name)
180+
{
181+
cefIsolationServiceManager->ExecuteSceneChangeJSCallback(name);
182+
}
183+
174184
static BrowserManager *instance;
175185

176186
BrowserManager *BrowserManager::Instance()

obs-browser/apple/cef-isolation-service-manager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class CEFIsolationServiceManager {
5858

5959
void ExecuteVisiblityJSCallback(int browserIdentifier, bool visible);
6060

61+
void ExecuteSceneChangeJSCallback(const char *name);
62+
6163
public:
6264
NSString *GetUniqueClientName() { return _uniqueClientName; }
6365

obs-browser/apple/cef-isolation-service-manager.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,13 @@
245245
}
246246
@catch (NSException *exception) {}
247247
}
248+
249+
void CEFIsolationServiceManager::ExecuteSceneChangeJSCallback(const char *name)
250+
{
251+
id<CEFIsolatedClient> cefIsolatedClient =
252+
[_cefIsolationService client];
253+
@try {
254+
[cefIsolatedClient executeSceneChangeJSCallback:name];
255+
}
256+
@catch (NSException *exception) {}
257+
}

obs-browser/apple/texture-ref.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <obs-module.h>
2121
#import <Foundation/Foundation.h>
22+
#import <IOSurface/IOSurfaceAPI.h>
2223

2324
class TextureRef {
2425
public:
@@ -43,4 +44,4 @@ class TextureRef {
4344
private:
4445
IOSurfaceRef ioSurfaceRef;
4546
gs_texture_t *texture;
46-
};
47+
};

obs-browser/browser-manager-base.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ void BrowserManager::ExecuteVisiblityJSCallback(int browserIdentifier, bool visi
8080
pimpl->ExecuteVisiblityJSCallback(browserIdentifier, visible);
8181
}
8282

83+
void BrowserManager::ExecuteSceneChangeJSCallback(const char *name)
84+
{
85+
pimpl->ExecuteSceneChangeJSCallback(name);
86+
}
87+
8388
BrowserManager::Impl::Impl()
8489
{
8590
os_event_init(&dispatchEvent, OS_EVENT_TYPE_AUTO);
@@ -184,6 +189,29 @@ void BrowserManager::Impl::ExecuteOnBrowser(int browserIdentifier,
184189
}
185190
}
186191

192+
void BrowserManager::Impl::ExecuteOnAllBrowsers(
193+
std::function<void(CefRefPtr<CefBrowser>)> f,
194+
bool async)
195+
{
196+
for (auto& x: browserMap) {
197+
CefRefPtr<CefBrowser> browser = x.second;
198+
if (async) {
199+
CefPostTask(TID_UI, BrowserTask::newTask([&] {
200+
f(browser);
201+
}));
202+
} else {
203+
os_event_t *finishedEvent;
204+
os_event_init(&finishedEvent, OS_EVENT_TYPE_AUTO);
205+
CefPostTask(TID_UI, BrowserTask::newTask([&] {
206+
f(browser);
207+
os_event_signal(finishedEvent);
208+
}));
209+
os_event_wait(finishedEvent);
210+
os_event_destroy(finishedEvent);
211+
}
212+
}
213+
}
214+
187215
void BrowserManager::Impl::SendMouseClick(int browserIdentifier,
188216
const struct obs_mouse_event *event, int32_t type,
189217
bool mouse_up, uint32_t click_count)
@@ -283,6 +311,17 @@ void BrowserManager::Impl::ExecuteVisiblityJSCallback(int browserIdentifier, boo
283311
});
284312
}
285313

314+
void BrowserManager::Impl::ExecuteSceneChangeJSCallback(const char *name)
315+
{
316+
ExecuteOnAllBrowsers([&](CefRefPtr<CefBrowser> b)
317+
{
318+
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("SceneChange");
319+
CefRefPtr<CefListValue> args = msg->GetArgumentList();
320+
args->SetString(0, name);
321+
b->SendProcessMessage(PID_RENDERER, msg);
322+
});
323+
}
324+
286325
void
287326
BrowserManager::Impl::Startup()
288327
{

obs-browser/browser-manager-base.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,16 @@ class BrowserManager::Impl
5656

5757
void ExecuteVisiblityJSCallback(int browserIdentifier, bool visible);
5858

59+
void ExecuteSceneChangeJSCallback(const char *name);
60+
5961
private:
6062
void ExecuteOnBrowser(int browserIdentifier,
6163
std::function<void(CefRefPtr<CefBrowser>)> f,
6264
bool async = false);
6365

66+
void ExecuteOnAllBrowsers(std::function<void(CefRefPtr<CefBrowser>)> f,
67+
bool async = false);
68+
6469
private:
6570
bool threadAlive;
6671
os_event_t *dispatchEvent;

0 commit comments

Comments
 (0)