From c40648e27c2c00d621215cb1ac1ed7e460458020 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Thu, 3 Jul 2014 15:13:44 -0400 Subject: [PATCH] Added exception handling in videobuffer --- src/videobuffer.cpp | 78 +++++++++++++++++++++++++++------------------ src/videobuffer.h | 1 + 2 files changed, 48 insertions(+), 31 deletions(-) diff --git a/src/videobuffer.cpp b/src/videobuffer.cpp index 4b75f51..c646ce2 100644 --- a/src/videobuffer.cpp +++ b/src/videobuffer.cpp @@ -95,43 +95,59 @@ void imageCollectionThread(void* arg) VideoDispatcher* dispatcher = (VideoDispatcher*) arg; - cv::VideoCapture cap=cv::VideoCapture(); - cap.open(dispatcher->mjpeg_url); - - cv::Mat frame; - while (dispatcher->active) { - while (dispatcher->active) + try { + cv::VideoCapture cap=cv::VideoCapture(); + cap.open(dispatcher->mjpeg_url); - dispatcher->mMutex.lock(); - bool hasImage = false; - try - { - hasImage = cap.read(frame); - dispatcher->setLatestFrame(&frame); - } - catch (int e) - { - // Error occured while trying to gather image. Retry, don't exit. - std::cerr << "Exception happened " << e << std::endl; - } - // Double check the image to make sure it's valid. - if (frame.cols == 0 || frame.rows == 0) - hasImage = false; - - dispatcher->mMutex.unlock(); - - if (hasImage == false) - break; - + cv::Mat frame; - // Delay 15ms - usleep(15000); + while (dispatcher->active) + { + while (dispatcher->active) + { + + dispatcher->mMutex.lock(); + bool hasImage = false; + try + { + hasImage = cap.read(frame); + dispatcher->setLatestFrame(&frame); + } + catch (int e) + { + // Error occured while trying to gather image. Retry, don't exit. + std::cerr << "Exception happened " << e << std::endl; + } + // Double check the image to make sure it's valid. + if (frame.cols == 0 || frame.rows == 0) + hasImage = false; + + dispatcher->mMutex.unlock(); + + if (hasImage == false) + break; + + + // Delay 15ms + usleep(15000); + } + + // Delay 100ms + usleep(100000); + } } + catch (const std::runtime_error& error) + { + // Error occured while trying to gather image. Retry, don't exit. + std::cerr << "VideoBuffer exception: " << error.what() << std::endl; + } + // Delay 1 second + usleep(1000000); - // Delay 100ms - usleep(100000); } + + } \ No newline at end of file diff --git a/src/videobuffer.h b/src/videobuffer.h index fab6be0..d7e7f57 100644 --- a/src/videobuffer.h +++ b/src/videobuffer.h @@ -2,6 +2,7 @@ #define OPENALPR_VIDEOBUFFER_H #include +#include #include "opencv2/highgui/highgui.hpp"