Before we start
- If you do not familiar with QThread, the document of Qt5 show us how to use QThread properly already, please check it out by google(keyword is "QThread doc", or you could open the page by this link), and read this post, it may save you a lot of troubles.
- If you do not familiar with thread, I suggest you read the book c++ concurrency in action, chapter 1~4 and basic atomic knowledge from this site should be more than enough for most of the tasks.
Why do we need asynchronous video capture
- Performance, VideoCapture could be slow when capturing the frame from rtsp protocol, especially when the frame size is big, the ideal solution is capture the frame and process the frame in different thread.
- Do not freeze the ui. cv::waitKey is a blocking operation, it is a bad idea to use it directly in gui programming.
opencv4(using 4.3.0 in this tutorial)
Qt5(using 5.13.2 in this tutorial)
If you do not want to register a new account in order to download Qt5(they did a great job to piss off open source communities), you can try this link.
Define interfaces for workerIn order to make us easier to switch the frame capture in the future, I create a worker_base class for this purpose.
Implement frame capture by opencv
Compare with another libraries like gstreamer, ffmpeg, libvlc, Qt etc. cv::VideoCapture has the simplest api to capture the frame, although c++ api of this class do not work on android yet(you have to use jni, this make porting the app to android become more troubles).
Please read the comments carefully, they may help you avoid subtle bugs.