From ab623b4738130048ff12d632cec6778e1187d9dd Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 19 May 2020 03:21:30 +0300 Subject: cam: Rename BufferWriter to FileSink MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rename the BufferWriter class to FileSink to establish a common naming scheme for all sinks. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Paul Elder Reviewed-by: Kieran Bingham Reviewed-by: Umang Jain --- src/cam/buffer_writer.cpp | 128 --------------------------------------------- src/cam/buffer_writer.h | 38 -------------- src/cam/camera_session.cpp | 6 +-- src/cam/file_sink.cpp | 128 +++++++++++++++++++++++++++++++++++++++++++++ src/cam/file_sink.h | 38 ++++++++++++++ src/cam/meson.build | 2 +- 6 files changed, 170 insertions(+), 170 deletions(-) delete mode 100644 src/cam/buffer_writer.cpp delete mode 100644 src/cam/buffer_writer.h create mode 100644 src/cam/file_sink.cpp create mode 100644 src/cam/file_sink.h diff --git a/src/cam/buffer_writer.cpp b/src/cam/buffer_writer.cpp deleted file mode 100644 index 2f4b2b02..00000000 --- a/src/cam/buffer_writer.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * buffer_writer.cpp - Buffer writer - */ - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "buffer_writer.h" - -using namespace libcamera; - -BufferWriter::BufferWriter(const std::string &pattern) - : pattern_(pattern) -{ -} - -BufferWriter::~BufferWriter() -{ - for (auto &iter : mappedBuffers_) { - void *memory = iter.second.first; - unsigned int length = iter.second.second; - munmap(memory, length); - } - mappedBuffers_.clear(); -} - -int BufferWriter::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 BufferWriter::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 BufferWriter::processRequest(Request *request) -{ - for (auto [stream, buffer] : request->buffers()) - writeBuffer(stream, buffer); - - return true; -} - -void BufferWriter::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); -} diff --git a/src/cam/buffer_writer.h b/src/cam/buffer_writer.h deleted file mode 100644 index 32bb6ed5..00000000 --- a/src/cam/buffer_writer.h +++ /dev/null @@ -1,38 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * buffer_writer.h - Buffer writer - */ -#ifndef __CAM_BUFFER_WRITER_H__ -#define __CAM_BUFFER_WRITER_H__ - -#include -#include - -#include - -#include "frame_sink.h" - -class BufferWriter : public FrameSink -{ -public: - BufferWriter(const std::string &pattern = ""); - ~BufferWriter(); - - int configure(const libcamera::CameraConfiguration &config) override; - - void mapBuffer(libcamera::FrameBuffer *buffer) override; - - bool processRequest(libcamera::Request *request) override; - -private: - void writeBuffer(const libcamera::Stream *stream, - libcamera::FrameBuffer *buffer); - - std::map streamNames_; - std::string pattern_; - std::map> mappedBuffers_; -}; - -#endif /* __CAM_BUFFER_WRITER_H__ */ diff --git a/src/cam/camera_session.cpp b/src/cam/camera_session.cpp index f91b5234..f34a6ed5 100644 --- a/src/cam/camera_session.cpp +++ b/src/cam/camera_session.cpp @@ -13,9 +13,9 @@ #include #include -#include "buffer_writer.h" #include "camera_session.h" #include "event_loop.h" +#include "file_sink.h" #include "main.h" #include "stream_options.h" @@ -163,9 +163,9 @@ int CameraSession::start() if (options_.isSet(OptFile)) { if (!options_[OptFile].toString().empty()) - sink_ = std::make_unique(options_[OptFile]); + sink_ = std::make_unique(options_[OptFile]); else - sink_ = std::make_unique(); + sink_ = std::make_unique(); } if (sink_) { 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 +#include +#include +#include +#include +#include +#include + +#include + +#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); +} diff --git a/src/cam/file_sink.h b/src/cam/file_sink.h new file mode 100644 index 00000000..c3eb230a --- /dev/null +++ b/src/cam/file_sink.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * file_sink.h - File Sink + */ +#ifndef __CAM_FILE_SINK_H__ +#define __CAM_FILE_SINK_H__ + +#include +#include + +#include + +#include "frame_sink.h" + +class FileSink : public FrameSink +{ +public: + FileSink(const std::string &pattern = ""); + ~FileSink(); + + int configure(const libcamera::CameraConfiguration &config) override; + + void mapBuffer(libcamera::FrameBuffer *buffer) override; + + bool processRequest(libcamera::Request *request) override; + +private: + void writeBuffer(const libcamera::Stream *stream, + libcamera::FrameBuffer *buffer); + + std::map streamNames_; + std::string pattern_; + std::map> mappedBuffers_; +}; + +#endif /* __CAM_FILE_SINK_H__ */ diff --git a/src/cam/meson.build b/src/cam/meson.build index 649cc990..e692ea35 100644 --- a/src/cam/meson.build +++ b/src/cam/meson.build @@ -10,9 +10,9 @@ endif cam_enabled = true cam_sources = files([ - 'buffer_writer.cpp', 'camera_session.cpp', 'event_loop.cpp', + 'file_sink.cpp', 'frame_sink.cpp', 'main.cpp', 'options.cpp', -- cgit v1.2.1