1- using Microsoft . Extensions . Configuration ;
2- using Microsoft . Extensions . Logging ;
3- using Microsoft . VisualBasic . CompilerServices ;
4- using OBSWebsocketDotNet ;
5- using OBSWebsocketDotNet . Types ;
1+ using OBSWebsocketDotNet ;
62using SixLabors . ImageSharp ;
73using SixLabors . ImageSharp . Processing ;
8- using System ;
9- using System . Collections . Generic ;
10- using System . Diagnostics ;
11- using System . IO ;
12- using System . Linq ;
13- using System . Runtime . InteropServices ;
14- using System . Text ;
15- using System . Threading ;
16- using System . Threading . Tasks ;
174
185namespace Fritz . ObsProxy ;
196
207
21- public class ObsClient : IDisposable {
8+ public class ObsClient : IDisposable
9+ {
2210 private bool _DisposedValue ;
2311 private static OBSWebsocket _OBS ;
2412
@@ -29,16 +17,19 @@ public class ObsClient : IDisposable {
2917
3018 public bool IsReady { get ; set ; } = false ;
3119
32- public ObsClient ( ILoggerFactory loggerFactory , IConfiguration configuration ) {
20+ public ObsClient ( ILoggerFactory loggerFactory , IConfiguration configuration )
21+ {
3322 _Logger = loggerFactory . CreateLogger ( "ObsClient" ) ;
3423 _Configuration = configuration ;
3524 _IpAddress = string . IsNullOrEmpty ( configuration [ "ObsIpAddress" ] ) ? "127.0.0.1:4455" : configuration [ "ObsIpAddress" ] ;
3625 _Password = configuration [ "ObsPassword" ] ;
3726
38- if ( _OBS == null ) {
27+ if ( _OBS == null )
28+ {
3929 _OBS = new OBSWebsocket ( ) ;
4030 _OBS . Connected += _OBS_Connected ;
41- _OBS . Disconnected += ( s , e ) => {
31+ _OBS . Disconnected += ( s , e ) =>
32+ {
4233 OnDisconnect ( ) ;
4334 } ;
4435 }
@@ -50,15 +41,17 @@ public ObsClient(ILoggerFactory loggerFactory, IConfiguration configuration) {
5041 /// </summary>
5142 /// <param name="port"></param>
5243 /// <returns></returns>
53- public void Connect ( ) {
44+ public void Connect ( )
45+ {
5446
5547 Task . Run ( ( ) => _OBS . ConnectAsync ( $ "ws://{ _IpAddress } ", _Password ) ) ;
5648
5749 }
5850
5951 public static Action OnDisconnect { get ; set ; } = ( ) => { } ;
6052
61- private void _OBS_Connected ( object sender , EventArgs e ) {
53+ private void _OBS_Connected ( object sender , EventArgs e )
54+ {
6255
6356 IsReady = true ;
6457 var versionInfo = _OBS . GetVersion ( ) ;
@@ -70,16 +63,29 @@ private void _OBS_Connected(object sender, EventArgs e) {
7063 public string CameraSource => _Configuration [ "CameraSource" ] ;
7164
7265
73- public string TakeScreenshot ( ) {
66+ public string TakeScreenshot ( )
67+ {
7468
7569 Console . WriteLine ( $ "IsConnected: { _OBS . IsConnected } ") ;
7670
77- try {
71+ try
72+ {
73+
74+ // show the pre-screenshot countdown animation
75+ var sceneName = _Configuration [ "OverlayScene" ] ;
76+ var sourceName = _Configuration [ "OverlayCountdownSource" ] ;
77+ var shutterSourceId = _OBS . GetSceneItemId ( sceneName , sourceName , 0 ) ;
78+ _OBS . SetSceneItemEnabled ( sceneName , shutterSourceId , false ) ;
79+ _OBS . SetSceneItemEnabled ( sceneName , shutterSourceId , true ) ;
80+ Task . Delay ( 3500 ) . Wait ( ) ;
81+ _OBS . SetSceneItemEnabled ( sceneName , shutterSourceId , false ) ;
82+
7883 var imageFileName = System . IO . Path . GetTempFileName ( ) ;
7984 _OBS . SaveSourceScreenshot ( CameraSource , "png" , imageFileName ) ;
8085
8186 var outString = string . Empty ;
82- using ( var tempFile = File . OpenRead ( imageFileName ) ) {
87+ using ( var tempFile = File . OpenRead ( imageFileName ) )
88+ {
8389
8490 outString = ProcessImage ( CameraSource , tempFile ) ;
8591
@@ -89,18 +95,21 @@ public string TakeScreenshot() {
8995 return outString ;
9096
9197 }
92- catch ( Exception e ) {
98+ catch ( Exception e )
99+ {
93100 _Logger . LogError ( e , "Error while taking screenshot" ) ;
94101 return null ;
95102 }
96103
97104 }
98105
99- private string ProcessImage ( string cameraSource , Stream image ) {
106+ private string ProcessImage ( string cameraSource , Stream image )
107+ {
100108
101109 var outString = string . Empty ;
102110
103- using ( var img = Image . Load ( image ) ) {
111+ using ( var img = Image . Load ( image ) )
112+ {
104113
105114 // TODO: Crop appropriately for the camerasource
106115 var memStream = new MemoryStream ( ) ;
@@ -119,9 +128,12 @@ private string ProcessImage(string cameraSource, Stream image) {
119128
120129 #region Dispose OBS Connection
121130
122- protected virtual void Dispose ( bool disposing ) {
123- if ( ! _DisposedValue ) {
124- if ( disposing ) {
131+ protected virtual void Dispose ( bool disposing )
132+ {
133+ if ( ! _DisposedValue )
134+ {
135+ if ( disposing )
136+ {
125137 // TODO: dispose managed state (managed objects)
126138 }
127139
@@ -131,12 +143,14 @@ protected virtual void Dispose(bool disposing) {
131143 }
132144 }
133145
134- ~ ObsClient ( ) {
146+ ~ ObsClient ( )
147+ {
135148 // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
136149 Dispose ( disposing : false ) ;
137150 }
138151
139- public void Dispose ( ) {
152+ public void Dispose ( )
153+ {
140154 // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
141155 Dispose ( disposing : true ) ;
142156 GC . SuppressFinalize ( this ) ;
0 commit comments