diff --git a/src/main/java/dev/slimevr/poserecorder/BVHFileStream.java b/src/main/java/dev/slimevr/poserecorder/BVHFileStream.java index 01814a0a4..ab856a5f9 100644 --- a/src/main/java/dev/slimevr/poserecorder/BVHFileStream.java +++ b/src/main/java/dev/slimevr/poserecorder/BVHFileStream.java @@ -180,5 +180,6 @@ public class BVHFileStream extends PoseDataStream { @Override public void close() throws IOException { writer.close(); + super.close(); } } diff --git a/src/main/java/dev/slimevr/poserecorder/PoseDataStream.java b/src/main/java/dev/slimevr/poserecorder/PoseDataStream.java index 42da437db..414e30f0c 100644 --- a/src/main/java/dev/slimevr/poserecorder/PoseDataStream.java +++ b/src/main/java/dev/slimevr/poserecorder/PoseDataStream.java @@ -10,6 +10,7 @@ import io.eiren.vr.processor.HumanSkeleton; public abstract class PoseDataStream implements AutoCloseable { + protected boolean closed = false; protected final OutputStream outputStream; protected PoseDataStream(OutputStream outputStream) { @@ -32,8 +33,13 @@ public abstract class PoseDataStream implements AutoCloseable { public void writeFooter(HumanSkeleton skeleton) throws IOException { } + public boolean isClosed() { + return closed; + } + @Override public void close() throws IOException { outputStream.close(); + closed = true; } } diff --git a/src/main/java/dev/slimevr/poserecorder/PoseRecorder.java b/src/main/java/dev/slimevr/poserecorder/PoseRecorder.java index 2aa5955f6..1be97dcad 100644 --- a/src/main/java/dev/slimevr/poserecorder/PoseRecorder.java +++ b/src/main/java/dev/slimevr/poserecorder/PoseRecorder.java @@ -34,35 +34,47 @@ public class PoseRecorder { @VRServerThread public void onTick() { - if(numFrames > 0) { - PoseFrames poseFrame = this.poseFrame; - List> trackers = this.trackers; - if (poseFrame != null && trackers != null) { - if (frameCursor < numFrames) { - long curTime = System.currentTimeMillis(); - if (curTime >= nextFrameTimeMs) { - nextFrameTimeMs += frameRecordingInterval; + if (numFrames <= 0) { + return; + } - // To prevent duplicate frames, make sure the frame time is always in the future - if (nextFrameTimeMs <= curTime) { - nextFrameTimeMs = curTime + frameRecordingInterval; - } + PoseFrames poseFrame = this.poseFrame; + List> trackers = this.trackers; + if (poseFrame == null || trackers == null) { + return; + } - int cursor = frameCursor++; - for(Pair tracker : trackers) { - // Add a frame for each tracker - tracker.getRight().addFrame(cursor, tracker.getLeft()); - } + if (frameCursor >= numFrames) { + // If done and hasn't yet, send finished recording + stopFrameRecording(); + return; + } + + long curTime = System.currentTimeMillis(); + if (curTime < nextFrameTimeMs) { + return; + } - // If done, send finished recording - if(frameCursor >= numFrames) { - internalStopRecording(); - } - } - } else { - // If done and hasn't yet, send finished recording - internalStopRecording(); - } + nextFrameTimeMs += frameRecordingInterval; + + // To prevent duplicate frames, make sure the frame time is always in the future + if (nextFrameTimeMs <= curTime) { + nextFrameTimeMs = curTime + frameRecordingInterval; + } + + // Make sure it's synchronized since this is the server thread interacting with + // an unknown outside thread controlling this class + synchronized (this) { + // A stopped recording will be accounted for by an empty "trackers" list + int cursor = frameCursor++; + for(Pair tracker : trackers) { + // Add a frame for each tracker + tracker.getRight().addFrame(cursor, tracker.getLeft()); + } + + // If done, send finished recording + if(frameCursor >= numFrames) { + stopFrameRecording(); } } } @@ -116,7 +128,7 @@ public class PoseRecorder { return currentRecording; } - private void internalStopRecording() { + public synchronized void stopFrameRecording() { CompletableFuture currentRecording = this.currentRecording; if(currentRecording != null && !currentRecording.isDone()) { // Stop the recording, returning the frames recorded @@ -129,10 +141,6 @@ public class PoseRecorder { poseFrame = null; } - public synchronized void stopFrameRecording() { - internalStopRecording(); - } - public synchronized void cancelFrameRecording() { CompletableFuture currentRecording = this.currentRecording; if(currentRecording != null && !currentRecording.isDone()) { @@ -146,23 +154,23 @@ public class PoseRecorder { poseFrame = null; } - public boolean isReadyToRecord() { + public synchronized boolean isReadyToRecord() { return server.getTrackersCount() > 0; } - public boolean isRecording() { + public synchronized boolean isRecording() { return numFrames > frameCursor; } - public boolean hasRecording() { + public synchronized boolean hasRecording() { return currentRecording != null; } - public Future getFramesAsync() { + public synchronized Future getFramesAsync() { return currentRecording; } - public PoseFrames getFrames() throws ExecutionException, InterruptedException { + public synchronized PoseFrames getFrames() throws ExecutionException, InterruptedException { CompletableFuture currentRecording = this.currentRecording; return currentRecording != null ? currentRecording.get() : null; } diff --git a/src/main/java/dev/slimevr/poserecorder/PoseStreamer.java b/src/main/java/dev/slimevr/poserecorder/PoseStreamer.java index 3d16869eb..88570cbfb 100644 --- a/src/main/java/dev/slimevr/poserecorder/PoseStreamer.java +++ b/src/main/java/dev/slimevr/poserecorder/PoseStreamer.java @@ -33,28 +33,45 @@ public class PoseStreamer { @VRServerThread public void onTick() { PoseDataStream poseFileStream = this.poseFileStream; - if (poseFileStream != null) { + if (poseFileStream == null) { + return; + } - long curTime = System.currentTimeMillis(); - if (curTime >= nextFrameTimeMs) { - nextFrameTimeMs += frameRecordingInterval; + HumanSkeleton skeleton = this.skeleton; + if (skeleton == null) { + return; + } + + long curTime = System.currentTimeMillis(); + if (curTime < nextFrameTimeMs) { + return; + } + + nextFrameTimeMs += frameRecordingInterval; - // To prevent duplicate frames, make sure the frame time is always in the future - if (nextFrameTimeMs <= curTime) { - nextFrameTimeMs = curTime + frameRecordingInterval; - } - - try { - poseFileStream.writeFrame(skeleton); - } catch (Exception e) { - // Handle any exceptions without crashing the program - LogManager.log.severe("[PoseStreamer] Exception while saving frame", e); - } + // To prevent duplicate frames, make sure the frame time is always in the future + if (nextFrameTimeMs <= curTime) { + nextFrameTimeMs = curTime + frameRecordingInterval; + } + + // Make sure it's synchronized since this is the server thread interacting with + // an unknown outside thread controlling this class + synchronized (this) { + // Make sure the stream is open before trying to write + if (poseFileStream.isClosed()) { + return; + } + + try { + poseFileStream.writeFrame(skeleton); + } catch (Exception e) { + // Handle any exceptions without crashing the program + LogManager.log.severe("[PoseStreamer] Exception while saving frame", e); } } } - public void setFrameInterval(long intervalMs) { + public synchronized void setFrameInterval(long intervalMs) { if(intervalMs < 1) { throw new IllegalArgumentException("intervalMs must at least have a value of 1"); } @@ -62,22 +79,26 @@ public class PoseStreamer { this.frameRecordingInterval = intervalMs; } - public void setOutput(PoseDataStream poseFileStream) throws IOException { + public synchronized long getFrameInterval() { + return frameRecordingInterval; + } + + public synchronized void setOutput(PoseDataStream poseFileStream) throws IOException { poseFileStream.writeHeader(skeleton, this); this.poseFileStream = poseFileStream; nextFrameTimeMs = -1L; // Reset the frame timing } - public void setOutput(PoseDataStream poseFileStream, long intervalMs) throws IOException { + public synchronized void setOutput(PoseDataStream poseFileStream, long intervalMs) throws IOException { setFrameInterval(intervalMs); setOutput(poseFileStream); } - public PoseDataStream getOutput() { + public synchronized PoseDataStream getOutput() { return poseFileStream; } - public void closeOutput() throws IOException { + public synchronized void closeOutput() throws IOException { PoseDataStream poseFileStream = this.poseFileStream; if (poseFileStream != null) {