private Thread grabberThread;
private Consumer<VideoFrame> frameConsumer;
private VideoDeviceListener deviceListener;
+ private boolean isPaused = false;
public VideoMode() {
deviceListener = new VideoDeviceListener();
@Override
public void enter(Configuration config) {
this.config = config;
- deviceListener.startListening();
+ if (config.video.deviceIsAutomatic) {
+ deviceListener.startListening();
+ } else {
+ File videoDevice = new File(config.video.device);
+ startGrabberThread(videoDevice);
+ }
}
@Override
public void pause() {
- try {
- grabberThread.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
+ isPaused = true;
}
@Override
public void resume() {
- grabberThread.notify();
+ isPaused = false;
+ synchronized (grabberThread) {
+ grabberThread.notify();
+ }
}
@Override
public void exit() {
grabberThread.interrupt();
- deviceListener.stopListening();
+ if (config.video.deviceIsAutomatic) {
+ deviceListener.stopListening();
+ }
}
private void startGrabberThread(File videoDevice) {
try (FrameGrabber grabber = FrameGrabber.from(videoDevice, config)) {
while (!isInterrupted()) {
Optional<VideoFrame> frame = grabber.grabFrame();
+ if (isPaused) {
+ synchronized (grabberThread) {
+ wait();
+ }
+ }
if (frameConsumer != null) frame.ifPresent(frameConsumer);
frame.ifPresent(VideoMode.this::onVideoFrame);
// timeIt("frame", grabber::grabFrame);
}
- } catch (IOException e) {
+ } catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}