-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfile_sink.cpp
More file actions
161 lines (134 loc) · 4.04 KB
/
file_sink.cpp
File metadata and controls
161 lines (134 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* file_sink.cpp - File Sink
*/
#include <assert.h>
#include <fcntl.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string.h>
#include <unistd.h>
#include <libcamera/camera.h>
#include "image.h"
#include "file_sink.h"
using namespace libcamera;
FileSink::FileSink([[maybe_unused]] const libcamera::Camera *camera,
const std::map<const libcamera::Stream *, std::string> &streamNames,
const std::string &pattern)
:
#ifdef HAVE_TIFF
camera_(camera),
#endif
streamNames_(streamNames), pattern_(pattern)
{
}
FileSink::~FileSink()
{
}
int FileSink::configure(const libcamera::CameraConfiguration &config)
{
int ret = FrameSink::configure(config);
if (ret < 0)
return ret;
return 0;
}
void FileSink::mapBuffer(FrameBuffer *buffer)
{
std::unique_ptr<Image> image =
Image::fromFrameBuffer(buffer, Image::MapMode::ReadOnly);
assert(image != nullptr);
mappedBuffers_[buffer] = std::move(image);
}
bool FileSink::processRequest(Request *request)
{
// std::cout<<"\nInside Process Request FileSink"<<std::endl;
for (auto [stream, buffer] : request->buffers())
{
// std::cout<<"\nBefore for loop"<<std::endl;
writeBuffer(stream, buffer, request->metadata());
// std::cout<<"\nAfter for loop"<<std::endl;
}
// std::cout<<"Leaving Process Request FileSink"<<std::endl;
return true;
}
void FileSink::writeBuffer(const Stream *stream, FrameBuffer *buffer,
[[maybe_unused]] const ControlList &metadata)
{
// std::cout<<"\nStart writeBuffer"<<std::endl;
std::string filename;
size_t pos;
int fd, ret = 0;
// std::cout<<"\nFile name = "<<filename<<std::endl;
if (!pattern_.empty())
filename = pattern_;
#ifdef HAVE_TIFF
bool dng = filename.find(".dng", filename.size() - 4) != std::string::npos;
#endif /* HAVE_TIFF */
if (filename.empty() || filename.back() == '/')
filename += "frame-#.bin";
// std::cout<<"\nFile name = "<<filename<<std::endl;
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());
}
// std::cout<<"\nFile name = "<<filename<<std::endl;
Image *image = mappedBuffers_[buffer].get();
// std::cout<<"\nDefine image"<<std::endl;
#ifdef HAVE_TIFF
if (dng) {
ret = DNGWriter::write(filename.c_str(), camera_,
stream->configuration(), metadata,
buffer, image->data(0).data());
if (ret < 0)
std::cerr << "failed to write DNG file `" << filename
<< "'" << std::endl;
return;
}
#endif /* HAVE_TIFF */
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;
}
// std::cout<<"\nFile opened"<<std::endl;
for (unsigned int i = 0; i < buffer->planes().size(); ++i) {
// std::cout<<"\ninside for loop of planes"<<std::endl;
/*
* This was formerly a local "const FrameMetadata::Plane &"
* however this causes a false positive warning for dangling
* references on gcc 13.
*/
const unsigned int bytesused = buffer->metadata().planes()[i].bytesused;
// std::cout<<"\nBytesused = "<<bytesused<<std::endl;
Span<uint8_t> data = image->data(i);
// std::cout<<"\nData obtained "<<std::endl;
const unsigned int length = std::min<unsigned int>(bytesused, data.size());
if (bytesused > data.size())
std::cerr << "payload size " << bytesused
<< " larger than plane size " << data.size()
<< std::endl;
ret = ::write(fd, data.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);
}