1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43
44 #include <QtMultimedia/QAbstractVideoSurface>
45 #include <QtMultimedia/QVideoSurfaceFormat>
46
47 class tst_QAbstractVideoSurface : public QObject
48 {
49 Q_OBJECT
50 public:
51 tst_QAbstractVideoSurface();
52 ~tst_QAbstractVideoSurface();
53
54 public slots:
55 void initTestCase();
56 void cleanupTestCase();
57 void init();
58 void cleanup();
59
60 private slots:
61 void setError();
62 void isFormatSupported_data();
63 void isFormatSupported();
64 void nearestFormat_data();
65 void nearestFormat();
66 void start_data();
67 void start();
68 };
69
70 typedef QMap<QAbstractVideoBuffer::HandleType, QVideoFrame::PixelFormat> SupportedFormatMap;
71
72 Q_DECLARE_METATYPE(SupportedFormatMap)
73 Q_DECLARE_METATYPE(QVideoSurfaceFormat)
74 Q_DECLARE_METATYPE(QAbstractVideoSurface::Error);
75
76 class QtTestVideoSurface : public QAbstractVideoSurface
77 {
78 Q_OBJECT
79 public:
QtTestVideoSurface(QObject * parent=0)80 explicit QtTestVideoSurface(QObject *parent = 0) : QAbstractVideoSurface(parent) {}
QtTestVideoSurface(SupportedFormatMap formats,QObject * parent=0)81 explicit QtTestVideoSurface(SupportedFormatMap formats, QObject *parent = 0)
82 : QAbstractVideoSurface(parent), supportedFormats(formats) {}
83
supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType=QAbstractVideoBuffer::NoHandle) const84 QList<QVideoFrame::PixelFormat> supportedPixelFormats(
85 QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const
86 {
87 return supportedFormats.values(handleType);
88 }
89
present(const QVideoFrame &)90 bool present(const QVideoFrame &) { return false; }
91
92 using QAbstractVideoSurface::setError;
93
94 private:
95 SupportedFormatMap supportedFormats;
96 };
97
tst_QAbstractVideoSurface()98 tst_QAbstractVideoSurface::tst_QAbstractVideoSurface()
99 {
100 }
101
~tst_QAbstractVideoSurface()102 tst_QAbstractVideoSurface::~tst_QAbstractVideoSurface()
103 {
104 }
105
initTestCase()106 void tst_QAbstractVideoSurface::initTestCase()
107 {
108 }
109
cleanupTestCase()110 void tst_QAbstractVideoSurface::cleanupTestCase()
111 {
112 }
113
init()114 void tst_QAbstractVideoSurface::init()
115 {
116 }
117
cleanup()118 void tst_QAbstractVideoSurface::cleanup()
119 {
120 }
121
setError()122 void tst_QAbstractVideoSurface::setError()
123 {
124 qRegisterMetaType<QAbstractVideoSurface::Error>();
125
126 QtTestVideoSurface surface;
127
128 QCOMPARE(surface.error(), QAbstractVideoSurface::NoError);
129
130 surface.setError(QAbstractVideoSurface::StoppedError);
131 QCOMPARE(surface.error(), QAbstractVideoSurface::StoppedError);
132
133 surface.setError(QAbstractVideoSurface::ResourceError);
134 QCOMPARE(surface.error(), QAbstractVideoSurface::ResourceError);
135
136 surface.setError(QAbstractVideoSurface::NoError);
137 QCOMPARE(surface.error(), QAbstractVideoSurface::NoError);
138 }
139
isFormatSupported_data()140 void tst_QAbstractVideoSurface::isFormatSupported_data()
141 {
142 QTest::addColumn<SupportedFormatMap>("supportedFormats");
143 QTest::addColumn<QVideoSurfaceFormat>("format");
144 QTest::addColumn<bool>("supported");
145
146 SupportedFormatMap formats;
147
148 QTest::newRow("no formats: rgb32")
149 << formats
150 << QVideoSurfaceFormat(QSize(800, 600), QVideoFrame::Format_RGB32)
151 << false;
152 QTest::newRow("no formats: yv12")
153 << formats
154 << QVideoSurfaceFormat(QSize(800, 600), QVideoFrame::Format_YV12)
155 << false;
156 QTest::newRow("no formats: rgb32 gl")
157 << formats
158 << QVideoSurfaceFormat(
159 QSize(800, 600),
160 QVideoFrame::Format_RGB32,
161 QAbstractVideoBuffer::GLTextureHandle)
162 << false;
163 QTest::newRow("no formats: rgb24 gl")
164 << formats
165 << QVideoSurfaceFormat(
166 QSize(800, 600),
167 QVideoFrame::Format_RGB24,
168 QAbstractVideoBuffer::GLTextureHandle)
169 << false;
170
171 formats.insertMulti(QAbstractVideoBuffer::NoHandle, QVideoFrame::Format_RGB32);
172 formats.insertMulti(QAbstractVideoBuffer::NoHandle, QVideoFrame::Format_RGB24);
173 formats.insertMulti(QAbstractVideoBuffer::NoHandle, QVideoFrame::Format_YUV444);
174 formats.insertMulti(QAbstractVideoBuffer::GLTextureHandle, QVideoFrame::Format_RGB32);
175
176 QTest::newRow("supported: rgb32")
177 << formats
178 << QVideoSurfaceFormat(QSize(800, 600), QVideoFrame::Format_RGB32)
179 << true;
180 QTest::newRow("supported: rgb24")
181 << formats
182 << QVideoSurfaceFormat(QSize(800, 600), QVideoFrame::Format_RGB24)
183 << true;
184 QTest::newRow("unsupported: yv12")
185 << formats
186 << QVideoSurfaceFormat(QSize(800, 600), QVideoFrame::Format_YV12)
187 << false;
188 QTest::newRow("supported: rgb32 gl")
189 << formats
190 << QVideoSurfaceFormat(
191 QSize(800, 600),
192 QVideoFrame::Format_RGB32,
193 QAbstractVideoBuffer::GLTextureHandle)
194 << true;
195 QTest::newRow("unsupported: rgb24 gl")
196 << formats
197 << QVideoSurfaceFormat(
198 QSize(800, 600),
199 QVideoFrame::Format_RGB24,
200 QAbstractVideoBuffer::GLTextureHandle)
201 << false;
202 QTest::newRow("unsupported: yv12 gl")
203 << formats
204 << QVideoSurfaceFormat(
205 QSize(800, 600),
206 QVideoFrame::Format_YV12,
207 QAbstractVideoBuffer::GLTextureHandle)
208 << false;
209
210 formats.insertMulti(QAbstractVideoBuffer::NoHandle, QVideoFrame::Format_YV12);
211 formats.insertMulti(QAbstractVideoBuffer::GLTextureHandle, QVideoFrame::Format_RGB24);
212
213 QTest::newRow("supported: yv12")
214 << formats
215 << QVideoSurfaceFormat(QSize(800, 600), QVideoFrame::Format_YV12)
216 << true;
217 QTest::newRow("supported: rgb24 gl")
218 << formats
219 << QVideoSurfaceFormat(
220 QSize(800, 600),
221 QVideoFrame::Format_RGB24,
222 QAbstractVideoBuffer::GLTextureHandle)
223 << true;
224 }
225
isFormatSupported()226 void tst_QAbstractVideoSurface::isFormatSupported()
227 {
228 QFETCH(SupportedFormatMap, supportedFormats);
229 QFETCH(QVideoSurfaceFormat, format);
230 QFETCH(bool, supported);
231
232 QtTestVideoSurface surface(supportedFormats);
233
234 QCOMPARE(surface.isFormatSupported(format), supported);
235 }
236
nearestFormat_data()237 void tst_QAbstractVideoSurface::nearestFormat_data()
238 {
239 isFormatSupported_data();
240 }
241
nearestFormat()242 void tst_QAbstractVideoSurface::nearestFormat()
243 {
244 QFETCH(SupportedFormatMap, supportedFormats);
245 QFETCH(QVideoSurfaceFormat, format);
246 QFETCH(bool, supported);
247
248 QtTestVideoSurface surface(supportedFormats);
249
250 QCOMPARE(surface.nearestFormat(format) == format, supported);
251 }
252
start_data()253 void tst_QAbstractVideoSurface::start_data()
254 {
255 QTest::addColumn<QVideoSurfaceFormat>("format");
256
257 QTest::newRow("rgb32") << QVideoSurfaceFormat(
258 QSize(800, 600),
259 QVideoFrame::Format_RGB32);
260 QTest::newRow("yv12") << QVideoSurfaceFormat(
261 QSize(800, 600),
262 QVideoFrame::Format_YV12);
263 QTest::newRow("rgb32 gl") << QVideoSurfaceFormat(
264 QSize(800, 600),
265 QVideoFrame::Format_RGB32,
266 QAbstractVideoBuffer::GLTextureHandle);
267 }
268
start()269 void tst_QAbstractVideoSurface::start()
270 {
271 QFETCH(QVideoSurfaceFormat, format);
272
273 QtTestVideoSurface surface;
274 surface.setError(QAbstractVideoSurface::ResourceError);
275
276 QSignalSpy formatSpy(&surface, SIGNAL(surfaceFormatChanged(QVideoSurfaceFormat)));
277 QSignalSpy activeSpy(&surface, SIGNAL(activeChanged(bool)));
278
279 QVERIFY(!surface.isActive());
280 QCOMPARE(surface.surfaceFormat(), QVideoSurfaceFormat());
281
282 QVERIFY(surface.start(format));
283
284 QVERIFY(surface.isActive());
285 QCOMPARE(surface.surfaceFormat(), format);
286
287 QCOMPARE(formatSpy.count(), 1);
288 QCOMPARE(qvariant_cast<QVideoSurfaceFormat>(formatSpy.last().at(0)), format);
289
290 QCOMPARE(activeSpy.count(), 1);
291 QCOMPARE(activeSpy.last().at(0).toBool(), true);
292
293 // error() is reset on a successful start.
294 QCOMPARE(surface.error(), QAbstractVideoSurface::NoError);
295
296 surface.stop();
297
298 QVERIFY(!surface.isActive());
299 QCOMPARE(surface.surfaceFormat(), QVideoSurfaceFormat());
300
301 QCOMPARE(formatSpy.count(), 2);
302 QCOMPARE(qvariant_cast<QVideoSurfaceFormat>(formatSpy.last().at(0)), QVideoSurfaceFormat());
303
304 QCOMPARE(activeSpy.count(), 2);
305 QCOMPARE(activeSpy.last().at(0).toBool(), false);
306 }
307
308 QTEST_MAIN(tst_QAbstractVideoSurface)
309
310 #include "tst_qabstractvideosurface.moc"
311