From ec2ee92edae42fe66c537c505773c7f64bd52e60 Mon Sep 17 00:00:00 2001 From: Denis Erokhin Date: Fri, 21 Jun 2024 19:54:43 +0300 Subject: [PATCH] QtPlayer input --- example/QtPlayer/mainwindow.cpp | 25 ++++++++++++-- example/QtPlayer/mainwindow.h | 3 +- example/QtPlayer/mainwindow.ui | 14 ++++++++ lib/include/qframgrabber/MetaUtils.hpp | 33 +++++++++++++++++++ lib/include/qframgrabber/QFrameProvider.hpp | 4 +++ lib/include/qframgrabber/Types.hpp | 4 +-- lib/src/FrameGrabberImpl.cpp | 36 ++++++++++++++------- lib/src/FrameGrabberImpl.hpp | 3 ++ lib/src/QFrameProvider.cpp | 19 +++++++++++ 9 files changed, 123 insertions(+), 18 deletions(-) create mode 100644 lib/include/qframgrabber/MetaUtils.hpp diff --git a/example/QtPlayer/mainwindow.cpp b/example/QtPlayer/mainwindow.cpp index f4b3ca0..463e4f9 100644 --- a/example/QtPlayer/mainwindow.cpp +++ b/example/QtPlayer/mainwindow.cpp @@ -15,7 +15,9 @@ MainWindow::MainWindow(QWidget* parent) { ui->setupUi(this); - _frameProvider.Start(); + //_frameProvider.Start(); + + ui->button->setText("Start"); connect(&_frameProvider, &frame_grabber::QFrameProvider::SendFrame, this, &MainWindow::OnRcvFrame); } @@ -28,7 +30,7 @@ MainWindow::~MainWindow() void MainWindow::OnRcvFrame(cv::Mat frame) { - qDebug() << "MainWindow::OnRcvFrame"; + // qDebug() << "MainWindow::OnRcvFrame"; if (frame.size() == cv::Size { 0, 0 }) { @@ -41,3 +43,22 @@ void MainWindow::OnRcvFrame(cv::Mat frame) ui->label->setPixmap( QPixmap::fromImage(frame_grabber::utils::CvMat2QImage(frame)).scaled(ui->label->size(), Qt::KeepAspectRatio)); } + +void MainWindow::on_button_clicked() +{ + qDebug() << "MainWindow::on_button_clicked"; + + auto input = ui->lineEdit->text(); + + QRegExp re("\\d*"); + if (re.exactMatch(input)) + { + bool isInt { false }; + int src = input.toInt(&isInt); + if (isInt) + _frameProvider.Start(src); + return; + } + + _frameProvider.Start(input.toStdString()); +} diff --git a/example/QtPlayer/mainwindow.h b/example/QtPlayer/mainwindow.h index c3ef1ee..cb005bd 100644 --- a/example/QtPlayer/mainwindow.h +++ b/example/QtPlayer/mainwindow.h @@ -3,7 +3,6 @@ #include -#include #include #include @@ -28,6 +27,8 @@ public slots: void OnRcvFrame(cv::Mat); + void on_button_clicked(); + private: Ui::MainWindow* ui; diff --git a/example/QtPlayer/mainwindow.ui b/example/QtPlayer/mainwindow.ui index 2b0c848..5ba6fe4 100644 --- a/example/QtPlayer/mainwindow.ui +++ b/example/QtPlayer/mainwindow.ui @@ -16,6 +16,20 @@ + + + + + + + + PushButton + + + + + + TextLabel diff --git a/lib/include/qframgrabber/MetaUtils.hpp b/lib/include/qframgrabber/MetaUtils.hpp new file mode 100644 index 0000000..cbe0907 --- /dev/null +++ b/lib/include/qframgrabber/MetaUtils.hpp @@ -0,0 +1,33 @@ +#pragma once + +namespace frame_grabber::meta::utils +{ +/// {@ +/// \brief Overload pattern for std::visit +template +struct Overload : Ts... +{ + using Ts::operator()...; +}; + +template +Overload(Ts...) -> Overload; +/// @} + +/*! +\code{.cpp} + +const std::variant v{true}; + +std::visit( + Overload{ + [](int val) { std::cout << val; }, + [](bool val) { std::cout << std::boolalpha << val; }, + }, + v); + +\endcode + + */ + +} // namespace videostreamer::meta::utils diff --git a/lib/include/qframgrabber/QFrameProvider.hpp b/lib/include/qframgrabber/QFrameProvider.hpp index 3fc4894..5307aeb 100644 --- a/lib/include/qframgrabber/QFrameProvider.hpp +++ b/lib/include/qframgrabber/QFrameProvider.hpp @@ -22,6 +22,10 @@ class QFrameProvider final : public QThread bool Start(); void Stop(); + bool IsWorking() const noexcept; + + bool Start(InputInfo&& inputInfo); + signals: void SendFrame(cv::Mat); diff --git a/lib/include/qframgrabber/Types.hpp b/lib/include/qframgrabber/Types.hpp index badb530..8afae42 100644 --- a/lib/include/qframgrabber/Types.hpp +++ b/lib/include/qframgrabber/Types.hpp @@ -4,9 +4,7 @@ #include #include - - namespace frame_grabber { -using InputInfo = std::variant; +using InputInfo = std::variant; } // namespace frame_grabber \ No newline at end of file diff --git a/lib/src/FrameGrabberImpl.cpp b/lib/src/FrameGrabberImpl.cpp index d44ee0f..1977c77 100644 --- a/lib/src/FrameGrabberImpl.cpp +++ b/lib/src/FrameGrabberImpl.cpp @@ -1,5 +1,7 @@ #include "FrameGrabberImpl.hpp" +#include + #include namespace @@ -16,6 +18,15 @@ FrameGrabberImpl::FrameGrabberImpl(InputInfo inputInfo) { } +FrameGrabberImpl::FrameGrabberImpl() {} + +bool FrameGrabberImpl::Start(InputInfo&& inputInfo) +{ + Stop(); + _inputInfo = std::move(inputInfo); + return Start(); +} + FrameGrabberImpl::~FrameGrabberImpl() { Stop(); @@ -76,18 +87,19 @@ void FrameGrabberImpl::Worker() bool FrameGrabberImpl::Reconnect() { - return std::visit( - [this](const auto& input) -> bool - { - _videoCapture.release(); - - if (_videoCapture.open(input)) - return true; - - std::cerr << "FrameGrabberImpl::Reconnect unable to open: " << input << std::endl; - return false; - }, - _inputInfo); + return std::visit(meta::utils::Overload { [this](const auto& input) -> bool + { + _videoCapture.release(); + + if (_videoCapture.open(input)) + return true; + + std::cerr << "FrameGrabberImpl::Reconnect unable to open: " << input + << std::endl; + return false; + }, + [](std::monostate) { return false; } }, + _inputInfo); } } // namespace frame_grabber::impl diff --git a/lib/src/FrameGrabberImpl.hpp b/lib/src/FrameGrabberImpl.hpp index 4e86646..d13e7f4 100644 --- a/lib/src/FrameGrabberImpl.hpp +++ b/lib/src/FrameGrabberImpl.hpp @@ -17,10 +17,13 @@ class FrameGrabberImpl final public: explicit FrameGrabberImpl(InputInfo inputInfo); + explicit FrameGrabberImpl(); ~FrameGrabberImpl(); + void SetBuffer(BufferT buffer); bool Start(); + bool Start(InputInfo&& inputInfo); void Stop(); private: diff --git a/lib/src/QFrameProvider.cpp b/lib/src/QFrameProvider.cpp index c76c94d..8b28d54 100644 --- a/lib/src/QFrameProvider.cpp +++ b/lib/src/QFrameProvider.cpp @@ -49,4 +49,23 @@ void QFrameProvider::Stop() this->wait(); } +bool QFrameProvider::IsWorking() const noexcept +{ + return _isWorking; +} + +bool QFrameProvider::Start(InputInfo&& inputInfo) +{ + if (!_frameGrabber || !_buffer) + return false; + + _frameGrabber->SetBuffer(_buffer); + _isWorking = true; + if (_frameGrabber->Start(std::move(inputInfo))) + { + start(); + } + return false; +} + } // namespace frame_grabber \ No newline at end of file