X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fkaka%2Fcakelight%2FVideoMode.java;h=e0dc386417a58802c02b4adaca08f389646f3bd3;hb=03670958a5eab132a4f1a6ae5ae58b88f55bfe2f;hp=be2ee4121526939ed4054e3941403b8db0b271d9;hpb=4a2d60564647052562fad28644904298ba83667b;p=kaka%2Fcakelight.git diff --git a/src/kaka/cakelight/VideoMode.java b/src/kaka/cakelight/VideoMode.java index be2ee41..e0dc386 100644 --- a/src/kaka/cakelight/VideoMode.java +++ b/src/kaka/cakelight/VideoMode.java @@ -1,33 +1,42 @@ package kaka.cakelight; +import java.io.File; import java.io.IOException; import java.util.Optional; +import java.util.function.Consumer; -import static kaka.cakelight.Main.log; -import static kaka.cakelight.Main.timeIt; - -public class VideoMode implements Mode { +public class VideoMode implements Mode, Consumer> { private Configuration config; private Thread thread; + private Consumer frameConsumer; + private VideoDeviceListener deviceListener; + + public VideoMode() { + deviceListener = new VideoDeviceListener(); + deviceListener.onVideoDeviceChange(this); + } @Override public void enter(Configuration config) { this.config = config; - startGrabberThread(); + deviceListener.startListening(); } @Override public void exit() { thread.interrupt(); + deviceListener.stopListening(); } - private void startGrabberThread() { + private void startGrabberThread(File videoDevice) { + assert frameConsumer != null; thread = new Thread() { public void run() { - try (FrameGrabber grabber = FrameGrabber.from(config)) { + try (FrameGrabber grabber = FrameGrabber.from(videoDevice, config)) { while (!isInterrupted()) { // Optional frame = grabber.grabFrame(); - timeIt("frame", grabber::grabFrame); + grabber.grabFrame().ifPresent(frameConsumer); +// timeIt("frame", grabber::grabFrame); // TODO: process frame // TODO: save where the LedController can access it } @@ -38,4 +47,17 @@ public class VideoMode implements Mode { }; thread.start(); } + + public void onFrame(Consumer consumer) { + frameConsumer = consumer; + } + + @Override + public void accept(Optional videoDevice) { + // Should only happen when this mode is active! + if (thread != null) { + thread.interrupt(); + } + videoDevice.ifPresent(this::startGrabberThread); + } }