Merge pull request #6359 from alalek:videocapture_exceptions

This commit is contained in:
Alexander Alekhin 2016-04-17 09:30:42 +00:00
commit 169b5e9fde

View File

@ -117,6 +117,36 @@ CV_IMPL int cvGetCaptureDomain( CvCapture* capture)
return capture ? capture->getCaptureDomain() : 0;
}
static bool get_capture_debug_flag()
{
static bool initialized = false;
static bool flag = false;
if (!initialized)
{
#ifndef NO_GETENV
flag = getenv("OPENCV_VIDEOCAPTURE_DEBUG") ? true : false; // TODO Use getBoolParameter
#endif
initialized = true;
}
return flag;
}
#define TRY_OPEN(capture, backend_func) \
{ \
if (!capture) \
try { \
if (get_capture_debug_flag()) fprintf(stderr, "VIDEOIO(%s): trying ...\n", #backend_func); \
capture = backend_func; \
if (get_capture_debug_flag()) fprintf(stderr, "VIDEOIO(%s): result=%p ...\n", #backend_func, capture); \
} catch (const cv::Exception& e) { \
fprintf(stderr, "VIDEOIO(%s): raised OpenCV exception:\n\n%s\n", #backend_func, e.what()); \
} catch (const std::exception& e) { \
fprintf(stderr, "VIDEOIO(%s): raised C++ exception:\n\n%s\n", #backend_func, e.what()); \
} catch (...) { \
fprintf(stderr, "VIDEOIO(%s): raised unknown C++ exception!\n\n", #backend_func); \
} \
}
/**
* Camera dispatching method: index is the camera number.
@ -144,120 +174,101 @@ CV_IMPL CvCapture * cvCreateCameraCapture (int index)
#ifdef HAVE_MSMF
case CV_CAP_MSMF:
if (!capture)
capture = cvCreateCameraCapture_MSMF(index);
TRY_OPEN(capture, cvCreateCameraCapture_MSMF(index))
if (pref) break;
#endif
#ifdef HAVE_TYZX
case CV_CAP_STEREO:
if (!capture)
capture = cvCreateCameraCapture_TYZX(index);
TRY_OPEN(capture, cvCreateCameraCapture_TYZX(index))
if (pref) break;
#endif
case CV_CAP_VFW:
#ifdef HAVE_VFW
if (!capture)
capture = cvCreateCameraCapture_VFW(index);
#endif
#if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
if (!capture)
capture = cvCreateCameraCapture_V4L(index);
#endif
#ifdef HAVE_GSTREAMER
if (!capture)
capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2,
reinterpret_cast<char *>(index));
if (!capture)
capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L,
reinterpret_cast<char *>(index));
TRY_OPEN(capture, cvCreateCameraCapture_VFW(index))
#endif
if (pref) break; // CV_CAP_VFW
#if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
TRY_OPEN(capture, cvCreateCameraCapture_V4L(index))
#endif
#ifdef HAVE_GSTREAMER
TRY_OPEN(capture, cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, reinterpret_cast<char *>(index)))
TRY_OPEN(capture, cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L, reinterpret_cast<char *>(index)))
#endif
case CV_CAP_FIREWIRE:
#ifdef HAVE_DC1394_2
if (!capture)
capture = cvCreateCameraCapture_DC1394_2(index);
TRY_OPEN(capture, cvCreateCameraCapture_DC1394_2(index))
#endif
#ifdef HAVE_DC1394
if (!capture)
capture = cvCreateCameraCapture_DC1394(index);
TRY_OPEN(capture, cvCreateCameraCapture_DC1394(index))
#endif
#ifdef HAVE_CMU1394
if (!capture)
capture = cvCreateCameraCapture_CMU(index);
TRY_OPEN(capture, cvCreateCameraCapture_CMU(index))
#endif
#if defined(HAVE_GSTREAMER) && 0
// Re-enable again when gstreamer 1394 support will land in the backend code
if (!capture)
capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_1394, 0);
TRY_OPEN(capture, cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_1394, 0))
#endif
if (pref) break; // CV_CAP_FIREWIRE
#ifdef HAVE_MIL
case CV_CAP_MIL:
if (!capture)
capture = cvCreateCameraCapture_MIL(index);
TRY_OPEN(capture, cvCreateCameraCapture_MIL(index))
if (pref) break;
#endif
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
case CV_CAP_QT:
if (!capture)
capture = cvCreateCameraCapture_QT(index);
TRY_OPEN(capture, cvCreateCameraCapture_QT(index))
if (pref) break;
#endif
#ifdef HAVE_UNICAP
case CV_CAP_UNICAP:
if (!capture)
capture = cvCreateCameraCapture_Unicap(index);
TRY_OPEN(capture, cvCreateCameraCapture_Unicap(index))
if (pref) break;
#endif
#ifdef HAVE_PVAPI
case CV_CAP_PVAPI:
if (!capture)
capture = cvCreateCameraCapture_PvAPI(index);
TRY_OPEN(capture, cvCreateCameraCapture_PvAPI(index))
if (pref) break;
#endif
#ifdef HAVE_OPENNI
case CV_CAP_OPENNI:
if (!capture)
capture = cvCreateCameraCapture_OpenNI(index);
TRY_OPEN(capture, cvCreateCameraCapture_OpenNI(index))
if (pref) break;
#endif
#ifdef HAVE_OPENNI2
case CV_CAP_OPENNI2:
if (!capture)
capture = cvCreateCameraCapture_OpenNI2(index);
TRY_OPEN(capture, cvCreateCameraCapture_OpenNI2(index))
if (pref) break;
#endif
#ifdef HAVE_XIMEA
case CV_CAP_XIAPI:
if (!capture)
capture = cvCreateCameraCapture_XIMEA(index);
TRY_OPEN(capture, cvCreateCameraCapture_XIMEA(index))
if (pref) break;
#endif
#ifdef HAVE_AVFOUNDATION
case CV_CAP_AVFOUNDATION:
if (!capture)
capture = cvCreateCameraCapture_AVFoundation(index);
TRY_OPEN(capture, cvCreateCameraCapture_AVFoundation(index))
if (pref) break;
#endif
#ifdef HAVE_GIGE_API
case CV_CAP_GIGANETIX:
if (!capture)
capture = cvCreateCameraCapture_Giganetix(index);
TRY_OPEN(capture, cvCreateCameraCapture_Giganetix(index))
if (pref) break; // CV_CAP_GIGANETIX
#endif
}
@ -281,66 +292,56 @@ CV_IMPL CvCapture * cvCreateFileCaptureWithPreference (const char * filename, in
#ifdef HAVE_FFMPEG
case CV_CAP_FFMPEG:
if (! result)
result = cvCreateFileCapture_FFMPEG_proxy (filename);
TRY_OPEN(result, cvCreateFileCapture_FFMPEG_proxy (filename))
if (apiPreference) break;
#endif
#ifdef HAVE_VFW
case CV_CAP_VFW:
if (! result)
result = cvCreateFileCapture_VFW (filename);
TRY_OPEN(result, cvCreateFileCapture_VFW (filename))
if (apiPreference) break;
#endif
#if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
if (!result)
result = cvCreateCameraCapture_V4L(filename);
TRY_OPEN(result, cvCreateCameraCapture_V4L(filename))
if (apiPreference) break;
#endif
case CV_CAP_MSMF:
#ifdef HAVE_MSMF
if (! result)
result = cvCreateFileCapture_MSMF (filename);
TRY_OPEN(result, cvCreateFileCapture_MSMF (filename))
#endif
#ifdef HAVE_XINE
if (! result)
result = cvCreateFileCapture_XINE (filename);
TRY_OPEN(result, cvCreateFileCapture_XINE (filename))
#endif
if (apiPreference) break;
#ifdef HAVE_GSTREAMER
case CV_CAP_GSTREAMER:
if (! result)
result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
TRY_OPEN(result, cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename))
if (apiPreference) break;
#endif
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
case CV_CAP_QT:
if (! result)
result = cvCreateFileCapture_QT (filename);
TRY_OPEN(result, cvCreateFileCapture_QT (filename))
if (apiPreference) break;
#endif
#ifdef HAVE_AVFOUNDATION
case CV_CAP_AVFOUNDATION:
if (! result)
result = cvCreateFileCapture_AVFoundation (filename);
TRY_OPEN(result, cvCreateFileCapture_AVFoundation (filename))
if (apiPreference) break;
#endif
#ifdef HAVE_OPENNI
case CV_CAP_OPENNI:
if (! result)
result = cvCreateFileCapture_OpenNI (filename);
TRY_OPEN(result, cvCreateFileCapture_OpenNI (filename))
if (apiPreference) break;
#endif
case CV_CAP_IMAGES:
if (! result)
result = cvCreateFileCapture_Images (filename);
TRY_OPEN(result, cvCreateFileCapture_Images (filename))
}
return result;
@ -358,63 +359,47 @@ CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
double fps, CvSize frameSize, int is_color )
{
// If none of the writers is used
// these statements suppress 'unused parameter' warnings.
CV_UNUSED(frameSize);
CV_UNUSED(is_color);
//CV_FUNCNAME( "cvCreateVideoWriter" );
CvVideoWriter *result = 0;
if(!fourcc || !fps)
result = cvCreateVideoWriter_Images(filename);
TRY_OPEN(result, cvCreateVideoWriter_Images(filename))
#ifdef HAVE_FFMPEG
if(!result)
result = cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color);
TRY_OPEN(result, cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color))
#endif
#ifdef HAVE_VFW
if(!result)
result = cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color);
TRY_OPEN(result, cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color))
#endif
#ifdef HAVE_MSMF
if (!result)
result = cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color);
TRY_OPEN(result, cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color))
#endif
/* #ifdef HAVE_XINE
if(!result)
result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
TRY_OPEN(result, cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color))
#endif
*/
#ifdef HAVE_AVFOUNDATION
if (! result)
result = cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color);
TRY_OPEN(result, cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color))
#endif
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
if(!result)
result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
TRY_OPEN(result, cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color))
#endif
#ifdef HAVE_GSTREAMER
if (! result)
result = cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color);
TRY_OPEN(result, cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color))
#endif
#if !defined(HAVE_FFMPEG) && \
!defined(HAVE_VFW) && \
!defined(HAVE_MSMF) && \
!defined(HAVE_AVFOUNDATION) && \
!defined(HAVE_QUICKTIME) && \
!defined(HAVE_QTKIT) && \
!defined(HAVE_GSTREAMER)
// If none of the writers is used
// these statements suppress 'unused parameter' warnings.
(void)frameSize;
(void)is_color;
#endif
if(!result)
result = cvCreateVideoWriter_Images(filename);
TRY_OPEN(result, cvCreateVideoWriter_Images(filename))
return result;
}