Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
ScreenCapture.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QLoggingCategory>
4#include <QApplication>
5#include <QDesktopServices>
6#include "ScreenCapture.h"
7#include "DlgCapture.h"
8#include "RabbitCommonTools.h"
9#include "PluginClient.h"
10
11static Q_LOGGING_CATEGORY(log, "Screen.Capture")
13 : CConnecter(plugin)
14 , m_pWidget(nullptr)
15{
16 qDebug(log) << Q_FUNC_INFO;
17}
18
19CScreenCapture::~CScreenCapture()
20{
21 qDebug(log) << Q_FUNC_INFO;
22}
23
25{
26 return 0;
27}
28
30{
31 qDebug(log) << Q_FUNC_INFO;
32 Q_ASSERT(!m_pWidget);
33 m_pWidget = new QVideoWidget();
34 int nRet = SetParameter(&m_Parameter);
35 if(nRet) return nRet;
36 bool check = false;
37
38 CPluginClient* plugin = GetPlugClient();
39 QString szTitle(plugin->DisplayName());
40 m_Menu.setTitle(szTitle);
41 m_Menu.setToolTip(szTitle);
42 m_Menu.setStatusTip(szTitle);
43 m_Menu.setWhatsThis(szTitle);
44 m_Menu.setIcon(plugin->Icon());
45 check = m_Menu.addAction(
46 QIcon::fromTheme("media-playback-start"), tr("Start"),
47 this, SLOT(slotStart()));
48 Q_ASSERT(check);
49 check = m_Menu.addAction(
50 QIcon::fromTheme("media-playback-stop"), tr("Stop"),
51 this, SLOT(slotStop()));
52 Q_ASSERT(check);
53
54 check = connect(
55 &m_ScreenCapture, &QScreenCapture::errorOccurred,
56 this, [&](QScreenCapture::Error error, const QString &errorString){
57 qDebug(log) << "Capture screen error occurred:" << error << errorString;
58 slotStop();
59 emit sigError(error, errorString);
60 });
61 Q_ASSERT(check);
62 check = connect(
63 &m_WindowCapture, &QWindowCapture::errorOccurred,
64 this, [&](QWindowCapture::Error error, const QString &errorString){
65 qDebug(log) << "Capture windows error occurred:" << error << errorString;
66 slotStop();
67 emit sigError(error, errorString);
68 });
69 Q_ASSERT(check);
70
71 check = connect(
72 &m_Recorder, &QMediaRecorder::errorOccurred,
73 this, [&](QMediaRecorder::Error error, const QString &errorString) {
74 qDebug(log) << "Recorder error occurred:" << error << errorString;
75 slotStop();
76 emit sigError(error, errorString);
77 });
78 Q_ASSERT(check);
79 check = connect(
80 &m_Recorder, &QMediaRecorder::recorderStateChanged,
81 this, [&](QMediaRecorder::RecorderState state){
82 qDebug(log) << "Recorder state changed:" << state;
83 if(QMediaRecorder::StoppedState == state)
84 switch(m_Parameter.m_Record.GetEndAction())
85 {
86 case CParameterRecord::ENDACTION::OpenFile:
87 QDesktopServices::openUrl(QUrl::fromLocalFile(m_szRecordFile));
88 break;
89 case CParameterRecord::ENDACTION::OpenFolder: {
90 QFileInfo fi(m_szRecordFile);
91 QDesktopServices::openUrl(QUrl::fromLocalFile(fi.absolutePath()));
92 break;
93 }
94 default:
95 break;
96 }
97 });
98 Q_ASSERT(check);
99 check = connect(
100 &m_Recorder, &QMediaRecorder::actualLocationChanged,
101 this, [&](const QUrl &location){
102 qInfo(log) << "Recorder actual location changed:" << location;
103 m_szRecordFile = location.toLocalFile();
104 });
105 Q_ASSERT(check);
106 check = connect(
107 &m_ImageCapture, &QImageCapture::errorOccurred,
108 this, [&](int id, QImageCapture::Error error, const QString &errorString) {
109 qDebug(log) << "Capture image error occurred:" << id << error << errorString;
110 slotStop();
111 emit sigError(error, errorString);
112 });
113 Q_ASSERT(check);
114 check = connect(
115 &m_ImageCapture, &QImageCapture::imageCaptured,
116 this, [&](int id, const QImage &image){
117 qDebug(log) << "Capture image:" << id << image;
118 QString szFile = m_Parameter.m_Record.GetImageFile(true);
119 if(!image.save(szFile, "PNG"))
120 {
121 qCritical(log) << "Capture image save to file fail." << szFile;
122 slotStop();
123 return;
124 }
125 qDebug(log) << "Capture image to file:" << szFile;
126 switch(m_Parameter.m_Record.GetEndAction())
127 {
128 case CParameterRecord::ENDACTION::OpenFile: {
129 bool bRet = QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
130 if(!bRet)
131 qCritical(log) << "Fail: Open capture image file" << szFile;
132 break;
133 }
134 case CParameterRecord::ENDACTION::OpenFolder: {
135 QFileInfo fi(szFile);
136 QDesktopServices::openUrl(QUrl::fromLocalFile(fi.absolutePath()));
137 break;
138 }
139 default:
140 break;
141 }
142 });
143 Q_ASSERT(check);
144 return 0;
145}
146
148{
149 if(m_pWidget)
150 delete m_pWidget;
151 return 0;
152}
153
155{
156 return m_pWidget;
157}
158
160{
161 return nullptr;
162}
163
164int CScreenCapture::Connect()
165{
166 int nRet = 0;
167 slotStart();
168 emit sigConnected();
169 return nRet;
170}
171
172int CScreenCapture::DisConnect()
173{
174 int nRet = 0;
175 slotStop();
176 emit sigDisconnected();
177 return nRet;
178}
179
180int CScreenCapture::slotStart()
181{
182 qDebug(log) << "CScreenCapture::slotStart()";
183 int nRet = 0;
184 CDlgCapture dlg(&m_Parameter);
185 nRet = RC_SHOW_WINDOW(&dlg);
186 if(QDialog::Accepted != nRet)
187 return 0;
188
189 int nIndex = m_Parameter.GetScreen();
190 if(m_Parameter.GetTarget() == CParameterScreenCapture::TARGET::Screen
191 && -1 < nIndex && nIndex < QApplication::screens().size()) {
192 m_ScreenCapture.setScreen(QApplication::screens().at(nIndex));
193 m_CaptureSessioin.setScreenCapture(&m_ScreenCapture);
194 m_ScreenCapture.start();
195 }else
196 m_CaptureSessioin.setScreenCapture(nullptr);
197
198 if(m_Parameter.GetTarget() == CParameterScreenCapture::TARGET::Window) {
199 m_CaptureSessioin.setWindowCapture(&m_WindowCapture);
200 m_WindowCapture.setWindow(m_Parameter.GetWindow());
201 m_WindowCapture.start();
202 } else
203 m_CaptureSessioin.setWindowCapture(nullptr);
204
205 if(m_Parameter.GetOperate() == CParameterScreenCapture::OPERATE::Record) {
206 m_CaptureSessioin.setRecorder(&m_Recorder);
207 m_Parameter.m_Record >> m_Recorder;
208 m_Recorder.record();
209 qDebug(log) << "Record to file:" << m_Recorder.actualLocation();
210 }
211 else
212 m_CaptureSessioin.setRecorder(nullptr);
213
214 if(m_Parameter.GetOperate() == CParameterScreenCapture::OPERATE::Shot) {
215 m_CaptureSessioin.setImageCapture(&m_ImageCapture);
216 m_ImageCapture.capture();
217 } else
218 m_CaptureSessioin.setImageCapture(nullptr);
219
220 if(m_pWidget)
221 m_CaptureSessioin.setVideoOutput(m_pWidget);
222
223 return 0;
224}
225
226int CScreenCapture::slotStop()
227{
228 qDebug(log) << "CScreenCapture::slotStop()";
229 if(m_Parameter.GetTarget() == CParameterScreenCapture::TARGET::Screen)
230 m_ScreenCapture.stop();
231 if(m_Parameter.GetTarget() == CParameterScreenCapture::TARGET::Window)
232 m_WindowCapture.stop();
233
234 if(m_Parameter.GetOperate() == CParameterScreenCapture::OPERATE::Record)
235 m_Recorder.stop();
236
237 return 0;
238}
Connecter interface.
Definition Connecter.h:62
void sigConnected()
Successful connection signal.
void sigDisconnected()
Successful disconnection signal.
virtual int SetParameter(CParameter *p)
Set parameters.
void sigError(const int nError, const QString &szError)
Triggered when an error is generated.
The plugin interface.
virtual const QString DisplayName() const
The plugin display name.
virtual QWidget * GetViewer() override
Get Viewer.
virtual QDialog * OnOpenDialogSettings(QWidget *parent) override
Open settgins dialog.
virtual qint16 Version() override
Version.
virtual int Initial() override
Initial parameters and resource.
virtual int Clean() override
Clean parameters and resource.