-
Notifications
You must be signed in to change notification settings - Fork 2
Qstreamer.cpp
Q-engineering edited this page Mar 9, 2022
·
3 revisions
The main while loop checks that both files (SWAP_IMG_FILE
and SWAP_RDY_FILE
) are present.
If one or both are present, MainEvent.cpp has not yet processed the image.
Until then, Qstreamer captures frames without doing anything with them. This way, latency stays low.
Once both files are deleted by MainEvent, Qstreamer sends the most recent frame by storing it in SWAP_IMG_FILE
.
The empty file SWAP_RDY_FILE
is created after this save completes.
MainEvent waits for SWAP_RDY_FILE
to be present. Now the whole frame is saved and ready to read.
Without this mechanism, MainEvent can easily make mistakes while reading images still written.
#include <opencv2/opencv.hpp>
#include "General.h"
#include "Settings.h"
#include "OkFile.h"
//----------------------------------------------------------------------------------
using namespace cv;
using namespace std;
//---------------------------------------------------------------------------
TSettings Settings(SETTING_FILE); //the settings (they are loaded here for the first time)
//----------------------------------------------------------------------------------
int main()
{
Mat frame;
Mat norm_frame;
ofstream Fdone;
bool FrameError=false;
if(!Settings.Loaded){
OkFileShow();
return -1;
}
VideoCapture cap(VIDEO_FILE, CAP_FFMPEG);
if(!cap.isOpened()){ FrameError=true; goto Ferr; }
if (!cap.read(frame)) { FrameError=true; goto Ferr; }
cout << "Camera start streaming...." << endl;
//reset lock mechanism
unlink(SWAP_IMG_FILE);
unlink(SWAP_RDY_FILE);
while(true)
{
//wait until ready
while(FileExists(SWAP_IMG_FILE) || FileExists(SWAP_RDY_FILE)) cap.read(frame);
//event app is ready to recieve a new frame
cap.read(frame);
cv::resize(frame, norm_frame, cv::Size(320, 240));
imwrite(SWAP_IMG_FILE,norm_frame);
//signal image to file writing is done
Fdone.open(SWAP_RDY_FILE);
Fdone.close(); //write empty file
}
cap.release();
Ferr:
if(FrameError) cout << "Failed to open camera. Is your ffmpeg pipeline active?" << endl;
return 0;
}
//----------------------------------------------------------------------------------