frame_handler_impl.cpp
Go to the documentation of this file.
1 /*
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2019, Analog Devices, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "frame_handler_impl.h"
34 #ifdef USE_GLOG
35 #include <glog/logging.h>
36 #else
37 #include <aditof/log.h>
38 #endif
39 #include <cstring>
40 #include <memory>
41 
42 using namespace aditof;
43 
45  : m_concatFrames(true), m_enableMultithreading(false),
46  m_customFormat(false), m_bitsInDepth(0), m_bitsInAB(0), m_bitsInConf(0),
47  m_frameWidth(0), m_frameHeight(0), m_frameIndex(0), m_fileCreated(false),
48  m_endOfFile(false), m_dir("."), m_pos(0), m_threadRunning(false) {}
49 
51  if (m_threadWorker.joinable()) {
52  m_threadWorker.join();
53  }
54 }
55 
57  Status status = Status::OK;
58  m_dir = filePath;
59  m_fileCreated = false;
60  return status;
61 }
62 
64  Status status = Status::OK;
65  m_fullInputFileName = fullFileName;
66  m_pos = 0;
67  return status;
68 }
69 
71  const std::string &fileName) {
72  Status status = Status::OK;
73 
74  if (m_concatFrames) {
75  if (!m_fileCreated) {
76  status = createFile(fileName);
77  } else {
79  std::ios::app | std::ios::binary);
80  m_file.seekg(std::ios::end);
81  }
82  } else {
83  status = createFile(fileName);
84  }
85 
86  m_inputFileName = fileName;
87 
88  if (status != Status::OK) {
89  LOG(ERROR) << "Failed to create file!";
90  return status;
91  }
92 
93  //Store frames in file in followind order: metadata depth ab conf
94  uint16_t *metaData;
95  uint16_t *depthData;
96  uint16_t *abData;
97  uint16_t *confData;
98  uint16_t *xyzData;
99 
100  frame.getData("metadata", &metaData);
101 
102  Metadata metadataStruct;
103  frame.getMetadataStruct(metadataStruct);
104 
105  //at first we assume that we have metadata enabled by default
106  //TO DO: implement use-case where we don't have metadata
107  m_file.write(reinterpret_cast<char *>(metaData), METADATA_SIZE);
108 
109  if (metadataStruct.bitsInDepth) {
110  frame.getData("depth", &depthData);
111  m_file.write(reinterpret_cast<char *>(depthData),
112  metadataStruct.width * metadataStruct.height * 2);
113  }
114 
115  if (metadataStruct.bitsInAb) {
116  frame.getData("ab", &abData);
117  m_file.write(reinterpret_cast<char *>(abData),
118  metadataStruct.width * metadataStruct.height * 2);
119  }
120 
121  if (metadataStruct.bitsInConfidence) {
122  frame.getData("conf", &confData);
123  m_file.write(reinterpret_cast<char *>(confData),
124  metadataStruct.width * metadataStruct.height * 4);
125  }
126 
127  if (metadataStruct.xyzEnabled) {
128  frame.getData("xyz", &xyzData);
129  m_file.write(reinterpret_cast<char *>(xyzData),
130  metadataStruct.width * metadataStruct.height * 6);
131  }
132 
133  m_file.close();
134 
135  if (!m_frameQueue.empty()) {
136  m_mutex.lock();
137  m_frameQueue.pop();
138  m_frameNameQueue.pop();
139  m_mutex.unlock();
140  }
141 
142  return status;
143 }
144 
145 Status
147  const std::string &fileName) {
148 
149  using namespace aditof;
150  Status status = Status::OK;
151 
152  m_mutex.lock();
153  m_frameQueue.push(std::move(frame));
154  m_frameNameQueue.push(fileName);
155  m_mutex.unlock();
156 
157  frame = Frame();
158 
159  if (!m_threadRunning) {
160  if (m_threadWorker.joinable()) {
161  m_threadWorker.join();
162  }
163 
165  std::thread(std::bind(&FrameHandlerImpl::threadWritter, this));
166  }
167 
168  return status;
169 }
170 
172  m_threadRunning = true;
173  aditof::Status status;
174 
175  while (!m_frameQueue.empty()) {
176  status =
178  if (status != aditof::Status::OK)
179  return; // status;
180  }
181 
182  m_threadRunning = false;
183  return; // status;
184 }
185 
187  const std::string &fullFileName) {
188  Status status = Status::OK;
189  if (m_fullInputFileName.empty() && fullFileName.empty()) {
190  LOG(ERROR) << "No input file provided!";
191  return Status::GENERIC_ERROR;
192  }
193 
194  if (fullFileName != m_fullInputFileName) {
195  m_fullInputFileName = fullFileName;
196  m_pos = 0;
197  }
198 
199  m_file = std::fstream(m_fullInputFileName, std::ios::in | std::ios::binary);
200 
201  if (!m_file) {
202  LOG(ERROR) << "Failed open file!";
203  return Status::GENERIC_ERROR;
204  }
205 
206  m_file.seekg(m_pos, std::ios::beg);
207  if (m_file.eof()) {
208  LOG(WARNING) << "End of file reached! No more frames left to read.";
209  m_file.close();
210  return Status::UNAVAILABLE;
211  }
212 
213  m_file.read(reinterpret_cast<char *>(&m_metadataStruct), METADATA_SIZE);
214 
220 
221  FrameDataDetails frDataDetails;
222  frDataDetails.type = "metadata";
223  frDataDetails.width = METADATA_SIZE;
224  frDataDetails.height = 1;
225  frDataDetails.subelementSize = sizeof(uint8_t);
226  frDataDetails.subelementsPerElement = 1;
227  m_frDetails.dataDetails.emplace_back(frDataDetails);
228 
230  frDataDetails.type = "depth";
231  frDataDetails.width = m_metadataStruct.width;
232  frDataDetails.height = m_metadataStruct.height;
233  frDataDetails.subelementSize = sizeof(uint16_t);
234  frDataDetails.subelementsPerElement = 1;
235  m_frDetails.dataDetails.emplace_back(frDataDetails);
236  }
237 
239  frDataDetails.type = "ab";
240  frDataDetails.width = m_metadataStruct.width;
241  frDataDetails.height = m_metadataStruct.height;
242  frDataDetails.subelementSize = sizeof(uint16_t);
243  frDataDetails.subelementsPerElement = 1;
244  m_frDetails.dataDetails.emplace_back(frDataDetails);
245  }
246 
248  frDataDetails.type = "conf";
249  frDataDetails.width = m_metadataStruct.width;
250  frDataDetails.height = m_metadataStruct.height;
251  frDataDetails.subelementSize = sizeof(float);
252  frDataDetails.subelementsPerElement = 1;
253  m_frDetails.dataDetails.emplace_back(frDataDetails);
254  }
255 
257  frDataDetails.type = "xyz";
258  frDataDetails.width = m_metadataStruct.width;
259  frDataDetails.height = m_metadataStruct.height;
260  frDataDetails.subelementSize = sizeof(uint16_t);
261  frDataDetails.subelementsPerElement = 3;
262  m_frDetails.dataDetails.emplace_back(frDataDetails);
263  }
264 
265  status = frame.setDetails(m_frDetails);
266  if (status != aditof::Status::OK) {
267  LOG(ERROR) << "Failed to set frame details.";
268  return status;
269  }
270 
271  //Read frames from the file in followind order: metadata depth ab conf xyz
272  uint16_t *metaData;
273  uint16_t *depthData;
274  uint16_t *abData;
275  uint16_t *confData;
276  uint16_t *xyzData;
277 
278  frame.getData("metadata", &metaData);
279  memcpy(metaData, reinterpret_cast<uint8_t *>(&m_metadataStruct),
280  METADATA_SIZE);
281 
283  frame.getData("depth", &depthData);
284  m_file.read(reinterpret_cast<char *>(depthData),
286  }
287 
289  frame.getData("ab", &abData);
290  m_file.read(reinterpret_cast<char *>(abData),
292  }
293 
295  frame.getData("conf", &confData);
296  m_file.read(reinterpret_cast<char *>(confData),
298  }
299 
301  frame.getData("xyz", &xyzData);
302  m_file.read(reinterpret_cast<char *>(xyzData),
304  }
305 
306  m_pos = m_file.tellg();
307  m_file.close();
308 
309  return Status::OK;
310 }
311 
313  return Status::UNAVAILABLE;
314 }
315 
317  Status status = Status::OK;
318  m_concatFrames = enable;
319 
320  return status;
321 }
322 
324  return Status::UNAVAILABLE;
325 }
326 
328  if (fileName.empty()) {
329  char time_buffer[128];
330  time_t rawtime;
331  time(&rawtime);
332  struct tm timeinfo;
333 #ifdef _WIN32
334  localtime_s(&timeinfo, &rawtime);
335 #else
336  localtime_r(&rawtime, &timeinfo);
337 #endif
338  strftime(time_buffer, sizeof(time_buffer), "%Y_%m_%d_%H_%M_%S",
339  &timeinfo);
340  m_outputFileName = "frame" + std::string(time_buffer) + "_" +
341  std::to_string(m_frameCount) + ".bin";
342  m_frameCount++;
343  } else {
344  m_outputFileName = fileName;
345  }
346 
348  std::ios::app | std::ios::out | std::ios::binary);
349 
350  if (!m_file) {
351  LOG(ERROR) << "Failed to create output file!";
352  return Status::GENERIC_ERROR;
353  }
354  m_fileCreated = true;
355 
356  return Status::OK;
357 }
358 
361  std::string fullPath;
362 #ifdef _WIN32
363  const std::string pathSeparator = "\\";
364 #else
365  const std::string pathSeparator = "//";
366 #endif
367 
368  if (m_dir.empty()) {
369  fullPath = fileName;
370  } else {
371  fullPath = m_dir + pathSeparator + fileName;
372  }
373 
374  return fullPath;
375 }
aditof::FrameDataDetails::subelementsPerElement
unsigned int subelementsPerElement
The number of sub-elements that an element has. An element is the smallest part of the image (a....
Definition: frame_definitions.h:78
localtime_r
struct tm * localtime_r(const time_t *timep, struct tm *result)
Definition: port.cc:52
METADATA_SIZE
#define METADATA_SIZE
Definition: frame_handler.h:47
FrameHandlerImpl::m_file
std::fstream m_file
Definition: frame_handler_impl.h:131
FrameHandlerImpl::m_frameNameQueue
std::queue< std::string > m_frameNameQueue
Definition: frame_handler_impl.h:139
end
GLuint GLuint end
Definition: glcorearb.h:2858
ERROR
const int ERROR
Definition: log_severity.h:60
FrameHandlerImpl::getOutputFileFullPath
std::string getOutputFileFullPath(const std::string &fileName)
Definition: frame_handler_impl.cpp:360
FrameHandlerImpl::m_mutex
std::mutex m_mutex
Definition: frame_handler_impl.h:136
log.h
aditof::Metadata::height
uint16_t height
Height of frame.
Definition: frame_definitions.h:157
aditof::FrameDataDetails::type
std::string type
The type of data that can be found in a frame. For example it could be depth data or IR data,...
Definition: frame_definitions.h:53
FrameHandlerImpl::m_threadRunning
volatile bool m_threadRunning
Definition: frame_handler_impl.h:140
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
aditof::FrameDataDetails::width
unsigned int width
The width of the frame data.
Definition: frame_definitions.h:58
aditof::Metadata::width
uint16_t width
Width of frame.
Definition: frame_definitions.h:152
FrameHandlerImpl::saveFrameToFileMultithread
aditof::Status saveFrameToFileMultithread(aditof::Frame &frame, const std::string &filename="")
Definition: frame_handler_impl.cpp:146
WARNING
const int WARNING
Definition: log_severity.h:59
FrameHandlerImpl::m_frDetails
aditof::FrameDetails m_frDetails
Definition: frame_handler_impl.h:121
google::protobuf::util::error::UNAVAILABLE
@ UNAVAILABLE
Definition: status.h:62
aditof::FrameDetails::width
unsigned int width
The width of the frame.
Definition: frame_definitions.h:114
FrameHandlerImpl::m_frameQueue
std::queue< aditof::Frame > m_frameQueue
Definition: frame_handler_impl.h:138
frame_handler_impl.h
FrameHandlerImpl::setFrameContent
aditof::Status setFrameContent(const std::string &frameContent)
Definition: frame_handler_impl.cpp:323
aditof::FrameDetails::dataDetails
std::vector< FrameDataDetails > dataDetails
A frame can have multiple types of data. For example it could hold data about depth and/or data about...
Definition: frame_definitions.h:104
aditof::FrameDetails::height
unsigned int height
The height of the frame.
Definition: frame_definitions.h:119
aditof::Metadata
Contains all of the metadata components.
Definition: frame_definitions.h:147
FrameHandlerImpl::setInputFileName
aditof::Status setInputFileName(const std::string &fullFileName)
Definition: frame_handler_impl.cpp:63
aditof
Namespace aditof.
Definition: adsd_errs.h:40
FrameHandlerImpl::threadWritter
void threadWritter()
Definition: frame_handler_impl.cpp:171
aditof::FrameDataDetails::subelementSize
unsigned int subelementSize
The size in bytes of a sub-element. A sub-element is a sub-component of an element....
Definition: frame_definitions.h:71
FrameHandlerImpl::setOutputFilePath
aditof::Status setOutputFilePath(const std::string &filePath)
Definition: frame_handler_impl.cpp:56
format
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:2773
google::protobuf::util::error::OK
@ OK
Definition: status.h:47
aditof::Metadata::imagerMode
uint8_t imagerMode
Imager mode.
Definition: frame_definitions.h:216
aditof::Frame::setDetails
SDK_API Status setDetails(const FrameDetails &details)
Configures the frame with the given details.
Definition: frame.cpp:57
aditof::FrameDataDetails
Describes the properties of a data that embedded within the frame.
Definition: frame_definitions.h:48
aditof::Frame::getData
SDK_API Status getData(const std::string &dataType, uint16_t **dataPtr)
Gets the address where the specified data is being stored.
Definition: frame.cpp:70
FrameHandlerImpl::storeFramesToSingleFile
aditof::Status storeFramesToSingleFile(bool enable)
Definition: frame_handler_impl.cpp:316
FrameHandlerImpl::setCustomFormat
aditof::Status setCustomFormat(const std::string &format)
Definition: frame_handler_impl.cpp:312
aditof::FrameDetails::totalCaptures
uint8_t totalCaptures
totalCaptures or subframes in a frame
Definition: frame_definitions.h:124
FrameHandlerImpl::createFile
aditof::Status createFile(const std::string &fileName)
Definition: frame_handler_impl.cpp:327
aditof::Status
Status
Status of any operation that the TOF sdk performs.
Definition: status_definitions.h:48
FrameHandlerImpl::m_threadWorker
std::thread m_threadWorker
Definition: frame_handler_impl.h:137
FrameHandlerImpl::m_fullInputFileName
std::string m_fullInputFileName
Definition: frame_handler_impl.h:127
aditof::Metadata::bitsInDepth
uint8_t bitsInDepth
Number of bits in depth.
Definition: frame_definitions.h:175
LOG
#define LOG(x)
Definition: sdk/include/aditof/log.h:72
FrameHandlerImpl::m_metadataStruct
aditof::Metadata m_metadataStruct
Definition: frame_handler_impl.h:122
aditof::Status::OK
@ OK
Success.
FrameHandlerImpl::~FrameHandlerImpl
~FrameHandlerImpl()
Definition: frame_handler_impl.cpp:50
aditof::Metadata::xyzEnabled
uint8_t xyzEnabled
True if xyz is being generated for the current frame. (set by sdk)
Definition: frame_definitions.h:233
aditof::FrameDetails::cameraMode
std::string cameraMode
The mode the camera was set when the frame was captured.
Definition: frame_definitions.h:109
aditof::Frame::getMetadataStruct
virtual SDK_API Status getMetadataStruct(Metadata &metadata) const
Extracts the metadata content and returns a struct with values.
Definition: frame.cpp:74
aditof::Metadata::bitsInAb
uint8_t bitsInAb
Number of bits in AB.
Definition: frame_definitions.h:180
FrameHandlerImpl::FrameHandlerImpl
FrameHandlerImpl()
Definition: frame_handler_impl.cpp:44
aditof::Frame
Frame of a camera.
Definition: frame.h:50
true
#define true
Definition: cJSON.c:65
FrameHandlerImpl::m_concatFrames
bool m_concatFrames
Definition: frame_handler_impl.h:100
aditof::Metadata::bitsInConfidence
uint8_t bitsInConfidence
Number of bits in confidence.
Definition: frame_definitions.h:185
FrameHandlerImpl::m_inputFileName
std::string m_inputFileName
Definition: frame_handler_impl.h:128
binary
const GLuint GLenum const GLvoid * binary
Definition: glcorearb.h:3962
FrameHandlerImpl::m_outputFileName
std::string m_outputFileName
Definition: frame_handler_impl.h:126
false
#define false
Definition: cJSON.c:70
FrameHandlerImpl::saveFrameToFile
aditof::Status saveFrameToFile(aditof::Frame &frame, const std::string &fileName="")
Definition: frame_handler_impl.cpp:70
aditof::FrameDataDetails::height
unsigned int height
The height of the frame data.
Definition: frame_definitions.h:63
FrameHandlerImpl::m_fileCreated
bool m_fileCreated
Definition: frame_handler_impl.h:129
FrameHandlerImpl::readNextFrame
aditof::Status readNextFrame(aditof::Frame &frame, const std::string &fullFileName="")
Definition: frame_handler_impl.cpp:186
FrameHandlerImpl::m_pos
size_t m_pos
Definition: frame_handler_impl.h:132
FrameHandlerImpl::m_dir
std::string m_dir
Definition: frame_handler_impl.h:125
FrameHandlerImpl::m_frameCount
int m_frameCount
Definition: frame_handler_impl.h:133


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:51