From patchwork Wed Feb 6 06:08:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 541 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C3BAE6104C for ; Wed, 6 Feb 2019 07:08:29 +0100 (CET) Received: from pendragon.ideasonboard.com (d51A4137F.access.telenet.be [81.164.19.127]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 7146C2D7 for ; Wed, 6 Feb 2019 07:08:29 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1549433309; bh=YV6PONACtOw0/26YMEsBf0W+VyGYT+We1vhnqpPw4Ag=; h=From:To:Subject:Date:In-Reply-To:References:From; b=cyA8wSHf1/mISH5pFB/O9Q6ARYR5YWzh/WjtQlVLQxLo0aQkjTZju1QC6+uK5L2XF pzyQyKz22yajNQGjj4h2yPUGan9fZqs1ft/X+iWXB8hq/jDMhTDuaS0qdeHuBSC0TA 1mES401DvDhCwsAofjHEvcLRfoSVqqZbSdrESWVM= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Wed, 6 Feb 2019 08:08:17 +0200 Message-Id: <20190206060818.13907-27-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190206060818.13907-1-laurent.pinchart@ideasonboard.com> References: <20190206060818.13907-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 26/27] cam: Add BufferWriter helper X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 06 Feb 2019 06:08:30 -0000 From: Niklas Söderlund Add a simpler helper to allow the cam application to write raw captured frames to disk. Signed-off-by: Niklas Söderlund Signed-off-by: Jacopo Mondi Signed-off-by: Kieran Bingham Signed-off-by: Laurent Pinchart --- src/cam/buffer_writer.cpp | 63 +++++++++++++++++++++++++++++++++++++++ src/cam/buffer_writer.h | 25 ++++++++++++++++ src/cam/meson.build | 1 + 3 files changed, 89 insertions(+) create mode 100644 src/cam/buffer_writer.cpp create mode 100644 src/cam/buffer_writer.h diff --git a/src/cam/buffer_writer.cpp b/src/cam/buffer_writer.cpp new file mode 100644 index 000000000000..2d2258b4cd1c --- /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 +#include +#include +#include +#include +#include + +#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; +} diff --git a/src/cam/buffer_writer.h b/src/cam/buffer_writer.h new file mode 100644 index 000000000000..9705773e0e39 --- /dev/null +++ b/src/cam/buffer_writer.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * buffer_writer.h - Buffer writer + */ +#ifndef __LIBCAMERA_BUFFER_WRITER_H__ +#define __LIBCAMERA_BUFFER_WRITER_H__ + +#include + +#include + +class BufferWriter +{ +public: + BufferWriter(const std::string &pattern = "frame-#.bin"); + + int write(libcamera::Buffer *buffer); + +private: + std::string pattern_; +}; + +#endif /* __LIBCAMERA_BUFFER_WRITER_H__ */ diff --git a/src/cam/meson.build b/src/cam/meson.build index cc647523955f..1a1244fe25cd 100644 --- a/src/cam/meson.build +++ b/src/cam/meson.build @@ -1,4 +1,5 @@ cam_sources = files([ + 'buffer_writer.cpp', 'event_loop.cpp', 'main.cpp', 'options.cpp',