how to reconnect to rtsp stream with VideoCapture in c++

asked 2020-01-20 01:40:46 -0600

updated 2020-11-06 01:20:19 -0600

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();
}
edit retag flag offensive close merge delete

Comments

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...)

mvuori gravatar imagemvuori ( 2020-01-20 04:17:47 -0600 )edit

@alon_yair. Are you running 24 hrs 7 days? In home or office?

supra56 gravatar imagesupra56 ( 2020-01-20 06:48:54 -0600 )edit

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.

alon_yair gravatar imagealon_yair ( 2020-01-20 07:04:02 -0600 )edit

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.

supra56 gravatar imagesupra56 ( 2020-01-20 09:03:00 -0600 )edit

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

alon_yair gravatar imagealon_yair ( 2020-01-20 09:39:41 -0600 )edit

I used python on raspberry pi. What happened if your router dropped? It will wait for automatically reconnected depending on delay.

supra56 gravatar imagesupra56 ( 2020-01-20 10:00:25 -0600 )edit