-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhsRTSP.cxx
236 lines (191 loc) · 7.62 KB
/
hsRTSP.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "hsRTSP.h"
// Create
hsRTSP* hsRTSP::Create( const char* username,
const char* password,
const char* ipaddr,
const char* port,
uint32_t width,
uint32_t height,
const char* camera )
{
hsRTSP* rtsp = new hsRTSP();
if( !rtsp )
return NULL;
if( !rtsp->parseCameraStr( camera ) )
return NULL;
rtsp->mWidth = width;
rtsp->mHeight = height;
rtsp->mDepth = rtsp->csiCamera() ? 12 : 24;
rtsp->mSize = (width * height * rtsp->mDepth) / 8;
rtsp->mUserName = username;
rtsp->mPassWord = password;
rtsp->mIpAddress = ipaddr;
rtsp->mPort = port;
/*
cout << "hsRTSP: Going to use Username : " << rtsp->mUserName.c_str() << endl;
cout << "hsRTSP: Going to use Password : " << rtsp->mPassWord.c_str() << endl;
cout << "hsRTSP: Going to use IP Address : " << rtsp->mIpAddress.c_str() << endl;
cout << "hsRTSP: Going to use Port : " << rtsp->mPort.c_str() << endl;
*/
if( !rtsp->init( GST_SOURCE_NVARGUS ) )
{
cerr << "hsRTSP: failed to init rtsp stream with GST_SOURCE_NVARGUS, " << rtsp->mCameraStr.c_str() << endl;
if( !rtsp->init( GST_SOURCE_NVCAMERA ) )
{
cerr << "hsRTSP: failed to init rtsp stream with GST_SOURCE_NVCAMERA, " << rtsp->mCameraStr.c_str() << endl;
if( rtsp->mSensorCSI >= 0 ) rtsp->mSensorCSI = -1;
if( !rtsp->init( GST_SOURCE_V4L2 ) )
{
cerr << "hsRTSP: failed to init rtsp stream with GST_SOURCE_V4L2, " << rtsp->mCameraStr.c_str() << endl;
return NULL;
}
}
}
cout << "hsRTSP: rtsp stream successfully initialized with " << rtsp->mCameraStr.c_str() << endl;
return rtsp;
}
// Create with several defaults
hsRTSP* hsRTSP::Create( const char* username,
const char* password,
const char* camera )
{
return Create( username, password, DefaultRtspIpAddress, DefaultRtspPort, DefaultWidth, DefaultHeight, camera );
}
// constructor
hsRTSP::hsRTSP()
{
mServingLoop = NULL;
mStreaming = false;
mWidth = 0;
mHeight = 0;
mDepth = 0;
mSize = 0;
mSource = GST_SOURCE_NVCAMERA;
mSensorCSI = -1;
}
// destructor
hsRTSP::~hsRTSP()
{
}
// startStreaming
void hsRTSP::startStreaming()
{
GstRTSPServer *server;
GstRTSPMountPoints *mounts;
GstRTSPMediaFactory *factory;
mStreaming = true;
mServingLoop = g_main_loop_new( NULL, FALSE );
/* create a server instance */
server = gst_rtsp_server_new();
//g_object_set( server, "service", port, NULL );
g_object_set( server, "service", mPort.c_str(), NULL );
gst_rtsp_server_set_address( server, mIpAddress.c_str() ); /* IP address of Jetson Nano */
/* get the mount points for this server, every server has a default object
* that be used to map uri mount points to media factories */
mounts = gst_rtsp_server_get_mount_points( server );
/* make a media factory for a test stream. The default media factory can use
* gst-launch syntax to create pipelines.
* any launch line works as long as it contains elements named pay%d. Each
* element with pay%d names will be a stream */
factory = gst_rtsp_media_factory_new();
gst_rtsp_media_factory_set_launch( factory, mLaunchStr.c_str() );
gst_rtsp_media_factory_set_shared( factory, TRUE );
/* attach the test factory to the /test url */
gst_rtsp_mount_points_add_factory( mounts, "/test", factory );
/* don't need the ref to the mapper anymore */
g_object_unref( mounts );
/* attach the server to the default maincontext */
gst_rtsp_server_attach( server, NULL );
/* start serving */
cout << "hsRTSP: pipeline launch string: " << mLaunchStr.c_str() << endl;
cout << "hsRTSP: stream serving at rtsp://" << mIpAddress.c_str() << ":" << mPort.c_str() << "/test" << endl;
g_main_loop_run( mServingLoop );
}
// stopStreaming
void hsRTSP::stopStreaming()
{
if( mServingLoop != NULL )
if( g_main_loop_is_running( mServingLoop ) )
g_main_loop_quit( mServingLoop );
mStreaming = false;
}
// isStreaming
bool hsRTSP::isStreaming()
{
return( mStreaming );
}
// init
bool hsRTSP::init( gstCameraSrc src )
{
// build pipeline string
if( !buildLaunchStr( src ) )
{
return false;
}
return true;
}
// buildLaunchStr
bool hsRTSP::buildLaunchStr( gstCameraSrc src )
{
// gst-launch-1.0 nvcamerasrc fpsRange="30.0 30.0" ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1' ! \
// nvvidconv flip-method=2 ! 'video/x-raw(memory:NVMM), format=(string)I420' ! fakesink silent=false -v
// #define CAPS_STR "video/x-raw(memory:NVMM), width=(int)2592, height=(int)1944, format=(string)I420, framerate=(fraction)30/1"
// #define CAPS_STR "video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1"
std::ostringstream ss;
if( csiCamera() && src != GST_SOURCE_V4L2 )
{
mSource = src;
#if NV_TENSORRT_MAJOR > 1 && NV_TENSORRT_MAJOR < 5 // if JetPack 3.1-3.3 (different flip-method)
const int flipMethod = 0; // Xavier (w/TRT5) camera is mounted inverted
#else
const int flipMethod = 2;
#endif
if( src == GST_SOURCE_NVCAMERA )
ss << "nvcamerasrc fpsRange=\"30.0 30.0\" ! video/x-raw(memory:NVMM), width=(int)" << mWidth << ", height=(int)" << mHeight << ", format=(string)NV12 ! nvvidconv flip-method=" << flipMethod << " ! ";
else if( src == GST_SOURCE_NVARGUS )
ss << "nvarguscamerasrc sensor-id=" << mSensorCSI << " ! video/x-raw(memory:NVMM), width=(int)" << mWidth << ", height=(int)" << mHeight << ", framerate=30/1, format=(string)NV12 ! nvvidconv flip-method=" << flipMethod << " ! ";
ss << "x264enc ! rtph264pay name=pay0 pt=96";
}
else
{
ss << "v4l2src device=" << mCameraStr << " ! ";
ss << "video/x-raw, width=(int)" << mWidth << ", height=(int)" << mHeight << ", ";
#if NV_TENSORRT_MAJOR >= 5
ss << "format=YUY2 ! videoconvert ! video/x-raw, format=RGB ! videoconvert !";
#else
ss << "format=RGB ! videoconvert ! video/x-raw, format=RGB ! videoconvert !";
#endif
ss << "x264enc ! rtph264pay name=pay0 pt=96";
mSource = GST_SOURCE_V4L2;
}
//mLaunchStr = ss.str();
mLaunchStr = "videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96";
return true;
}
// parseCameraStr
bool hsRTSP::parseCameraStr( const char* camera )
{
if( !camera || strlen( camera ) == 0 )
{
mSensorCSI = 0;
mCameraStr = "0";
return true;
}
mCameraStr = camera;
// check if the string is a V4L2 device
const char* prefixV4L2 = "/dev/video";
const size_t prefixLength = strlen( prefixV4L2 );
const size_t cameraLength = strlen( camera );
if( cameraLength < prefixLength )
{
const int result = sscanf( camera, "%i", &mSensorCSI );
if( result == 1 && mSensorCSI >= 0 )
return true;
}
else if( strncmp( camera, prefixV4L2, prefixLength ) == 0 )
{
return true;
}
cerr << "hsRTSP: Invalid camera device requested by Create: " << camera << endl;
return false;
}