From f001d877ba80c8d2817eefe6ca09ae5a601da7bc Mon Sep 17 00:00:00 2001 From: glax Date: Tue, 16 Apr 2024 22:18:41 +0200 Subject: [PATCH] Display Capture Offset --- OBSBlur/Config.cs | 5 ++++- OBSBlur/OBS/Blur.cs | 40 +++++++++++++++++++++------------ OBSBlur/Program.cs | 2 +- OBSBlur/Window/WindowManager.cs | 2 +- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/OBSBlur/Config.cs b/OBSBlur/Config.cs index 3c0c89e..150fbd3 100644 --- a/OBSBlur/Config.cs +++ b/OBSBlur/Config.cs @@ -16,6 +16,8 @@ public struct Config public string[] BlurPrograms = { "Discord", "thunderbird", "Signal" }; [JsonRequired] public string[] BlurWindows = { "Task Switching" }; + [JsonRequired] + public string DisplayCaptureName = "Display Capture"; public Config() { @@ -28,6 +30,7 @@ public struct Config $"Websocket-Password='{new string(ObsWebsocketPassword.Substring(Math.Min(3, ObsWebsocketPassword.Length)).Concat(new string('*', Math.Max(3, ObsWebsocketPassword.Length)+6)).ToArray())}'\n" + $"Enabled Scenes={{{string.Join(',', EnabledScenes)}}}\n" + $"Blur Programs={{{string.Join(',', BlurPrograms)}}}\n" + - $"Blur Windows={{{string.Join(',', BlurWindows)}}}"; + $"Blur Windows={{{string.Join(',', BlurWindows)}}}\n" + + $"Display Capture Name='{DisplayCaptureName}'"; } } \ No newline at end of file diff --git a/OBSBlur/OBS/Blur.cs b/OBSBlur/OBS/Blur.cs index c4e4f47..70a5037 100644 --- a/OBSBlur/OBS/Blur.cs +++ b/OBSBlur/OBS/Blur.cs @@ -13,19 +13,22 @@ public class Blur private readonly WindowManager _windowManager = new (); private readonly OBSWebsocket _websocket = new (); private string _currentObsScene = ""; + private readonly string _displayCaptureName; public readonly List EnabledObsScenes = new(); public readonly List BlurPrograms = new(); public readonly List BlurWindows = new(); private readonly Dictionary _windowHandleSceneItems = new(); private readonly ILogger? _logger; - private float _scaleWidth = 1, _scaleHeight = 1; + private double _scaleWidth = 1, _scaleHeight = 1, _xOffset, _yOffset; - public Blur(string obsUrl, string obsPassword, string[] enabledObsScenes, string[] blurPrograms, string[] blurWindows, ILogger? logger = null) + public Blur(string obsUrl, string obsPassword, string[] enabledObsScenes, string[] blurPrograms, string[] blurWindows, string displayCaptureName, ILogger? logger = null) { this._logger = logger; this.EnabledObsScenes.AddRange(enabledObsScenes); this.BlurPrograms.AddRange(blurPrograms); this.BlurWindows.AddRange(blurWindows); + this._displayCaptureName = displayCaptureName; + _websocket.CurrentProgramSceneChanged += WebsocketOnCurrentProgramSceneChanged; _websocket.Connected += WebsocketOnConnected; _websocket.Disconnected += WebsocketOnDisconnected; @@ -96,7 +99,7 @@ public class Blur Dictionary duplicateSceneItemRequest = new() { {"sceneName", _currentObsScene}, - {"sceneItemId", GetBlurSource()} + {"sceneItemId", GetSceneItemUuid("Blur")} }; try { @@ -123,8 +126,8 @@ public class Blur SceneItemTransformInfo info = new() { - X = windowInfo.WindowRectangle.X * _scaleWidth, - Y = windowInfo.WindowRectangle.Y * _scaleHeight, + X = windowInfo.WindowRectangle.X * _scaleWidth + _xOffset, + Y = windowInfo.WindowRectangle.Y * _scaleHeight + _yOffset, BoundsWidth = windowInfo.WindowRectangle.Width * _scaleWidth, BoundsHeight = windowInfo.WindowRectangle.Height * _scaleHeight, BoundsType = SceneItemBoundsType.OBS_BOUNDS_STRETCH, @@ -191,12 +194,12 @@ public class Blur DeleteBlur(windowInfo.WindowHandle); } - private uint GetBlurSource() + private uint GetSceneItemUuid(string sourceName) { Dictionary request = new() { {"sceneName", _currentObsScene}, - {"sourceName", "Blur"} + {"sourceName", sourceName} }; try { @@ -207,21 +210,30 @@ public class Blur } catch (Exception e) { - _logger?.LogError(e, "Request 'GetSceneItemId'"); + _logger?.LogError(e, $"Request 'GetSceneItemId' in GetSceneItemUuid {sourceName}"); throw; } } private void SetScaleFactors() { + Dictionary request = new() + { + {"sceneName", _currentObsScene}, + {"sceneItemId", GetSceneItemUuid(_displayCaptureName)} + }; try { - JObject response = _websocket.SendRequest("GetVideoSettings"); - if(!response.TryGetValue("baseWidth", out JToken? baseWidth) || !response.TryGetValue("baseHeight", out JToken? baseHeight)) - throw new KeyNotFoundException("Response missing key baseWidth or baseHeight"); - Rectangle desktopRectangle = _windowManager.GetDesktopRectangle(); - this._scaleWidth = baseWidth.Value() / desktopRectangle.Width; - this._scaleHeight = baseHeight.Value() / desktopRectangle.Height; + JObject response = _websocket.SendRequest("GetSceneItemTransform", JObject.FromObject(request)); + if(!response.TryGetValue("sceneItemTransform", out JToken? sceneItemTransform)) + throw new KeyNotFoundException("Response missing key sceneItemTransform"); + + SceneItemTransformInfo sceneItemTransformInfo = sceneItemTransform.ToObject()!; + Rectangle desktopRectangle = WindowManager.GetDesktopRectangle(); + this._scaleWidth = sceneItemTransformInfo.Width / desktopRectangle.Width; + this._scaleHeight = sceneItemTransformInfo.Height / desktopRectangle.Height; + this._xOffset = sceneItemTransformInfo.X; + this._yOffset = sceneItemTransformInfo.Y; } catch (Exception e) { diff --git a/OBSBlur/Program.cs b/OBSBlur/Program.cs index 98a8f6d..9cae582 100644 --- a/OBSBlur/Program.cs +++ b/OBSBlur/Program.cs @@ -14,7 +14,7 @@ Config config = File.Exists(configFilePath) switch }; File.WriteAllText(configFilePath, JsonConvert.SerializeObject(config, Formatting.Indented)); logger.LogInformation($"Config\n{config}"); -Blur _ = new (config.ObsWebsocketUrl, config.ObsWebsocketPassword, config.EnabledScenes, config.BlurPrograms, config.BlurWindows, logger); +Blur _ = new (config.ObsWebsocketUrl, config.ObsWebsocketPassword, config.EnabledScenes, config.BlurPrograms, config.BlurWindows, config.DisplayCaptureName, logger); while (true) Thread.Sleep(100); \ No newline at end of file diff --git a/OBSBlur/Window/WindowManager.cs b/OBSBlur/Window/WindowManager.cs index 0582739..3fcdf0f 100644 --- a/OBSBlur/Window/WindowManager.cs +++ b/OBSBlur/Window/WindowManager.cs @@ -83,7 +83,7 @@ public partial class WindowManager : IDisposable return true; } - public Rectangle GetDesktopRectangle() + public static Rectangle GetDesktopRectangle() { GetWindowRect(GetDesktopWindow(), out Rectangle rect); return rect;