Commit | Line | Data |
---|---|---|
67b0a758 TW |
1 | package kaka.cakelight.mode; |
2 | ||
3 | import kaka.cakelight.Configuration; | |
4 | import kaka.cakelight.FrameGrabber; | |
5 | import kaka.cakelight.VideoDeviceListener; | |
6 | import kaka.cakelight.VideoFrame; | |
4a2d6056 | 7 | |
03670958 | 8 | import java.io.File; |
4a2d6056 TW |
9 | import java.io.IOException; |
10 | import java.util.Optional; | |
100b82fe | 11 | import java.util.function.Consumer; |
4a2d6056 | 12 | |
d182b8cc | 13 | public class VideoMode extends Mode { |
4a2d6056 | 14 | private Configuration config; |
8418fbda | 15 | private Thread grabberThread; |
adc29b9a | 16 | private Consumer<VideoFrame> frameConsumer; |
03670958 TW |
17 | private VideoDeviceListener deviceListener; |
18 | ||
19 | public VideoMode() { | |
20 | deviceListener = new VideoDeviceListener(); | |
d182b8cc | 21 | deviceListener.onVideoDeviceChange(this::onVideoDeviceChange); |
03670958 | 22 | } |
4a2d6056 TW |
23 | |
24 | @Override | |
25 | public void enter(Configuration config) { | |
26 | this.config = config; | |
03670958 | 27 | deviceListener.startListening(); |
4a2d6056 TW |
28 | } |
29 | ||
30 | @Override | |
d0afa6fb TW |
31 | public void pause() { |
32 | try { | |
33 | grabberThread.wait(); | |
34 | } catch (InterruptedException e) { | |
35 | e.printStackTrace(); | |
36 | } | |
37 | } | |
38 | ||
39 | @Override | |
40 | public void resume() { | |
41 | grabberThread.notify(); | |
42 | } | |
43 | ||
44 | @Override | |
4a2d6056 | 45 | public void exit() { |
8418fbda | 46 | grabberThread.interrupt(); |
03670958 | 47 | deviceListener.stopListening(); |
4a2d6056 TW |
48 | } |
49 | ||
03670958 | 50 | private void startGrabberThread(File videoDevice) { |
100b82fe | 51 | assert frameConsumer != null; |
8418fbda | 52 | grabberThread = new Thread() { |
4a2d6056 | 53 | public void run() { |
03670958 | 54 | try (FrameGrabber grabber = FrameGrabber.from(videoDevice, config)) { |
4a2d6056 | 55 | while (!isInterrupted()) { |
adc29b9a | 56 | Optional<VideoFrame> frame = grabber.grabFrame(); |
03b67a73 | 57 | if (frameConsumer != null) frame.ifPresent(frameConsumer); |
adc29b9a | 58 | frame.ifPresent(VideoMode.this::onVideoFrame); |
100b82fe | 59 | // timeIt("frame", grabber::grabFrame); |
4a2d6056 TW |
60 | } |
61 | } catch (IOException e) { | |
62 | e.printStackTrace(); | |
63 | } | |
64 | } | |
65 | }; | |
8418fbda | 66 | grabberThread.start(); |
4a2d6056 | 67 | } |
100b82fe | 68 | |
adc29b9a | 69 | public void onVideoFrame(Consumer<VideoFrame> consumer) { |
100b82fe TW |
70 | frameConsumer = consumer; |
71 | } | |
03670958 | 72 | |
adc29b9a | 73 | private void onVideoFrame(VideoFrame frame) { |
6b569670 | 74 | updateWithFrame(frame.getLedFrame()); |
03b67a73 TW |
75 | } |
76 | ||
d182b8cc | 77 | public void onVideoDeviceChange(Optional<File> videoDevice) { |
03670958 | 78 | // Should only happen when this mode is active! |
8418fbda TW |
79 | if (grabberThread != null) { |
80 | grabberThread.interrupt(); | |
03670958 TW |
81 | } |
82 | videoDevice.ifPresent(this::startGrabberThread); | |
83 | } | |
4a2d6056 | 84 | } |