Skip to content

Commit

Permalink
bug-fix concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
olir committed Mar 31, 2018
1 parent c7eb5c6 commit de8a33d
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 44 deletions.
58 changes: 58 additions & 0 deletions app/src/main/java/de/serviceflow/frankenstein/ExecutorThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package de.serviceflow.frankenstein;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.Executor;

public class ExecutorThread implements Executor {
final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
Runnable active;
boolean isAlive = true;
final Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("ExecutorThread running");
while (isAlive) {
scheduleNext();
Thread.sleep(10L);
}
System.out.println("ExecutorThread shudown");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

private ExecutorThread() {
thread.start();
}

private void stop() {
isAlive=false;
}

private static ExecutorThread instance = null;

public static synchronized ExecutorThread getInstance() {
if (instance==null)
instance = new ExecutorThread();
return instance;
}

public static void shutdown() {
if (instance!=null)
instance.stop();
}

public synchronized void execute(final Runnable r) {
tasks.offer(r);
}

protected synchronized void scheduleNext() {
if ((active = tasks.poll()) != null) {
active.run();
}
}

}
73 changes: 42 additions & 31 deletions app/src/main/java/de/serviceflow/frankenstein/MovieProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void processStreamFrame(ProcessingListener l) {
}
}

public boolean process(ProcessingListener l) {
public boolean processVideo(ProcessingListener l) {
try {
streamStopped = !configuration.doInput || !(configuration.getSource() instanceof VideoStreamSource);
if (!configuration.doInput || !(configuration.getSource() instanceof VideoStreamSource)) {
Expand Down Expand Up @@ -435,6 +435,7 @@ public void closeOutput() {
}

public void seek(final ProcessingListener l, int frameId) {

// System.out.println("MovieProcessor.seek @"+frameId);
if (configuration.doInput && frameId < currentPos) {
if (l != null)
Expand All @@ -443,41 +444,51 @@ public void seek(final ProcessingListener l, int frameId) {
currentPos = 0;
configuration.getSource().reopen(l);
}
currentPos = configuration.getSource().seek(frameId, l);
frame = configuration.getSource().getFrame();
if (frame != null && !frame.empty()) {
FilterContext context = new DefaultFilterContext();
Mat newFrame = frame;
for (VideoFilter filter : filters) {
// System.out.println("MovieProcessor process
// "+filter.getClass().getName());
newFrame = filter.process(newFrame, frameId, context);
}
if (localFilters != null && !localFilters.isEmpty()) {
for (FilterElement element : localFilters) {
if (element.filter != null) {
if (element.r.start <= currentPos && currentPos < element.r.end) {
// System.out.println("MovieProcessor
// processStreamFrame " +
// element.filter);
newFrame = element.filter.process(newFrame, currentPos, context);

ExecutorThread.getInstance().execute(new Runnable() {
@Override
public void run() {

currentPos = configuration.getSource().seek(frameId, l);
frame = configuration.getSource().getFrame();
if (frame != null && !frame.empty()) {
FilterContext context = new DefaultFilterContext();
Mat newFrame = frame;
for (VideoFilter filter : filters) {
// System.out.println("MovieProcessor process
// "+filter.getClass().getName());
newFrame = filter.process(newFrame, frameId, context);
}
if (localFilters != null && !localFilters.isEmpty()) {
for (FilterElement element : localFilters) {
if (element.filter != null) {
if (element.r.start <= currentPos && currentPos < element.r.end) {
// System.out.println("MovieProcessor
// processStreamFrame " +
// element.filter);
newFrame = element.filter.process(newFrame, currentPos, context);
}
}
}
}
if (previewFilter != null) {
// System.out.println("MovieProcessor processStreamFrame
// " +
// previewFilter);
newFrame = previewFilter.process(newFrame, currentPos, context);
}
if (l != null)
l.nextFrameProcessed(newFrame, currentPos);
} else {
if (frameId <= movie_frameCount && l != null)
l.prematureEnd(frameId - 1);
}

l.seekDone(frameId);

}
if (previewFilter != null) {
// System.out.println("MovieProcessor processStreamFrame " +
// previewFilter);
newFrame = previewFilter.process(newFrame, currentPos, context);
}
if (l != null)
l.nextFrameProcessed(newFrame, currentPos);
} else {
if (frameId <= movie_frameCount && l != null)
l.prematureEnd(frameId - 1);
}
});

l.seekDone(frameId);
}

public List<String> getVideoDevices() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.ResourceBundle;

import de.serviceflow.frankenstein.Configuration;
import de.serviceflow.frankenstein.ExecutorThread;
import de.serviceflow.frankenstein.MovieProcessor;
import de.serviceflow.frankenstein.plugin.api.ConfigManager;
import de.serviceflow.frankenstein.plugin.api.NativeSegmentFilter;
Expand Down Expand Up @@ -185,6 +186,7 @@ public void stop() {
// Stage is closing
System.out.println("stopping");
MovieProcessor.stop();
ExecutorThread.shutdown();
}

public void setDocumentInTitle(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.opencv.core.Range;

import de.serviceflow.frankenstein.Configuration;
import de.serviceflow.frankenstein.ExecutorThread;
import de.serviceflow.frankenstein.MovieProcessor;
import de.serviceflow.frankenstein.ProcessingListener;
import de.serviceflow.frankenstein.plugin.api.SegmentConfigController;
Expand Down Expand Up @@ -193,7 +194,7 @@ public void run() {
processor.seek(ProcessingSceneController.this, position);
}
};
new Thread(r).start();
ExecutorThread.getInstance().execute(r);
}
}
}
Expand Down Expand Up @@ -468,7 +469,7 @@ public void run() {

long seconds = System.currentTimeMillis() / 1000;

if (processor.process(ProcessingSceneController.this)) {
if (processor.processVideo(ProcessingSceneController.this)) {

seconds = System.currentTimeMillis() / 1000 - seconds;

Expand All @@ -488,7 +489,7 @@ public void run() {
});
}
};
new Thread(r).start();
ExecutorThread.getInstance().execute(r);
}

void startProcessing(Configuration configuration) {
Expand Down Expand Up @@ -583,12 +584,17 @@ public void nextFrameLoaded(VideoStreamSource s) {
Mat frame = s.getFrame();
int frameId = s.getCurrentPos() + 1;
this.frames = s.getFrames();
processor.processStreamFrame(this);
Platform.runLater(() -> {
// System.out.println("nextFrameProcessed "+frameId);
this.currentFrameIndex.setText("" + frameId);
this.currentTime.setText("" + time((frameId - 1) / fps));
drawEditCanvas();
ExecutorThread.getInstance().execute(new Runnable() {
@Override
public void run() {
processor.processStreamFrame(ProcessingSceneController.this);
Platform.runLater(() -> {
// System.out.println("nextFrameProcessed "+frameId);
ProcessingSceneController.this.currentFrameIndex.setText("" + frameId);
ProcessingSceneController.this.currentTime.setText("" + time((frameId - 1) / fps));
drawEditCanvas();
});
}
});
adjustVideoLengthDisplay();
}
Expand Down Expand Up @@ -661,7 +667,7 @@ public void run() {
processor.seek(ProcessingSceneController.this, position);
}
};
new Thread(r).start();
ExecutorThread.getInstance().execute(r);
}
} else {
seeking = false;
Expand Down Expand Up @@ -849,7 +855,7 @@ public void run() {
processor.seek(ProcessingSceneController.this, position);
}
};
new Thread(r).start();
ExecutorThread.getInstance().execute(r);
Platform.runLater(() -> {
listViewFilter.refresh();
drawEditCanvas();
Expand Down Expand Up @@ -894,7 +900,7 @@ public void run() {
processor.seek(ProcessingSceneController.this, position);
}
};
new Thread(r).start();
ExecutorThread.getInstance().execute(r);
}

@FXML
Expand Down Expand Up @@ -922,7 +928,7 @@ public void run() {
processor.seek(ProcessingSceneController.this, position);
}
};
new Thread(r).start();
ExecutorThread.getInstance().execute(r);
}

public List<SegmentVideoFilter> getLocalFilters() {
Expand Down

0 comments on commit de8a33d

Please sign in to comment.