playback_sensor.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 <core/roi.h>
6 #include <core/extension.h>
7 #include <core/serialization.h>
8 #include "core/streaming.h"
9 #include "archive.h"
10 #include "concurrency.h"
11 #include "sensor.h"
12 #include "types.h"
13 
14 namespace librealsense
15 {
17  public extendable_interface,
18  public info_container,
19  public options_container,
20  public std::enable_shared_from_this<playback_sensor>
21  {
22  public:
23  using frame_interface_callback_t = std::function<void(frame_holder)>;
28 
29  playback_sensor(device_interface& parent_device, const device_serializer::sensor_snapshot& sensor_description);
30  virtual ~playback_sensor();
31 
33  void open(const stream_profiles& requests) override;
34  void close() override;
37  void start(frame_callback_ptr callback) override;
38  void stop() override;
39  bool is_streaming() const override;
40  bool extend_to(rs2_extension extension_type, void** ext) override;
41  device_interface& get_device() override;
42  void update_option(rs2_option id, std::shared_ptr<option> option);
43  void stop(bool invoke_required);
44  void flush_pending_frames();
46  frame_callback_ptr get_frames_callback() const override;
48  stream_profiles get_active_streams() const override;
49  int register_before_streaming_changes_callback(std::function<void(bool)> callback) override;
50  void unregister_before_start_callback(int token) override;
51  void raise_notification(const notification& n);
54  {
56  if (processing_blocks_snapshot == nullptr)
57  {
58  throw invalid_value_exception("Recorded file does not contain sensor processing blocks");
59  }
60  auto processing_blocks_api = As<recommended_proccesing_blocks_interface>(processing_blocks_snapshot);
61  if (processing_blocks_api == nullptr)
62  {
63  throw invalid_value_exception("Failed to get options interface from sensor snapshots");
64  }
65  return processing_blocks_api->get_recommended_processing_blocks();
66  }
67  protected:
68  void set_active_streams(const stream_profiles& requests);
69 
70  private:
71  void register_sensor_streams(const stream_profiles& vector);
74 
75 
76 
79  using stream_unique_id = int;
80  std::map<stream_unique_id, std::shared_ptr<dispatcher>> m_dispatchers;
81  std::atomic<bool> m_is_started;
84  std::mutex m_mutex;
85  std::map<std::pair<rs2_stream, uint32_t>, std::shared_ptr<stream_profile_interface>> m_streams;
89  mutable std::mutex m_active_profile_mutex;
90  const unsigned int _default_queue_size;
91 
92  public:
93  //handle frame use 3 lambda functions that determines if and when a frame should be published.
94  //calc_sleep - calculates the duration that the sensor should wait before publishing the frame,
95  // the start point for this calculation is the last playback resume.
96  //is_paused - check if the playback was paused while waiting for the frame publish time.
97  //update_last_pushed_frame - lets the playback device know that a specific frame was published,
98  // the playback device will use this info to determine which frames should be played next in a pause/resume scenario.
99  template <class T, class K, class P>
100  void handle_frame(frame_holder frame, bool is_real_time, T calc_sleep, K is_paused, P update_last_pushed_frame)
101  {
102  if (frame == nullptr)
103  {
104  throw invalid_value_exception("null frame passed to handle_frame");
105  }
106  if (m_is_started)
107  {
108  frame->get_owner()->set_sensor(shared_from_this());
109  auto type = frame->get_stream()->get_stream_type();
110  auto index = static_cast<uint32_t>(frame->get_stream()->get_stream_index());
111  frame->set_stream(m_streams[std::make_pair(type, index)]);
112  frame->set_sensor(shared_from_this());
113  auto stream_id = frame.frame->get_stream()->get_unique_id();
114  //TODO: Ziv, remove usage of shared_ptr when frame_holder is cpoyable
115  auto pf = std::make_shared<frame_holder>(std::move(frame));
116 
117  auto callback = [this, is_real_time, stream_id, pf, calc_sleep, is_paused, update_last_pushed_frame](dispatcher::cancellable_timer t)
118  {
119  device_serializer::nanoseconds sleep_for = calc_sleep();
120  if (sleep_for.count() > 0)
121  t.try_sleep(sleep_for.count() * 1e-6);
122 
123  LOG_DEBUG("callback--> "<< frame_holder_to_string(*pf));
124  if(is_paused())
125  return;
126 
127  frame_interface* pframe = nullptr;
128  std::swap((*pf).frame, pframe);
129 
130  m_user_callback->on_frame((rs2_frame*)pframe);
131  update_last_pushed_frame();
132  };
133  m_dispatchers.at(stream_id)->invoke(callback, !is_real_time);
134  }
135  }
136  };
137 }
device_interface & m_parent_device
device_serializer::sensor_snapshot m_sensor_description
boost_foreach_argument_dependent_lookup_hack tag
Definition: foreach_fwd.hpp:31
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
std::function< void(frame_holder)> frame_interface_callback_t
const unsigned int _default_queue_size
signal< playback_sensor, const std::vector< device_serializer::stream_identifier > & > opened
std::shared_ptr< rs2_frame_callback > frame_callback_ptr
Definition: src/types.h:1071
void register_sensor_streams(const stream_profiles &vector)
void set_frames_callback(frame_callback_ptr callback) override
int register_before_streaming_changes_callback(std::function< void(bool)> callback) override
void register_notifications_callback(notifications_callback_ptr callback) override
virtual processing_blocks get_recommended_processing_blocks() const override
std::atomic< bool > m_is_started
notifications_processor _notifications_processor
GLdouble n
Definition: glext.h:1966
e
Definition: rmse.py:177
void start(frame_callback_ptr callback) override
signal< playback_sensor, uint32_t, frame_callback_ptr > started
GLuint index
GLdouble t
void update(const device_serializer::sensor_snapshot &sensor_snapshot)
bool extend_to(rs2_extension extension_type, void **ext) override
stream_profiles get_stream_profiles(int tag=profile_tag::PROFILE_TAG_ANY) const override
void raise_notification(const notification &n)
std::shared_ptr< extension_snapshot > find(rs2_extension t) const
unsigned int uint32_t
Definition: stdint.h:80
virtual archive_interface * get_owner() const =0
std::shared_ptr< rs2_notifications_callback > notifications_callback_ptr
Definition: src/types.h:1073
signal< playback_sensor, const std::vector< device_serializer::stream_identifier > & > closed
frame_callback_ptr m_user_callback
void open(const stream_profiles &requests) override
void register_sensor_options(const device_serializer::sensor_snapshot &sensor_snapshot)
void update_option(rs2_option id, std::shared_ptr< option > option)
def callback(frame)
Definition: t265_stereo.py:91
stream_profiles m_available_profiles
virtual void set_sensor(std::shared_ptr< sensor_interface > s)=0
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
std::vector< std::shared_ptr< stream_profile_interface >> stream_profiles
Definition: streaming.h:165
signal< playback_sensor, uint32_t, bool > stopped
void set_active_streams(const stream_profiles &requests)
void register_sensor_infos(const device_serializer::sensor_snapshot &sensor_snapshot)
std::chrono::duration< uint64_t, std::nano > nanoseconds
stream_profiles get_active_streams() const override
virtual std::shared_ptr< stream_profile_interface > get_stream() const =0
notifications_callback_ptr get_notifications_callback() const override
frame_interface * frame
Definition: streaming.h:126
rs2_extension
Specifies advanced interfaces (capabilities) objects may implement.
Definition: rs_types.h:166
int stream_id
Definition: sync.h:17
GLenum type
void unregister_before_start_callback(int token) override
device_interface & get_device() override
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
std::string frame_holder_to_string(const frame_holder &f)
Definition: streaming.cpp:9
std::map< stream_unique_id, std::shared_ptr< dispatcher > > m_dispatchers
#define LOG_DEBUG(...)
Definition: src/types.h:239
frame_callback_ptr get_frames_callback() const override
bool is_streaming() const override
std::map< std::pair< rs2_stream, uint32_t >, std::shared_ptr< stream_profile_interface > > m_streams
void handle_frame(frame_holder frame, bool is_real_time, T calc_sleep, K is_paused, P update_last_pushed_frame)
struct rs2_frame rs2_frame
Definition: rs_types.h:261
virtual void set_stream(std::shared_ptr< stream_profile_interface > sp)=0
playback_sensor(device_interface &parent_device, const device_serializer::sensor_snapshot &sensor_description)


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