summaryrefslogtreecommitdiff
path: root/src/cam/file_sink.cpp
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-05-19 03:21:30 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2021-08-05 16:23:11 +0300
commitab623b4738130048ff12d632cec6778e1187d9dd (patch)
treec2e97b1885e395202c90dde1311e58bffbb3a2b2 /src/cam/file_sink.cpp
parent02001fecb0f578438bcfb27cc7496d721c6a18d1 (diff)
cam: Rename BufferWriter to FileSink
Rename the BufferWriter class to FileSink to establish a common naming scheme for all sinks. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>
Diffstat (limited to 'src/cam/file_sink.cpp')
-rw-r--r--src/cam/file_sink.cpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/src/cam/file_sink.cpp b/src/cam/file_sink.cpp
new file mode 100644
index 00000000..2d30694a
--- /dev/null
+++ b/src/cam/file_sink.cpp
@@ -0,0 +1,128 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2019, Google Inc.
+ *
+ * file_sink.cpp - File Sink
+ */
+
+#include <fcntl.h>
+#include <iomanip>
+#include <iostream>
+#include <sstream>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <libcamera/camera.h>
+
+#include "file_sink.h"
+
+using namespace libcamera;
+
+FileSink::FileSink(const std::string &pattern)
+ : pattern_(pattern)
+{
+}
+
+FileSink::~FileSink()
+{
+ for (auto &iter : mappedBuffers_) {
+ void *memory = iter.second.first;
+ unsigned int length = iter.second.second;
+ munmap(memory, length);
+ }
+ mappedBuffers_.clear();
+}
+
+int FileSink::configure(const libcamera::CameraConfiguration &config)
+{
+ int ret = FrameSink::configure(config);
+ if (ret < 0)
+ return ret;
+
+ streamNames_.clear();
+ for (unsigned int index = 0; index < config.size(); ++index) {
+ const StreamConfiguration &cfg = config.at(index);
+ streamNames_[cfg.stream()] = "stream" + std::to_string(index);
+ }
+
+ return 0;
+}
+
+void FileSink::mapBuffer(FrameBuffer *buffer)
+{
+ for (const FrameBuffer::Plane &plane : buffer->planes()) {
+ void *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED,
+ plane.fd.fd(), 0);
+
+ mappedBuffers_[plane.fd.fd()] =
+ std::make_pair(memory, plane.length);
+ }
+}
+
+bool FileSink::processRequest(Request *request)
+{
+ for (auto [stream, buffer] : request->buffers())
+ writeBuffer(stream, buffer);
+
+ return true;
+}
+
+void FileSink::writeBuffer(const Stream *stream, FrameBuffer *buffer)
+{
+ std::string filename;
+ size_t pos;
+ int fd, ret = 0;
+
+ if (!pattern_.empty())
+ filename = pattern_;
+
+ if (filename.empty() || filename.back() == '/')
+ filename += "frame-#.bin";
+
+ pos = filename.find_first_of('#');
+ if (pos != std::string::npos) {
+ std::stringstream ss;
+ ss << streamNames_[stream] << "-" << std::setw(6)
+ << std::setfill('0') << buffer->metadata().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) {
+ ret = -errno;
+ std::cerr << "failed to open file " << filename << ": "
+ << strerror(-ret) << std::endl;
+ return;
+ }
+
+ for (unsigned int i = 0; i < buffer->planes().size(); ++i) {
+ const FrameBuffer::Plane &plane = buffer->planes()[i];
+ const FrameMetadata::Plane &meta = buffer->metadata().planes[i];
+
+ void *data = mappedBuffers_[plane.fd.fd()].first;
+ unsigned int length = std::min(meta.bytesused, plane.length);
+
+ if (meta.bytesused > plane.length)
+ std::cerr << "payload size " << meta.bytesused
+ << " larger than plane size " << plane.length
+ << std::endl;
+
+ 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);
+}