Fix Pose Recorder and Streamer synchronization and reduce if nesting

This commit is contained in:
ButterscotchVanilla committed 2021-10-13 07:05:29 -04:00
1 parent 8f05284792
commit 35c26bec0f
4 files changed
+92 -56

No files matched your search

@@ -180,5 +180,6 @@ public class BVHFileStream extends PoseDataStream {
@Override
public void close() throws IOException {
writer.close();
super.close();
}
}
@@ -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;
}
}
@@ -34,35 +34,47 @@ public class PoseRecorder {
@VRServerThread
public void onTick() {
if(numFrames > 0) {
PoseFrames poseFrame = this.poseFrame;
List<Pair<Tracker, PoseFrameTracker>> 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<Pair<Tracker, PoseFrameTracker>> trackers = this.trackers;
if (poseFrame == null || trackers == null) {
return;
}
int cursor = frameCursor++;
for(Pair<Tracker, PoseFrameTracker> 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, PoseFrameTracker> 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<PoseFrames> 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<PoseFrames> 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<PoseFrames> getFramesAsync() {
public synchronized Future<PoseFrames> getFramesAsync() {
return currentRecording;
}
public PoseFrames getFrames() throws ExecutionException, InterruptedException {
public synchronized PoseFrames getFrames() throws ExecutionException, InterruptedException {
CompletableFuture<PoseFrames> currentRecording = this.currentRecording;
return currentRecording != null ? currentRecording.get() : null;
}
@@ -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) {