RsSource.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 "RsSource.hh"
5 #include "BasicUsageEnvironment.hh"
6 #include "RsStatistics.h"
7 #include <GroupsockHelper.hh>
8 #include <cassert>
13 
14 RsDeviceSource* RsDeviceSource::createNew(UsageEnvironment& t_env, rs2::video_stream_profile& t_videoStreamProfile, rs2::frame_queue& t_queue)
15 {
16  return new RsDeviceSource(t_env, t_videoStreamProfile, t_queue);
17 }
18 
19 RsDeviceSource::RsDeviceSource(UsageEnvironment& t_env, rs2::video_stream_profile& t_videoStreamProfile, rs2::frame_queue& t_queue)
20  : FramedSource(t_env)
21 {
22  m_framesQueue = &t_queue;
23  m_streamProfile = &t_videoStreamProfile;
24 }
25 
27 
29 {
30  // This function is called (by our 'downstream' object) when it asks for new data.
31 
32  rs2::frame frame;
33  try
34  {
35  if(!m_framesQueue->poll_for_frame(&frame))
36  {
37  nextTask() = envir().taskScheduler().scheduleDelayedTask(0, (TaskFunc*)waitForFrame, this);
38  }
39  else
40  {
41  frame.keep();
42  deliverRSFrame(&frame);
43  }
44  }
45  catch(const std::exception& e)
46  {
47  envir() << "RsDeviceSource: " << e.what() << '\n';
48  }
49 }
50 
52 {
53  // If a new frame of data is immediately available to be delivered, then do this now:
54  rs2::frame frame;
55  try
56  {
57  if(!(getFramesQueue()->poll_for_frame(&frame)))
58  {
59  nextTask() = envir().taskScheduler().scheduleDelayedTask(0, (TaskFunc*)RsDeviceSource::waitForFrame, this);
60  }
61  else
62  {
63  frame.keep();
64  deliverRSFrame(&frame);
65  }
66  }
67  catch(const std::exception& e)
68  {
69  envir() << "RsDeviceSource: " << e.what() << '\n';
70  }
71 }
72 
73 // The following is called after each delay between packet sends:
75 {
76  t_deviceSource->handleWaitForFrame();
77 }
78 
80 {
81  if(!isCurrentlyAwaitingData())
82  {
83  envir() << "isCurrentlyAwaitingData returned false\n";
84  return; // we're not ready for the data yet
85  }
86 
87  unsigned newFrameSize = t_frame->get_data_size();
88 
89  gettimeofday(&fPresentationTime, NULL); // If you have a more accurate time - e.g., from an encoder - then use that instead.
91  unsigned char* data;
93  {
94  fFrameSize = ((int*)t_frame->get_data())[0];
95  data = (unsigned char*)t_frame->get_data() + sizeof(int);
96  }
97  else
98  {
99  fFrameSize = t_frame->get_data_size();
100  data = (unsigned char*)t_frame->get_data();
101  }
102  memmove(fTo + sizeof(RsFrameHeader), data, fFrameSize);
103  fFrameSize += sizeof(RsMetadataHeader);
104  header.networkHeader.data.frameSize = fFrameSize;
105  fFrameSize += sizeof(RsNetworkHeader);
107  {
109  }
110  else
111  {
112  header.metadataHeader.data.timestamp = t_frame->get_timestamp();
113  }
114 
116  {
118  }
119  else
120  {
121  header.metadataHeader.data.frameCounter = t_frame->get_frame_number();
122  }
123 
125  {
127  }
128 
130 
131  memmove(fTo, &header, sizeof(header));
132 
133  // After delivering the data, inform the reader that it is now available:
134  FramedSource::afterGetting(this);
135 }
void deliverRSFrame(rs2::frame *t_frame)
Definition: RsSource.cpp:79
RsDeviceSource(UsageEnvironment &t_env, rs2::video_stream_profile &t_videoStreamProfile, rs2::frame_queue &t_queue)
Definition: RsSource.cpp:19
virtual ~RsDeviceSource()
Definition: RsSource.cpp:26
rs2::frame_queue * m_framesQueue
Definition: RsSource.hh:32
uint32_t frameSize
Definition: RsCommon.h:15
struct RsNetworkHeader::@4 data
rs2_metadata_type get_frame_metadata(rs2_frame_metadata_value frame_metadata) const
Definition: rs_frame.hpp:497
std::enable_if< std::is_base_of< rs2::frame, T >::value, bool >::type poll_for_frame(T *output) const
stream_profile get_profile() const
Definition: rs_frame.hpp:557
rs2::frame_queue * getFramesQueue()
Definition: RsSource.hh:25
const void * get_data() const
Definition: rs_frame.hpp:545
bool supports_frame_metadata(rs2_frame_metadata_value frame_metadata) const
Definition: rs_frame.hpp:509
struct RsMetadataHeader::@5 data
std_msgs::Header * header(M &m)
returns Header<M>::pointer(m);
static void waitForFrame(RsDeviceSource *t_deviceSource)
Definition: RsSource.cpp:74
e
Definition: rmse.py:177
RsNetworkHeader networkHeader
Definition: RsCommon.h:32
rs2_timestamp_domain timestampDomain
Definition: RsCommon.h:26
double get_timestamp() const
Definition: rs_frame.hpp:474
Exposes RealSense sensor functionality for C compilers.
static bool isCompressionSupported(rs2_format t_format, rs2_stream t_streamType)
void keep()
Definition: rs_frame.hpp:437
virtual void doGetNextFrame()
Definition: RsSource.cpp:28
rs2::video_stream_profile * m_streamProfile
Definition: RsSource.hh:33
rs2_format format() const
Definition: rs_frame.hpp:44
RsMetadataHeader metadataHeader
Definition: RsCommon.h:33
double timestamp
Definition: RsCommon.h:23
void handleWaitForFrame()
Definition: RsSource.cpp:51
rs2_timestamp_domain get_frame_timestamp_domain() const
Definition: rs_frame.hpp:485
const int get_data_size() const
Definition: rs_frame.hpp:533
#define NULL
Definition: tinycthread.c:47
static RsDeviceSource * createNew(UsageEnvironment &t_env, rs2::video_stream_profile &t_videoStreamProfile, rs2::frame_queue &t_queue)
Definition: RsSource.cpp:14
GLboolean * data
unsigned long long get_frame_number() const
Definition: rs_frame.hpp:521
rs2_stream stream_type() const
Definition: rs_frame.hpp:39
long long frameCounter
Definition: RsCommon.h:24


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