-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageWriter.h
87 lines (71 loc) · 1.69 KB
/
ImageWriter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef _ImageWriter_H_INCLUDED_
#define _ImageWriter_H_INCLUDED_
#include <QImage>
#include <QTcpServer>
#include <QTcpSocket>
#include <QThread>
#include <QScopedPointer>
#include <stdexcept>
class ImageWriter: public QTcpServer
{
Q_OBJECT
public:
ImageWriter(quint16 port, QObject* parent = 0);
~ImageWriter();
Q_SLOT void pause();
Q_SLOT void resume();
Q_SLOT void distribute(const QImage& img);
protected:
void incomingConnection(int socket);
private:
Q_SLOT void discardClient();
bool m_disabled;
QTcpSocket* m_socket;
};
class ImageWriterLauncher:
public QObject
{
Q_OBJECT
public:
ImageWriterLauncher(const int port)
: m_port(port)
{
connect(&m_worker, SIGNAL (started()), this, SLOT (init()));
moveToThread(&m_worker);
m_worker.start();
}
~ImageWriterLauncher()
{
if(m_worker.isRunning())
{
m_worker.quit();
m_worker.wait();
}
}
void save(const QImage& image)
{
if(m_imageWriter)
QMetaObject::invokeMethod(m_imageWriter.data(), "distribute", Qt::QueuedConnection, Q_ARG(QImage, image));
}
void start()
{
if(m_imageWriter)
QMetaObject::invokeMethod(m_imageWriter.data(), "resume", Qt::QueuedConnection);
else
throw std::runtime_error("Unable to start recording a video stream until the worker thread not started");
}
void stop()
{
if(m_imageWriter)
QMetaObject::invokeMethod(m_imageWriter.data(), "pause", Qt::QueuedConnection);
}
private:
Q_SLOT void init()
{
m_imageWriter.reset(new ImageWriter(m_port, this));
}
const int m_port;
QScopedPointer<ImageWriter> m_imageWriter;
QThread m_worker;
}; // class ImageWriterRunner
#endif //_ImageWriter_H_INCLUDED_