forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoCapture.cpp
287 lines (259 loc) · 8.08 KB
/
VideoCapture.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/******************************************************************************
VideoCapture.cpp: description
Copyright (C) 2012-2018 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QtAV/VideoCapture.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QRunnable>
#include <QtCore/QThreadPool>
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#include <QtGui/QDesktopServices>
#else
#include <QtCore/QStandardPaths>
#endif
#include "utils/Logger.h"
namespace QtAV {
Q_GLOBAL_STATIC(QThreadPool, videoCaptureThreadPool)
static bool app_is_dieing = false;
// TODO: cancel if qapp is quit
class CaptureTask : public QRunnable
{
public:
CaptureTask(VideoCapture* c)
: cap(c)
, save(true)
, original_fmt(false)
, quality(-1)
, format(QStringLiteral("PNG"))
, qfmt(QImage::Format_ARGB32)
{
setAutoDelete(true);
}
virtual void run() {
if (app_is_dieing) {
qDebug("app is dieing. cancel capture task %p", this);
return;
}
QImage image(frame.toImage());
if (image.isNull()) {
qWarning("Failed to convert to QImage");
QMetaObject::invokeMethod(cap, "failed");
return;
}
QMetaObject::invokeMethod(cap, "imageCaptured", Q_ARG(QImage, image));
if (!save)
return;
bool main_thread = QThread::currentThread() == qApp->thread();
qDebug("capture task running in thread %p [main thread=%d]", QThread::currentThreadId(), main_thread);
if (!QDir(dir).exists()) {
if (!QDir().mkpath(dir)) {
qWarning("Failed to create capture dir [%s]", qPrintable(dir));
QMetaObject::invokeMethod(cap, "failed");
return;
}
}
name = QString::number(frame.timestamp(), 'f', 3);
QString path(dir QStringLiteral("/") name QStringLiteral("."));
if (original_fmt) {
if (!frame.constBits(0)) {
frame = frame.to(frame.format());
}
path.append(frame.format().name());
qDebug("Saving capture to %s", qPrintable(path));
QFile file(path);
if (!file.open(QIODevice::WriteOnly)) {
qWarning("VideoCapture is failed to open file %s", qPrintable(path));
QMetaObject::invokeMethod(cap, "failed");
return;
}
int sz = 0;
const char* data = (const char*)frame.frameDataPtr(&sz);
if (file.write(data, sz) <= 0) {
qWarning("VideoCapture is failed to write captured frame with original format");
QMetaObject::invokeMethod(cap, "failed");
file.close();
return;
}
file.close();
QMetaObject::invokeMethod(cap, "saved", Q_ARG(QString, path));
return;
}
if (image.isNull())
return;
path.append(format.toLower());
qDebug("Saving capture to %s", qPrintable(path));
bool ok = image.save(path, format.toLatin1().constData(), quality);
if (!ok) {
qWarning("Failed to save capture");
QMetaObject::invokeMethod(cap, "failed");
}
QMetaObject::invokeMethod(cap, "saved", Q_ARG(QString, path));
}
VideoCapture *cap;
bool save;
bool original_fmt;
int quality;
QString format, dir, name;
QImage::Format qfmt;
VideoFrame frame;
};
VideoCapture::VideoCapture(QObject *parent) :
QObject(parent)
, async(true)
, auto_save(true)
, original_fmt(false)
, qfmt(QImage::Format_ARGB32)
{
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
dir = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
#else
dir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
#endif
if (dir.isEmpty())
dir = qApp->applicationDirPath() QStringLiteral("/capture");
fmt = QStringLiteral("PNG");
qual = -1;
// seems no direct connection is fine too
connect(qApp, SIGNAL(aboutToQuit()), SLOT(handleAppQuit()), Qt::DirectConnection);
}
void VideoCapture::setAsync(bool value)
{
if (async == value)
return;
async = value;
Q_EMIT asyncChanged();
}
bool VideoCapture::isAsync() const
{
return async;
}
void VideoCapture::setAutoSave(bool value)
{
if (auto_save == value)
return;
auto_save = value;
Q_EMIT autoSaveChanged();
}
bool VideoCapture::autoSave() const
{
return auto_save;
}
void VideoCapture::setOriginalFormat(bool value)
{
if (original_fmt == value)
return;
original_fmt = value;
Q_EMIT originalFormatChanged();
}
bool VideoCapture::isOriginalFormat() const
{
return original_fmt;
}
void VideoCapture::handleAppQuit()
{
app_is_dieing = true;
// TODO: how to cancel? since qt5.2, we can use clear()
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
videoCaptureThreadPool()->clear();
#endif
videoCaptureThreadPool()->setExpiryTimeout(0);
videoCaptureThreadPool()->waitForDone();
}
void VideoCapture::capture()
{
Q_EMIT requested();
}
void VideoCapture::start()
{
Q_EMIT frameAvailable(frame);
if (!frame.isValid() || !frame.constBits(0)) { // if frame is always cloned, then size is at least width*height
qDebug("Captured frame from hardware decoder surface.");
}
CaptureTask *task = new CaptureTask(this);
// copy properties so the task will not be affect even if VideoCapture properties changed
task->save = autoSave();
task->original_fmt = original_fmt;
task->quality = qual;
task->dir = dir;
task->name = name;
task->format = fmt;
task->qfmt = qfmt;
task->frame = frame; //copy here and it's safe in capture thread because start() is called immediatly after setVideoFrame
if (isAsync()) {
videoCaptureThreadPool()->start(task);
} else {
task->run();
delete task;
}
}
void VideoCapture::setSaveFormat(const QString &format)
{
if (format.toLower() == fmt.toLower())
return;
fmt = format;
Q_EMIT saveFormatChanged();
}
QString VideoCapture::saveFormat() const
{
return fmt;
}
void VideoCapture::setQuality(int value)
{
if (qual == value)
return;
qual = value;
Q_EMIT qualityChanged();
}
int VideoCapture::quality() const
{
return qual;
}
void VideoCapture::setCaptureName(const QString &value)
{
if (name == value)
return;
name = value;
Q_EMIT captureNameChanged();
}
QString VideoCapture::captureName() const
{
return name;
}
void VideoCapture::setCaptureDir(const QString &value)
{
if (dir == value)
return;
dir = value;
Q_EMIT captureDirChanged();
}
QString VideoCapture::captureDir() const
{
return dir;
}
/*
* If the frame is not created for direct rendering, then the frame data is already deep copied, so detach is enough.
* TODO: map frame from texture etc.
*/
void VideoCapture::setVideoFrame(const VideoFrame &frame)
{
// parameter in ready(QtAV::VideoFrame) ensure we can access the frame without lock
/*
* clone here may block VideoThread. But if not clone here, the frame may be
* modified outside and is not safe.
*/
this->frame = frame.clone(); // TODO: no clone, use detach()
}
} //namespace QtAV