-
Notifications
You must be signed in to change notification settings - Fork 784
/
gazebo_gst_camera_plugin.cpp
348 lines (287 loc) · 9.8 KB
/
gazebo_gst_camera_plugin.cpp
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright (C) 2012-2016 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifdef _WIN32
// Ensure that Winsock2.h is included before Windows.h, which can get
// pulled in by anybody (e.g., Boost).
#include <Winsock2.h>
#endif
#include "gazebo/sensors/DepthCameraSensor.hh"
#include "gazebo_gst_camera_plugin.h"
#include <gst/app/gstappsrc.h>
#include <math.h>
#include <string>
#include <iostream>
#include <thread>
#include <time.h>
#include "Int32.pb.h"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace gazebo;
using namespace cv;
GZ_REGISTER_SENSOR_PLUGIN(GstCameraPlugin)
static void* start_thread(void* param) {
GstCameraPlugin* plugin = (GstCameraPlugin*)param;
plugin->startGstThread();
return nullptr;
}
/////////////////////////////////////////////////
void GstCameraPlugin::startGstThread() {
gst_init(nullptr, nullptr);
this->gst_loop = g_main_loop_new(nullptr, FALSE);
if (!this->gst_loop) {
gzerr << "Create loop failed. \n";
return;
}
GstElement* pipeline = gst_pipeline_new(nullptr);
if (!pipeline) {
gzerr << "ERR: Create pipeline failed. \n";
return;
}
GstElement* source = gst_element_factory_make("appsrc", nullptr);
GstElement* queue = gst_element_factory_make("queue", nullptr);
GstElement* converter = gst_element_factory_make("videoconvert", nullptr);
GstElement* encoder;
if (useCuda) {
encoder = gst_element_factory_make("nvh264enc", nullptr);
g_object_set(G_OBJECT(encoder), "bitrate", 800, "preset", 1, nullptr);
} else {
encoder = gst_element_factory_make("x264enc", nullptr);
g_object_set(G_OBJECT(encoder), "bitrate", 800, "speed-preset", 6, "tune", 4, "key-int-max", 10, nullptr);
}
GstElement* payloader;
GstElement* sink;
if (useRtmp) {
payloader = gst_element_factory_make("flvmux", nullptr);
sink = gst_element_factory_make("rtmpsink", nullptr);
g_object_set(G_OBJECT(sink), "location", this->rtmpLocation.c_str(), nullptr);
} else {
payloader = gst_element_factory_make("rtph264pay", nullptr);
sink = gst_element_factory_make("udpsink", nullptr);
g_object_set(G_OBJECT(sink), "host", this->udpHost.c_str(), "port", this->udpPort, nullptr);
}
if (!source || !queue || !converter || !encoder || !payloader || !sink) {
gzerr << "ERR: Create elements failed. \n";
return;
}
// gzerr <<"width"<< this->width<<"\n";
// gzerr <<"height"<< this->height<<"\n";
// gzerr <<"rate"<< this->rate<<"\n";
// Configure source element
g_object_set(G_OBJECT(source), "caps",
gst_caps_new_simple ("video/x-raw",
"format", G_TYPE_STRING, "I420",
"width", G_TYPE_INT, this->width,
"height", G_TYPE_INT, this->height,
"framerate", GST_TYPE_FRACTION, (unsigned int)this->rate, 1, nullptr),
"is-live", TRUE,
"do-timestamp", TRUE,
"stream-type", GST_APP_STREAM_TYPE_STREAM,
"format", GST_FORMAT_TIME, nullptr);
// Connect all elements to pipeline
gst_bin_add_many(GST_BIN(pipeline), source, queue, converter, encoder, payloader, sink, nullptr);
// Link all elements
if (gst_element_link_many(source, queue, converter, encoder, payloader, sink, nullptr) != TRUE) {
gzerr << "ERR: Link all the elements failed. \n";
return;
}
this->source = source;
gst_object_ref(this->source);
// Start
gst_element_set_state(pipeline, GST_STATE_PLAYING);
g_main_loop_run(this->gst_loop);
// Clean up
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(GST_OBJECT(pipeline));
gst_object_unref(this->source);
g_main_loop_unref(this->gst_loop);
this->gst_loop = nullptr;
this->source = nullptr;
}
/////////////////////////////////////////////////
void GstCameraPlugin::stopGstThread()
{
if(this->gst_loop) {
g_main_loop_quit(this->gst_loop);
}
}
/////////////////////////////////////////////////
GstCameraPlugin::GstCameraPlugin()
: SensorPlugin(), width(0), height(0), depth(0), gst_loop(nullptr), source(nullptr), mIsActive(false)
{
}
/////////////////////////////////////////////////
GstCameraPlugin::~GstCameraPlugin()
{
this->parentSensor.reset();
this->camera.reset();
if (this->gst_loop) {
g_main_loop_quit(this->gst_loop);
}
}
/////////////////////////////////////////////////
void GstCameraPlugin::Load(sensors::SensorPtr sensor, sdf::ElementPtr sdf)
{
if (!sensor)
gzerr << "Invalid sensor pointer.\n";
this->parentSensor =
#if GAZEBO_MAJOR_VERSION >= 7
std::dynamic_pointer_cast<sensors::CameraSensor>(sensor);
#else
boost::dynamic_pointer_cast<sensors::CameraSensor>(sensor);
#endif
if (!this->parentSensor)
{
gzerr << "GstCameraPlugin requires a CameraSensor.\n";
#if GAZEBO_MAJOR_VERSION >= 7
if (std::dynamic_pointer_cast<sensors::DepthCameraSensor>(sensor))
#else
if (boost::dynamic_pointer_cast<sensors::DepthCameraSensor>(sensor))
#endif
gzmsg << "It is a depth camera sensor\n";
}
#if GAZEBO_MAJOR_VERSION >= 7
this->camera = this->parentSensor->Camera();
#else
this->camera = this->parentSensor->GetCamera();
#endif
if (!this->parentSensor)
{
gzerr << "GstCameraPlugin not attached to a camera sensor\n";
return;
}
#if GAZEBO_MAJOR_VERSION >= 7
this->width = this->camera->ImageWidth();
this->height = this->camera->ImageHeight();
this->depth = this->camera->ImageDepth();
this->format = this->camera->ImageFormat();
this->rate = this->camera->RenderRate();
#else
this->width = this->camera->GetImageWidth();
this->height = this->camera->GetImageHeight();
this->depth = this->camera->GetImageDepth();
this->format = this->camera->GetImageFormat();
this->rate = this->camera->GetRenderRate();
#endif
if (!isfinite(this->rate)) {
this->rate = 60.0;
}
if (sdf->HasElement("robotNamespace"))
namespace_ = sdf->GetElement("robotNamespace")->Get<std::string>();
else
gzwarn << "[gazebo_gst_camera_plugin] Please specify a robotNamespace.\n";
this->udpHost = "127.0.0.1";
const char *host_ip = std::getenv("PX4_VIDEO_HOST_IP");
if (host_ip) {
this->udpHost = std::string(host_ip);
} else if (sdf->HasElement("udpHost")) {
this->udpHost = sdf->GetElement("udpHost")->Get<string>();
}
this->udpPort = 5600;
if (sdf->HasElement("udpPort")) {
this->udpPort = sdf->GetElement("udpPort")->Get<int>();
}
gzwarn << "[gazebo_gst_camera_plugin] Streaming video to ip: " << this->udpHost << " port: " << this->udpPort << std::endl;
if (sdf->HasElement("rtmpLocation")) {
this->rtmpLocation = sdf->GetElement("rtmpLocation")->Get<string>();
this->useRtmp = true;
} else {
this->useRtmp = false;
}
// Use CUDA for video encoding
if (sdf->HasElement("useCuda")) {
this->useCuda = sdf->GetElement("useCuda")->Get<bool>();
} else {
this->useCuda = false;
}
node_handle_ = transport::NodePtr(new transport::Node());
node_handle_->Init(namespace_);
// Listen to Gazebo topic
mVideoSub = node_handle_->Subscribe<msgs::Int>(mTopicName, &GstCameraPlugin::cbVideoStream, this);
// And start by default
startStreaming();
}
/////////////////////////////////////////////////
void GstCameraPlugin::cbVideoStream(const boost::shared_ptr<const msgs::Int> &_msg)
{
gzwarn << "Video Streaming callback: " << _msg->data() << "\n";
int enable = _msg->data();
if(enable)
startStreaming();
else
stopStreaming();
}
/////////////////////////////////////////////////
void GstCameraPlugin::startStreaming()
{
if(!mIsActive) {
this->newFrameConnection = this->camera->ConnectNewImageFrame(
boost::bind(&GstCameraPlugin::OnNewFrame, this, _1, this->width, this->height, this->depth, this->format));
this->parentSensor->SetActive(true);
/* start the gstreamer event loop */
pthread_create(&mThreadId, NULL, start_thread, this);
mIsActive = true;
}
}
/////////////////////////////////////////////////
void GstCameraPlugin::stopStreaming()
{
if(mIsActive) {
stopGstThread();
pthread_join(mThreadId, NULL);
this->parentSensor->SetActive(false);
this->newFrameConnection->~Connection();
mIsActive = false;
}
}
/////////////////////////////////////////////////
void GstCameraPlugin::OnNewFrame(const unsigned char * image,
unsigned int width,
unsigned int height,
unsigned int depth,
const std::string &format)
{
#if GAZEBO_MAJOR_VERSION >= 7
image = this->camera->ImageData(0);
#else
image = this->camera->GetImageData(0);
#endif
// Alloc buffer
const guint size = width * height * 1.5;
GstBuffer* buffer = gst_buffer_new_allocate(NULL, size, NULL);
if (!buffer) {
gzerr << "gst_buffer_new_allocate failed" << endl;
return;
}
GstMapInfo map;
if (!gst_buffer_map(buffer, &map, GST_MAP_WRITE)) {
gzerr << "gst_buffer_map failed" << endl;
return;
}
// Color Conversion from RGB to YUV
Mat frame = Mat(height, width, CV_8UC3);
Mat frameYUV = Mat(height, width, CV_8UC3);
frame.data = (uchar*)image;
cvtColor(frame, frameYUV, COLOR_RGB2YUV_I420);
memcpy(map.data, frameYUV.data, size);
gst_buffer_unmap(buffer, &map);
GstFlowReturn ret = gst_app_src_push_buffer(GST_APP_SRC(this->source), buffer);
if (ret != GST_FLOW_OK) {
/* something wrong, stop pushing */
gzerr << "gst_app_src_push_buffer failed" << endl;
g_main_loop_quit(this->gst_loop);
}
}