sync.h
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 #pragma once
5 
6 #include "types.h"
7 #include "archive.h"
8 
9 #include <stdint.h>
10 #include <vector>
11 #include <mutex>
12 #include <memory>
13 
14 namespace librealsense
15 {
16 
17  typedef int stream_id;
18 
19  class sync_lock
20  {
21  public:
22  sync_lock(std::mutex& mutex) : _mutex(mutex)
23  {
24  mutex.lock();
25  }
26 
28  {
29  // NOTE: The Sync_Lock is itself a single-threaded object
30  // It maintains a state, and does not protect its state.
31  // That is acceptable for our use case,
32  // because we use it to communicate within a single thread
33  if (!_is_locked) return;
34  _mutex.unlock();
35  _is_locked = false;
36 
37  }
38 
40  {
41  if (_is_locked)
42  {
43  _mutex.unlock();
44 
45  }
46  }
47 
48  private:
49  bool _is_locked = true;
50 
51  std::mutex& _mutex;
52  };
53  //sync_lock::ref = 0;
54 
56 
58  {
61  bool log )
62  : source( source )
63  , matches( matches )
64  , log( log )
65  {
66  }
68  // sync_lock& lock_ref;
70  bool log = true;
71  };
72 
73  typedef int stream_id;
74  typedef std::function<void(frame_holder, const syncronization_environment&)> sync_callback;
75 
77  {
78  public:
79  virtual void dispatch(frame_holder f, const syncronization_environment& env) = 0;
80  virtual void sync(frame_holder f, const syncronization_environment& env) = 0;
81  virtual void set_callback(sync_callback f) = 0;
82  virtual const std::vector<stream_id>& get_streams() const = 0;
83  virtual const std::vector<rs2_stream>& get_streams_types() const = 0;
84  virtual std::string get_name() const = 0;
85  virtual void stop() = 0;
86  };
87 
88  class matcher: public matcher_interface
89  {
90  public:
91  matcher(std::vector<stream_id> streams_id = {});
92  virtual void sync(frame_holder f, const syncronization_environment& env) override;
93  virtual void set_callback(sync_callback f) override;
94  const std::vector<stream_id>& get_streams() const override;
95  const std::vector<rs2_stream>& get_streams_types() const override;
96 
97  callback_invocation_holder begin_callback();
98  virtual ~matcher();
99 
100  virtual std::string get_name() const override;
101  bool get_active() const;
102  void set_active(const bool active);
103  virtual void stop() override {}
104 
105  protected:
106  std::vector<stream_id> _streams_id;
107  std::vector<rs2_stream> _streams_type;
108  sync_callback _callback;
111  bool _active = true;
112  };
113 
114  class identity_matcher : public matcher
115  {
116  public:
117  identity_matcher(stream_id stream, rs2_stream streams_type);
118 
119  void dispatch(frame_holder f, const syncronization_environment& env) override;
120 
121  };
122 
123  class composite_matcher : public matcher
124  {
125  public:
126  composite_matcher(std::vector<std::shared_ptr<matcher>> matchers, std::string name);
127 
128 
129  virtual bool are_equivalent(frame_holder& a, frame_holder& b) = 0;
130  virtual bool is_smaller_than(frame_holder& a, frame_holder& b) = 0;
131  virtual bool skip_missing_stream(std::vector<matcher*> synced, matcher* missing, const syncronization_environment& env) = 0;
132  virtual void clean_inactive_streams(frame_holder& f) = 0;
133  virtual void update_last_arrived(frame_holder& f, matcher* m) = 0;
134 
135  void dispatch(frame_holder f, const syncronization_environment& env) override;
136  std::string frames_to_string(std::vector<librealsense::matcher*> matchers);
137  void sync(frame_holder f, const syncronization_environment& env) override;
138  std::shared_ptr<matcher> find_matcher(const frame_holder& f);
139  virtual void stop() override;
140 
141 
142  protected:
143  virtual void update_next_expected(const frame_holder& f) = 0;
144 
145  std::map<matcher*, single_consumer_frame_queue<frame_holder>> _frames_queue;
146  std::map<stream_id, std::shared_ptr<matcher>> _matchers;
147  std::map<matcher*, double> _next_expected;
148  std::map<matcher*, rs2_timestamp_domain> _next_expected_domain;
149  };
150 
151  // composite matcher that does not synchronize between any frames, and instead just passes them on to callback
153  {
154  public:
155  composite_identity_matcher(std::vector<std::shared_ptr<matcher>> matchers);
156 
157  void sync(frame_holder f, const syncronization_environment& env) override;
158  virtual bool are_equivalent(frame_holder& a, frame_holder& b) override { return false; }
159  virtual bool is_smaller_than(frame_holder& a, frame_holder& b) override { return false; }
160  virtual bool skip_missing_stream(std::vector<matcher*> synced, matcher* missing, const syncronization_environment& env) override { return false; }
161  virtual void clean_inactive_streams(frame_holder& f) override {}
162  virtual void update_last_arrived(frame_holder& f, matcher* m) override {}
163 
164  protected:
165  virtual void update_next_expected(const frame_holder& f) override {}
166  };
167 
169  {
170  public:
171  frame_number_composite_matcher(std::vector<std::shared_ptr<matcher>> matchers);
172  virtual void update_last_arrived(frame_holder& f, matcher* m) override;
173  bool are_equivalent(frame_holder& a, frame_holder& b) override;
174  bool is_smaller_than(frame_holder& a, frame_holder& b) override;
175  bool skip_missing_stream(std::vector<matcher*> synced, matcher* missing, const syncronization_environment& env) override;
176  void clean_inactive_streams(frame_holder& f) override;
177  void update_next_expected(const frame_holder& f) override;
178 
179  private:
180  std::map<matcher*,unsigned long long> _last_arrived;
181  };
182 
184  {
185  public:
186  timestamp_composite_matcher(std::vector<std::shared_ptr<matcher>> matchers);
187  bool are_equivalent(frame_holder& a, frame_holder& b) override;
188  bool is_smaller_than(frame_holder& a, frame_holder& b) override;
189  virtual void update_last_arrived(frame_holder& f, matcher* m) override;
190  void clean_inactive_streams(frame_holder& f) override;
191  bool skip_missing_stream(std::vector<matcher*> synced, matcher* missing, const syncronization_environment& env) override;
192  void update_next_expected(const frame_holder & f) override;
193 
194  private:
195  unsigned int get_fps(const frame_holder & f);
196  bool are_equivalent(double a, double b, int fps);
197  std::map<matcher*, double> _last_arrived;
198  std::map<matcher*, unsigned int> _fps;
199 
200  };
201 }
GLboolean GLboolean GLboolean b
std::map< matcher *, double > _next_expected
Definition: sync.h:147
GLuint const GLchar * name
virtual bool skip_missing_stream(std::vector< matcher * > synced, matcher *missing, const syncronization_environment &env) override
Definition: sync.h:160
syncronization_environment(synthetic_source_interface *source, single_consumer_frame_queue< frame_holder > &matches, bool log)
Definition: sync.h:59
sync_callback _callback
Definition: sync.h:108
virtual bool is_smaller_than(frame_holder &a, frame_holder &b) override
Definition: sync.h:159
const GLfloat * m
Definition: glext.h:6814
virtual void clean_inactive_streams(frame_holder &f) override
Definition: sync.h:161
std::function< void(frame_holder, const syncronization_environment &)> sync_callback
Definition: sync.h:74
sync_lock(std::mutex &mutex)
Definition: sync.h:22
virtual void stop() override
Definition: sync.h:103
std::vector< rs2_stream > _streams_type
Definition: sync.h:107
virtual void update_next_expected(const frame_holder &f) override
Definition: sync.h:165
matches
Definition: test-fg.py:19
GLsizei const GLchar *const * string
static const textual_icon stop
Definition: model-views.h:225
GLboolean GLboolean GLboolean GLboolean a
GLdouble f
std::mutex & _mutex
Definition: sync.h:51
std::map< stream_id, std::shared_ptr< matcher > > _matchers
Definition: sync.h:146
callbacks_heap _callback_inflight
Definition: sync.h:109
single_consumer_frame_queue< frame_holder > & matches
Definition: sync.h:69
void unlock_preemptively()
Definition: sync.h:27
std::string _name
Definition: sync.h:110
virtual bool are_equivalent(frame_holder &a, frame_holder &b) override
Definition: sync.h:158
synthetic_source_interface * source
Definition: sync.h:67
std::map< matcher *, single_consumer_frame_queue< frame_holder > > _frames_queue
Definition: sync.h:145
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:42
virtual void update_last_arrived(frame_holder &f, matcher *m) override
Definition: sync.h:162
std::map< matcher *, rs2_timestamp_domain > _next_expected_domain
Definition: sync.h:148
std::map< matcher *, unsigned long long > _last_arrived
Definition: sync.h:180
int stream_id
Definition: sync.h:17
std::map< matcher *, double > _last_arrived
Definition: sync.h:197
void log(std::string message)
GLsizei GLsizei GLchar * source
std::map< matcher *, unsigned int > _fps
Definition: sync.h:198
std::vector< stream_id > _streams_id
Definition: sync.h:106


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