Skip to content

Commit

Permalink
Add function to signal the browser source from JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
exeldro committed Apr 21, 2024
1 parent b4f724a commit 22476b5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ Permissions required: ALL
window.obsstudio.stopVirtualcam()
```

#### Signal the browser source
Permissions required: BASIC
```js
/**
* @param {string} signal - signal name
*/
window.obsstudio.signal(signal)
```

### Register for visibility callbacks

Expand Down
28 changes: 20 additions & 8 deletions browser-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,26 @@ void BrowserApp::OnBeforeCommandLineProcessing(
#endif
}

std::vector<std::string> exposedFunctions = {
"getControlLevel", "getCurrentScene", "getStatus",
"startRecording", "stopRecording", "startStreaming",
"stopStreaming", "pauseRecording", "unpauseRecording",
"startReplayBuffer", "stopReplayBuffer", "saveReplayBuffer",
"startVirtualcam", "stopVirtualcam", "getScenes",
"setCurrentScene", "getTransitions", "getCurrentTransition",
"setCurrentTransition"};
std::vector<std::string> exposedFunctions = {"getControlLevel",
"getCurrentScene",
"getStatus",
"startRecording",
"stopRecording",
"startStreaming",
"stopStreaming",
"pauseRecording",
"unpauseRecording",
"startReplayBuffer",
"stopReplayBuffer",
"saveReplayBuffer",
"startVirtualcam",
"stopVirtualcam",
"getScenes",
"setCurrentScene",
"getTransitions",
"getCurrentTransition",
"setCurrentTransition",
"signal"};

void BrowserApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame>,
Expand Down
20 changes: 20 additions & 0 deletions browser-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,26 @@ bool BrowserClient::OnProcessMessageReceived(
case ControlLevel::Basic:
if (name == "saveReplayBuffer") {
obs_frontend_replay_buffer_save();
} else if (name == "signal") {
struct calldata data;
calldata_init(&data);
calldata_set_ptr(&data, "source", bs->source);
if (input_args->GetType(1) == VTYPE_STRING) {
const std::string signal =
input_args->GetString(1).ToString();
calldata_set_string(&data, "signal",
signal.c_str());
}
signal_handler_t *gsh = obs_get_signal_handler();
if (gsh && !obs_obj_is_private(bs->source))
signal_handler_signal(
gsh, "source_browser_signal", &data);
signal_handler_t *sh =
obs_source_get_signal_handler(bs->source);
if (sh)
signal_handler_signal(sh, "browser_signal",
&data);
calldata_free(&data);
}
[[fallthrough]];
case ControlLevel::ReadUser:
Expand Down
3 changes: 3 additions & 0 deletions obs-browser-plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,9 @@ bool obs_module_load(void)
cef_version_info(7), CEF_VERSION);

RegisterBrowserSource();
signal_handler_add(
obs_get_signal_handler(),
"void source_browser_signal(ptr source, string signal)");
obs_frontend_add_event_callback(handle_obs_frontend_event, nullptr);

#ifdef ENABLE_BROWSER_SHARED_TEXTURE
Expand Down
4 changes: 4 additions & 0 deletions obs-browser-source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ BrowserSource::BrowserSource(obs_data_t *, obs_source_t *source_)
"void javascript_event(string eventName, string jsonString)",
jsEventFunction, (void *)this);

signal_handler_t *sh = obs_source_get_signal_handler(source);
signal_handler_add(sh,
"void browser_signal(ptr source, string signal)");

/* defer update */
obs_source_update(source, nullptr);

Expand Down

0 comments on commit 22476b5

Please sign in to comment.