1 package kaka.cakelight;
4 import java.util.Optional;
6 import static kaka.cakelight.Main.log;
8 public class FrameGrabber implements Closeable {
9 private Configuration config;
11 private int bytesPerFrame;
12 private InputStream fileStream;
14 private FrameGrabber() {
17 public static FrameGrabber from(File videoDevice, Configuration config) {
18 FrameGrabber fg = new FrameGrabber();
20 fg.file = videoDevice;
21 fg.bytesPerFrame = config.video.width * config.video.height * config.video.bpp;
26 private boolean prepare() {
28 fileStream = new FileInputStream(file);
30 } catch (FileNotFoundException e) {
31 // TODO: handle java.io.FileNotFoundException: /dev/video1 (Permission denied)
38 * Must be run in the same thread as {@link #prepare}.
40 public Optional<VideoFrame> grabFrame() {
42 byte[] data = new byte[bytesPerFrame];
43 int count = fileStream.read(data);
44 if (count != bytesPerFrame) {
45 log("Expected to read " + bytesPerFrame + " bytes per frame but read " + count);
47 return Optional.of(VideoFrame.of(data, config));
48 } catch (IOException e) {
52 return Optional.empty();
56 public void close() throws IOException {