|
| 1 | +package com.gravitylab.obscontrollerapi.websocket; |
| 2 | + |
| 3 | +import java.net.URI; |
| 4 | +import java.nio.charset.StandardCharsets; |
| 5 | +import java.security.MessageDigest; |
| 6 | +import java.security.NoSuchAlgorithmException; |
| 7 | +import java.util.Base64; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.concurrent.ScheduledExecutorService; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | + |
| 12 | +import org.java_websocket.client.WebSocketClient; |
| 13 | +import org.java_websocket.handshake.ServerHandshake; |
| 14 | +import org.json.JSONObject; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.beans.factory.annotation.Value; |
| 17 | +import org.springframework.stereotype.Component; |
| 18 | + |
| 19 | +import lombok.extern.slf4j.Slf4j; |
| 20 | + |
| 21 | +@Slf4j |
| 22 | +@Component |
| 23 | +public class OBSWebSocketClient extends WebSocketClient { |
| 24 | + |
| 25 | + @Value("${obs.websocket.password}") |
| 26 | + private String obsPassword; |
| 27 | + |
| 28 | + private String salt = ""; |
| 29 | + private String challenge = ""; |
| 30 | + private String authToken = ""; |
| 31 | + |
| 32 | + private URI serverUri; |
| 33 | + |
| 34 | + static int requestID = 0; |
| 35 | + static int rpcVersion = 1; |
| 36 | + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); |
| 37 | + |
| 38 | + @Autowired |
| 39 | + public OBSWebSocketClient(@Value("${obs.websocket.uri}") URI serverUri) { |
| 40 | + super(serverUri); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void onOpen(ServerHandshake serverHandshake) { |
| 45 | + log.info("Connected to OBS Websocket"); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void onMessage(String s) { |
| 50 | + JSONObject receivedJson = new JSONObject(s); |
| 51 | + if (!receivedJson.has("op") || !receivedJson.has("d")) { |
| 52 | + log.info("Received message from OBS Websocket {}", receivedJson.toString(4)); |
| 53 | + return; |
| 54 | + } |
| 55 | + log.info("Message from OBS Websocket {}", receivedJson.toString(4)); |
| 56 | + |
| 57 | + int operation = receivedJson.getInt("op"); |
| 58 | + rpcVersion = setRpcVersion(receivedJson); |
| 59 | + |
| 60 | + if (operation == 0) { |
| 61 | + handleAuthentication(receivedJson); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onClose(int i, String s, boolean b) { |
| 67 | + String message = new String(s.getBytes(), StandardCharsets.UTF_8); |
| 68 | + log.info("Disconnected from OBS Websocket {} {}", i, message); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void onError(Exception e) { |
| 73 | + log.error("Error from OBS Websocket", e); |
| 74 | + } |
| 75 | + |
| 76 | + public void authenticate() { |
| 77 | + sendIdentifyMessage(this.authToken); |
| 78 | + } |
| 79 | + |
| 80 | + public void startRecording() { |
| 81 | + sendStartRecordRequest(); |
| 82 | + } |
| 83 | + |
| 84 | + public void stopRecording() { |
| 85 | + sendStopRecordRequest(); |
| 86 | + } |
| 87 | + |
| 88 | + private void handleAuthentication(JSONObject receivedJson) { |
| 89 | + JSONObject authenticationData = receivedJson.optJSONObject("d").getJSONObject("authentication"); |
| 90 | + this.salt = authenticationData.getString("salt"); |
| 91 | + this.challenge = authenticationData.getString("challenge"); |
| 92 | + this.authToken = generateAuthToken(salt, challenge); |
| 93 | + log.info("Token generated :)"); |
| 94 | + } |
| 95 | + |
| 96 | + private void sendStartRecordRequest() { |
| 97 | + JSONObject request = new JSONObject(); |
| 98 | + request.put("op", 6); |
| 99 | + JSONObject data = new JSONObject(); |
| 100 | + data.put("requestType", "StartRecord"); |
| 101 | + data.put("requestId", requestID++); |
| 102 | + JSONObject requestData = new JSONObject(); |
| 103 | + requestData.put("sceneName", "Scene 1"); |
| 104 | + data.put("requestData", requestData); |
| 105 | + request.put("d", data); |
| 106 | + this.send(request.toString()); |
| 107 | + log.info("Sent request to OBS Websocket {}", request.toString(4)); |
| 108 | + } |
| 109 | + |
| 110 | + private void handleRecordStateChange(JSONObject receivedJson) { |
| 111 | + JSONObject eventData = receivedJson.optJSONObject("d").getJSONObject("eventData"); |
| 112 | + String eventType = receivedJson.getJSONObject("d").getString("eventType"); |
| 113 | + boolean outputActive = eventData.getBoolean("outputActive"); |
| 114 | + String outputState = eventData.getString("outputState"); |
| 115 | + var outputPath = eventData.get("outputPath"); |
| 116 | + |
| 117 | + if (outputActive && outputState.equals("OBS_WEBSOCKET_OUTPUT_STARTED")) { |
| 118 | + log.info("Event type: {} ,Output active: {}, Output state: {}, Output path: {}", eventType, outputActive, |
| 119 | + outputState, outputPath); |
| 120 | + scheduler.schedule(this::sendStopRecordRequest, 30, TimeUnit.SECONDS); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private void sendStopRecordRequest() { |
| 125 | + JSONObject stopRecordRequest = new JSONObject(); |
| 126 | + stopRecordRequest.put("op", 6); // Assuming '6' is the operation code for StopRecord |
| 127 | + JSONObject data = new JSONObject(); |
| 128 | + data.put("requestType", "StopRecord"); |
| 129 | + data.put("requestId", requestID++); |
| 130 | + stopRecordRequest.put("d", data); |
| 131 | + this.send(stopRecordRequest.toString()); |
| 132 | + log.info("Sent StopRecord request to OBS Websocket {}", stopRecordRequest.toString(4)); |
| 133 | + } |
| 134 | + |
| 135 | + private void sendIdentifyMessage(String authToken) { |
| 136 | + JSONObject identifyMessage = new JSONObject(); |
| 137 | + identifyMessage.put("op", 1); |
| 138 | + JSONObject data = new JSONObject(); |
| 139 | + data.put("rpcVersion", rpcVersion); |
| 140 | + data.put("authentication", authToken); |
| 141 | + identifyMessage.put("d", data); |
| 142 | + this.send(identifyMessage.toString()); |
| 143 | + log.info("Sent identify message to OBS Websocket {}", identifyMessage.toString(4)); |
| 144 | + } |
| 145 | + |
| 146 | + private String generateAuthToken(String salt, String challenge) { |
| 147 | + try { |
| 148 | + return generateSecret(salt, challenge); |
| 149 | + } catch (NoSuchAlgorithmException e) { |
| 150 | + throw new RuntimeException(e); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private String generateSecret(String salt, String challenge) throws NoSuchAlgorithmException { |
| 155 | + // Step 1: Concatenate password and salt |
| 156 | + String passAndSalt = this.obsPassword + salt; |
| 157 | + // Step 2: SHA256 hash and base64 encode |
| 158 | + String base64Secret = base64Encode(sha256Hash(passAndSalt)); |
| 159 | + // Step 3: Concatenate base64 secret with challenge |
| 160 | + String secretAndChallenge = base64Secret + challenge; |
| 161 | + // Step 4: SHA256 hash of the result and base64 encode |
| 162 | + return base64Encode(sha256Hash(secretAndChallenge)); |
| 163 | + } |
| 164 | + |
| 165 | + private static byte[] sha256Hash(String input) throws NoSuchAlgorithmException { |
| 166 | + MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 167 | + return digest.digest(input.getBytes()); |
| 168 | + } |
| 169 | + |
| 170 | + private static String base64Encode(byte[] bytes) { |
| 171 | + return Base64.getEncoder().encodeToString(bytes); |
| 172 | + } |
| 173 | + |
| 174 | + private int setRpcVersion(JSONObject receivedJson) { |
| 175 | + int rpcVersion = 1; |
| 176 | + JSONObject data = receivedJson.getJSONObject("d"); |
| 177 | + if (data.has("rpcVersion")) { |
| 178 | + rpcVersion = data.getInt("rpcVersion"); |
| 179 | + } |
| 180 | + if (data.has("negotiatedRpcVersion")) { |
| 181 | + rpcVersion = data.getInt("negotiatedRpcVersion"); |
| 182 | + } |
| 183 | + return rpcVersion; |
| 184 | + } |
| 185 | +} |
0 commit comments