src/core/serialization.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 #include <chrono>
6 #include <map>
7 #include <memory>
8 #include <vector>
9 #include "types.h"
10 #include "extension.h"
11 #include "streaming.h"
12 
13 namespace librealsense
14 {
15  namespace device_serializer
16  {
18  {
21  };
23  {
28  };
29  inline bool operator==(const stream_identifier& lhs, const stream_identifier& rhs)
30  {
31  return lhs.device_index == rhs.device_index &&
32  lhs.sensor_index == rhs.sensor_index &&
33  lhs.stream_type == rhs.stream_type &&
34  lhs.stream_index == rhs.stream_index;
35  }
36  inline bool operator<(const stream_identifier& lhs, const stream_identifier& rhs)
37  {
38  return std::make_tuple(lhs.device_index, lhs.sensor_index, lhs.stream_type, lhs.stream_index) < std::make_tuple(rhs.device_index, rhs.sensor_index, rhs.stream_type, rhs.stream_index);
39  }
40  inline std::ostream& operator<<(std::ostream& os, const stream_identifier& id)
41  {
42  os << id.device_index << "/" << id.sensor_index << "/" << id.stream_type << "/" << id.stream_index;
43  return os;
44  }
45 
46  using nanoseconds = std::chrono::duration<uint64_t, std::nano>;
47 
48  class serialized_data : public std::enable_shared_from_this<serialized_data>
49  {
50  protected:
52  {
59  max
60  };
61  public:
62  explicit serialized_data(const device_serializer::nanoseconds& timestamp = device_serializer::nanoseconds::max()) :
63  _timestamp(timestamp)
64  {
65  }
66  virtual ~serialized_data() = default;
67 
68 
69  template <typename T>
70  bool is() const
71  {
72  return T::get_type() == type();
73  }
74 
75  template <typename T>
76  std::shared_ptr<T> as() const
77  {
78  if (!is<T>())
79  return nullptr;
80 
81  switch (T::get_type())
82  {
83  case end_of_file:
84  case frame:
85  case option:
86  case notificaion:
87  return std::static_pointer_cast<T>(std::const_pointer_cast<serialized_data>(shared_from_this()));
88  }
89  return nullptr;
90  }
91 
93  {
94  return _timestamp;
95  }
96 
97  virtual serialized_data_type type() const = 0;
98 
99  private:
101  };
102 
104  {
105  public:
107  serialized_data(time),
108  stream_id(id),
109  frame(std::move(f))
110  {}
114  {
115  return serialized_data_type::frame;
116  }
117  serialized_data_type type() const override
118  {
120  }
121  };
122 
124  {
125  public:
128  {
129  return serialized_data_type::invalid_frame;
130  }
131  serialized_data_type type() const override
132  {
134  }
135  };
136 
138  {
139  public:
140  serialized_option(device_serializer::nanoseconds time, sensor_identifier id, rs2_option opt_id, std::shared_ptr<librealsense::option> o) :
141  serialized_data(time),
142  sensor_id(id), option(o), option_id(opt_id)
143  {}
145  std::shared_ptr<librealsense::option> option;
148  {
150  }
151  serialized_data_type type() const override
152  {
154  }
155  };
156 
158  {
159  public:
162  {
163  return serialized_data_type::end_of_file;
164  }
165  serialized_data_type type() const override
166  {
168  }
169  };
170 
172  {
173  public:
175  serialized_data(time),
176  sensor_id(id), notif(n)
177  {}
181  {
182  return serialized_data_type::notificaion;
183  }
184  serialized_data_type type() const override
185  {
187  }
188  };
189 
191  {
192  public:
194  snapshot_collection(const std::map<rs2_extension, std::shared_ptr<extension_snapshot>>& snapshots) :
195  m_snapshots(snapshots)
196  {
197  }
198 
199  std::shared_ptr<extension_snapshot> find(rs2_extension t) const
200  {
201  auto snapshot_it = m_snapshots.find(t);
202  if (snapshot_it == std::end(m_snapshots))
203  {
204  return nullptr;
205  }
206  return snapshot_it->second;
207  }
208  std::map<rs2_extension, std::shared_ptr<extension_snapshot>> get_snapshots() const
209  {
210  return m_snapshots;
211  }
212 
213  const std::shared_ptr<extension_snapshot>& operator[](rs2_extension extension) const
214  {
215  return m_snapshots.at(extension);
216  }
217 
218  std::shared_ptr<extension_snapshot>& operator[](rs2_extension extension)
219  {
220  return m_snapshots[extension];
221  }
222  private:
223  std::map<rs2_extension, std::shared_ptr<extension_snapshot>> m_snapshots;
224  };
225 
227  {
228  public:
229  sensor_snapshot(uint32_t index, const snapshot_collection& sensor_extensions) :
230  m_snapshots(sensor_extensions),
231  m_index(index)
232  {
233  }
234 
236  m_snapshots(sensor_extensions),
237  m_streams(streams),
238  m_index(index)
239  {
240  }
242  {
243  return m_snapshots;
244  }
245 
247  {
248  return m_snapshots;
249  }
251  {
252  return m_streams;
253  }
254 
256  {
257  return m_index;
258  }
259  private:
263  };
264  using device_extrinsics = std::map<std::tuple<size_t, rs2_stream, size_t, rs2_stream>, rs2_extrinsics>;
265 
267  {
268  public:
270  device_snapshot(const snapshot_collection& device_extensios, const std::vector<sensor_snapshot>& sensors_snapshot, const std::map<stream_identifier, std::pair<uint32_t, rs2_extrinsics>>& extrinsics_map) :
271  m_device_snapshots(device_extensios),
272  m_sensors_snapshot(sensors_snapshot),
273  m_extrinsics_map(extrinsics_map)
274  {
275 
276  }
277  std::vector<sensor_snapshot> get_sensors_snapshots() const
278  {
279  return m_sensors_snapshot;
280  }
281  std::vector<sensor_snapshot>& get_sensors_snapshots()
282  {
283  return m_sensors_snapshot;
284  }
286  {
287  return m_device_snapshots;
288  }
289  std::map<stream_identifier, std::pair<uint32_t, rs2_extrinsics>> get_extrinsics_map() const
290  {
291  return m_extrinsics_map;
292  }
293  private:
295  std::vector<sensor_snapshot> m_sensors_snapshot;
296  std::map<stream_identifier, std::pair<uint32_t, rs2_extrinsics>> m_extrinsics_map;
297  };
298 
299 
305  enum status
306  {
307  /* success */
310  /* errors */
322  };
323 
324  class writer
325  {
326  public:
327  virtual void write_device_description(const device_snapshot& device_description) = 0;
328  virtual void write_frame(const stream_identifier& stream_id, const nanoseconds& timestamp, frame_holder&& frame) = 0;
329  virtual void write_snapshot(uint32_t device_index, const nanoseconds& timestamp, rs2_extension type, const std::shared_ptr<extension_snapshot>& snapshot) = 0;
330  virtual void write_snapshot(const sensor_identifier& sensor_id, const nanoseconds& timestamp, rs2_extension type, const std::shared_ptr<extension_snapshot>& snapshot) = 0;
331  virtual void write_notification(const sensor_identifier& stream_id, const nanoseconds& timestamp, const notification& n) = 0;
332  virtual const std::string& get_file_name() const = 0;
333  virtual ~writer() = default;
334  };
335 
336  class reader
337  {
338  public:
339  virtual ~reader() = default;
340  virtual device_snapshot query_device_description(const nanoseconds& time) = 0;
341  virtual std::shared_ptr<serialized_data> read_next_data() = 0;
342  virtual void seek_to_time(const nanoseconds& time) = 0;
343  virtual nanoseconds query_duration() const = 0;
344  virtual void reset() = 0;
345  virtual void enable_stream(const std::vector<device_serializer::stream_identifier>& stream_ids) = 0;
346  virtual void disable_stream(const std::vector<device_serializer::stream_identifier>& stream_ids) = 0;
347  virtual const std::string& get_file_name() const = 0;
348  virtual std::vector<std::shared_ptr<serialized_data>> fetch_last_frames(const nanoseconds& seek_time) = 0;
349  };
350  }
351 }
device_snapshot(const snapshot_collection &device_extensios, const std::vector< sensor_snapshot > &sensors_snapshot, const std::map< stream_identifier, std::pair< uint32_t, rs2_extrinsics >> &extrinsics_map)
std::map< stream_identifier, std::pair< uint32_t, rs2_extrinsics > > get_extrinsics_map() const
GLuint GLuint end
std::shared_ptr< extension_snapshot > & operator[](rs2_extension extension)
std::map< stream_identifier, std::pair< uint32_t, rs2_extrinsics > > m_extrinsics_map
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
GLboolean reset
serialized_notification(device_serializer::nanoseconds time, sensor_identifier id, const notification &n)
const std::shared_ptr< extension_snapshot > & operator[](rs2_extension extension) const
GLsizei const GLchar *const * string
std::vector< sensor_snapshot > & get_sensors_snapshots()
GLdouble n
Definition: glext.h:1966
std::shared_ptr< librealsense::option > option
bool operator<(const stream_identifier &lhs, const stream_identifier &rhs)
std::map< rs2_extension, std::shared_ptr< extension_snapshot > > get_snapshots() const
status
Defines return codes that SDK interfaces use. Negative values indicate errors, a zero value indicates...
GLuint index
GLdouble t
GLdouble f
GLuint GLenum option
Definition: glext.h:5923
dictionary streams
Definition: t265_stereo.py:140
std::ostream & operator<<(std::ostream &os, const stream_identifier &id)
std::shared_ptr< extension_snapshot > find(rs2_extension t) const
unsigned int uint32_t
Definition: stdint.h:80
virtual device_serializer::nanoseconds get_timestamp() const
serialized_option(device_serializer::nanoseconds time, sensor_identifier id, rs2_option opt_id, std::shared_ptr< librealsense::option > o)
std::vector< std::shared_ptr< stream_profile_interface >> stream_profiles
Definition: streaming.h:165
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:42
std::chrono::duration< uint64_t, std::nano > nanoseconds
serialized_invalid_frame(device_serializer::nanoseconds time, stream_identifier id)
std::map< std::tuple< size_t, rs2_stream, size_t, rs2_stream >, rs2_extrinsics > device_extrinsics
Cross-stream extrinsics: encodes the topology describing how the different devices are oriented...
Definition: rs_sensor.h:96
std::string get_file_name(const std::string &path)
Definition: os.cpp:224
rs2_extension
Specifies advanced interfaces (capabilities) objects may implement.
Definition: rs_types.h:166
int stream_id
Definition: sync.h:17
GLenum type
std::map< rs2_extension, std::shared_ptr< extension_snapshot > > m_snapshots
sensor_snapshot(uint32_t index, const snapshot_collection &sensor_extensions)
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
serialized_frame(device_serializer::nanoseconds time, stream_identifier id, frame_holder f)
serialized_data(const device_serializer::nanoseconds &timestamp=device_serializer::nanoseconds::max())
std::vector< sensor_snapshot > get_sensors_snapshots() const
snapshot_collection(const std::map< rs2_extension, std::shared_ptr< extension_snapshot >> &snapshots)
sensor_snapshot(uint32_t index, const snapshot_collection &sensor_extensions, stream_profiles streams)
bool operator==(const stream_identifier &lhs, const stream_identifier &rhs)
YYCODETYPE lhs
Definition: sqlite3.c:132469
GeneratorWrapper< T > map(Func &&function, GeneratorWrapper< U > &&generator)
Definition: catch.hpp:4271


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Sat Apr 3 2021 02:48:26