diff options
Diffstat (limited to 'src/cam')
-rw-r--r-- | src/cam/buffer_writer.cpp | 63 | ||||
-rw-r--r-- | src/cam/buffer_writer.h | 25 | ||||
-rw-r--r-- | src/cam/meson.build | 1 |
3 files changed, 89 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; +} diff --git a/src/cam/buffer_writer.h b/src/cam/buffer_writer.h new file mode 100644 index 00000000..9705773e --- /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 <string> + +#include <libcamera/buffer.h> + +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 cc647523..1a1244fe 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', |