Added a draft for an ambient mode
[kaka/cakelight.git] / src / kaka / cakelight / VideoMode.java
index e0dc386..6da36f2 100644 (file)
@@ -5,7 +5,7 @@ import java.io.IOException;
 import java.util.Optional;
 import java.util.function.Consumer;
 
-public class VideoMode implements Mode, Consumer<Optional<File>> {
+public class VideoMode extends Mode {
     private Configuration config;
     private Thread thread;
     private Consumer<Frame> frameConsumer;
@@ -13,7 +13,7 @@ public class VideoMode implements Mode, Consumer<Optional<File>> {
 
     public VideoMode() {
         deviceListener = new VideoDeviceListener();
-        deviceListener.onVideoDeviceChange(this);
+        deviceListener.onVideoDeviceChange(this::onVideoDeviceChange);
     }
 
     @Override
@@ -34,8 +34,9 @@ public class VideoMode implements Mode, Consumer<Optional<File>> {
             public void run() {
                 try (FrameGrabber grabber = FrameGrabber.from(videoDevice, config)) {
                     while (!isInterrupted()) {
-//                        Optional<Frame> frame = grabber.grabFrame();
-                        grabber.grabFrame().ifPresent(frameConsumer);
+                        Optional<Frame> frame = grabber.grabFrame();
+                        if (frameConsumer != null) frame.ifPresent(frameConsumer);
+                        frame.ifPresent(VideoMode.this::onFrame);
 //                        timeIt("frame", grabber::grabFrame);
                         // TODO: process frame
                         // TODO: save where the LedController can access it
@@ -48,12 +49,15 @@ public class VideoMode implements Mode, Consumer<Optional<File>> {
         thread.start();
     }
 
-    public void onFrame(Consumer<Frame> consumer) {
+    public void onVideoFrame(Consumer<Frame> consumer) {
         frameConsumer = consumer;
     }
 
-    @Override
-    public void accept(Optional<File> videoDevice) {
+    private void onFrame(Frame frame) {
+        updateWithFrame(frame.getLedFrame());
+    }
+
+    public void onVideoDeviceChange(Optional<File> videoDevice) {
         // Should only happen when this mode is active!
         if (thread != null) {
             thread.interrupt();