Add video device to config
[kaka/cakelight.git] / src / kaka / cakelight / mode / VideoMode.java
index 91a9f04..a8397dc 100644 (file)
@@ -15,6 +15,7 @@ public class VideoMode extends Mode {
     private Thread grabberThread;
     private Consumer<VideoFrame> frameConsumer;
     private VideoDeviceListener deviceListener;
+    private boolean isPaused = false;
 
     public VideoMode() {
         deviceListener = new VideoDeviceListener();
@@ -24,13 +25,33 @@ public class VideoMode extends Mode {
     @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() {
+        isPaused = true;
+    }
+
+    @Override
+    public void resume() {
+        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) {
@@ -40,11 +61,16 @@ public class VideoMode extends Mode {
                 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();
                 }
             }