source.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #include "source.h"
5 #include "option.h"
6 #include "environment.h"
7 
8 namespace librealsense
9 {
11  {
12  public:
13  frame_queue_size(std::atomic<uint32_t>* ptr, const option_range& opt_range)
14  : option_base(opt_range),
15  _ptr(ptr)
16  {}
17 
18  void set(float value) override
19  {
20  if (!is_valid(value))
21  throw invalid_value_exception(to_string() << "set(frame_queue_size) failed! Given value " << value << " is out of range.");
22 
23  *_ptr = static_cast<uint32_t>(value);
24  _recording_function(*this);
25  }
26 
27  float query() const override { return static_cast<float>(_ptr->load()); }
28 
29  bool is_enabled() const override { return true; }
30 
31  const char* get_description() const override
32  {
33  return "Max number of frames you can hold at a given time. Increasing this number will reduce frame drops but increase latency, and vice versa";
34  }
35  private:
36  std::atomic<uint32_t>* _ptr;
37  };
38 
39  std::shared_ptr<option> frame_source::get_published_size_option()
40  {
41  return std::make_shared<frame_queue_size>(&_max_publish_list_size, option_range{ 0, 32, 1, 16 });
42  }
43 
44  frame_source::frame_source(uint32_t max_publish_list_size)
45  : _callback(nullptr, [](rs2_frame_callback*) {}),
46  _max_publish_list_size(max_publish_list_size),
47  _ts(environment::get_instance().get_time_service())
48  {}
49 
50  void frame_source::init(std::shared_ptr<metadata_parser_map> metadata_parsers)
51  {
52  std::lock_guard<std::mutex> lock(_callback_mutex);
53 
54  std::vector<rs2_extension> supported { RS2_EXTENSION_VIDEO_FRAME,
61 
62  for (auto type : supported)
63  {
64  _archive[type] = make_archive(type, &_max_publish_list_size, _ts, metadata_parsers);
65  }
66 
67  _metadata_parsers = metadata_parsers;
68  }
69 
71  {
72  return _archive[RS2_EXTENSION_VIDEO_FRAME]->begin_callback();
73 // return _archive[RS2_EXTENSION_DEPTH_FRAME]->begin_callback();
74  }
75 
77  {
78  std::lock_guard<std::mutex> lock(_callback_mutex);
79  _callback.reset();
80  for (auto&& kvp : _archive)
81  {
82  kvp.second.reset();
83  }
84  _metadata_parsers.reset();
85  }
86 
87  frame_interface* frame_source::alloc_frame(rs2_extension type, size_t size, frame_additional_data additional_data, bool requires_memory) const
88  {
89  auto it = _archive.find(type);
90  if (it == _archive.end()) throw wrong_api_call_sequence_exception("Requested frame type is not supported!");
91  return it->second->alloc_and_track(size, additional_data, requires_memory);
92  }
93 
94  void frame_source::set_sensor(const std::shared_ptr<sensor_interface>& s)
95  {
96  for (auto&& a : _archive)
97  {
98  a.second->set_sensor(s);
99  }
100  }
101 
103  {
104  std::lock_guard<std::mutex> lock(_callback_mutex);
106  }
107 
109  {
110  std::lock_guard<std::mutex> lock(_callback_mutex);
111  return _callback;
112  }
114  {
115  if (frame)
116  {
117  auto callback = frame.frame->get_owner()->begin_callback();
118  try
119  {
120  frame->log_callback_start(_ts ? _ts->get_time() : 0);
121  if (_callback)
122  {
123  frame_interface* ref = nullptr;
124  std::swap(frame.frame, ref);
125  _callback->on_frame((rs2_frame*)ref);
126  }
127  }
128  catch( const std::exception & e )
129  {
130  LOG_ERROR( "Exception was thrown during user callback: " + std::string( e.what() ));
131  }
132  catch(...)
133  {
134  LOG_ERROR("Exception was thrown during user callback!");
135  }
136  }
137  }
138 
139  void frame_source::flush() const
140  {
141  for (auto&& kvp : _archive)
142  {
143  if (kvp.second)
144  kvp.second->flush();
145  }
146  }
147 }
148 
static const textual_icon lock
Definition: model-views.h:218
frame_callback_ptr _callback
Definition: source.h:60
std::shared_ptr< platform::time_service > _ts
Definition: source.h:61
void invoke_callback(frame_holder frame) const
Definition: source.cpp:113
GLdouble s
std::map< rs2_extension, std::shared_ptr< archive_interface > > _archive
Definition: source.h:57
bool is_enabled() const override
Definition: source.cpp:29
void init(std::shared_ptr< metadata_parser_map > metadata_parsers)
Definition: source.cpp:50
std::shared_ptr< rs2_frame_callback > frame_callback_ptr
Definition: src/types.h:1071
std::shared_ptr< archive_interface > make_archive(rs2_extension type, std::atomic< uint32_t > *in_max_frame_queue_size, std::shared_ptr< platform::time_service > ts, std::shared_ptr< metadata_parser_map > parsers)
Definition: archive.cpp:158
std::atomic< uint32_t > _max_publish_list_size
Definition: source.h:59
GLfloat value
GLsizei const GLchar *const * string
e
Definition: rmse.py:177
GLboolean GLboolean GLboolean GLboolean a
std::atomic< uint32_t > * _ptr
Definition: source.cpp:36
Definition: parser.hpp:154
std::mutex _callback_mutex
Definition: source.h:55
frame_callback_ptr get_callback() const
Definition: source.cpp:108
std::shared_ptr< option > get_published_size_option()
Definition: source.cpp:39
GLsizeiptr size
const char * get_description() const override
Definition: source.cpp:31
float query() const override
Definition: source.cpp:27
unsigned int uint32_t
Definition: stdint.h:80
virtual archive_interface * get_owner() const =0
void set_sensor(const std::shared_ptr< sensor_interface > &s)
Definition: source.cpp:94
def callback(frame)
Definition: t265_stereo.py:91
#define LOG_ERROR(...)
Definition: src/types.h:242
void swap(nlohmann::json &j1, nlohmann::json &j2) noexcept(is_nothrow_move_constructible< nlohmann::json >::value andis_nothrow_move_assignable< nlohmann::json >::value)
exchanges the values of two JSON objects
Definition: json.hpp:12141
frame_interface * alloc_frame(rs2_extension type, size_t size, frame_additional_data additional_data, bool requires_memory) const
Definition: source.cpp:87
static environment & get_instance()
std::function< void(const option &)> _recording_function
Definition: option.h:100
frame_interface * frame
Definition: streaming.h:126
virtual void log_callback_start(rs2_time_t timestamp)=0
frame_queue_size(std::atomic< uint32_t > *ptr, const option_range &opt_range)
Definition: source.cpp:13
rs2_extension
Specifies advanced interfaces (capabilities) objects may implement.
Definition: rs_types.h:166
static auto it
frame_source(uint32_t max_publish_list_size=16)
Definition: source.cpp:44
GLenum type
GLint ref
void set_callback(frame_callback_ptr callback)
Definition: source.cpp:102
virtual callback_invocation_holder begin_callback()=0
std::shared_ptr< metadata_parser_map > _metadata_parsers
Definition: source.h:62
bool is_valid(float value) const
Definition: option.cpp:6
struct rs2_frame rs2_frame
Definition: rs_types.h:261
callback_invocation_holder begin_callback()
Definition: source.cpp:70
std::string to_string(T value)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:41