Skip to content

Add function to signal the browser source from JavaScript #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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