Skip to content

Commit afcbb15

Browse files
committed
Process events while waiting for camera thread to spin up. Cleanup.
1 parent 9849a42 commit afcbb15

File tree

7 files changed

+52
-23
lines changed

7 files changed

+52
-23
lines changed

Common/Qt/SpinWaitWithEvents.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* SpinWaitWithEvents
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_SpinWaitWithEvents_H
8+
#define PokemonAutomation_SpinWaitWithEvents_H
9+
10+
#include <atomic>
11+
#include <QApplication>
12+
#include "Common/Cpp/Concurrency/SpinPause.h"
13+
14+
//#include <iostream>
15+
//using std::cout;
16+
//using std::endl;
17+
18+
namespace PokemonAutomation{
19+
20+
21+
class SpinWaitWithEvents{
22+
public:
23+
SpinWaitWithEvents()
24+
: m_finished(false)
25+
{}
26+
void process_events_while_waiting() const{
27+
if (m_finished.load(std::memory_order_acquire)){
28+
return;
29+
}
30+
do{
31+
QApplication::processEvents();
32+
pause();
33+
}while (!m_finished.load(std::memory_order_acquire));
34+
}
35+
void signal(){
36+
m_finished.store(true, std::memory_order_release);
37+
}
38+
39+
private:
40+
std::atomic<bool> m_finished;
41+
};
42+
43+
44+
45+
}
46+
#endif

SerialPrograms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ file(GLOB MAIN_SOURCES
272272
../Common/Qt/Options/TimeExpressionWidget.h
273273
../Common/Qt/Redispatch.cpp
274274
../Common/Qt/Redispatch.h
275+
../Common/Qt/SpinWaitWithEvents.h
275276
../Common/Qt/StringToolsQt.cpp
276277
../Common/Qt/StringToolsQt.h
277278
../Common/Qt/TimeQt.cpp

SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.5.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,8 @@ CameraVideoSource::CameraVideoSource(
148148
m_logger.log("Resolution: " + m_resolution.to_string());
149149

150150
m_camera.reset(new QCameraThread(m_logger, *device, *format));
151-
// m_camera.reset(new QCamera(*device));
152-
// m_camera->setCameraFormat(*format);
153151

154152
m_capture_session.reset(new QMediaCaptureSession());
155-
// m_capture_session->setCamera(m_camera.get());
156153
m_capture_session->setCamera(&m_camera->camera());
157154

158155
#if 0
@@ -188,8 +185,7 @@ void CameraVideoSource::set_video_output(QGraphicsVideoItem& item){
188185
return;
189186
}
190187
report_source_frame(std::make_shared<VideoFrame>(now, frame));
191-
}//,
192-
// Qt::DirectConnection
188+
}
193189
);
194190
}
195191

SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.5.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ class CameraVideoSource : public QObject, public VideoSource{
127127
Logger& m_logger;
128128
Resolution m_resolution;
129129

130-
// std::unique_ptr<QCamera> m_camera;
131130
std::unique_ptr<QCameraThread> m_camera;
132131
std::unique_ptr<QVideoSink> m_video_sink;
133132
std::unique_ptr<QMediaCaptureSession> m_capture_session;

SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,8 @@ CameraVideoSource::CameraVideoSource(
140140
m_logger.log("Resolution: " + m_resolution.to_string());
141141

142142
m_camera.reset(new QCameraThread(m_logger, *device, *format));
143-
// m_camera.reset(new QCamera(*device));
144-
// m_camera->setCameraFormat(*format);
145143
m_video_sink.reset(new QVideoSink());
146144
m_capture.reset(new QMediaCaptureSession());
147-
// m_capture->setCamera(m_camera.get());
148145
m_capture->setCamera(&m_camera->camera());
149146
m_capture->setVideoSink(m_video_sink.get());
150147

@@ -167,8 +164,6 @@ CameraVideoSource::CameraVideoSource(
167164
report_source_frame(std::make_shared<VideoFrame>(now, frame));
168165
}
169166
);
170-
171-
// m_camera->start();
172167
}
173168

174169

SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ class CameraVideoSource : public QObject, public VideoSource{
8686

8787
std::mutex m_snapshot_lock;
8888

89-
// std::unique_ptr<QCamera> m_camera;
9089
std::unique_ptr<QCameraThread> m_camera;
9190
std::unique_ptr<QVideoSink> m_video_sink;
9291
std::unique_ptr<QMediaCaptureSession> m_capture;

SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/QCameraThread.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <QThread>
1111
#include <QCamera>
1212
#include "Common/Cpp/AbstractLogger.h"
13+
#include "Common/Qt/SpinWaitWithEvents.h"
1314

1415
//#include <iostream>
1516
//using std::cout;
@@ -19,8 +20,6 @@ namespace PokemonAutomation{
1920

2021

2122
class QCameraThread : public QThread{
22-
Q_OBJECT
23-
2423
public:
2524
QCameraThread(
2625
Logger& logger,
@@ -34,8 +33,7 @@ class QCameraThread : public QThread{
3433
{
3534
start();
3635

37-
std::unique_lock<std::mutex> lg(m_lock);
38-
m_cv.wait(lg, [this]{ return m_camera != nullptr; });
36+
m_spin_waiter.process_events_while_waiting();
3937
}
4038
~QCameraThread(){
4139
quit();
@@ -62,10 +60,7 @@ class QCameraThread : public QThread{
6260

6361
camera.start();
6462

65-
{
66-
std::lock_guard<std::mutex> lg(m_lock);
67-
}
68-
m_cv.notify_all();
63+
m_spin_waiter.signal();
6964

7065
// cout << "start" << endl;
7166
exec();
@@ -80,9 +75,7 @@ class QCameraThread : public QThread{
8075
QCameraDevice m_device;
8176
QCameraFormat m_format;
8277
QCamera* m_camera;
83-
84-
std::mutex m_lock;
85-
std::condition_variable m_cv;
78+
SpinWaitWithEvents m_spin_waiter;
8679
};
8780

8881

0 commit comments

Comments
 (0)