RsRTSPServer.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 "RTSPServer.hh"
5 #include "RTSPCommon.hh"
6 #include "RTSPRegisterSender.hh"
7 #include "Base64.hh"
8 #include <GroupsockHelper.hh>
9 #include "RsCommon.h"
10 #include "RsRTSPServer.hh"
11 #include "RsServerMediaSession.h"
15 #include "RsUsageEnvironment.h"
16 
17 // RTSPServer implementation
18 
19 RsRTSPServer* RsRTSPServer::createNew(UsageEnvironment& t_env, std::shared_ptr<RsDevice> t_device, Port t_ourPort, UserAuthenticationDatabase* t_authDatabase, unsigned t_reclamationSeconds)
20 {
21  int ourSocket = setUpOurSocket(t_env, t_ourPort);
22  if(ourSocket == -1)
23  return NULL;
24 
25  return new RsRTSPServer(t_env, t_device, ourSocket, t_ourPort, t_authDatabase, t_reclamationSeconds);
26 }
27 
28 RsRTSPServer::RsRTSPServer(UsageEnvironment& t_env, std::shared_ptr<RsDevice> t_device, int t_ourSocket, Port t_ourPort, UserAuthenticationDatabase* t_authDatabase, unsigned t_reclamationSeconds)
29  : RTSPServer(t_env, t_ourSocket, t_ourPort, t_authDatabase, t_reclamationSeconds)
30  , m_device(t_device)
31 {}
32 
34 
35 std::string getOptionString(rs2_option t_opt, float t_min, float t_max, float t_def, float t_step)
36 {
37  std::ostringstream oss;
38  oss << (int)t_opt << "{" << t_min << "," << t_max << "," << t_def << "," << t_step << "}"
39  << ";";
40  return oss.str();
41 }
42 
44 {
45  m_supportedOptionsStr.clear();
46  for(const auto& optionsPair : m_supportedOptions)
47  {
48  m_supportedOptionsStr.append(optionsPair.first);
49  m_supportedOptionsStr.append("[");
50  for(auto option : optionsPair.second)
51  {
52  m_supportedOptionsStr.append(getOptionString(option.m_opt, option.m_range.min, option.m_range.max, option.m_range.def, option.m_range.step));
53  }
54  m_supportedOptionsStr.append("]");
55  }
56  return m_supportedOptionsStr.c_str();
57 }
58 
59 void RsRTSPServer::setSupportedOptions(std::string t_key, std::vector<RsOption> t_supportedOptions)
60 {
61  m_supportedOptions.insert(std::pair<std::string, std::vector<RsOption>>(t_key, t_supportedOptions));
62 }
63 
64 // RTSPServer::RTSPClientConnection implementation
65 
66 RsRTSPServer::RsRTSPClientConnection ::RsRTSPClientConnection(RsRTSPServer& t_ourServer, int t_clientSocket, struct sockaddr_storage const& t_clientAddr)
67  : RTSPClientConnection(t_ourServer, t_clientSocket, t_clientAddr)
68  , m_fOurRsRTSPServer(t_ourServer)
69 {}
70 
72 
73 void RsRTSPServer::RsRTSPClientConnection::handleCmd_DESCRIBE(char const* urlPreSuffix, char const* urlSuffix, char const* fullRequestStr) {
74  fOurRTSPServer.closeAllClientSessionsForServerMediaSession(STEREO_SENSOR_NAME.c_str());
75  fOurRTSPServer.closeAllClientSessionsForServerMediaSession(RGB_SENSOR_NAME.c_str());
76 
77  RTSPServer::RTSPClientConnection::handleCmd_DESCRIBE(urlPreSuffix, urlSuffix, fullRequestStr);
78 }
79 
81 {
82  std::ostringstream oss;
83  std::vector<RsSensor> sensors;
84  std::string str(t_fullRequestStr);
85  std::string ContentLength("Content-Length:");
86  std::string afterSplit;
87 
88  afterSplit = str.substr(str.find(ContentLength) + ContentLength.size());
89  char* contLength = strtok((char*)afterSplit.c_str(), "\r\n: ");
90  char* sensorName = strtok(NULL, "_\r\n:");
91  char* option = strtok(NULL, "\r\n:");
92  sensors = m_fOurRsRTSPServer.m_device.get()->getSensors();
93  for(auto sensor : sensors)
94  {
95  if(sensor.getSensorName().compare(sensorName) == 0)
96  {
97  try
98  {
99  float value = sensor.getRsSensor().get_option((rs2_option)stoi(std::string(option)));
100  char* paramString = new char[strlen(sensorName) + 1 + strlen(option) + strlen(std::to_string(value).c_str()) + 10];
101  sprintf(paramString, "%s_%s: %s\r\n", sensorName, option, std::to_string(value).c_str());
102  envir() << "GET_PARAMETER: sensor '" << sensorName << "', option '" << option << "', value " << value << "\n";
103  setRTSPResponse("200 OK", paramString);
104  return;
105  }
106  catch(const std::exception& e)
107  {
108  std::string error("500 " + std::string(e.what()));
109  setRTSPResponse(error.c_str());
110  return;
111  }
112  }
113  }
114  setRTSPResponse("500 Invalid Option");
115 }
116 
118 {
119  std::ostringstream oss;
120  std::vector<RsSensor> sensors;
121  std::string str(t_fullRequestStr);
122  std::string ContentLength("Content-Length:");
123  std::string afterSplit; //, opt, val;
124 
125  afterSplit = str.substr(str.find(ContentLength) + ContentLength.size());
126  char* contLength = strtok((char*)afterSplit.c_str(), "\r\n: ");
127  char* sensorName = strtok(NULL, "_\r\n:");
128  char* option = strtok(NULL, "\r\n:");
129  char* value = strtok(NULL, "\r\n:");
130 
131  envir() << "SET_PARAMETER: sensor '" << sensorName << "', option '" << option << "', value " << value << "\n";
132  sensors = m_fOurRsRTSPServer.m_device.get()->getSensors();
133  for(auto sensor : sensors)
134  {
135  if(sensor.getSensorName().compare(sensorName) == 0)
136  {
137  try
138  {
139  sensor.getRsSensor().set_option((rs2_option)stoi(std::string(option)), stof(std::string(value)));
140  setRTSPResponse("200 OK");
141  return;
142  }
143  catch(const std::exception& e)
144  {
145  std::string error("500 " + std::string(e.what()));
146  setRTSPResponse(error.c_str());
147  return;
148  }
149  }
150  }
151  setRTSPResponse("500 Invalid Option");
152 }
153 
154 // RsRTSPServer::RsRTSPClientSession implementation
155 
156 RsRTSPServer::RsRTSPClientSession ::RsRTSPClientSession(RTSPServer& t_ourServer, u_int32_t t_sessionId)
157  : RTSPClientSession(t_ourServer, t_sessionId)
158 {}
159 
161  try
162  {
163  closeRsCamera();
164  }
165  catch(const std::exception& e)
166  {
167  std::cout << "Camera closed already\n";
168  }
169 }
170 
171 void RsRTSPServer::RsRTSPClientSession::handleCmd_TEARDOWN(RTSPClientConnection* t_ourClientConnection, ServerMediaSubsession* t_subsession)
172 {
173  envir() << "TEARDOWN \n";
174  try
175  {
176  closeRsCamera();
177  }
178  catch(const std::exception& e)
179  {
180  std::string error("500 " + std::string(e.what()));
181  setRTSPResponse(t_ourClientConnection, error.c_str());
182  return;
183  }
184 
185  RTSPServer::RTSPClientSession::handleCmd_TEARDOWN(t_ourClientConnection, t_subsession);
186 }
187 
188 void RsRTSPServer::RsRTSPClientSession::handleCmd_PLAY(RTSPClientConnection* t_ourClientConnection, ServerMediaSubsession* t_subsession, char const* t_fullRequestStr)
189 {
190  envir() << "PLAY \n";
191  try
192  {
193  openRsCamera();
194  }
195  catch(const std::exception& e)
196  {
197  std::string error("500 " + std::string(e.what()));
198  setRTSPResponse(t_ourClientConnection, error.c_str());
199  return;
200  }
201 
202  RTSPServer::RTSPClientSession::handleCmd_PLAY(t_ourClientConnection, t_subsession, t_fullRequestStr);
203 }
204 
205 void RsRTSPServer::RsRTSPClientSession::handleCmd_PAUSE(RTSPClientConnection* t_ourClientConnection, ServerMediaSubsession* t_subsession)
206 {
207  envir() << "PAUSE \n";
208  RTSPServer::RTSPClientSession::handleCmd_PAUSE(t_ourClientConnection, t_subsession);
209  try
210  {
211  closeRsCamera();
212  }
213  catch(const std::exception& e)
214  {
215  std::string error("500 " + std::string(e.what()));
216  setRTSPResponse(t_ourClientConnection, error.c_str());
217  return;
218  }
219 }
220 void RsRTSPServer::RsRTSPClientSession::handleCmd_GET_PARAMETER(RTSPClientConnection* t_ourClientConnection, ServerMediaSubsession* t_subsession, char const* t_fullRequestStr)
221 {
222  std::ostringstream oss;
223  std::vector<RsSensor> sensors;
224  std::string str(t_fullRequestStr);
225  std::string ContentLength("Content-Length:");
226  std::string afterSplit; //, opt, val;
227 
228  envir() << "GET_PARAMETER\n";
229  afterSplit = str.substr(str.find(ContentLength) + ContentLength.size());
230  char* contLength = strtok((char*)afterSplit.c_str(), "\r\n: ");
231  char* sensorName = strtok(NULL, "_\r\n:");
232  char* option = strtok(NULL, "\r\n:");
233  try
234  {
235  float value = static_cast<RsServerMediaSession*>(fOurServerMediaSession)->getRsSensor().getRsSensor().get_option((rs2_option)stoi(std::string(option)));
236  char* paramString = new char[strlen(sensorName) + 1 + strlen(option) + strlen(std::to_string(value).c_str()) + 10];
237  sprintf(paramString, "%s_%s: %s\r\n", sensorName, option, std::to_string(value).c_str());
238  setRTSPResponse(t_ourClientConnection, "200 OK", paramString);
239  }
240  catch(const std::exception& e)
241  {
242  std::string error("500 " + std::string(e.what()));
243  setRTSPResponse(t_ourClientConnection, error.c_str());
244  return;
245  }
246 }
247 
248 void RsRTSPServer::RsRTSPClientSession::handleCmd_SET_PARAMETER(RTSPClientConnection* t_ourClientConnection, ServerMediaSubsession* t_subsession, char const* t_fullRequestStr)
249 {
250  std::ostringstream oss;
251  std::vector<RsSensor> sensors;
252  std::string str(t_fullRequestStr);
253  std::string ContentLength("Content-Length:");
254  std::string afterSplit; //, opt, val;
255 
256  envir() << "SET_PARAMETER \n";
257  afterSplit = str.substr(str.find(ContentLength) + ContentLength.size());
258  char* contLength = strtok((char*)afterSplit.c_str(), "\r\n: ");
259  char* sensorName = strtok(NULL, "_\r\n:");
260  char* option = strtok(NULL, "\r\n:");
261  char* value = strtok(NULL, "\r\n:");
262 
263  try
264  {
265  static_cast<RsServerMediaSession*>(fOurServerMediaSession)->getRsSensor().getRsSensor().set_option((rs2_option)stoi(std::string(option)), stof(std::string(value)));
266  setRTSPResponse(t_ourClientConnection, "200 OK");
267  }
268  catch(const std::exception& e)
269  {
270  std::string error("500 " + std::string(e.what()));
271  setRTSPResponse(t_ourClientConnection, error.c_str());
272  return;
273  }
274 }
275 void RsRTSPServer::RsRTSPClientSession::handleCmd_SETUP(RTSPServer::RTSPClientConnection* t_ourClientConnection, char const* t_urlPreSuffix, char const* t_urlSuffix, char const* t_fullRequestStr)
276 {
277  RTSPServer::RTSPClientSession::handleCmd_SETUP(t_ourClientConnection, t_urlPreSuffix, t_urlSuffix, t_fullRequestStr);
278  ServerMediaSubsession* subsession;
279  if(t_urlSuffix[0] != '\0' && strcmp(fOurServerMediaSession->streamName(), t_urlPreSuffix) == 0)
280  {
281  // Non-aggregated operation.
282  // Look up the media subsession whose track id is "urlSuffix":
283  ServerMediaSubsessionIterator iter(*fOurServerMediaSession);
284  while((subsession = iter.next()) != NULL)
285  {
286  if(strcmp(subsession->trackId(), t_urlSuffix) == 0)
287  {
288  long long int profileKey = static_cast<RsServerMediaSession*>(fOurServerMediaSession)->getRsSensor().getStreamProfileKey(((RsServerMediaSubsession*)(subsession))->getStreamProfile());
289  m_streamProfiles[profileKey] = ((RsServerMediaSubsession*)(subsession))->getFrameQueue();
290  break; // success
291  }
292  }
293  }
294 }
295 
297 {
298  static_cast<RsServerMediaSession*>(fOurServerMediaSession)->openRsCamera(m_streamProfiles);
299 }
300 
302 {
303  ((RsServerMediaSession*)fOurServerMediaSession)->closeRsCamera();
304  for(int i = 0; i < fNumStreamStates; ++i)
305  {
306  if(fStreamStates[i].subsession != NULL)
307  {
308  long long int profile_key = static_cast<RsServerMediaSession*>(fOurServerMediaSession)->getRsSensor().getStreamProfileKey(((RsServerMediaSubsession*)(fStreamStates[i].subsession))->getStreamProfile());
309  emptyStreamProfileQueue(profile_key);
310  }
311  }
312 }
313 
315 {
316  rs2::frame f;
317  if(m_streamProfiles.find(profile_key) != m_streamProfiles.end())
318  {
319  while(m_streamProfiles[profile_key].poll_for_frame(&f))
320  ;
321  }
322 }
323 
324 GenericMediaServer::ClientConnection* RsRTSPServer::createNewClientConnection(int clientSocket, struct sockaddr_storage const& clientAddr)
325 {
326  return new RsRTSPClientConnection(*this, clientSocket, clientAddr);
327 }
328 
329 GenericMediaServer::ClientSession* RsRTSPServer::createNewClientSession(u_int32_t t_sessionId)
330 {
331  return new RsRTSPClientSession(*this, t_sessionId);
332 }
std::unordered_map< long long int, rs2::frame_queue > m_streamProfiles
Definition: RsRTSPServer.hh:66
float stof(const std::string &value)
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
void setSupportedOptions(std::string t_key, std::vector< RsOption > t_supportedOptions)
GLfloat value
const std::string RGB_SENSOR_NAME("RGB Camera")
RsRTSPClientSession(RTSPServer &t_ourServer, u_int32_t t_sessionId)
virtual ClientSession * createNewClientSession(u_int32_t t_sessionId)
virtual void handleCmd_GET_PARAMETER(RTSPClientConnection *t_ourClientConnection, ServerMediaSubsession *t_subsession, char const *t_fullRequestStr)
virtual void handleCmd_SET_PARAMETER(RTSPClientConnection *t_ourClientConnection, ServerMediaSubsession *t_subsession, char const *t_fullRequestStr)
GLsizei const GLchar *const * string
std::string m_supportedOptionsStr
Definition: RsRTSPServer.hh:23
e
Definition: rmse.py:177
virtual void handleCmd_PAUSE(RTSPClientConnection *t_ourClientConnection, ServerMediaSubsession *t_subsession)
GLdouble f
std::shared_ptr< RsDevice > m_device
Definition: RsRTSPServer.hh:24
Definition: getopt.h:41
virtual void handleCmd_PLAY(RTSPClientConnection *t_ourClientConnection, ServerMediaSubsession *t_subsession, char const *t_fullRequestStr)
RsRTSPServer(UsageEnvironment &t_env, std::shared_ptr< RsDevice > t_device, int t_ourSocket, Port t_ourPort, UserAuthenticationDatabase *t_authDatabase, unsigned t_reclamationSeconds)
std::ostream & cout()
char const * allowedCommandNames()
virtual void handleCmd_SETUP(RTSPServer::RTSPClientConnection *t_ourClientConnection, char const *t_urlPreSuffix, char const *t_urlSuffix, char const *t_fullRequestStr)
virtual void handleCmd_SET_PARAMETER(char const *fullRequestStr)
const std::string STEREO_SENSOR_NAME("Stereo Module")
virtual void handleCmd_DESCRIBE(char const *urlPreSuffix, char const *urlSuffix, char const *fullRequestStr)
RsRTSPClientConnection(RsRTSPServer &t_ourServer, int t_clientSocket, struct sockaddr_storage const &t_clientAddr)
void emptyStreamProfileQueue(long long int t_profile_key)
virtual void handleCmd_TEARDOWN(RTSPClientConnection *t_ourClientConnection, ServerMediaSubsession *t_subsession)
std::string getOptionString(rs2_option t_opt, float t_min, float t_max, float t_def, float t_step)
virtual void handleCmd_GET_PARAMETER(char const *fullRequestStr)
int stoi(const std::string &value)
#define NULL
Definition: tinycthread.c:47
virtual ClientConnection * createNewClientConnection(int t_clientSocket, struct sockaddr_storage const &t_clientAddr)
int i
static RsRTSPServer * createNew(UsageEnvironment &t_env, std::shared_ptr< RsDevice > t_device, Port t_ourPort=554, UserAuthenticationDatabase *t_authDatabase=NULL, unsigned t_reclamationSeconds=20)
std::map< std::string, std::vector< RsOption > > m_supportedOptions
Definition: RsRTSPServer.hh:22
virtual ~RsRTSPServer()
std::string to_string(T value)


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