I', trying to write a code that does reconnection to stream when there is no frame or some error has occurred when reading a frame. or when I stop to read frames and then resume to read frames. when there is "long" latency between creating the VideoCapture object and reding the frames, I can't reconnect to read frames from the stream. if there is an issue with reading a frame after 30 continuous tries I try to reconnect. I don't know what I'm doing wrong and why the reconnecting doesn't work. code below
void StreamReader::ReadFrames()
{
int error_count = 0;
while (m_run)
{
cv::Mat frame;
if(m_stream.read(frame) == false || !frame.data || frame.channels() != 3 ) // Check for invalid input
{
LogError << "StreamReader::ReadFrames Could not open or read frame" ;
if(++error_count > 30)
{
//need to reconnect
PublishEngineStatus::Notify(EngineStatus::ENGINE_TRY_RECONNECT_TO_CAMERA, m_id);
return;
}
LogError << "StreamReader::ReadFrames Could not read frame, err count is: " << error_count;
continue;
}
error_count = 0;
LogDebug << "StreamReader::Run push frame";
m_frame.push(frame);
}
}
void StreamReader::ReconnectToStream()
{
//m_stream.release();
if(m_type == StreamType::URL_STREAM)
{
m_stream = VideoCapture(m_url);
if(m_stream.isOpened() == true)
{
PublishEngineStatus::Notify(EngineStatus::ENGINE_CONNECT_TO_CAMERA, m_id);
}
return;
}
if(m_type == StreamType::DESKTOP_CAM_STREAM)
{
m_stream = VideoCapture(m_cam_index);
if(m_stream.isOpened() == true)
{
PublishEngineStatus::Notify(EngineStatus::ENGINE_CONNECT_TO_CAMERA, m_id);
}
}
}
void StreamReader::Run()
{
m_run = true;
while (m_run)
{
//check if video device has been initialised
if ( (m_stream.isOpened() == false) && (m_type != StreamType::VIDEO_FILE_STREAM))
{
LogFatal << "StreamReader::Run camera stream is not open try to reconnect stream: " << m_url;
ReconnectToStream();
continue;
}
if ( (m_stream.isOpened() == false) && (m_type == StreamType::VIDEO_FILE_STREAM))
{
LogFatal << "StreamReader::Run VIDEO_FILE_STREAM is not open or ended stop reading files" << m_url;
return;
}
ReadFrames();
}
m_stream.release();
}