X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fkaka%2Fcakelight%2FVideoMode.java;h=134fa2d33ff574f837af3b68dbd7b0e967ddf4ed;hb=9abb178697c2d522a21370eeddcc1d635fc6cc47;hp=53cf09b5f8076562a2a1f78fcf4525ffbd08b17f;hpb=100b82fe1c5ada6ef2ce768bf7b9f6f469650e11;p=kaka%2Fcakelight.git
diff --git a/src/kaka/cakelight/VideoMode.java b/src/kaka/cakelight/VideoMode.java
index 53cf09b..134fa2d 100644
--- a/src/kaka/cakelight/VideoMode.java
+++ b/src/kaka/cakelight/VideoMode.java
@@ -1,49 +1,65 @@
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 extends Mode {
private Configuration config;
- private Thread thread;
- private Consumer frameConsumer;
+ private Thread grabberThread;
+ private Consumer frameConsumer;
+ private VideoDeviceListener deviceListener;
+
+ public VideoMode() {
+ deviceListener = new VideoDeviceListener();
+ deviceListener.onVideoDeviceChange(this::onVideoDeviceChange);
+ }
@Override
public void enter(Configuration config) {
this.config = config;
- startGrabberThread();
+ deviceListener.startListening();
}
@Override
public void exit() {
- thread.interrupt();
+ grabberThread.interrupt();
+ deviceListener.stopListening();
}
- private void startGrabberThread() {
+ private void startGrabberThread(File videoDevice) {
assert frameConsumer != null;
- thread = new Thread() {
+ grabberThread = new Thread() {
public void run() {
- try (FrameGrabber grabber = FrameGrabber.from(config)) {
+ try (FrameGrabber grabber = FrameGrabber.from(videoDevice, config)) {
while (!isInterrupted()) {
-// Optional frame = grabber.grabFrame();
- grabber.grabFrame().ifPresent(frameConsumer);
+ Optional frame = grabber.grabFrame();
+ if (frameConsumer != null) frame.ifPresent(frameConsumer);
+ frame.ifPresent(VideoMode.this::onVideoFrame);
// timeIt("frame", grabber::grabFrame);
- // TODO: process frame
- // TODO: save where the LedController can access it
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
- thread.start();
+ grabberThread.start();
}
- public void onFrame(Consumer consumer) {
+ public void onVideoFrame(Consumer consumer) {
frameConsumer = consumer;
}
+
+ private void onVideoFrame(VideoFrame frame) {
+ updateWithFrame(frame.getLedFrame());
+ }
+
+ public void onVideoDeviceChange(Optional videoDevice) {
+ // Should only happen when this mode is active!
+ if (grabberThread != null) {
+ grabberThread.interrupt();
+ }
+ videoDevice.ifPresent(this::startGrabberThread);
+ }
}