Skip to content

Commit 8a0d6e8

Browse files
committed
make sure image is ready to publish before re-publishing
1 parent 616d666 commit 8a0d6e8

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/video_stream.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ boost::shared_ptr<dynamic_reconfigure::Server<VideoStreamConfig> > dyn_srv;
6363
VideoStreamConfig config;
6464
std::mutex q_mutex, s_mutex, c_mutex, p_mutex;
6565
std::queue<cv::Mat> framesQueue;
66+
bool has_captured;
6667
cv::Mat frame;
6768
boost::shared_ptr<cv::VideoCapture> cap;
6869
std::string video_stream_provider;
@@ -165,13 +166,15 @@ virtual void do_capture() {
165166
framesQueue.pop();
166167
}
167168
framesQueue.push(capture_frame.clone());
169+
has_captured = true;
168170
}
169171
}
170172
NODELET_DEBUG("Capture thread finished");
171173
}
172174

173175
virtual void do_publish(const ros::TimerEvent& event) {
174176
bool is_new_image = false;
177+
bool ready_to_publish = false;
175178
sensor_msgs::ImagePtr msg;
176179
std_msgs::Header header;
177180
VideoStreamConfig latest_config;
@@ -189,10 +192,11 @@ virtual void do_publish(const ros::TimerEvent& event) {
189192
framesQueue.pop();
190193
is_new_image = true;
191194
}
195+
ready_to_publish = !frame.empty() && has_captured;
192196
}
193197

194198
// Check if grabbed frame is actually filled with some content
195-
if(!frame.empty() && is_new_image) {
199+
if(ready_to_publish && is_new_image) {
196200
// From http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#void flip(InputArray src, OutputArray dst, int flipCode)
197201
// FLIP_HORIZONTAL == 1, FLIP_VERTICAL == 0 or FLIP_BOTH == -1
198202
// Flip the image if necessary
@@ -306,6 +310,15 @@ virtual bool subscribe() {
306310
cap->set(cv::CAP_PROP_EXPOSURE, latest_config.exposure);
307311
}
308312

313+
// cleanup previous session
314+
{
315+
std::lock_guard<std::mutex> g(q_mutex);
316+
while (!framesQueue.empty()) {
317+
framesQueue.pop();
318+
}
319+
has_captured = false;
320+
}
321+
309322
try {
310323
capture_thread = boost::thread(
311324
boost::bind(&VideoStreamNodelet::do_capture, this));
@@ -326,10 +339,6 @@ virtual void unsubscribe() {
326339
capture_thread.join();
327340
}
328341
cap.reset();
329-
frame = cv::Mat();
330-
while (!framesQueue.empty()) {
331-
framesQueue.pop();
332-
}
333342
}
334343

335344
virtual void connectionCallbackImpl() {

0 commit comments

Comments
 (0)