how to reconnect to rtsp stream with VideoCapture in c++
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();
}
I don't know... Just a thought: sometimes thing take time and need delays. The old stream will be closed in its destructor just before the new one is created. I'd delete the old one, wait a second and then try opening the new stream.
(Other than that, I'm sure you have tested that all relevant code lines really get called...)
@alon_yair. Are you running 24 hrs 7 days? In home or office?
yes running 24 hrs 7 days, the system can stop to change the stream URL or add another stream, I see when I adding a new stream and resume the run the stream is not working.
I'm running too.The problem is not rtsp. It is web browser. You have to refresh it. Even if you're building an app.When rtsp stopped, you will see on browser error refuse connection.
there is no app or web browser involved in mt code, this process that does all the work, and I see from my error log that there is an issue, if I kill the process and restart it there is no problem with the stream
I used python on raspberry pi. What happened if your router dropped? It will wait for automatically reconnected depending on delay.