Skip to content

Commit

Permalink
QtPlayer input
Browse files Browse the repository at this point in the history
  • Loading branch information
D3Nd3R committed Jun 21, 2024
1 parent a9a15fe commit ec2ee92
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 18 deletions.
25 changes: 23 additions & 2 deletions example/QtPlayer/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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 })
{
Expand All @@ -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());
}
3 changes: 2 additions & 1 deletion example/QtPlayer/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <qframgrabber/QFrameProvider.hpp>

#include <QLabel>
#include <QLayout>
#include <QMainWindow>

Expand All @@ -28,6 +27,8 @@ public slots:

void OnRcvFrame(cv::Mat);

void on_button_clicked();

private:
Ui::MainWindow* ui;

Expand Down
14 changes: 14 additions & 0 deletions example/QtPlayer/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QPushButton" name="button">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
Expand Down
33 changes: 33 additions & 0 deletions lib/include/qframgrabber/MetaUtils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

namespace frame_grabber::meta::utils
{
/// {@
/// \brief Overload pattern for std::visit
template<class... Ts>
struct Overload : Ts...
{
using Ts::operator()...;
};

template<class... Ts>
Overload(Ts...) -> Overload<Ts...>;
/// @}

/*!
\code{.cpp}
const std::variant<int, bool> v{true};
std::visit(
Overload{
[](int val) { std::cout << val; },
[](bool val) { std::cout << std::boolalpha << val; },
},
v);
\endcode
*/

} // namespace videostreamer::meta::utils
4 changes: 4 additions & 0 deletions lib/include/qframgrabber/QFrameProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions lib/include/qframgrabber/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
#include <string>
#include <variant>



namespace frame_grabber
{
using InputInfo = std::variant<int32_t, std::string>;
using InputInfo = std::variant<std::monostate, int32_t, std::string>;
} // namespace frame_grabber
36 changes: 24 additions & 12 deletions lib/src/FrameGrabberImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "FrameGrabberImpl.hpp"

#include <qframgrabber/MetaUtils.hpp>

#include <iostream>

namespace
Expand All @@ -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();
Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions lib/src/FrameGrabberImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions lib/src/QFrameProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ec2ee92

Please sign in to comment.