I'm building a simple application in c++ (32-bits) that uses opencv to grab frames from a rtsp camera.
The function that grabs the camera frames run in a separated thread from the main program.
I've testes this application with a mp4 video, and it works fine. I was able to grab frames and process them. However when I use the rtsp link, although I was able to open a connection with the camera, whenever I tried to read both grab() and read() functions returns False.
First, i thought it was an issue with the rtsp link, but I made a simple Python application to test it, and it worked as well. So it was not the link.
This is the code that I'm using to grab the frames:
#ifndef _IMAGE_BUFFER_H_
#define _IMAGE_BUFFER_H_
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
.
.
.
VideoCapture capture_;
string address_;
atomic<bool> keep_alive_;
thread thread_;
int fps_;
mutex mutex_;
list<FramePair> frames_;
int Run()
{
capture_.open(address_, cv::CAP_ANY);
if (!capture_.isOpened()) {
printf("Could not open Camera feed! \n");
return -1;
}
uint64_t period = uint64_t((float(1) / float(fps_)) * float(1000));
period = period - (period / 20);
uint64_t t0 = getCurrentTimeInMilli();
while (keep_alive_) {
uint64_t difftime = getCurrentTimeInMilli() - t0;
if (difftime < period) {
uint64_t sleep_time = period - difftime;
if (sleep_time < period) {
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time));
}
}
t0 = getCurrentTimeInMilli();
CaptureFrame();
}
return 0;
}
void CaptureFrame()
{
Mat frame;
bool ret = capture_.read(frame);
if (!ret) {
printf("Error in frame reading! \n");
}
vector<uint8_t> jpeg;
cv::imencode(".jpg", frame, jpeg, vector<int>());
mutex_.lock();
frames_.push_back(FramePair(getCurrentTimeInMilli(), jpeg));
if (frames_.size() > FRAME_QUEUE_SIZE)
frames_.pop_front();
mutex_.unlock();
}
The OpenCv version that I'm using is 3.4.5.
The link : rtsp://<user>:<pass>@<ip>:<port>/media
I appreciate any help on this matter.
Edit1:
What I have tried:
- I`ve tried this this, but it didn't work
- Also Tried with a pre-built opencv 3.4.0 version for 64 bits and still the same