From 6c5bb23ddba0c3292bb1f61feea8439e19485ce3 Mon Sep 17 00:00:00 2001 From: Exeldro Date: Wed, 23 Aug 2023 09:28:43 +0200 Subject: [PATCH] Add function to resize the browser source from javascript --- README.md | 9 +++++++++ browser-app.cpp | 14 +++++++------- browser-client.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c05512ba0..9c2c03a1a 100644 --- a/README.md +++ b/README.md @@ -312,6 +312,15 @@ Permissions required: ALL window.obsstudio.stopVirtualcam() ``` +#### Set the browser size +Permissions required: BASIC +```js +/** + * @param {int} width - Width of the browser size + * @param {int} height - Height of the browser size + */ +window.obsstudio.setBrowserSize(width, height) +``` ### Register for visibility callbacks diff --git a/browser-app.cpp b/browser-app.cpp index 0f7a68dcd..f5a84d4d8 100644 --- a/browser-app.cpp +++ b/browser-app.cpp @@ -100,13 +100,13 @@ void BrowserApp::OnBeforeCommandLineProcessing( } std::vector exposedFunctions = { - "getControlLevel", "getCurrentScene", "getStatus", - "startRecording", "stopRecording", "startStreaming", - "stopStreaming", "pauseRecording", "unpauseRecording", - "startReplayBuffer", "stopReplayBuffer", "saveReplayBuffer", - "startVirtualcam", "stopVirtualcam", "getScenes", - "setCurrentScene", "getTransitions", "getCurrentTransition", - "setCurrentTransition"}; + "getControlLevel", "getCurrentScene", "getStatus", + "startRecording", "stopRecording", "startStreaming", + "stopStreaming", "pauseRecording", "unpauseRecording", + "startReplayBuffer", "stopReplayBuffer", "saveReplayBuffer", + "startVirtualcam", "stopVirtualcam", "getScenes", + "setCurrentScene", "getTransitions", "getCurrentTransition", + "setCurrentTransition", "setBrowserSize"}; void BrowserApp::OnContextCreated(CefRefPtr browser, CefRefPtr, diff --git a/browser-client.cpp b/browser-client.cpp index 5a2f3a7f5..b1b0e31ca 100644 --- a/browser-client.cpp +++ b/browser-client.cpp @@ -198,6 +198,30 @@ bool BrowserClient::OnProcessMessageReceived( case ControlLevel::Basic: if (name == "saveReplayBuffer") { obs_frontend_replay_buffer_save(); + } else if (name == "setBrowserSize") { + int width = input_args->GetInt(1); + int height = input_args->GetInt(2); + if (!width) + width = (int)round(input_args->GetDouble(1)); + if (!height) + height = (int)round(input_args->GetDouble(2)); + if (width > 0 && height > 0) { + obs_data_t *s = + obs_source_get_settings(bs->source); + if (s && + (obs_data_get_int(s, "width") != width || + obs_data_get_int(s, "height") != height)) { + obs_data_set_int(s, "width", width); + obs_data_set_int(s, "height", height); + obs_source_update(bs->source, nullptr); + obs_data_release(s); + } + } else { + blog(LOG_WARNING, + "Browser source '%s' tried to change its size to %ix%i", + obs_source_get_name(bs->source), width, + height); + } } [[fallthrough]]; case ControlLevel::ReadUser: