Display Capture Offset

This commit is contained in:
glax 2024-04-16 22:18:41 +02:00
parent 71156fc733
commit f001d877ba
4 changed files with 32 additions and 17 deletions

View File

@ -16,6 +16,8 @@ public struct Config
public string[] BlurPrograms = { "Discord", "thunderbird", "Signal" }; public string[] BlurPrograms = { "Discord", "thunderbird", "Signal" };
[JsonRequired] [JsonRequired]
public string[] BlurWindows = { "Task Switching" }; public string[] BlurWindows = { "Task Switching" };
[JsonRequired]
public string DisplayCaptureName = "Display Capture";
public Config() 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" + $"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" + $"Enabled Scenes={{{string.Join(',', EnabledScenes)}}}\n" +
$"Blur Programs={{{string.Join(',', BlurPrograms)}}}\n" + $"Blur Programs={{{string.Join(',', BlurPrograms)}}}\n" +
$"Blur Windows={{{string.Join(',', BlurWindows)}}}"; $"Blur Windows={{{string.Join(',', BlurWindows)}}}\n" +
$"Display Capture Name='{DisplayCaptureName}'";
} }
} }

View File

@ -13,19 +13,22 @@ public class Blur
private readonly WindowManager _windowManager = new (); private readonly WindowManager _windowManager = new ();
private readonly OBSWebsocket _websocket = new (); private readonly OBSWebsocket _websocket = new ();
private string _currentObsScene = ""; private string _currentObsScene = "";
private readonly string _displayCaptureName;
public readonly List<string> EnabledObsScenes = new(); public readonly List<string> EnabledObsScenes = new();
public readonly List<string> BlurPrograms = new(); public readonly List<string> BlurPrograms = new();
public readonly List<string> BlurWindows = new(); public readonly List<string> BlurWindows = new();
private readonly Dictionary<IntPtr, uint> _windowHandleSceneItems = new(); private readonly Dictionary<IntPtr, uint> _windowHandleSceneItems = new();
private readonly ILogger? _logger; 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._logger = logger;
this.EnabledObsScenes.AddRange(enabledObsScenes); this.EnabledObsScenes.AddRange(enabledObsScenes);
this.BlurPrograms.AddRange(blurPrograms); this.BlurPrograms.AddRange(blurPrograms);
this.BlurWindows.AddRange(blurWindows); this.BlurWindows.AddRange(blurWindows);
this._displayCaptureName = displayCaptureName;
_websocket.CurrentProgramSceneChanged += WebsocketOnCurrentProgramSceneChanged; _websocket.CurrentProgramSceneChanged += WebsocketOnCurrentProgramSceneChanged;
_websocket.Connected += WebsocketOnConnected; _websocket.Connected += WebsocketOnConnected;
_websocket.Disconnected += WebsocketOnDisconnected; _websocket.Disconnected += WebsocketOnDisconnected;
@ -96,7 +99,7 @@ public class Blur
Dictionary<string, object> duplicateSceneItemRequest = new() Dictionary<string, object> duplicateSceneItemRequest = new()
{ {
{"sceneName", _currentObsScene}, {"sceneName", _currentObsScene},
{"sceneItemId", GetBlurSource()} {"sceneItemId", GetSceneItemUuid("Blur")}
}; };
try try
{ {
@ -123,8 +126,8 @@ public class Blur
SceneItemTransformInfo info = new() SceneItemTransformInfo info = new()
{ {
X = windowInfo.WindowRectangle.X * _scaleWidth, X = windowInfo.WindowRectangle.X * _scaleWidth + _xOffset,
Y = windowInfo.WindowRectangle.Y * _scaleHeight, Y = windowInfo.WindowRectangle.Y * _scaleHeight + _yOffset,
BoundsWidth = windowInfo.WindowRectangle.Width * _scaleWidth, BoundsWidth = windowInfo.WindowRectangle.Width * _scaleWidth,
BoundsHeight = windowInfo.WindowRectangle.Height * _scaleHeight, BoundsHeight = windowInfo.WindowRectangle.Height * _scaleHeight,
BoundsType = SceneItemBoundsType.OBS_BOUNDS_STRETCH, BoundsType = SceneItemBoundsType.OBS_BOUNDS_STRETCH,
@ -191,12 +194,12 @@ public class Blur
DeleteBlur(windowInfo.WindowHandle); DeleteBlur(windowInfo.WindowHandle);
} }
private uint GetBlurSource() private uint GetSceneItemUuid(string sourceName)
{ {
Dictionary<string, string> request = new() Dictionary<string, string> request = new()
{ {
{"sceneName", _currentObsScene}, {"sceneName", _currentObsScene},
{"sourceName", "Blur"} {"sourceName", sourceName}
}; };
try try
{ {
@ -207,21 +210,30 @@ public class Blur
} }
catch (Exception e) catch (Exception e)
{ {
_logger?.LogError(e, "Request 'GetSceneItemId'"); _logger?.LogError(e, $"Request 'GetSceneItemId' in GetSceneItemUuid {sourceName}");
throw; throw;
} }
} }
private void SetScaleFactors() private void SetScaleFactors()
{ {
Dictionary<string, object> request = new()
{
{"sceneName", _currentObsScene},
{"sceneItemId", GetSceneItemUuid(_displayCaptureName)}
};
try try
{ {
JObject response = _websocket.SendRequest("GetVideoSettings"); JObject response = _websocket.SendRequest("GetSceneItemTransform", JObject.FromObject(request));
if(!response.TryGetValue("baseWidth", out JToken? baseWidth) || !response.TryGetValue("baseHeight", out JToken? baseHeight)) if(!response.TryGetValue("sceneItemTransform", out JToken? sceneItemTransform))
throw new KeyNotFoundException("Response missing key baseWidth or baseHeight"); throw new KeyNotFoundException("Response missing key sceneItemTransform");
Rectangle desktopRectangle = _windowManager.GetDesktopRectangle();
this._scaleWidth = baseWidth.Value<float>() / desktopRectangle.Width; SceneItemTransformInfo sceneItemTransformInfo = sceneItemTransform.ToObject<SceneItemTransformInfo>()!;
this._scaleHeight = baseHeight.Value<float>() / desktopRectangle.Height; 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) catch (Exception e)
{ {

View File

@ -14,7 +14,7 @@ Config config = File.Exists(configFilePath) switch
}; };
File.WriteAllText(configFilePath, JsonConvert.SerializeObject(config, Formatting.Indented)); File.WriteAllText(configFilePath, JsonConvert.SerializeObject(config, Formatting.Indented));
logger.LogInformation($"Config\n{config}"); 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) while (true)
Thread.Sleep(100); Thread.Sleep(100);

View File

@ -83,7 +83,7 @@ public partial class WindowManager : IDisposable
return true; return true;
} }
public Rectangle GetDesktopRectangle() public static Rectangle GetDesktopRectangle()
{ {
GetWindowRect(GetDesktopWindow(), out Rectangle rect); GetWindowRect(GetDesktopWindow(), out Rectangle rect);
return rect; return rect;