RsSink.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 "RsSink.h"
6 
7 #include "stdio.h"
8 #include <string>
9 
10 #include <NetdevLog.h>
11 
12 #define WRITE_FRAMES_TO_FILE 0
13 
14 RsSink* RsSink::createNew(UsageEnvironment& t_env, MediaSubsession& t_subsession, rs2_video_stream t_stream, MemoryPool* t_memPool, char const* t_streamId)
15 {
16  return new RsSink(t_env, t_subsession, t_stream, t_memPool, t_streamId);
17 }
18 
19 RsSink::RsSink(UsageEnvironment& t_env, MediaSubsession& t_subsession, rs2_video_stream t_stream, MemoryPool* t_memPool, char const* t_streamId)
20  : MediaSink(t_env)
21  , m_memPool(t_memPool)
22  , m_subsession(t_subsession)
23 {
24  m_stream = t_stream;
25  m_streamId = strDup(t_streamId);
26  m_bufferSize = t_stream.width * t_stream.height * t_stream.bpp + sizeof(RsFrameHeader);
27  m_receiveBuffer = nullptr;
28  m_to = nullptr;
29  std::string urlStr = m_streamId;
34 
35  // Remove last "/"
36  /*
37  urlStr = urlStr.substr(0, urlStr.size()-1);
38  std::size_t streamNameIndex = urlStr.find_last_of("/") + 1;
39  std::string streamName = urlStr.substr(streamNameIndex, urlStr.size());
40 
41  if (streamName.compare("depth") == 0)
42  {
43  fp = fopen("file_depth.bin", "ab");
44  }
45  else if((streamName.compare("color") == 0))
46  {
47  fp = fopen("file_rgb.bin", "ab");
48  }
49  */
51  {
53  }
54  else
55  {
56  INF << "compression is disabled or configured unsupported format to zip, run without compression";
57  }
58 }
59 
61 {
62  if(m_receiveBuffer != nullptr)
63  {
65  }
66  delete[] m_streamId;
67  //fclose(fp);
68 }
69 
70 void RsSink::afterGettingFrameUid0(void* t_clientData, unsigned t_frameSize, unsigned t_numTruncatedBytes, struct timeval t_presentationTime, unsigned t_durationInMicroseconds)
71 {
72 
73  RsSink* sink = (RsSink*)t_clientData;
74  sink->afterGettingFrame(t_frameSize, t_numTruncatedBytes, t_presentationTime, t_durationInMicroseconds);
75 }
76 
77 void RsSink::afterGettingFrameUid1(void* t_clientData, unsigned t_frameSize, unsigned t_numTruncatedBytes, struct timeval t_presentationTime, unsigned t_durationInMicroseconds)
78 {
79 
80  RsSink* sink = (RsSink*)t_clientData;
81  sink->afterGettingFrame(t_frameSize, t_numTruncatedBytes, t_presentationTime, t_durationInMicroseconds);
82 }
83 
84 void RsSink::afterGettingFrameUid2(void* t_clientData, unsigned t_frameSize, unsigned t_numTruncatedBytes, struct timeval t_presentationTime, unsigned t_durationInMicroseconds)
85 {
86 
87  RsSink* sink = (RsSink*)t_clientData;
88  sink->afterGettingFrame(t_frameSize, t_numTruncatedBytes, t_presentationTime, t_durationInMicroseconds);
89 }
90 
91 void RsSink::afterGettingFrameUid3(void* t_clientData, unsigned t_frameSize, unsigned t_numTruncatedBytes, struct timeval t_presentationTime, unsigned t_durationInMicroseconds)
92 {
93 
94  RsSink* sink = (RsSink*)t_clientData;
95  sink->afterGettingFrame(t_frameSize, t_numTruncatedBytes, t_presentationTime, t_durationInMicroseconds);
96 }
97 
98 // If you don't want to see debugging output for each received frame, then comment out the following line:
99 #define DEBUG_PRINT_EACH_RECEIVED_FRAME 0
100 
101 void RsSink::afterGettingFrame(unsigned t_frameSize, unsigned t_numTruncatedBytes, struct timeval t_presentationTime, unsigned /*t_durationInMicroseconds*/)
102 {
104  if(header->data.frameSize == t_frameSize - sizeof(RsNetworkHeader))
105  {
106  if(this->m_rtpCallback != NULL)
107  {
109  {
111  if(m_to == nullptr)
112  {
113  return;
114  }
115  int decompressedSize = m_iCompress->decompressBuffer(m_receiveBuffer + sizeof(RsFrameHeader), header->data.frameSize - sizeof(RsMetadataHeader), m_to + sizeof(RsFrameHeader));
116  if(decompressedSize != -1)
117  {
118  // copy metadata
119  memcpy(m_to + sizeof(RsNetworkHeader), m_receiveBuffer + sizeof(RsNetworkHeader), sizeof(RsMetadataHeader));
120  this->m_rtpCallback->on_frame((u_int8_t*)m_to + sizeof(RsNetworkHeader), decompressedSize + sizeof(RsMetadataHeader), t_presentationTime);
121  }
123  }
124  else
125  {
126  this->m_rtpCallback->on_frame(m_receiveBuffer + sizeof(RsNetworkHeader), header->data.frameSize, t_presentationTime);
127  }
128  }
129  else
130  {
131  // TODO: error, no call back
133  envir() << "Frame call back is NULL\n";
134  }
135  }
136  else
137  {
138  envir() << m_streamId << ":corrupted frame!!!: data size is " << header->data.frameSize << " frame size is " << t_frameSize << "\n";
140  }
141  m_receiveBuffer = nullptr;
142 
143  // Then continue, to request the next frame of data
144  continuePlaying();
145 }
146 
148 {
149  if(fSource == NULL)
150  return False; // sanity check (should not happen)
151 
152  // Request the next frame of data from our input source. "afterGettingFrame()" will get called later, when it arrives:
154  if(m_receiveBuffer == nullptr)
155  {
156  return false;
157  }
158 
159  if(m_stream.uid >= 0 && m_stream.uid < m_afterGettingFunctions.size())
160  {
161  fSource->getNextFrame(m_receiveBuffer, m_bufferSize, m_afterGettingFunctions.at(m_stream.uid), this, onSourceClosure, this);
162  }
163  else
164  {
165  return false;
166  }
167 
168  return True;
169 }
170 
172 {
173  this->m_rtpCallback = t_callback;
174 }
rs2_format fmt
Definition: rs_internal.h:50
virtual void on_frame(unsigned char *buffer, ssize_t size, struct timeval presentationTime)=0
void setCallback(rtp_callback *t_callback)
Definition: RsSink.cpp:171
uint32_t frameSize
Definition: RsCommon.h:15
struct RsNetworkHeader::@4 data
Definition: RsSink.h:16
#define INF
Definition: NetdevLog.h:11
All the parameters required to define a video stream.
Definition: rs_internal.h:41
rs2_stream type
Definition: rs_internal.h:43
std::vector< FramedSource::afterGettingFunc * > m_afterGettingFunctions
Definition: RsSink.h:54
GLsizei const GLchar *const * string
std_msgs::Header * header(M &m)
returns Header<M>::pointer(m);
RsSink(UsageEnvironment &t_env, MediaSubsession &t_subsession, rs2_video_stream t_stream, MemoryPool *t_mempool, char const *t_streamId)
Definition: RsSink.cpp:19
GLsizei GLenum GLboolean sink
Definition: glext.h:3601
unsigned char * m_to
Definition: RsSink.h:44
void returnMem(unsigned char *t_mem)
Definition: MemoryPool.h:61
char * m_streamId
Definition: RsSink.h:47
static void afterGettingFrameUid0(void *t_clientData, unsigned t_frameSize, unsigned t_numTruncatedBytes, struct timeval t_presentationTime, unsigned t_durationInMicroseconds)
Definition: RsSink.cpp:70
virtual Boolean continuePlaying()
Definition: RsSink.cpp:147
static bool isCompressionSupported(rs2_format t_format, rs2_stream t_streamType)
MemoryPool * m_memPool
Definition: RsSink.h:53
unsigned char * m_receiveBuffer
Definition: RsSink.h:43
std::shared_ptr< ICompression > m_iCompress
Definition: RsSink.h:52
static std::shared_ptr< ICompression > getObject(int t_width, int t_height, rs2_format t_format, rs2_stream t_streamType, int t_bpp)
rtp_callback * m_rtpCallback
Definition: RsSink.h:50
MediaSubsession & m_subsession
Definition: RsSink.h:46
virtual ~RsSink()
Definition: RsSink.cpp:60
static void afterGettingFrameUid2(void *t_clientData, unsigned t_frameSize, unsigned t_numTruncatedBytes, struct timeval t_presentationTime, unsigned t_durationInMicroseconds)
Definition: RsSink.cpp:84
static RsSink * createNew(UsageEnvironment &t_env, MediaSubsession &t_subsession, rs2_video_stream t_stream, MemoryPool *t_mempool, char const *t_streamId=NULL)
Definition: RsSink.cpp:14
void afterGettingFrame(unsigned t_frameSize, unsigned t_numTruncatedBytes, struct timeval t_presentationTime, unsigned t_durationInMicroseconds)
Definition: RsSink.cpp:101
#define NULL
Definition: tinycthread.c:47
static void afterGettingFrameUid1(void *t_clientData, unsigned t_frameSize, unsigned t_numTruncatedBytes, struct timeval t_presentationTime, unsigned t_durationInMicroseconds)
Definition: RsSink.cpp:77
unsigned char * getNextMem()
Definition: MemoryPool.h:43
int m_bufferSize
Definition: RsSink.h:45
static void afterGettingFrameUid3(void *t_clientData, unsigned t_frameSize, unsigned t_numTruncatedBytes, struct timeval t_presentationTime, unsigned t_durationInMicroseconds)
Definition: RsSink.cpp:91
rs2_video_stream m_stream
Definition: RsSink.h:51


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