RsServer.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 <iostream>
5 
6 #include <liveMedia.hh>
7 #include <GroupsockHelper.hh>
8 #include <signal.h>
9 #include "RsUsageEnvironment.h"
10 #include "RsSource.hh"
12 #include "RsDevice.hh"
13 #include "RsRTSPServer.hh"
14 #include "RsServerMediaSession.h"
15 #include "RsCommon.h"
17 
18 #include "tclap/CmdLine.h"
19 #include "tclap/ValueArg.h"
20 
21 using namespace TCLAP;
22 
23 struct server
24 {
27  UsageEnvironment* env;
28  std::shared_ptr<RsDevice> rsDevice;
29  std::vector<rs2::video_stream_profile> supported_stream_profiles; // streams for extrinsics map creation
30  std::vector<RsSensor> sensors;
31  TaskScheduler* scheduler;
32  unsigned int port = 8554;
33 
34  void main(int argc, char** argv)
35  {
36  std::cout << "Rs-server is running\n";
37 
38  START_EASYLOGGINGPP(argc, argv);
39 
40  CmdLine cmd("LRS Network Extentions Server", ' ', RS2_API_VERSION_STR);
41 
42  SwitchArg arg_enable_compression("c", "enable-compression", "Enable video compression");
43  ValueArg<std::string> arg_address("i", "interface-address", "Address of the interface to bind on", false, "", "string");
44  ValueArg<unsigned int> arg_port("p", "port", "RTSP port to listen on", false, 8554, "integer");
45 
46  cmd.add(arg_enable_compression);
47  cmd.add(arg_address);
48  cmd.add(arg_port);
49 
50  cmd.parse(argc, argv);
51 
53  if (arg_enable_compression.isSet())
54  {
56  }
57 
58  if (arg_address.isSet())
59  {
60  ReceivingInterfaceAddr = inet_addr(arg_address.getValue().c_str());
61  }
62 
63  if (arg_port.isSet())
64  {
65  port = arg_port.getValue();
66  }
67 
68  OutPacketBuffer::increaseMaxSizeTo(MAX_MESSAGE_SIZE);
69 
70  // Begin by setting up our usage environment:
71  scheduler = BasicTaskScheduler::createNew();
72  env = RSUsageEnvironment::createNew(*scheduler);
73 
74  rsDevice = std::make_shared<RsDevice>(env);
75  rtspServer = RsRTSPServer::createNew(*env, rsDevice, port);
76 
77  if(rtspServer == NULL)
78  {
79  *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
80  exit(1);
81  }
82 
83  sensors = rsDevice.get()->getSensors();
84  for(auto sensor : sensors)
85  {
87  if(sensor.getSensorName().compare(STEREO_SENSOR_NAME) == 0 || sensor.getSensorName().compare(RGB_SENSOR_NAME) == 0)
88  {
89  sms = RsServerMediaSession::createNew(*env, sensor, sensor.getSensorName().data(), "", "Session streamed by \"realsense streamer\"", False);
90  }
91  else
92  {
93  break;
94  }
95 
96  for(auto stream_profile : sensor.getStreamProfiles())
97  {
98  rs2::video_stream_profile stream = stream_profile.second;
99  if(stream.format() == RS2_FORMAT_BGR8
100  || stream.format() == RS2_FORMAT_RGB8
101  || stream.format() == RS2_FORMAT_Z16
102  || stream.format() == RS2_FORMAT_Y8
103  || stream.format() == RS2_FORMAT_YUYV
104  || stream.format() == RS2_FORMAT_UYVY)
105  {
106  if(stream.fps() == 6)
107  {
108  if((stream.width() == 1280 && stream.height() == 720) || (stream.width() == 640 && stream.height() == 480))
109  {
110  sms->addSubsession(RsServerMediaSubsession::createNew(*env, stream, rsDevice));
111 
112  supported_stream_profiles.push_back(stream);
113  continue;
114  }
115  }
116  else if(stream.fps() == 15 || stream.fps() == 30)
117  {
118  if((stream.width() == 1280 && stream.height() == 720) || (stream.width() == 640 && stream.height() == 480))
119  {
120  sms->addSubsession(RsServerMediaSubsession::createNew(*env, stream, rsDevice));
121  supported_stream_profiles.push_back(stream);
122  continue;
123  }
124  }
125  else if(stream.fps() == 60)
126  {
127  if((stream.width() == 640 && stream.height() == 480))
128  {
129  sms->addSubsession(RsServerMediaSubsession::createNew(*env, stream, rsDevice));
130  supported_stream_profiles.push_back(stream);
131  continue;
132  }
133  }
134  else if(stream.fps() == 10)
135  {
136  if(stream.format() == RS2_FORMAT_YUYV && stream.width() == 1280 && stream.height() == 720)
137  {
138  sms->addSubsession(RsServerMediaSubsession::createNew(*env, stream, rsDevice));
139  supported_stream_profiles.push_back(stream);
140  continue;
141  }
142  }
143  }
144  if (stream.format() == RS2_FORMAT_Z16 || stream.format() == RS2_FORMAT_Y8 )
145  {
146  if(stream.fps() == 6 || stream.fps() == 15 || stream.fps() == 30)
147  {
148  if(stream.width() == 848 && stream.height() == 480)
149  {
150  sms->addSubsession(RsServerMediaSubsession::createNew(*env, stream, rsDevice));
151  supported_stream_profiles.push_back(stream);
152  continue;
153  }
154  }
155  if(stream.fps() == 30 || stream.fps() == 60)
156  {
157  if(stream.width() == 640 && stream.height() == 360)
158  {
159  sms->addSubsession(RsServerMediaSubsession::createNew(*env, stream, rsDevice));
160  supported_stream_profiles.push_back(stream);
161  continue;
162  }
163  }
164  }
165  if (stream.format() == RS2_FORMAT_YUYV)
166  {
167  if(stream.fps() == 6 || stream.fps() == 15 || stream.fps() == 30 || stream.fps() == 60)
168  {
169  if(stream.width() == 424 && stream.height() == 240)
170  {
171  sms->addSubsession(RsServerMediaSubsession::createNew(*env, stream, rsDevice));
172  supported_stream_profiles.push_back(stream);
173  continue;
174  }
175  }
176  }
177  if (stream.format() == RS2_FORMAT_Z16 || stream.format() == RS2_FORMAT_Y8 || stream.format() == RS2_FORMAT_UYVY)
178  {
179  if(stream.fps() == 6 || stream.fps() == 15 || stream.fps() == 30 || stream.fps() == 60 || stream.fps() == 90)
180  {
181  if(stream.width() == 480 && stream.height() == 270)
182  {
183  sms->addSubsession(RsServerMediaSubsession::createNew(*env, stream, rsDevice));
184  supported_stream_profiles.push_back(stream);
185  continue;
186  }
187  }
188  }
189 
190  *env << "Ignoring stream: format: " << stream.format() << " width: " << stream.width() << " height: " << stream.height() << " fps: " << stream.fps() << "\n";
191  }
192 
193  calculate_extrinsics();
194 
195  rtspServer->addServerMediaSession(sms);
196  char* url = rtspServer->rtspURL(sms);
197  *env << "Play this stream using the URL \"" << url << "\"\n";
198 
199  // query camera options
200  rtspServer->setSupportedOptions(sensor.getSensorName(), sensor.getSupportedOptions());
201 
202  delete[] url;
203  }
204 
205  env->taskScheduler().doEventLoop(); // does not return
206  }
207 
209  {
210  for(auto stream_profile_from : supported_stream_profiles)
211  {
212  for(auto stream_profile_to : supported_stream_profiles)
213  {
214  int from_sensor_key = RsDevice::getPhysicalSensorUniqueKey(stream_profile_from.stream_type(), stream_profile_from.stream_index());
215  int to_sensor_key = RsDevice::getPhysicalSensorUniqueKey(stream_profile_to.stream_type(), stream_profile_to.stream_index());
216 
217  rsDevice.get()->minimal_extrinsics_map[std::make_pair(from_sensor_key, to_sensor_key)] = stream_profile_from.get_extrinsics_to(stream_profile_to);
218  }
219  }
220  }
221 
222  void sigint_handler(int sig)
223  {
224  Medium::close(rtspServer);
225  env->reclaim();
226  env = NULL;
227  delete scheduler;
228  scheduler = NULL;
229  std::cout << "Rs-server downloading\n";
230  }
231 
232  // Make server a proper singleton
233  // Otherwise, initialization of RsDevice is crashing
234  // in static build because of order of initialization
235  static server& instance()
236  {
237  static server inst;
238  return inst;
239  }
240 };
241 
242 void sigint_handler(int sig);
243 
244 int main(int argc, char** argv)
245 {
246  server::instance().main(argc, argv);
247  return 0;
248 }
249 
250 void sigint_handler(int sig)
251 {
253  exit(sig);
254 }
const int MAX_MESSAGE_SIZE
Definition: RsCommon.h:52
void sigint_handler(int sig)
Definition: RsServer.cpp:222
static server & instance()
Definition: RsServer.cpp:235
int main(int argc, char **argv)
Definition: RsServer.cpp:244
#define START_EASYLOGGINGPP(argc, argv)
const std::string RGB_SENSOR_NAME("RGB Camera")
RsRTSPServer * rtspServer
Definition: RsServer.cpp:26
std::shared_ptr< RsDevice > rsDevice
Definition: RsServer.cpp:28
GLuint GLuint stream
Definition: glext.h:1790
void sigint_handler(int sig)
Definition: RsServer.cpp:250
static int getPhysicalSensorUniqueKey(rs2_stream stream_type, int sensors_index)
Definition: RsDevice.cpp:9
std::ostream & cout()
std::vector< rs2::video_stream_profile > supported_stream_profiles
Definition: RsServer.cpp:29
rs2::device selected_device
Definition: RsServer.cpp:25
UsageEnvironment * env
Definition: RsServer.cpp:27
const std::string STEREO_SENSOR_NAME("Stereo Module")
bool isSet() const
Definition: Arg.h:575
TaskScheduler * scheduler
Definition: RsServer.cpp:31
void calculate_extrinsics()
Definition: RsServer.cpp:208
static const textual_icon exit
Definition: model-views.h:254
static RsServerMediaSubsession * createNew(UsageEnvironment &t_env, rs2::video_stream_profile &t_videoStreamProfile, std::shared_ptr< RsDevice > rsDevice)
T & getValue()
Definition: ValueArg.h:322
rs2_format format() const
Definition: rs_frame.hpp:44
void parse(int argc, const char *const *argv)
Definition: CmdLine.h:433
#define RS2_API_VERSION_STR
Definition: rs.h:43
static bool & getIsEnabled()
void add(Arg &a)
Definition: CmdLine.h:413
#define NULL
Definition: tinycthread.c:47
Definition: Arg.h:57
static RsServerMediaSession * createNew(UsageEnvironment &t_env, RsSensor &t_sensor, char const *t_streamName=NULL, char const *t_info=NULL, char const *t_description=NULL, Boolean t_isSSM=False, char const *t_miscSDPLines=NULL)
static RsRTSPServer * createNew(UsageEnvironment &t_env, std::shared_ptr< RsDevice > t_device, Port t_ourPort=554, UserAuthenticationDatabase *t_authDatabase=NULL, unsigned t_reclamationSeconds=20)
static RSUsageEnvironment * createNew(TaskScheduler &taskScheduler)
int fps() const
Definition: rs_frame.hpp:49
std::vector< RsSensor > sensors
Definition: RsServer.cpp:30
void main(int argc, char **argv)
Definition: RsServer.cpp:34


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