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 ;
6
2
using SixLabors . ImageSharp ;
7
3
using 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 ;
17
4
18
5
namespace Fritz . ObsProxy ;
19
6
20
7
21
- public class ObsClient : IDisposable {
8
+ public class ObsClient : IDisposable
9
+ {
22
10
private bool _DisposedValue ;
23
11
private static OBSWebsocket _OBS ;
24
12
@@ -29,16 +17,19 @@ public class ObsClient : IDisposable {
29
17
30
18
public bool IsReady { get ; set ; } = false ;
31
19
32
- public ObsClient ( ILoggerFactory loggerFactory , IConfiguration configuration ) {
20
+ public ObsClient ( ILoggerFactory loggerFactory , IConfiguration configuration )
21
+ {
33
22
_Logger = loggerFactory . CreateLogger ( "ObsClient" ) ;
34
23
_Configuration = configuration ;
35
24
_IpAddress = string . IsNullOrEmpty ( configuration [ "ObsIpAddress" ] ) ? "127.0.0.1:4455" : configuration [ "ObsIpAddress" ] ;
36
25
_Password = configuration [ "ObsPassword" ] ;
37
26
38
- if ( _OBS == null ) {
27
+ if ( _OBS == null )
28
+ {
39
29
_OBS = new OBSWebsocket ( ) ;
40
30
_OBS . Connected += _OBS_Connected ;
41
- _OBS . Disconnected += ( s , e ) => {
31
+ _OBS . Disconnected += ( s , e ) =>
32
+ {
42
33
OnDisconnect ( ) ;
43
34
} ;
44
35
}
@@ -50,15 +41,17 @@ public ObsClient(ILoggerFactory loggerFactory, IConfiguration configuration) {
50
41
/// </summary>
51
42
/// <param name="port"></param>
52
43
/// <returns></returns>
53
- public void Connect ( ) {
44
+ public void Connect ( )
45
+ {
54
46
55
47
Task . Run ( ( ) => _OBS . ConnectAsync ( $ "ws://{ _IpAddress } ", _Password ) ) ;
56
48
57
49
}
58
50
59
51
public static Action OnDisconnect { get ; set ; } = ( ) => { } ;
60
52
61
- private void _OBS_Connected ( object sender , EventArgs e ) {
53
+ private void _OBS_Connected ( object sender , EventArgs e )
54
+ {
62
55
63
56
IsReady = true ;
64
57
var versionInfo = _OBS . GetVersion ( ) ;
@@ -70,16 +63,29 @@ private void _OBS_Connected(object sender, EventArgs e) {
70
63
public string CameraSource => _Configuration [ "CameraSource" ] ;
71
64
72
65
73
- public string TakeScreenshot ( ) {
66
+ public string TakeScreenshot ( )
67
+ {
74
68
75
69
Console . WriteLine ( $ "IsConnected: { _OBS . IsConnected } ") ;
76
70
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
+
78
83
var imageFileName = System . IO . Path . GetTempFileName ( ) ;
79
84
_OBS . SaveSourceScreenshot ( CameraSource , "png" , imageFileName ) ;
80
85
81
86
var outString = string . Empty ;
82
- using ( var tempFile = File . OpenRead ( imageFileName ) ) {
87
+ using ( var tempFile = File . OpenRead ( imageFileName ) )
88
+ {
83
89
84
90
outString = ProcessImage ( CameraSource , tempFile ) ;
85
91
@@ -89,18 +95,21 @@ public string TakeScreenshot() {
89
95
return outString ;
90
96
91
97
}
92
- catch ( Exception e ) {
98
+ catch ( Exception e )
99
+ {
93
100
_Logger . LogError ( e , "Error while taking screenshot" ) ;
94
101
return null ;
95
102
}
96
103
97
104
}
98
105
99
- private string ProcessImage ( string cameraSource , Stream image ) {
106
+ private string ProcessImage ( string cameraSource , Stream image )
107
+ {
100
108
101
109
var outString = string . Empty ;
102
110
103
- using ( var img = Image . Load ( image ) ) {
111
+ using ( var img = Image . Load ( image ) )
112
+ {
104
113
105
114
// TODO: Crop appropriately for the camerasource
106
115
var memStream = new MemoryStream ( ) ;
@@ -119,9 +128,12 @@ private string ProcessImage(string cameraSource, Stream image) {
119
128
120
129
#region Dispose OBS Connection
121
130
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
+ {
125
137
// TODO: dispose managed state (managed objects)
126
138
}
127
139
@@ -131,12 +143,14 @@ protected virtual void Dispose(bool disposing) {
131
143
}
132
144
}
133
145
134
- ~ ObsClient ( ) {
146
+ ~ ObsClient ( )
147
+ {
135
148
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
136
149
Dispose ( disposing : false ) ;
137
150
}
138
151
139
- public void Dispose ( ) {
152
+ public void Dispose ( )
153
+ {
140
154
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
141
155
Dispose ( disposing : true ) ;
142
156
GC . SuppressFinalize ( this ) ;
0 commit comments