Dolda2000 GitWeb
/
kaka
/
cakelight.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Added a device listener
[kaka/cakelight.git]
/
src
/
kaka
/
cakelight
/
VideoMode.java
diff --git
a/src/kaka/cakelight/VideoMode.java
b/src/kaka/cakelight/VideoMode.java
index
53cf09b
..
e0dc386
100644
(file)
--- a/
src/kaka/cakelight/VideoMode.java
+++ b/
src/kaka/cakelight/VideoMode.java
@@
-1,33
+1,38
@@
package kaka.cakelight;
package kaka.cakelight;
+import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.function.Consumer;
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<Optional<File>> {
private Configuration config;
private Thread thread;
private Consumer<Frame> frameConsumer;
private Configuration config;
private Thread thread;
private Consumer<Frame> frameConsumer;
+ private VideoDeviceListener deviceListener;
+
+ public VideoMode() {
+ deviceListener = new VideoDeviceListener();
+ deviceListener.onVideoDeviceChange(this);
+ }
@Override
public void enter(Configuration config) {
this.config = config;
@Override
public void enter(Configuration config) {
this.config = config;
-
startGrabberThread
();
+
deviceListener.startListening
();
}
@Override
public void exit() {
thread.interrupt();
}
@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() {
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> frame = grabber.grabFrame();
grabber.grabFrame().ifPresent(frameConsumer);
while (!isInterrupted()) {
// Optional<Frame> frame = grabber.grabFrame();
grabber.grabFrame().ifPresent(frameConsumer);
@@
-46,4
+51,13
@@
public class VideoMode implements Mode {
public void onFrame(Consumer<Frame> consumer) {
frameConsumer = consumer;
}
public void onFrame(Consumer<Frame> consumer) {
frameConsumer = consumer;
}
+
+ @Override
+ public void accept(Optional<File> videoDevice) {
+ // Should only happen when this mode is active!
+ if (thread != null) {
+ thread.interrupt();
+ }
+ videoDevice.ifPresent(this::startGrabberThread);
+ }
}
}