4
4
import com .google .gson .JsonElement ;
5
5
import com .google .gson .JsonObject ;
6
6
import com .google .gson .JsonParser ;
7
- import net .twasi .obsremotejava .callbacks .Callback ;
8
- import net .twasi .obsremotejava .callbacks .ErrorCallback ;
9
- import net .twasi .obsremotejava .callbacks .StringCallback ;
10
- import net .twasi .obsremotejava .callbacks .VoidCallback ;
7
+ import net .twasi .obsremotejava .callbacks .*;
11
8
import net .twasi .obsremotejava .events .EventType ;
12
9
import net .twasi .obsremotejava .events .responses .*;
13
10
import net .twasi .obsremotejava .objects .throwables .InvalidResponseTypeError ;
24
21
import net .twasi .obsremotejava .requests .GetSceneItemProperties .GetSceneItemPropertiesRequest ;
25
22
import net .twasi .obsremotejava .requests .GetSceneList .GetSceneListRequest ;
26
23
import net .twasi .obsremotejava .requests .GetSceneList .GetSceneListResponse ;
24
+ import net .twasi .obsremotejava .requests .GetSourceFilterInfo .GetSourceFilterInfoRequest ;
25
+ import net .twasi .obsremotejava .requests .GetSourceFilterInfo .GetSourceFilterInfoResponse ;
26
+ import net .twasi .obsremotejava .requests .GetSourceFilters .GetSourceFiltersRequest ;
27
+ import net .twasi .obsremotejava .requests .GetSourceFilters .GetSourceFiltersResponse ;
27
28
import net .twasi .obsremotejava .requests .GetSourceSettings .GetSourceSettingsRequest ;
28
29
import net .twasi .obsremotejava .requests .GetSourceSettings .GetSourceSettingsResponse ;
29
30
import net .twasi .obsremotejava .requests .GetStreamingStatus .GetStreamingStatusRequest ;
55
56
import net .twasi .obsremotejava .requests .SetPreviewScene .SetPreviewSceneResponse ;
56
57
import net .twasi .obsremotejava .requests .SetSceneItemProperties .SetSceneItemPropertiesRequest ;
57
58
import net .twasi .obsremotejava .requests .SetSceneItemProperties .SetSceneItemPropertiesResponse ;
59
+ import net .twasi .obsremotejava .requests .SetSourceFilterVisibility .SetSourceFilterVisibilityRequest ;
60
+ import net .twasi .obsremotejava .requests .SetSourceFilterVisibility .SetSourceFilterVisibilityResponse ;
58
61
import net .twasi .obsremotejava .requests .SetSourceSettings .SetSourceSettingsRequest ;
59
62
import net .twasi .obsremotejava .requests .SetSourceSettings .SetSourceSettingsResponse ;
60
63
import net .twasi .obsremotejava .requests .SetStudioModeEnabled .SetStudioModeEnabledRequest ;
@@ -107,6 +110,7 @@ public class OBSCommunicator {
107
110
108
111
private Callback <GetVersionResponse > onConnect ;
109
112
private VoidCallback onDisconnect ;
113
+ private CloseCallback onClose ;
110
114
private StringCallback onConnectionFailed ;
111
115
private ErrorCallback onError ;
112
116
@@ -125,6 +129,7 @@ public class OBSCommunicator {
125
129
private Callback <TransitionListChangedResponse > onTransitionListChanged ;
126
130
private Callback <TransitionBeginResponse > onTransitionBegin ;
127
131
private Callback <TransitionEndResponse > onTransitionEnd ;
132
+ private Callback <SourceFilterVisibilityChangedResponse > onSourceFilterVisibilityChanged ;
128
133
129
134
private GetVersionResponse versionInfo ;
130
135
@@ -155,12 +160,9 @@ public void onError(Session session, Throwable throwable) {
155
160
@ OnWebSocketClose
156
161
public void onClose (int statusCode , String reason ) {
157
162
log .info (String .format ("Connection closed: %d - %s%n" , statusCode , reason ));
163
+ runOnDisconnect ();
158
164
this .closeLatch .countDown (); // trigger latch
159
- try {
160
- this .onDisconnect .run ();
161
- } catch (Throwable t ) {
162
- log .error ("Unable to disconnect OBS Client" , t );
163
- }
165
+ runOnClosed (statusCode , reason );
164
166
}
165
167
166
168
@ OnWebSocketConnect
@@ -191,6 +193,7 @@ public void onMessage(String msg) {
191
193
// Response
192
194
ResponseBase responseBase = new Gson ().fromJson (msg , ResponseBase .class );
193
195
Class type = messageTypes .get (responseBase .getMessageId ());
196
+ log .trace (String .format ("Trying to deserialize response with type %s and message '%s'" , type , msg ));
194
197
responseBase = (ResponseBase ) new Gson ().fromJson (msg , type );
195
198
196
199
try {
@@ -292,6 +295,11 @@ private void processIncomingEvent(String msg, EventType eventType) {
292
295
onScenesChanged .run (new Gson ().fromJson (msg , ScenesChangedResponse .class ));
293
296
}
294
297
break ;
298
+ case SourceFilterVisibilityChanged :
299
+ if (onSourceFilterVisibilityChanged != null ) {
300
+ onSourceFilterVisibilityChanged .run (new Gson ().fromJson (msg , SourceFilterVisibilityChangedResponse .class ));
301
+ }
302
+ break ;
295
303
case SwitchTransition :
296
304
if (onSwitchTransition != null ) {
297
305
onSwitchTransition .run (new Gson ().fromJson (msg , SwitchTransitionResponse .class ));
@@ -379,6 +387,8 @@ public void registerOnDisconnect(VoidCallback onDisconnect) {
379
387
this .onDisconnect = onDisconnect ;
380
388
}
381
389
390
+ public void registerOnClose (CloseCallback closeCallback ) { this .onClose = closeCallback ; }
391
+
382
392
public void registerOnConnectionFailed (StringCallback onConnectionFailed ) {
383
393
this .onConnectionFailed = onConnectionFailed ;
384
394
}
@@ -407,6 +417,10 @@ public void registerOnScenesChanged(Callback<ScenesChangedResponse> onScenesChan
407
417
this .onScenesChanged = onScenesChanged ;
408
418
}
409
419
420
+ public void registerOnSourceFilterVisibilityChanged (Callback <SourceFilterVisibilityChangedResponse > onSourceFilterVisibilityChanged ) {
421
+ this .onSourceFilterVisibilityChanged = onSourceFilterVisibilityChanged ;
422
+ }
423
+
410
424
public void registerOnSwitchTransition (Callback <SwitchTransitionResponse > onSwitchTransition ) {
411
425
this .onSwitchTransition = onSwitchTransition ;
412
426
}
@@ -493,6 +507,24 @@ public void setSourceSettings(String sourceName, Map<String, Object> settings, C
493
507
callbacks .put (SetSourceSettingsResponse .class , callback );
494
508
}
495
509
510
+ public void getSourceFilters (String sourceName , Callback <GetSourceFiltersResponse > callback ) {
511
+ GetSourceFiltersRequest request = new GetSourceFiltersRequest (sourceName );
512
+ session .getRemote ().sendStringByFuture (new Gson ().toJson (request ));
513
+ callbacks .put (GetSourceFiltersResponse .class , callback );
514
+ }
515
+
516
+ public void getSourceFilterInfo (String sourceName , String filterName , Callback <GetSourceFilterInfoResponse > callback ) {
517
+ GetSourceFilterInfoRequest request = new GetSourceFilterInfoRequest (sourceName , filterName );
518
+ session .getRemote ().sendStringByFuture (new Gson ().toJson (request ));
519
+ callbacks .put (GetSourceFilterInfoResponse .class , callback );
520
+ }
521
+
522
+ public void setSourceFilterVisibility (String sourceName , String filterName , boolean filterEnabled , Callback <SetSourceFilterVisibilityResponse > callback ) {
523
+ SetSourceFilterVisibilityRequest request = new SetSourceFilterVisibilityRequest (sourceName , filterName , filterEnabled );
524
+ session .getRemote ().sendStringByFuture (new Gson ().toJson (request ));
525
+ callbacks .put (SetSourceFilterVisibilityResponse .class , callback );
526
+ }
527
+
496
528
public void startRecording (Callback <StartRecordingResponse > callback ) {
497
529
StartRecordingRequest request = new StartRecordingRequest (this );
498
530
@@ -692,4 +724,34 @@ private void runOnConnect(GetVersionResponse versionInfo) {
692
724
log .error ("Unable to run OnConnect callback" , t );
693
725
}
694
726
}
727
+
728
+ void runOnDisconnect () {
729
+ log .debug ("Running onDisconnect" );
730
+ if (onDisconnect == null ) {
731
+ log .debug ("No onDisconnect callback was registered" );
732
+ return ;
733
+ }
734
+
735
+ try {
736
+ onDisconnect .run ();
737
+ } catch (Throwable t ) {
738
+ log .error ("Unable to run OnDisconnect callback" , t );
739
+ }
740
+ }
741
+
742
+
743
+ private void runOnClosed (int statusCode , String reason ) {
744
+ log .debug ("Running onClose with statusCode " + statusCode + " and reason: " + reason );
745
+
746
+ if (this .onClose == null ) {
747
+ log .debug ("No onClose was registered." );
748
+ return ;
749
+ }
750
+
751
+ try {
752
+ onClose .run (statusCode , reason );
753
+ } catch (Throwable t ) {
754
+ log .error ("Unable to run onClose callback" , t );
755
+ }
756
+ }
695
757
}
0 commit comments