summaryrefslogtreecommitdiff
path: root/src/cam/buffer_writer.cpp
diff options
context:
space:
mode:
authorNiklas Söderlund <niklas.soderlund@ragnatech.se>2019-01-29 03:34:31 +0100
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-02-06 08:03:03 +0200
commit46ca8eeca06de7e0657d628325876e7f0a15ef0b (patch)
tree29af3caa3d05e7cdcf10207ec23d4e545299ce92 /src/cam/buffer_writer.cpp
parentdbe7a28377b10804d2aa345dcfb215e97452e406 (diff)
cam: Add BufferWriter helper
Add a simpler helper to allow the cam application to write raw captured frames to disk. Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'src/cam/buffer_writer.cpp')
-rw-r--r--src/cam/buffer_writer.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/cam/buffer_writer.cpp b/src/cam/buffer_writer.cpp
new file mode 100644
index 00000000..2d2258b4
--- /dev/null
+++ b/src/cam/buffer_writer.cpp
@@ -0,0 +1,63 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2019, Google Inc.
+ *
+ * buffer_writer.cpp - Buffer writer
+ */
+
+#include <fcntl.h>
+#include <iomanip>
+#include <iostream>
+#include <sstream>
+#include <string.h>
+#include <unistd.h>
+
+#include "buffer_writer.h"
+
+BufferWriter::BufferWriter(const std::string &pattern)
+ : pattern_(pattern)
+{
+}
+
+int BufferWriter::write(libcamera::Buffer *buffer)
+{
+ std::string filename;
+ size_t pos;
+ int fd, ret = 0;
+
+ filename = pattern_;
+ pos = filename.find_first_of('#');
+ if (pos != std::string::npos) {
+ std::stringstream ss;
+ ss << std::setw(6) << std::setfill('0') << buffer->sequence();
+ filename.replace(pos, 1, ss.str());
+ }
+
+ fd = open(filename.c_str(), O_CREAT | O_WRONLY |
+ (pos == std::string::npos ? O_APPEND : O_TRUNC),
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
+ if (fd == -1)
+ return -errno;
+
+ for (libcamera::Plane &plane : buffer->planes()) {
+ void *data = plane.mem();
+ unsigned int length = plane.length();
+
+ ret = ::write(fd, data, length);
+ if (ret < 0) {
+ ret = -errno;
+ std::cerr << "write error: " << strerror(-ret)
+ << std::endl;
+ break;
+ } else if (ret != (int)length) {
+ std::cerr << "write error: only " << ret
+ << " bytes written instead of "
+ << length << std::endl;
+ break;
+ }
+ }
+
+ close(fd);
+
+ return ret;
+}