OpenCV C++ RTSP Stream VideoCapture Error - "Operation now in progress"

Viewed 471


I am attempting to write a short C++ script that can show me an RTSP stream from a camera on my local network. Currently my script is the following:

#include <iostream>

#include <opencv2/opencv.hpp>

int main()
{
    std::string window_name = "RGB Camera";
    setenv("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp", 1);

    const std::string rtsp_url = "rtsp://192.168.2.100:5010/video.sdp";

    cv::VideoCapture cap; // Declare capture stream
    cap.open(rtsp_url, cv::CAP_FFMPEG);

    if (!cap.isOpened()) // Prompt an error message if no video stream is found
    {
        perror("Video Stream Error");
        exit(EXIT_FAILURE);
    }

    cv::namedWindow(window_name, cv::WindowFlags::WINDOW_KEEPRATIO); // Declaring the video to show the video
    cv::Mat frame;                                                   // Declaring a matrix to load the frames

    while (1)
    {
        const bool read_success = cap.read(frame);

        if (!read_success)
        {
            printf("End of stream\n.");
            break;
        }

        cv::imshow(window_name, frame);

        char c = (char)cv::waitKey(25); // 25 milliseconds per frame

        if (c == 27) // If 'Esc' key is pressed, break the loop
        {
            printf("Esc key pressed, terminating loop...\n");
            break;
        }
    }

    cap.release();           // Release memory buffer
    cv::destroyAllWindows(); // Close all windows
    return 0;
}

However when running I am getting the following error message:

Video Stream Error: Operation now in progress

I am aware that my script is failing in the if (!cap.isOpened()) check, however I am able to open this stream in VLC and with ffplay with the rtsp url. I'm not sure why OpenCV is not working.

Also, when I remove cv::CAP_FFMPEG in the cap.open() function I get the error:

Video Stream Error: No such file or directory

Any suggestions would be greatly appreciated.

0 Answers
Related