tst_qgstreamerplaybin.cpp 6.72 KB
Newer Older
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QtTest>

#include <QMediaPlayer>
Val Doroshchuk's avatar
Val Doroshchuk committed
32
#include <QGstreamerPlaybin>
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
33
#include <QAbstractVideoSurface>
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
34 35 36

QT_USE_NAMESPACE

Val Doroshchuk's avatar
Val Doroshchuk committed
37
class tst_QGstreamerPlaybin: public QObject
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
38 39 40 41 42 43 44 45
{
    Q_OBJECT

public slots:
    void init();
    void cleanup();

private slots:
46
    void testVideoSink();
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
47 48 49 50 51 52 53 54 55
    void testShowText();
    void testTextUri();
    void testTextFont();
    void testAudioStreams();
    void testVideoStreams();
    void testTextStreams();
    void testSetTextStream();
    void testTextStreamProperties();
    void testTextUriStreamProperties();
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
56
    void testSetProperty();
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
57 58 59

private:
    QMediaPlayer *mediaPlayer = nullptr;
Val Doroshchuk's avatar
Val Doroshchuk committed
60
    QGstreamerPlaybin *control = nullptr;
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
61 62
};

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
63 64 65 66 67 68 69 70 71 72
struct Surface : QAbstractVideoSurface
{
    Surface(QObject *parent = nullptr)
        : QAbstractVideoSurface(parent)
    {
    }

    QList<QVideoFrame::PixelFormat> supportedPixelFormats(
        QAbstractVideoBuffer::HandleType) const override
    {
Val Doroshchuk's avatar
Val Doroshchuk committed
73
        return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_ARGB32;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
74 75
    }

Val Doroshchuk's avatar
Val Doroshchuk committed
76 77
    bool isFormatSupported(const QVideoSurfaceFormat &) const override { return true; }
    bool start(const QVideoSurfaceFormat &) override { return true; }
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
78
    void stop() override { }
Val Doroshchuk's avatar
Val Doroshchuk committed
79
    bool present(const QVideoFrame &) override { return true; }
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
80
};
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
81

Val Doroshchuk's avatar
Val Doroshchuk committed
82
void tst_QGstreamerPlaybin::init()
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
83 84 85
{
    mediaPlayer = new QMediaPlayer;
    mediaPlayer->setMedia(QUrl("qrc:/testdata/colors.mp4"));
Val Doroshchuk's avatar
Val Doroshchuk committed
86
    control = new QGstreamerPlaybin(mediaPlayer);
Val Doroshchuk's avatar
Val Doroshchuk committed
87 88
    Surface *s = new Surface(mediaPlayer);
    mediaPlayer->setVideoOutput(s);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
89 90
}

Val Doroshchuk's avatar
Val Doroshchuk committed
91
void tst_QGstreamerPlaybin::cleanup()
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
92 93 94 95
{
    delete mediaPlayer;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
96
void tst_QGstreamerPlaybin::testVideoSink()
97
{
Val Doroshchuk's avatar
Val Doroshchuk committed
98
    QSignalSpy spy(control, &QGstreamerPlaybin::videoSinkChanged);
99

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
100
    QVERIFY(control->videoSink().isEmpty());
101
    QString sink = "xvimagesink hue=100";
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
102
    control->setVideoSink(sink);
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
103
    QTRY_COMPARE(spy.count(), 1);
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
104
    QCOMPARE(sink, control->videoSink());
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
105 106
}

Val Doroshchuk's avatar
Val Doroshchuk committed
107
void tst_QGstreamerPlaybin::testShowText()
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
108
{
Val Doroshchuk's avatar
Val Doroshchuk committed
109
    QSignalSpy spy(control, &QGstreamerPlaybin::showTextChanged);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
110

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
111 112 113
    QVERIFY(!control->showText());
    control->setShowText(true);
    QVERIFY(control->showText());
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
114
    QCOMPARE(spy.count(), 1);
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
115 116
    control->setShowText(true);
    QVERIFY(control->showText());
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
117
    QCOMPARE(spy.count(), 1);
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
118 119
    control->setShowText(false);
    QVERIFY(!control->showText());
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
120 121 122
    QCOMPARE(spy.count(), 2);
}

Val Doroshchuk's avatar
Val Doroshchuk committed
123
void tst_QGstreamerPlaybin::testTextUri()
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
124
{
Val Doroshchuk's avatar
Val Doroshchuk committed
125
    QSignalSpy spy(control, &QGstreamerPlaybin::textUriChanged);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
126

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
127
    QCOMPARE(control->textUri(), QUrl());
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
128
    QUrl text("text.srt");
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
129 130 131
    control->setTextUri(text);
    QVERIFY(control->showText());
    QCOMPARE(control->textUri(), text);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
132 133 134
    QCOMPARE(spy.count(), 1);
}

Val Doroshchuk's avatar
Val Doroshchuk committed
135
void tst_QGstreamerPlaybin::testTextFont()
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
136
{
Val Doroshchuk's avatar
Val Doroshchuk committed
137
    QSignalSpy spy(control, &QGstreamerPlaybin::textFontChanged);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
138

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
139
    QCOMPARE(control->textFont(), QString());
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
140
    QString font = "Sans, 18";
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
141 142
    control->setTextFont(font);
    QCOMPARE(control->textFont(), font);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
143 144 145
    QCOMPARE(spy.count(), 1);
}

Val Doroshchuk's avatar
Val Doroshchuk committed
146
void tst_QGstreamerPlaybin::testAudioStreams()
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
147
{
Val Doroshchuk's avatar
Val Doroshchuk committed
148
    QSignalSpy spy(control, &QGstreamerPlaybin::audioStreamsCountChanged);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
149

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
150
    QCOMPARE(control->audioStreamsCount(), 0);
Val Doroshchuk's avatar
Val Doroshchuk committed
151 152
    QTRY_VERIFY(spy.count() > 0);
    QCOMPARE(control->audioStreamsCount(), 1);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
153 154
}

Val Doroshchuk's avatar
Val Doroshchuk committed
155
void tst_QGstreamerPlaybin::testVideoStreams()
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
156
{
Val Doroshchuk's avatar
Val Doroshchuk committed
157
    QSignalSpy spy(control, &QGstreamerPlaybin::videoStreamsCountChanged);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
158

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
159
    QCOMPARE(control->videoStreamsCount(), 0);
Val Doroshchuk's avatar
Val Doroshchuk committed
160 161
    QTRY_VERIFY(spy.count() > 0);
    QCOMPARE(control->videoStreamsCount(), 1);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
162 163
}

Val Doroshchuk's avatar
Val Doroshchuk committed
164
void tst_QGstreamerPlaybin::testTextStreams()
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
165
{
Val Doroshchuk's avatar
Val Doroshchuk committed
166
    QSignalSpy spy(control, &QGstreamerPlaybin::textStreamsCountChanged);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
167

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
168
    QCOMPARE(control->textStreamsCount(), 0);
Val Doroshchuk's avatar
Val Doroshchuk committed
169 170
    QTRY_VERIFY(spy.count() > 0);
    QCOMPARE(control->textStreamsCount(), 2);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
171 172
}

Val Doroshchuk's avatar
Val Doroshchuk committed
173
void tst_QGstreamerPlaybin::testSetTextStream()
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
174
{
Val Doroshchuk's avatar
Val Doroshchuk committed
175
    QSignalSpy spy(control, &QGstreamerPlaybin::textStreamChanged);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
176

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
177 178 179
    QCOMPARE(control->textStream(), -1);
    QTRY_COMPARE(control->textStreamsCount(), 2);
    control->setTextStream(0);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
180
    QCOMPARE(spy.count(), 1);
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
181 182
    QCOMPARE(control->textStream(), 0);
    control->setTextStream(1);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
183
    QCOMPARE(spy.count(), 2);
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
184
    QTRY_COMPARE(control->textStream(), 1);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
185 186
}

Val Doroshchuk's avatar
Val Doroshchuk committed
187
void tst_QGstreamerPlaybin::testTextStreamProperties()
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
188
{
Val Doroshchuk's avatar
Val Doroshchuk committed
189
    QSignalSpy spy(control, &QGstreamerPlaybin::textStreamPropertiesChanged);
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
190 191 192 193 194 195 196

    QCOMPARE(control->textStreamsCount(), 0);
    QTRY_COMPARE(control->textStreamsCount(), 2);
    QTRY_COMPARE(control->textStreamProperties(0).size(), 1);
    QCOMPARE(control->textStreamProperties(0)["Language"], QVariant("en"));
    QCOMPARE(control->textStreamProperties(1).size(), 1);
    QCOMPARE(control->textStreamProperties(1)["Language"], QVariant("no"));
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
197 198
}

Val Doroshchuk's avatar
Val Doroshchuk committed
199
void tst_QGstreamerPlaybin::testTextUriStreamProperties()
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
200
{
Val Doroshchuk's avatar
Val Doroshchuk committed
201
    QSignalSpy spy(control, &QGstreamerPlaybin::textStreamPropertiesChanged);
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
202 203

    mediaPlayer->setMedia(QUrl());
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
204
    control->setTextUri(QUrl::fromLocalFile(QFINDTESTDATA("testdata/text.srt")));
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
205 206
    mediaPlayer->setMedia(QUrl::fromLocalFile(QFINDTESTDATA("testdata/colors.mp4")));

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
207 208 209 210 211 212 213 214
    QTRY_COMPARE(control->textStreamsCount(), 3);
    QCOMPARE(control->textStreamProperties(0).size(), 0);
    QTRY_COMPARE(control->textStreamProperties(1).size(), 1);
    QCOMPARE(control->textStreamProperties(1)["Language"], QVariant("en"));
    QTRY_COMPARE(control->textStreamProperties(2).size(), 1);
    QCOMPARE(control->textStreamProperties(2)["Language"], QVariant("no"));
}

Val Doroshchuk's avatar
Val Doroshchuk committed
215
void tst_QGstreamerPlaybin::testSetProperty()
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
216 217 218
{
    QVariantMap m;
    m["name"] = "new_name";
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
219
    QVERIFY(control->set("source", m));
Val Doroshchuk's avatar
Init  
Val Doroshchuk committed
220 221
}

Val Doroshchuk's avatar
Val Doroshchuk committed
222 223
QTEST_GUILESS_MAIN(tst_QGstreamerPlaybin)
#include "tst_qgstreamerplaybin.moc"