From 2f390da4e0c47247a9080df7f3761b2e7ad011c5 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 6 Jun 2018 00:01:50 +0200 Subject: [PATCH] Actually start showing JPEGs on the screen. --- Makefile | 2 +- jpeg_frame_view.cpp | 27 +++++++++++++++++++++++++++ jpeg_frame_view.h | 36 ++++++++++++++++++++++++++++++++++++ main.cpp | 10 ++++++++++ mainwindow.h | 2 +- post_to_main_thread.h | 16 ++++++++++++++++ ui_mainwindow.ui | 33 ++++++++++++++++++++------------- 7 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 jpeg_frame_view.cpp create mode 100644 jpeg_frame_view.h create mode 100644 post_to_main_thread.h diff --git a/Makefile b/Makefile index 379a0a6..2608903 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ CXXFLAGS += -std=gnu++11 -fPIC $(shell pkg-config --cflags $(PKG_MODULES)) -pthr LDLIBS=$(shell pkg-config --libs $(PKG_MODULES)) -pthread -lavformat -lavcodec -lavutil -lswscale # Qt objects -OBJS_WITH_MOC = mainwindow.o +OBJS_WITH_MOC = mainwindow.o jpeg_frame_view.o OBJS += $(OBJS_WITH_MOC) OBJS += $(OBJS_WITH_MOC:.o=.moc.o) diff --git a/jpeg_frame_view.cpp b/jpeg_frame_view.cpp new file mode 100644 index 0000000..55a4857 --- /dev/null +++ b/jpeg_frame_view.cpp @@ -0,0 +1,27 @@ +#include "jpeg_frame_view.h" + +#include +#include + +using namespace std; + +string filename_for_frame(unsigned stream_idx, int64_t pts); + +JPEGFrameView::JPEGFrameView(QWidget *parent) + : QGraphicsView(parent) { + scene.addItem(&item); + setScene(&scene); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); +} + +void JPEGFrameView::update() +{ + item.setPixmap(QPixmap(QString::fromStdString(filename_for_frame(stream_idx, pts)))); + fitInView(&item, Qt::KeepAspectRatio); +} + +void JPEGFrameView::resizeEvent(QResizeEvent *event) +{ + fitInView(&item, Qt::KeepAspectRatio); +} diff --git a/jpeg_frame_view.h b/jpeg_frame_view.h new file mode 100644 index 0000000..219ee58 --- /dev/null +++ b/jpeg_frame_view.h @@ -0,0 +1,36 @@ +#ifndef _JPEG_FRAME_VIEW_H +#define _JPEG_FRAME_VIEW_H 1 + +#include +#include +#include + +#include + +class JPEGFrameView : public QGraphicsView { + Q_OBJECT + +public: + JPEGFrameView(QWidget *parent); + + void setFrame(unsigned stream_idx, int64_t pts) + { + this->stream_idx = stream_idx; + this->pts = pts; + update(); + } + +protected: + void resizeEvent(QResizeEvent *event) override; + +private: + void update(); + + QGraphicsPixmapItem item; + QGraphicsScene scene; + + unsigned stream_idx; + int64_t pts; +}; + +#endif // !defined(_JPEG_FRAME_VIEW_H) diff --git a/main.cpp b/main.cpp index 17431aa..0a976c3 100644 --- a/main.cpp +++ b/main.cpp @@ -17,6 +17,8 @@ extern "C" { #include "mainwindow.h" #include "ffmpeg_raii.h" +#include "post_to_main_thread.h" +#include "ui_mainwindow.h" #define MAX_STREAMS 16 @@ -78,6 +80,14 @@ int thread_func() fwrite(pkt.data, pkt.size, 1, fp); fclose(fp); + post_to_main_thread([pkt] { + if (pkt.stream_index == 0) { + global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pkt.pts); + } else if (pkt.stream_index == 1) { + global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pkt.pts); + } + }); + assert(pkt.stream_index < MAX_STREAMS); frames[pkt.stream_index].push_back(pkt.pts); diff --git a/mainwindow.h b/mainwindow.h index 4ee3bd9..f17757f 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -16,7 +16,7 @@ class MainWindow : public QMainWindow public: MainWindow(); -private: +//private: Ui::MainWindow *ui; }; diff --git a/post_to_main_thread.h b/post_to_main_thread.h new file mode 100644 index 0000000..0462c7b --- /dev/null +++ b/post_to_main_thread.h @@ -0,0 +1,16 @@ +#ifndef _POST_TO_MAIN_THREAD_H +#define _POST_TO_MAIN_THREAD_H 1 + +#include +#include +#include + +// http://stackoverflow.com/questions/21646467/how-to-execute-a-functor-in-a-given-thread-in-qt-gcd-style +template +static inline void post_to_main_thread(F &&fun) +{ + QObject signalSource; + QObject::connect(&signalSource, &QObject::destroyed, qApp, std::move(fun)); +} + +#endif // !defined(_POST_TO_MAIN_THREAD_H) diff --git a/ui_mainwindow.ui b/ui_mainwindow.ui index aeff6cb..17ff670 100644 --- a/ui_mainwindow.ui +++ b/ui_mainwindow.ui @@ -17,32 +17,32 @@ - 20 + 10 20 - 381 - 211 + 391 + 291 - + - + - + - + - 90 - 240 + 70 + 320 261 17 @@ -57,7 +57,7 @@ - 410 + 430 20 281 192 @@ -67,7 +67,7 @@ - 705 + 730 20 291 192 @@ -78,7 +78,7 @@ 470 - 240 + 230 191 17 @@ -93,7 +93,7 @@ - 760 + 770 230 191 17 @@ -108,6 +108,13 @@ + + + JPEGFrameView + QGraphicsView +
jpeg_frame_view.h
+
+
-- 2.39.2