OpenCV => 4.5.1
Trying to open 40 rtsp stream with VideoCapture, each VideoCapture is in one thread, can only opened 20 at the same time, the 20 other failed to get stream.
I tried with FFMPEG backend and Gstreamer (1.18.1).
With FFMPEG: After 20 VideoCapture opened, I got this error :
[rtsp @ 00000250169be640] method SETUP failed: 500 Internal Server Error
With GStreamer:
[ WARN:15] global C:\opencv\4.5.1\opencv\modules\videoio\src\cap_gstreamer.cpp (1825) cv::handleMessage OpenCV | GStream
er warning: Embedded video playback halted; module rtspsrc19 reported: Could not read from resource.
[ WARN:15] global C:\opencv\4.5.1\opencv\modules\videoio\src\cap_gstreamer.cpp (914) cv::GStreamerCapture::open OpenCV |
GStreamer warning: unable to start pipeline
[ WARN:15] global C:\opencv\4.5.1\opencv\modules\videoio\src\cap_gstreamer.cpp (501) cv::GStreamerCapture::isPipelinePla
ying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
Steps to reproduce
#include <QCoreApplication>
#include <QDebug>
#include <QThread>
#include <utility>
#include <QSettings>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <opencv2/opencv.hpp>
#include <QFile>
#include <thread>
class Camera {
public:
Camera() = delete;
Camera(QString url, QString name);
void start() {
auto t = new std::thread(&Camera::run, this);
}
void run();
private:
QString url_;
QString name_;
};
Camera::Camera(QString url, QString name): url_(std::move(url)), name_(std::move(name)) {
}
void Camera::run() {
auto cap = cv::VideoCapture(url_.toStdString(), cv::CAP_FFMPEG);
qDebug() << "Start new camera thread with name " << name_;
cv::Mat frame;
while (true) {
bool ret = cap.read(frame);
if (!ret) {
qDebug() << "Camera with name " << name_ << " FAILED !";
break;
}
}
}
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
// QString urlPattern = "rtspsrc location=<url> ! rtph264depay ! h264parse ! nvh264dec ! appsink";
// QString urlPattern = "rtspsrc location=<url> ! decodebin ! videoconvert ! appsink";
QString urlPattern = "<url>";
QFile file;
file.setFileName("cameras.json");
file.open(QIODevice::ReadOnly);
QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll());
qDebug() << jsonDoc;
std::vector<std::unique_ptr<Camera>> camerasToLaunch;
QList<QVariant> cameraList = jsonDoc.toVariant().toList();
for (const auto& camera: cameraList) {
qDebug() << camera;
qDebug() << "Get url of camera";
QString url = camera.toMap()["url"].toString();
QString name = camera.toMap()["name"].toString();
QString urlFull = urlPattern.replace("<url>", url);
auto pCamera = std::make_unique<Camera>(urlFull, name);
pCamera->start();
camerasToLaunch.push_back(std::move(pCamera));
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
return QCoreApplication::exec();
}
I tried to launch more than 20 process which create a VideoCapture and do “read”, I can notice that. only 20 processes work and other processes have 0% CPU.
I have the same issue on Ubuntu 18.04
If i launched 2 times my program above, the second program can not read any frame :
[ WARN:0] global C:\opencv\4.5.1\opencv\modules\videoio\src\cap_gstreamer.cpp (1825) cv::handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module rtspsrc0 reported: Could not write to resource.
[ WARN:0] global C:\opencv\4.5.1\opencv\modules\videoio\src\cap_gstreamer.cpp (914) cv::GStreamerCapture::open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global C:\opencv\4.5.1\opencv\modules\videoio\src\cap_gstreamer.cpp (501) cv::GStreamerCapture::isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
But I can display the rtsp stream with vlc or gstreamer in command line