Merge pull request #20541 from iyadahmed:video_capture_timeout_prop

* VideoCapture timeout set/get

* Common formatting for enum values

* Fix enum values wrongly in videoio.hpp

* Define timeout enum values in public api and align with master
This commit is contained in:
Iyad Ahmed 2021-08-12 16:51:02 +00:00 committed by GitHub
parent 6edc438789
commit 4300bb2e1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 7 deletions

View File

@ -179,6 +179,8 @@ enum VideoCaptureProperties {
CAP_PROP_BITRATE =47, //!< (read-only) Video bitrate in kbits/s CAP_PROP_BITRATE =47, //!< (read-only) Video bitrate in kbits/s
CAP_PROP_ORIENTATION_META=48, //!< (read-only) Frame rotation defined by stream meta (applicable for FFmpeg back-end only) CAP_PROP_ORIENTATION_META=48, //!< (read-only) Frame rotation defined by stream meta (applicable for FFmpeg back-end only)
CAP_PROP_ORIENTATION_AUTO=49, //!< if true - rotates output frames of CvCapture considering video file's metadata (applicable for FFmpeg back-end only) (https://github.com/opencv/opencv/issues/15499) CAP_PROP_ORIENTATION_AUTO=49, //!< if true - rotates output frames of CvCapture considering video file's metadata (applicable for FFmpeg back-end only) (https://github.com/opencv/opencv/issues/15499)
CAP_PROP_OPEN_TIMEOUT_MSEC=53,
CAP_PROP_READ_TIMEOUT_MSEC=54,
#ifndef CV_DOXYGEN #ifndef CV_DOXYGEN
CV__CAP_PROP_LATEST CV__CAP_PROP_LATEST
#endif #endif

View File

@ -30,7 +30,9 @@ enum
CV_FFMPEG_CAP_PROP_CODEC_PIXEL_FORMAT=46, CV_FFMPEG_CAP_PROP_CODEC_PIXEL_FORMAT=46,
CV_FFMPEG_CAP_PROP_BITRATE=47, CV_FFMPEG_CAP_PROP_BITRATE=47,
CV_FFMPEG_CAP_PROP_ORIENTATION_META=48, CV_FFMPEG_CAP_PROP_ORIENTATION_META=48,
CV_FFMPEG_CAP_PROP_ORIENTATION_AUTO=49 CV_FFMPEG_CAP_PROP_ORIENTATION_AUTO=49,
CV_FFMPEG_CAP_PROP_OPEN_TIMEOUT_MSEC=53,
CV_FFMPEG_CAP_PROP_READ_TIMEOUT_MSEC=54
}; };
typedef struct CvCapture_FFMPEG CvCapture_FFMPEG; typedef struct CvCapture_FFMPEG CvCapture_FFMPEG;

View File

@ -186,8 +186,8 @@ extern "C" {
#endif #endif
#if USE_AV_INTERRUPT_CALLBACK #if USE_AV_INTERRUPT_CALLBACK
#define LIBAVFORMAT_INTERRUPT_OPEN_TIMEOUT_MS 30000 #define LIBAVFORMAT_INTERRUPT_OPEN_DEFAULT_TIMEOUT_MS 30000
#define LIBAVFORMAT_INTERRUPT_READ_TIMEOUT_MS 30000 #define LIBAVFORMAT_INTERRUPT_READ_DEFAULT_TIMEOUT_MS 30000
#ifdef _WIN32 #ifdef _WIN32
// http://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows // http://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows
@ -534,6 +534,8 @@ struct CvCapture_FFMPEG
AVDictionary *dict; AVDictionary *dict;
#endif #endif
#if USE_AV_INTERRUPT_CALLBACK #if USE_AV_INTERRUPT_CALLBACK
int open_timeout_ms;
int read_timeout_ms;
AVInterruptCallbackMetadata interrupt_metadata; AVInterruptCallbackMetadata interrupt_metadata;
#endif #endif
@ -568,6 +570,11 @@ void CvCapture_FFMPEG::init()
frame_number = 0; frame_number = 0;
eps_zero = 0.000025; eps_zero = 0.000025;
#if USE_AV_INTERRUPT_CALLBACK
open_timeout_ms = LIBAVFORMAT_INTERRUPT_OPEN_DEFAULT_TIMEOUT_MS;
read_timeout_ms = LIBAVFORMAT_INTERRUPT_READ_DEFAULT_TIMEOUT_MS;
#endif
rotation_angle = 0; rotation_angle = 0;
#if (LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)) #if (LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0))
@ -923,7 +930,7 @@ bool CvCapture_FFMPEG::open( const char* _filename )
#if USE_AV_INTERRUPT_CALLBACK #if USE_AV_INTERRUPT_CALLBACK
/* interrupt callback */ /* interrupt callback */
interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_OPEN_TIMEOUT_MS; interrupt_metadata.timeout_after_ms = open_timeout_ms;
get_monotonic_time(&interrupt_metadata.value); get_monotonic_time(&interrupt_metadata.value);
ic = avformat_alloc_context(); ic = avformat_alloc_context();
@ -1227,7 +1234,7 @@ bool CvCapture_FFMPEG::grabFrame()
#if USE_AV_INTERRUPT_CALLBACK #if USE_AV_INTERRUPT_CALLBACK
// activate interrupt callback // activate interrupt callback
get_monotonic_time(&interrupt_metadata.value); get_monotonic_time(&interrupt_metadata.value);
interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_READ_TIMEOUT_MS; interrupt_metadata.timeout_after_ms = read_timeout_ms;
#endif #endif
// get the next frame // get the next frame
@ -1483,6 +1490,12 @@ double CvCapture_FFMPEG::getProperty( int property_id ) const
#else #else
return 0; return 0;
#endif #endif
#if USE_AV_INTERRUPT_CALLBACK
case CV_FFMPEG_CAP_PROP_OPEN_TIMEOUT_MSEC:
return static_cast<double>(open_timeout_ms);
case CV_FFMPEG_CAP_PROP_READ_TIMEOUT_MSEC:
return static_cast<double>(read_timeout_ms);
#endif // USE_AV_INTERRUPT_CALLBACK
default: default:
break; break;
} }
@ -1677,6 +1690,14 @@ bool CvCapture_FFMPEG::setProperty( int property_id, double value )
return false; return false;
#endif #endif
break; break;
#if USE_AV_INTERRUPT_CALLBACK
case CV_FFMPEG_CAP_PROP_OPEN_TIMEOUT_MSEC:
open_timeout_ms = (int)value;
break;
case CV_FFMPEG_CAP_PROP_READ_TIMEOUT_MSEC:
read_timeout_ms = (int)value;
break;
#endif // USE_AV_INTERRUPT_CALLBACK
default: default:
return false; return false;
} }
@ -3114,7 +3135,7 @@ bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma
#if USE_AV_INTERRUPT_CALLBACK #if USE_AV_INTERRUPT_CALLBACK
/* interrupt callback */ /* interrupt callback */
interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_OPEN_TIMEOUT_MS; interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_OPEN_DEFAULT_TIMEOUT_MS;
get_monotonic_time(&interrupt_metadata.value); get_monotonic_time(&interrupt_metadata.value);
ctx_ = avformat_alloc_context(); ctx_ = avformat_alloc_context();
@ -3241,7 +3262,7 @@ bool InputMediaStream_FFMPEG::read(unsigned char** data, int* size, int* endOfFi
#if USE_AV_INTERRUPT_CALLBACK #if USE_AV_INTERRUPT_CALLBACK
// activate interrupt callback // activate interrupt callback
get_monotonic_time(&interrupt_metadata.value); get_monotonic_time(&interrupt_metadata.value);
interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_READ_TIMEOUT_MS; interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_READ_DEFAULT_TIMEOUT_MS;
#endif #endif
// free last packet if exist // free last packet if exist