|
1 | 1 | using Microsoft.Extensions.Configuration;
|
2 | 2 | using Microsoft.Extensions.Logging;
|
3 | 3 | using Microsoft.VisualBasic.CompilerServices;
|
4 |
| -using OBS.WebSocket.NET; |
5 |
| -using OBS.WebSocket.NET.Types; |
| 4 | +using OBSWebsocketDotNet; |
| 5 | +using OBSWebsocketDotNet.Types; |
6 | 6 | using SixLabors.ImageSharp;
|
7 | 7 | using SixLabors.ImageSharp.Processing;
|
8 | 8 | using System;
|
9 | 9 | using System.Collections.Generic;
|
| 10 | +using System.Diagnostics; |
10 | 11 | using System.IO;
|
11 | 12 | using System.Linq;
|
12 | 13 | using System.Runtime.InteropServices;
|
13 | 14 | using System.Text;
|
| 15 | +using System.Threading; |
14 | 16 | using System.Threading.Tasks;
|
15 | 17 |
|
16 |
| -namespace Fritz.ObsProxy |
17 |
| -{ |
| 18 | +namespace Fritz.ObsProxy; |
18 | 19 |
|
19 |
| - public class ObsClient : IDisposable |
20 |
| - { |
21 |
| - private bool _DisposedValue; |
22 |
| - private ObsWebSocket _OBS; |
23 | 20 |
|
24 |
| - private readonly ILogger _Logger; |
25 |
| - private readonly IConfiguration _Configuration; |
26 |
| - private readonly string _IpAddress; |
| 21 | +public class ObsClient : IDisposable { |
| 22 | + private bool _DisposedValue; |
| 23 | + private static OBSWebsocket _OBS; |
27 | 24 |
|
28 |
| - public ObsClient(ILoggerFactory loggerFactory, IConfiguration configuration) |
29 |
| - { |
30 |
| - _Logger = loggerFactory.CreateLogger("ObsClient"); |
31 |
| - _Configuration = configuration; |
32 |
| - _IpAddress = string.IsNullOrEmpty(configuration["ObsIpAddress"]) ? "127.0.0.1:4444" : configuration["ObsIpAddress"]; |
33 |
| - } |
34 |
| - |
35 |
| - /// <summary> |
36 |
| - /// Establish a connection to OBS |
37 |
| - /// </summary> |
38 |
| - /// <param name="port"></param> |
39 |
| - /// <returns></returns> |
40 |
| - public Task Connect() |
41 |
| - { |
| 25 | + private readonly ILogger _Logger; |
| 26 | + private readonly IConfiguration _Configuration; |
| 27 | + private readonly string _IpAddress; |
| 28 | + private readonly string _Password; |
42 | 29 |
|
43 |
| - _OBS = new ObsWebSocket(); |
44 |
| - _OBS.Connect($"ws://{_IpAddress}", ""); |
| 30 | + public bool IsReady { get; set; } = false; |
45 | 31 |
|
46 |
| - return Task.CompletedTask; |
| 32 | + public ObsClient(ILoggerFactory loggerFactory, IConfiguration configuration) { |
| 33 | + _Logger = loggerFactory.CreateLogger("ObsClient"); |
| 34 | + _Configuration = configuration; |
| 35 | + _IpAddress = string.IsNullOrEmpty(configuration["ObsIpAddress"]) ? "127.0.0.1:4455" : configuration["ObsIpAddress"]; |
| 36 | + _Password = configuration["ObsPassword"]; |
47 | 37 |
|
| 38 | + if (_OBS == null) { |
| 39 | + _OBS = new OBSWebsocket(); |
| 40 | + _OBS.Connected += _OBS_Connected; |
| 41 | + _OBS.Disconnected += (s, e) => { |
| 42 | + OnDisconnect(); |
| 43 | + }; |
48 | 44 | }
|
49 | 45 |
|
50 |
| - public string ImageFolderLocation => _Configuration["ImageFolder"]; |
| 46 | + } |
51 | 47 |
|
52 |
| - public string CameraSource => _Configuration["CameraSource"]; |
| 48 | + /// <summary> |
| 49 | + /// Establish a connection to OBS |
| 50 | + /// </summary> |
| 51 | + /// <param name="port"></param> |
| 52 | + /// <returns></returns> |
| 53 | + public void Connect() { |
53 | 54 |
|
| 55 | + Task.Run(() => _OBS.ConnectAsync($"ws://{_IpAddress}", _Password)); |
54 | 56 |
|
55 |
| - public string TakeScreenshot() |
56 |
| - { |
| 57 | + } |
57 | 58 |
|
58 |
| - SourceScreenshotResponse response = null; |
59 |
| - if (string.IsNullOrEmpty(ImageFolderLocation)) |
60 |
| - { |
61 |
| - response = _OBS.Api.TakeSourceScreenshot(CameraSource, embedPictureFormat: "png"); |
62 |
| - var cleanString = response.ImageData.Replace("data:image/png;base64,", ""); |
63 |
| - var bytes = Convert.FromBase64String(cleanString); |
64 |
| - _Logger.LogWarning($"Took screenshot from scene '{CameraSource}'"); |
65 |
| - return ProcessImage(CameraSource, bytes); |
66 |
| - } |
67 |
| - else |
68 |
| - { |
69 |
| - |
70 |
| - try |
71 |
| - { |
72 |
| - var imageFileName = ImageFolderLocation + "\\test.png"; |
73 |
| - response = _OBS.Api.TakeSourceScreenshot(CameraSource, embedPictureFormat: "png"); |
74 |
| - var cleanString = response.ImageData.Replace("data:image/png;base64,", ""); |
75 |
| - var bytes = Convert.FromBase64String(cleanString); |
76 |
| - var outString = ProcessImage(CameraSource, bytes); |
77 |
| - _Logger.LogWarning($"${DateTime.Now} Took screenshot from scene '{CameraSource}'"); |
78 |
| - return outString; |
79 |
| - |
80 |
| - } |
81 |
| - catch (Exception e) |
82 |
| - { |
83 |
| - _Logger.LogError(e, "Error while taking screenshot"); |
84 |
| - return null; |
85 |
| - } |
| 59 | + public static Action OnDisconnect { get; set; } = () => { }; |
86 | 60 |
|
87 |
| - } |
| 61 | + private void _OBS_Connected(object sender, EventArgs e) { |
88 | 62 |
|
89 |
| - return response.ImageData; |
| 63 | + IsReady = true; |
| 64 | + var versionInfo = _OBS.GetVersion(); |
| 65 | + Console.WriteLine($"Plugin Version: {versionInfo.PluginVersion}"); |
| 66 | + } |
90 | 67 |
|
91 |
| - } |
| 68 | + public string ImageFolderLocation => _Configuration["ImageFolder"]; |
92 | 69 |
|
93 |
| - private string ProcessImage(string cameraSource, byte[] imageBytes) |
94 |
| - { |
| 70 | + public string CameraSource => _Configuration["CameraSource"]; |
95 | 71 |
|
96 |
| - var outString = string.Empty; |
97 | 72 |
|
98 |
| - using (var img = Image.Load(imageBytes)) { |
| 73 | + public string TakeScreenshot() { |
99 | 74 |
|
100 |
| - // TODO: Crop appropriately for the camerasource |
101 |
| - var memStream = new MemoryStream(); |
102 |
| - img.Clone(ctx => ctx.Crop(new Rectangle(450, 0, 900, 450))).SaveAsPng(memStream); |
103 |
| - memStream.Position = 0; |
| 75 | + Console.WriteLine($"IsConnected: {_OBS.IsConnected}"); |
104 | 76 |
|
105 |
| - outString = Convert.ToBase64String(memStream.ToArray()); |
106 |
| - memStream.Dispose(); |
| 77 | + try { |
| 78 | + var imageFileName = System.IO.Path.GetTempFileName(); |
| 79 | + _OBS.SaveSourceScreenshot(CameraSource, "png", imageFileName); |
107 | 80 |
|
108 |
| - } |
| 81 | + var outString = string.Empty; |
| 82 | + using (var tempFile = File.OpenRead(imageFileName)) { |
| 83 | + |
| 84 | + outString = ProcessImage(CameraSource, tempFile); |
109 | 85 |
|
| 86 | + } |
| 87 | + File.Delete(imageFileName); |
| 88 | + _Logger.LogWarning($"${DateTime.Now} Took screenshot from scene '{CameraSource}'"); |
110 | 89 | return outString;
|
111 | 90 |
|
112 | 91 | }
|
| 92 | + catch (Exception e) { |
| 93 | + _Logger.LogError(e, "Error while taking screenshot"); |
| 94 | + return null; |
| 95 | + } |
113 | 96 |
|
| 97 | + } |
114 | 98 |
|
115 |
| - #region Dispose OBS Connection |
| 99 | + private string ProcessImage(string cameraSource, Stream image) { |
116 | 100 |
|
117 |
| - protected virtual void Dispose(bool disposing) |
118 |
| - { |
119 |
| - if (!_DisposedValue) |
120 |
| - { |
121 |
| - if (disposing) |
122 |
| - { |
123 |
| - // TODO: dispose managed state (managed objects) |
124 |
| - } |
| 101 | + var outString = string.Empty; |
125 | 102 |
|
126 |
| - _OBS.Disconnect(); |
127 |
| - _OBS = null; |
128 |
| - _DisposedValue = true; |
129 |
| - } |
130 |
| - } |
| 103 | + using (var img = Image.Load(image)) { |
| 104 | + |
| 105 | + // TODO: Crop appropriately for the camerasource |
| 106 | + var memStream = new MemoryStream(); |
| 107 | + img.Clone(ctx => ctx.Crop(new Rectangle(450, 0, 900, 450))).SaveAsPng(memStream); |
| 108 | + memStream.Position = 0; |
| 109 | + |
| 110 | + outString = Convert.ToBase64String(memStream.ToArray()); |
| 111 | + memStream.Dispose(); |
131 | 112 |
|
132 |
| - ~ObsClient() |
133 |
| - { |
134 |
| - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method |
135 |
| - Dispose(disposing: false); |
136 | 113 | }
|
137 | 114 |
|
138 |
| - public void Dispose() |
139 |
| - { |
140 |
| - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method |
141 |
| - Dispose(disposing: true); |
142 |
| - GC.SuppressFinalize(this); |
| 115 | + return outString; |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + #region Dispose OBS Connection |
| 121 | + |
| 122 | + protected virtual void Dispose(bool disposing) { |
| 123 | + if (!_DisposedValue) { |
| 124 | + if (disposing) { |
| 125 | + // TODO: dispose managed state (managed objects) |
| 126 | + } |
| 127 | + |
| 128 | + _OBS.Disconnect(); |
| 129 | + _OBS = null; |
| 130 | + _DisposedValue = true; |
143 | 131 | }
|
| 132 | + } |
144 | 133 |
|
145 |
| - #endregion |
| 134 | + ~ObsClient() { |
| 135 | + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method |
| 136 | + Dispose(disposing: false); |
| 137 | + } |
146 | 138 |
|
| 139 | + public void Dispose() { |
| 140 | + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method |
| 141 | + Dispose(disposing: true); |
| 142 | + GC.SuppressFinalize(this); |
147 | 143 | }
|
148 | 144 |
|
| 145 | + #endregion |
| 146 | + |
| 147 | + |
| 148 | + |
149 | 149 | }
|
0 commit comments