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" };
[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}'";
}
}

View File

@ -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<string> EnabledObsScenes = new();
public readonly List<string> BlurPrograms = new();
public readonly List<string> BlurWindows = new();
private readonly Dictionary<IntPtr, uint> _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<string, object> 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<string, string> 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<string, object> 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<float>() / desktopRectangle.Width;
this._scaleHeight = baseHeight.Value<float>() / 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<SceneItemTransformInfo>()!;
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)
{

View File

@ -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);

View File

@ -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;