RsSensor.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 #include "RsCommon.h"
5 #include "RsDevice.hh"
6 #include "RsUsageEnvironment.h"
8 #include "string.h"
9 #include <BasicUsageEnvironment.hh>
10 #include <iostream>
11 #include <math.h>
12 #include <thread>
13 
14 RsSensor::RsSensor(UsageEnvironment* t_env, rs2::sensor t_sensor, rs2::device t_device)
15  : env(t_env)
16  , m_sensor(t_sensor)
17  , m_device(t_device)
18 {
19  for(rs2::stream_profile streamProfile : m_sensor.get_stream_profiles())
20  {
21  if(streamProfile.is<rs2::video_stream_profile>())
22  {
23  //make a map with all the sensor's stream profiles
24  m_streamProfiles.emplace(getStreamProfileKey(streamProfile), streamProfile.as<rs2::video_stream_profile>());
26  }
27  }
28  m_memPool = new MemoryPool();
29 }
30 
31 int RsSensor::open(std::unordered_map<long long int, rs2::frame_queue>& t_streamProfilesQueues)
32 {
33  std::vector<rs2::stream_profile> requestedStreamProfiles;
34  for(auto streamProfile : t_streamProfilesQueues)
35  {
36  //make a vector of all requested stream profiles
37  long long int streamProfileKey = streamProfile.first;
38  requestedStreamProfiles.push_back(m_streamProfiles.at(streamProfileKey));
39  if(CompressionFactory::isCompressionSupported(m_streamProfiles.at(streamProfileKey).format(), m_streamProfiles.at(streamProfileKey).stream_type()))
40  {
41  rs2::video_stream_profile vsp = m_streamProfiles.at(streamProfileKey);
42  std::shared_ptr<ICompression> compressPtr = CompressionFactory::getObject(vsp.width(), vsp.height(), vsp.format(), vsp.stream_type(), getStreamProfileBpp(vsp.format()));
43  if(compressPtr != nullptr)
44  {
45  m_iCompress.insert(std::pair<long long int, std::shared_ptr<ICompression>>(streamProfileKey, compressPtr));
46  }
47  }
48  else
49  {
50  *env << "unsupported compression format or compression is disabled, continue without compression\n";
51  }
52  }
53  m_sensor.open(requestedStreamProfiles);
54  return EXIT_SUCCESS;
55 }
56 
58 {
59  m_sensor.close();
60  return EXIT_SUCCESS;
61 }
62 
64 {
65  m_sensor.stop();
66  return EXIT_SUCCESS;
67 }
68 
69 int RsSensor::start(std::unordered_map<long long int, rs2::frame_queue>& t_streamProfilesQueues)
70 {
71  auto callback = [&](const rs2::frame& frame) {
72  long long int profileKey = getStreamProfileKey(frame.get_profile());
73  //check if profile exists in map:
74  if(t_streamProfilesQueues.find(profileKey) != t_streamProfilesQueues.end())
75  {
76  std::chrono::high_resolution_clock::time_point curSample = std::chrono::high_resolution_clock::now();
77  std::chrono::duration<double> timeSpan = std::chrono::duration_cast<std::chrono::duration<double>>(curSample - m_prevSample[profileKey]);
78  if(CompressionFactory::isCompressionSupported(frame.get_profile().format(), frame.get_profile().stream_type()))
79  {
80  unsigned char* buff = m_memPool->getNextMem();
81  int frameSize = m_iCompress.at(profileKey)->compressBuffer((unsigned char*)frame.get_data(), frame.get_data_size(), buff);
82  if(frameSize == -1)
83  {
84  m_memPool->returnMem(buff);
85  return;
86  }
87  memcpy((unsigned char*)frame.get_data(), buff, frameSize);
88  m_memPool->returnMem(buff);
89  }
90  //push frame to its queue
91  t_streamProfilesQueues[profileKey].enqueue(frame);
92  m_prevSample[profileKey] = curSample;
93  }
94  };
96  return EXIT_SUCCESS;
97 }
98 
100 {
101  long long int key;
102  key = t_profile.stream_type() * pow(10, 12) + t_profile.format() * pow(10, 10) + t_profile.fps() * pow(10, 8) + t_profile.stream_index();
103  if(t_profile.is<rs2::video_stream_profile>())
104  {
105  rs2::video_stream_profile videoStreamProfile = t_profile.as<rs2::video_stream_profile>();
106  key += videoStreamProfile.width() * pow(10, 4) + videoStreamProfile.height();
107  }
108  return key;
109 }
110 
112 {
114  {
115  // W/A for L515 sensor name
117  if (str.compare(L500_SENSOR_NAME) == 0 )
118  {
119  str = STEREO_SENSOR_NAME;
120  }
121  return str;
122  }
123  else
124  {
125  return "Unknown Sensor";
126  }
127 }
128 
129 std::vector<RsOption> RsSensor::getSupportedOptions()
130 {
131  std::vector<RsOption> returnedVector;
132  try
133  {
134 
135  std::vector<rs2_option> options = m_sensor.get_supported_options();
136  for(auto opt : options)
137  {
138  if(!m_sensor.supports(opt))
139  continue;
140 
142  option.m_opt = opt;
143  option.m_range = m_sensor.get_option_range(opt);
144  returnedVector.push_back(option);
145  }
146  }
147  catch(const std::exception& e)
148  {
149  *env << e.what() << "\n";
150  }
151  return returnedVector;
152 }
std::unordered_map< long long int, std::chrono::high_resolution_clock::time_point > m_prevSample
Definition: RsSensor.hh:50
rs2::sensor m_sensor
Definition: RsSensor.hh:45
int open(std::unordered_map< long long int, rs2::frame_queue > &t_streamProfilesQueues)
Definition: RsSensor.cpp:31
std::vector< RsOption > getSupportedOptions()
Definition: RsSensor.cpp:129
GLsizei const GLchar *const * string
bool is() const
Definition: rs_frame.hpp:92
e
Definition: rmse.py:177
GLuint64 key
Definition: glext.h:8966
std::vector< rs2_option > get_supported_options()
Definition: rs_options.hpp:119
std::string getSensorName()
Definition: RsSensor.cpp:111
GLuint GLenum option
Definition: glext.h:5923
rs2_option m_opt
Definition: RsSensor.hh:15
void returnMem(unsigned char *t_mem)
Definition: MemoryPool.h:61
int close()
Definition: RsSensor.cpp:57
const std::string STEREO_SENSOR_NAME("Stereo Module")
def callback(frame)
Definition: t265_stereo.py:91
std::unordered_map< long long int, rs2::video_stream_profile > m_streamProfiles
Definition: RsSensor.hh:46
MemoryPool * m_memPool
Definition: RsSensor.hh:49
static bool isCompressionSupported(rs2_format t_format, rs2_stream t_streamType)
const std::string L500_SENSOR_NAME("L500 Depth Sensor")
int stream_index() const
Definition: rs_frame.hpp:34
bool supports(rs2_camera_info info) const
Definition: rs_sensor.hpp:125
UsageEnvironment * env
Definition: RsSensor.hh:44
static std::shared_ptr< ICompression > getObject(int t_width, int t_height, rs2_format t_format, rs2_stream t_streamType, int t_bpp)
RsSensor(UsageEnvironment *t_env, rs2::sensor t_sensor, rs2::device t_device)
Definition: RsSensor.cpp:14
rs2_format format() const
Definition: rs_frame.hpp:44
static long long int getStreamProfileKey(rs2::stream_profile t_profile)
Definition: RsSensor.cpp:99
int getStreamProfileBpp(rs2_format t_format)
Definition: RsCommon.cpp:6
void open(const stream_profile &profile) const
Definition: rs_sensor.hpp:111
void close() const
Definition: rs_sensor.hpp:173
option_range get_option_range(rs2_option option) const
Definition: rs_options.hpp:84
const char * get_info(rs2_camera_info info) const
Definition: rs_sensor.hpp:138
int start(std::unordered_map< long long int, rs2::frame_queue > &t_streamProfilesQueues)
Definition: RsSensor.cpp:69
unsigned char * getNextMem()
Definition: MemoryPool.h:43
std::vector< stream_profile > get_stream_profiles() const
Definition: rs_sensor.hpp:219
rs2_stream stream_type() const
Definition: rs_frame.hpp:39
std::unordered_map< long long int, std::shared_ptr< ICompression > > m_iCompress
Definition: RsSensor.hh:47
int fps() const
Definition: rs_frame.hpp:49
int stop()
Definition: RsSensor.cpp:63
void start(T callback) const
Definition: rs_sensor.hpp:185
rs2::option_range m_range
Definition: RsSensor.hh:16
void stop() const
Definition: rs_sensor.hpp:195


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